package org.apache.poi.hssf.record.aggregates;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.model.RecordStream;
import org.apache.poi.hssf.record.CFHeaderRecord;
import org.apache.poi.hssf.record.CFRuleRecord;
import org.apache.poi.hssf.record.Record;
-import org.apache.poi.hssf.record.RecordInputStream;
import org.apache.poi.hssf.util.CellRangeAddress;
/**
* @author Dmitriy Kumshayev
*
*/
-public final class CFRecordsAggregate extends Record {
+public final class CFRecordsAggregate extends RecordAggregate {
/** Excel allows up to 3 conditional formating rules */
private static final int MAX_CONDTIONAL_FORMAT_RULES = 3;
- public final static short sid = -2008; // not a real BIFF record
-
private final CFHeaderRecord header;
/** List of CFRuleRecord objects */
return new CFRecordsAggregate((CFHeaderRecord) header.clone(), newRecs);
}
- protected void fillFields(RecordInputStream in)
- {
- // You never fill an aggregate record
- }
-
- public short getSid()
- {
- return sid;
- }
-
- /**
- * called by the class that is responsible for writing this sucker.
- * Subclasses should implement this so that their data is passed back in a
- * byte array.
- *
- * @param offset to begin writing at
- * @param data byte array containing instance data
- * @return number of bytes written
- */
-
- public int serialize(int offset, byte[] data)
- {
- int nRules = rules.size();
- header.setNumberOfConditionalFormats(nRules);
-
- int pos = offset;
-
- pos += header.serialize(pos, data);
- for(int i=0; i< nRules; i++) {
- pos += getRule(i).serialize(pos, data);
- }
- return pos - offset;
- }
-
- protected void validateSid(short id)
- {
- // do nothing here
- }
-
/**
* @return the header. Never <code>null</code>.
*/
return (CFRuleRecord) rules.get(idx);
}
public void setRule(int idx, CFRuleRecord r) {
+ if (r == null) {
+ throw new IllegalArgumentException("r must not be null");
+ }
checkRuleIndex(idx);
rules.set(idx, r);
}
public void addRule(CFRuleRecord r) {
+ if (r == null) {
+ throw new IllegalArgumentException("r must not be null");
+ }
if(rules.size() >= MAX_CONDTIONAL_FORMAT_RULES) {
throw new IllegalStateException("Cannot have more than "
+ MAX_CONDTIONAL_FORMAT_RULES + " conditional format rules");
return rules.size();
}
- /**
- * @return sum of sizes of all aggregated records
- */
- public int getRecordSize()
- {
- int size = 0;
- if( header != null)
- {
- size += header.getRecordSize();
- }
- if( rules != null)
- {
- for(Iterator irecs = rules.iterator(); irecs.hasNext(); )
- {
- size += (( Record ) irecs.next()).getRecordSize();
- }
- }
- return size;
- }
-
/**
* String representation of CFRecordsAggregate
*/
for(int i=0; i<rules.size(); i++)
{
CFRuleRecord cfRule = (CFRuleRecord)rules.get(i);
- if(cfRule!=null)
- {
- buffer.append(cfRule.toString());
- }
+ buffer.append(cfRule.toString());
}
buffer.append("[/CF]\n");
return buffer.toString();
}
+
+ public void visitContainedRecords(RecordVisitor rv) {
+ rv.visitRecord(header);
+ for(int i=0; i<rules.size(); i++) {
+ CFRuleRecord rule = (CFRuleRecord)rules.get(i);
+ rv.visitRecord(rule);
+ }
+ }
}
package org.apache.poi.hssf.usermodel;
+import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.hssf.record.CFRuleRecord.ComparisonOperator;
assertEquals("2",rule2.getFormula2());
assertEquals("1",rule2.getFormula1());
}
+
+ public void testClone() {
+
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ String formula = "7";
+
+ HSSFSheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
+
+ HSSFConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(formula);
+ HSSFFontFormatting fontFmt = rule1.createFontFormatting();
+ fontFmt.setFontStyle(true, false);
+
+ HSSFPatternFormatting patternFmt = rule1.createPatternFormatting();
+ patternFmt.setFillBackgroundColor(HSSFColor.YELLOW.index);
+
+
+ HSSFConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2");
+ HSSFConditionalFormattingRule [] cfRules =
+ {
+ rule1, rule2
+ };
+
+ short col = 1;
+ CellRangeAddress [] regions = {
+ new CellRangeAddress(0, 65535, col, col)
+ };
+
+ sheetCF.addConditionalFormatting(regions, cfRules);
+
+ try {
+ wb.cloneSheet(0);
+ } catch (RuntimeException e) {
+ if (e.getMessage().indexOf("needs to define a clone method") > 0) {
+ throw new AssertionFailedError("Indentified bug 45682");
+ }
+ throw e;
+ }
+ assertEquals(2, wb.getNumberOfSheets());
+ }
}