View | Details | Raw Unified | Return to bug 30311
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java (-25 / +67 lines)
Lines 27-42 Link Here
27
27
28
/**
28
/**
29
 * HSSFConditionalFormatting class encapsulates all settings of Conditional Formatting. 
29
 * HSSFConditionalFormatting class encapsulates all settings of Conditional Formatting. 
30
 * The class is not intended to be used explicitly except cases when there is a need 
31
 * to make a copy HSSFConditionalFormatting settings for some reason. 
32
 * 
30
 * 
31
 * The class can be used 
32
 * 
33
 * <UL>
34
 * <LI>
35
 * to make a copy HSSFConditionalFormatting settings.
36
 * </LI>
37
 *  
38
 * 
33
 * For example:
39
 * For example:
34
 * <PRE>
40
 * <PRE>
35
 * HSSFConditionalFormatting cf = sheet.getConditionalFormattingAt(index);
41
 * HSSFConditionalFormatting cf = sheet.getConditionalFormattingAt(index);
36
 * newSheet.addConditionalFormatting(cf);
42
 * newSheet.addConditionalFormatting(cf);
37
 * </PRE>
43
 * </PRE>
38
 * 
44
 * 
45
 *  <LI>
46
 *  or to modify existing Conditional Formatting settings (formatting regions and/or rules).
47
 *  </LI>
48
 *  </UL>
49
 * 
50
 * Use {@link HSSFSheet#getConditionalFormattingAt(int)} to get access to an instance of this class. 
51
 * <P>
39
 * To create a new Conditional Formatting set use the following approach:
52
 * To create a new Conditional Formatting set use the following approach:
53
 * 
40
 * <PRE>
54
 * <PRE>
41
 * // Create pattern with red background
55
 * // Create pattern with red background
42
 * HSSFPatternFormatting patternFormatting = new HSSFPatternFormatting();
56
 * HSSFPatternFormatting patternFormatting = new HSSFPatternFormatting();
Lines 70-92 Link Here
70
 * 
84
 * 
71
 * @author Dmitriy Kumshayev
85
 * @author Dmitriy Kumshayev
72
 */
86
 */
73
public class HSSFConditionalFormatting
87
public final class HSSFConditionalFormatting
74
{
88
{
75
	HSSFSheet sheet;
89
	private final HSSFSheet sheet;
76
	CFRecordsAggregate cfAggregate;
90
	private final CFRecordsAggregate cfAggregate;
77
	
91
	
78
	protected HSSFConditionalFormatting(HSSFSheet sheet)
92
	HSSFConditionalFormatting(HSSFSheet sheet) {
79
	{
93
		this(sheet, new CFRecordsAggregate());
80
		this.sheet = sheet;
81
		this.cfAggregate = new CFRecordsAggregate();
82
	}
94
	}
83
	
95
	
84
	protected HSSFConditionalFormatting(HSSFSheet sheet, CFRecordsAggregate cfAggregate)
96
	HSSFConditionalFormatting(HSSFSheet sheet, CFRecordsAggregate cfAggregate)
85
	{
97
	{
98
        if(sheet == null) {
99
            throw new IllegalArgumentException("sheet must not be null");
100
        }
101
        if(cfAggregate == null) {
102
            throw new IllegalArgumentException("cfAggregate must not be null");
103
        }
86
		this.sheet = sheet;
104
		this.sheet = sheet;
87
		this.cfAggregate = cfAggregate;
105
		this.cfAggregate = cfAggregate;
88
	}
106
	}
89
	
107
	CFRecordsAggregate getCFRecordsAggregate() {
108
        return cfAggregate;
109
    }
90
110
91
	public void setFormattingRegions(Region[] regions)
111
	public void setFormattingRegions(Region[] regions)
92
	{
112
	{
Lines 97-132 Link Here
97
		}
117
		}
98
	}
118
	}
99
119
120
	/**
121
	 * @return array of <tt>Region</tt>s. never <code>null</code>
122
	 */
100
	public Region[] getFormattingRegions()
123
	public Region[] getFormattingRegions()
101
	{
124
	{
102
		CFHeaderRecord cfh = cfAggregate.getHeader();
125
		CFHeaderRecord cfh = cfAggregate.getHeader();
103
		
126
		
104
		List cellRanges = cfh.getCellRanges();
127
		List cellRanges = cfh.getCellRanges();
105
		
128
		
106
		if (cellRanges != null)
129
		return toRegionArray(cellRanges);
107
		{
108
			return toRegionArray(cellRanges);
109
		}
110
		return null;
111
	}
130
	}
112
	
131
	
113
	public void setConditionalFormat(int idx, HSSFConditionalFormattingRule cfRule)
132
	/**
133
	 * set a Conditional Formatting rule at position idx. 
134
	 * Excel allows to create up to 3 Conditional Formatting rules.
135
	 * This method can be useful to modify existing  Conditional Formatting rules.
136
	 * 
137
	 * @param idx position of the rule. Should be between 0 and 2.
138
	 * @param cfRule - Conditional Formatting rule
139
	 */
140
	public void setRule(int idx, HSSFConditionalFormattingRule cfRule)
114
	{
141
	{
115
		cfAggregate.getRules().set(idx, cfRule);
142
		cfAggregate.getRules().set(idx, cfRule);
116
	}
143
	}
117
144
118
	public void addConditionalFormat(HSSFConditionalFormattingRule cfRule)
145
	/**
146
	 * add a Conditional Formatting rule. 
147
	 * Excel allows to create up to 3 Conditional Formatting rules.
148
	 * @param cfRule - Conditional Formatting rule
149
	 */
150
	public void addRule(HSSFConditionalFormattingRule cfRule)
119
	{
151
	{
120
		cfAggregate.getRules().add(cfRule);
152
		cfAggregate.getRules().add(cfRule);
121
	}
153
	}
122
	
154
123
	public HSSFConditionalFormattingRule getConditionalFormat(int idx)
155
	/**
156
	 * get a Conditional Formatting rule at position idx. 
157
	 * @param idx
158
	 * @return a Conditional Formatting rule at position idx.
159
	 */
160
	public HSSFConditionalFormattingRule getRule(int idx)
124
	{
161
	{
125
		CFRuleRecord ruleRecord = (CFRuleRecord)cfAggregate.getRules().get(idx);
162
		CFRuleRecord ruleRecord = (CFRuleRecord)cfAggregate.getRules().get(idx);
126
		return new HSSFConditionalFormattingRule(sheet.workbook, ruleRecord);
163
		return new HSSFConditionalFormattingRule(sheet.workbook, ruleRecord);
127
	}
164
	}
128
	
165
	
129
	/**
166
	/**
167
	 * @return number of Conditional Formatting rules.
168
	 */
169
	public int getNumbOfRules()
170
	{
171
		return cfAggregate.getRules().size();
172
	}
173
	
174
	
175
	/**
130
	 * Do all possible cell merges between cells of the list so that:<br>
176
	 * Do all possible cell merges between cells of the list so that:<br>
131
	 * 	<li>if a cell range is completely inside of another cell range, it gets removed from the list 
177
	 * 	<li>if a cell range is completely inside of another cell range, it gets removed from the list 
132
	 * 	<li>if two cells have a shared border, merge them into one bigger cell range
178
	 * 	<li>if two cells have a shared border, merge them into one bigger cell range
Lines 237-246 Link Here
237
283
238
	public String toString()
284
	public String toString()
239
	{
285
	{
240
		if(cfAggregate!=null)
286
		return cfAggregate.toString();
241
		{
242
			return cfAggregate.toString();
243
		}
244
		return null;
245
	}
287
	}
246
}
288
}
(-)src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (-5 / +5 lines)
Lines 1927-1935 Link Here
1927
      */
1927
      */
1928
     public int addConditionalFormatting( HSSFConditionalFormatting cf )
1928
     public int addConditionalFormatting( HSSFConditionalFormatting cf )
1929
     {
1929
     {
1930
      	HSSFConditionalFormatting cfClone = new HSSFConditionalFormatting(this,cf.cfAggregate.cloneCFAggregate());
1930
         CFRecordsAggregate cfraClone = cf.getCFRecordsAggregate().cloneCFAggregate();
1931
      	cfClone.sheet=this;
1931
         
1932
     	return sheet.addConditionalFormatting(cfClone.cfAggregate);
1932
         return sheet.addConditionalFormatting(cfraClone);
1933
     }
1933
     }
1934
1934
1935
     /**
1935
     /**
Lines 1949-1958 Link Here
1949
     	{
1949
     	{
1950
     		for( int i=0; i!= cfRules.length; i++ )
1950
     		for( int i=0; i!= cfRules.length; i++ )
1951
     		{
1951
     		{
1952
     	    	cf.addConditionalFormat(cfRules[i]);
1952
     	    	cf.addRule(cfRules[i]);
1953
     		}
1953
     		}
1954
     	}
1954
     	}
1955
     	return sheet.addConditionalFormatting(cf.cfAggregate);
1955
     	return sheet.addConditionalFormatting(cf.getCFRecordsAggregate());
1956
     }
1956
     }
1957
     
1957
     
1958
	/**
1958
	/**
(-)src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java (-8 / +5 lines)
Lines 44-50 Link Here
44
    private CFHeaderRecord header;
44
    private CFHeaderRecord header;
45
45
46
    // List of CFRuleRecord objects
46
    // List of CFRuleRecord objects
47
	private List rules;
47
	private final List rules;
48
	
48
	
49
    public CFRecordsAggregate()
49
    public CFRecordsAggregate()
50
    {
50
    {
Lines 206-220 Link Here
206
		{
206
		{
207
			buffer.append(header.toString());
207
			buffer.append(header.toString());
208
		}
208
		}
209
		if( rules != null )
209
		for(int i=0; i<rules.size(); i++)
210
		{
210
		{
211
			for(int i=0; i<rules.size(); i++)
211
			CFRuleRecord cfRule = (CFRuleRecord)rules.get(i);
212
			if(cfRule!=null)
212
			{
213
			{
213
				CFRuleRecord cfRule = (CFRuleRecord)rules.get(i);
214
				buffer.append(cfRule.toString());
214
				if(cfRule!=null)
215
				{
216
					buffer.append(cfRule.toString());
217
				}
218
			}
215
			}
219
		}
216
		}
220
		buffer.append("[/CF]\n");
217
		buffer.append("[/CF]\n");

Return to bug 30311