From 9c9023524a146acfe4752c3c44325df9c4fce579 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 15 Jul 2015 00:04:53 +0000 Subject: [PATCH] Further HSSF support towards CF IconSets #58130 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691113 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/CFRule12Record.java | 8 ++++ .../HSSFConditionalFormattingRule.java | 43 ++++++++++++++++++- .../HSSFSheetConditionalFormatting.java | 16 +++++++ .../BaseTestConditionalFormatting.java | 29 +++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/hssf/record/CFRule12Record.java b/src/java/org/apache/poi/hssf/record/CFRule12Record.java index 2c684e3c1..230cb1dff 100644 --- a/src/java/org/apache/poi/hssf/record/CFRule12Record.java +++ b/src/java/org/apache/poi/hssf/record/CFRule12Record.java @@ -173,6 +173,14 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { public IconMultiStateFormatting getMultiStateFormatting() { 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 diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java index f3a81c035..c36748e97 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java @@ -18,11 +18,13 @@ package org.apache.poi.hssf.usermodel; 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.ComparisonOperator; import org.apache.poi.hssf.record.CFRuleRecord; import org.apache.poi.hssf.record.cf.BorderFormatting; 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.ss.formula.ptg.Ptg; import org.apache.poi.ss.usermodel.ConditionType; @@ -165,7 +167,46 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin { 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, null 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 */ @@ -205,7 +246,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin } private String toFormulaString(Ptg[] parsedExpression) { - if(parsedExpression ==null) { + if(parsedExpression == null || parsedExpression.length == 0) { return null; } return HSSFFormulaParser.toFormulaString(workbook, parsedExpression); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java index 1064467db..f0df8e522 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java @@ -17,6 +17,7 @@ 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.CFRuleRecord; 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.usermodel.ConditionalFormatting; 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.util.CellRangeAddress; @@ -94,6 +96,20 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor CFRuleRecord rr = CFRuleRecord.create(_sheet, formula); 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 diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java index a803fc83c..6276b5829 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java @@ -24,6 +24,7 @@ import junit.framework.TestCase; import org.apache.poi.hssf.usermodel.HSSFConditionalFormatting; import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule; import org.apache.poi.ss.ITestDataProvider; +import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet; import org.apache.poi.ss.util.CellRangeAddress; /** @@ -544,6 +545,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { Sheet s = wb.getSheet("CF"); ConditionalFormatting cf = null; ConditionalFormattingRule cr = null; + IconMultiStateFormatting icon = null; // Sanity check data assertEquals("Values", s.getRow(0).getCell(0).toString()); @@ -644,7 +646,30 @@ public abstract class BaseTestConditionalFormatting extends TestCase { // Colours R->G - Column F // Colours BWR - Column G + // 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 traffic lights 2 - Column J // Icons : 4 traffic lights - Column K @@ -834,6 +859,10 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals(BorderFormatting.BORDER_HAIR, r1fp.getBorderRight()); } + public void testCreateIconFormatting() { + // TODO Implement for XSSF, then test here + } + public void testBug55380() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet = wb.createSheet();