Further HSSF support towards CF IconSets #58130

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691113 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-07-15 00:04:53 +00:00
parent 59a1946bf5
commit 9c9023524a
4 changed files with 95 additions and 1 deletions

View File

@ -173,6 +173,14 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord {
public IconMultiStateFormatting getMultiStateFormatting() { public IconMultiStateFormatting getMultiStateFormatting() {
return multistate; return multistate;
} }
public IconMultiStateFormatting createMultiStateFormatting() {
if (multistate != null) return multistate;
// Convert, setup and return
setConditionType(CONDITION_TYPE_ICON_SET);
multistate = new IconMultiStateFormatting();
return multistate;
}
/** /**
* get the stack of the scale expression as a list * get the stack of the scale expression as a list

View File

@ -18,11 +18,13 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import org.apache.poi.hssf.model.HSSFFormulaParser; import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.CFRule12Record;
import org.apache.poi.hssf.record.CFRuleBase; import org.apache.poi.hssf.record.CFRuleBase;
import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator; import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator;
import org.apache.poi.hssf.record.CFRuleRecord; import org.apache.poi.hssf.record.CFRuleRecord;
import org.apache.poi.hssf.record.cf.BorderFormatting; import org.apache.poi.hssf.record.cf.BorderFormatting;
import org.apache.poi.hssf.record.cf.FontFormatting; import org.apache.poi.hssf.record.cf.FontFormatting;
import org.apache.poi.hssf.record.cf.IconMultiStateFormatting;
import org.apache.poi.hssf.record.cf.PatternFormatting; import org.apache.poi.hssf.record.cf.PatternFormatting;
import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.ConditionType; import org.apache.poi.ss.usermodel.ConditionType;
@ -166,6 +168,45 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
return getPatternFormatting(true); return getPatternFormatting(true);
} }
private HSSFIconMultiStateFormatting getMultiStateFormatting(boolean create) {
if (cfRuleRecord instanceof CFRule12Record) {
// Good
} else {
if (create) throw new IllegalArgumentException("Can't convert a CF into a CF12 record");
return null;
}
CFRule12Record cfRule12Record = (CFRule12Record)cfRuleRecord;
IconMultiStateFormatting iconFormatting = cfRule12Record.getMultiStateFormatting();
if (iconFormatting != null)
{
return new HSSFIconMultiStateFormatting(cfRule12Record);
}
else if( create )
{
iconFormatting = cfRule12Record.createMultiStateFormatting();
return new HSSFIconMultiStateFormatting(cfRule12Record);
}
else
{
return null;
}
}
/**
* @return icon / multi-state formatting object if defined, <code>null</code> otherwise
*/
public HSSFIconMultiStateFormatting getMultiStateFormatting() {
return getMultiStateFormatting(false);
}
/**
* create a new icon / multi-state formatting object if it does not exist,
* otherwise just return the existing object.
*/
public HSSFIconMultiStateFormatting createMultiStateFormatting() {
return getMultiStateFormatting(true);
}
/** /**
* @return - the conditiontype for the cfrule * @return - the conditiontype for the cfrule
*/ */
@ -205,7 +246,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
} }
private String toFormulaString(Ptg[] parsedExpression) { private String toFormulaString(Ptg[] parsedExpression) {
if(parsedExpression ==null) { if(parsedExpression == null || parsedExpression.length == 0) {
return null; return null;
} }
return HSSFFormulaParser.toFormulaString(workbook, parsedExpression); return HSSFFormulaParser.toFormulaString(workbook, parsedExpression);

View File

@ -17,6 +17,7 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import org.apache.poi.hssf.record.CFRule12Record;
import org.apache.poi.hssf.record.CFRuleBase; import org.apache.poi.hssf.record.CFRuleBase;
import org.apache.poi.hssf.record.CFRuleRecord; import org.apache.poi.hssf.record.CFRuleRecord;
import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate; import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate;
@ -24,6 +25,7 @@ import org.apache.poi.hssf.record.aggregates.ConditionalFormattingTable;
import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.ConditionalFormatting; import org.apache.poi.ss.usermodel.ConditionalFormatting;
import org.apache.poi.ss.usermodel.ConditionalFormattingRule; import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
import org.apache.poi.ss.usermodel.SheetConditionalFormatting; import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
@ -95,6 +97,20 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor
return new HSSFConditionalFormattingRule(wb, rr); return new HSSFConditionalFormattingRule(wb, rr);
} }
/**
* A factory method allowing the creation of conditional formatting
* rules using an Icon Set / Multi-State formatting/
*/
// TODO Implement
/*
public HSSFConditionalFormattingRule createConditionalFormattingRule(
IconSet iconSet) { // TODO Multi-State data for it
HSSFWorkbook wb = _sheet.getWorkbook();
CFRule12Record rr = CFRule12Record.create(_sheet, iconSet);
return new HSSFConditionalFormattingRule(wb, rr);
}
*/
/** /**
* Adds a copy of HSSFConditionalFormatting object to the sheet * Adds a copy of HSSFConditionalFormatting object to the sheet
* <p>This method could be used to copy HSSFConditionalFormatting object * <p>This method could be used to copy HSSFConditionalFormatting object

View File

@ -24,6 +24,7 @@ import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFConditionalFormatting; import org.apache.poi.hssf.usermodel.HSSFConditionalFormatting;
import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule; import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule;
import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
/** /**
@ -544,6 +545,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
Sheet s = wb.getSheet("CF"); Sheet s = wb.getSheet("CF");
ConditionalFormatting cf = null; ConditionalFormatting cf = null;
ConditionalFormattingRule cr = null; ConditionalFormattingRule cr = null;
IconMultiStateFormatting icon = null;
// Sanity check data // Sanity check data
assertEquals("Values", s.getRow(0).getCell(0).toString()); assertEquals("Values", s.getRow(0).getCell(0).toString());
@ -644,7 +646,30 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
// Colours R->G - Column F // Colours R->G - Column F
// Colours BWR - Column G // Colours BWR - Column G
// Icons : Default - Column H // Icons : Default - Column H
cf = sheetCF.getConditionalFormattingAt(5);
assertEquals(1, cf.getFormattingRanges().length);
assertEquals("H2:H17", cf.getFormattingRanges()[0].formatAsString());
assertEquals(1, cf.getNumberOfRules());
cr = cf.getRule(0);
assertEquals(ConditionType.ICON_SET, cr.getConditionTypeType());
assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());
assertEquals(null, cr.getFormula1());
assertEquals(null, cr.getFormula2());
if (cr instanceof HSSFConditionalFormattingRule) {
HSSFConditionalFormattingRule hcr = (HSSFConditionalFormattingRule)cr;
icon = hcr.getMultiStateFormatting();
assertNotNull(icon);
assertEquals(IconSet.GYR_3_TRAFFIC_LIGHTS, icon.getIconSet());
assertEquals(false, icon.isIconOnly());
assertEquals(false, icon.isReversed());
// TODO Check the rest
} else {
// TODO XSSF Support
}
// Icons : 3 signs - Column I // Icons : 3 signs - Column I
// Icons : 3 traffic lights 2 - Column J // Icons : 3 traffic lights 2 - Column J
// Icons : 4 traffic lights - Column K // Icons : 4 traffic lights - Column K
@ -834,6 +859,10 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
assertEquals(BorderFormatting.BORDER_HAIR, r1fp.getBorderRight()); assertEquals(BorderFormatting.BORDER_HAIR, r1fp.getBorderRight());
} }
public void testCreateIconFormatting() {
// TODO Implement for XSSF, then test here
}
public void testBug55380() { public void testBug55380() {
Workbook wb = _testDataProvider.createWorkbook(); Workbook wb = _testDataProvider.createWorkbook();
Sheet sheet = wb.createSheet(); Sheet sheet = wb.createSheet();