From 886d73deee98fad27cb9bdfa9992ce7d771e3684 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 20 Jul 2015 03:52:50 +0000 Subject: [PATCH] #58130 CF DataBar support git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691868 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/CFRule12Record.java | 4 +- .../HSSFConditionalFormattingRule.java | 50 ++++++++--- .../hssf/usermodel/HSSFDataBarFormatting.java | 86 +++++++++++++++++++ .../usermodel/ConditionalFormattingRule.java | 5 ++ .../XSSFConditionalFormattingRule.java | 36 ++++++++ .../xssf/usermodel/XSSFDataBarFormatting.java | 83 ++++++++++++++++++ .../XSSFSheetConditionalFormatting.java | 8 +- .../BaseTestConditionalFormatting.java | 37 ++++++-- 8 files changed, 289 insertions(+), 20 deletions(-) create mode 100644 src/java/org/apache/poi/hssf/usermodel/HSSFDataBarFormatting.java create mode 100644 src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java diff --git a/src/java/org/apache/poi/hssf/record/CFRule12Record.java b/src/java/org/apache/poi/hssf/record/CFRule12Record.java index 6cb977433a..0f29305c5c 100644 --- a/src/java/org/apache/poi/hssf/record/CFRule12Record.java +++ b/src/java/org/apache/poi/hssf/record/CFRule12Record.java @@ -132,8 +132,8 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { ComparisonOperator.NO_COMPARISON); DataBarFormatting dbf = r.createDataBarFormatting(); dbf.setColor(color); - dbf.setPercentMin((byte)50); - dbf.setPercentMax((byte)50); + dbf.setPercentMin((byte)0); + dbf.setPercentMax((byte)100); DataBarThreshold min = new DataBarThreshold(); min.setType(RangeType.MIN.id); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java index a6cdbcfd03..79545327ba 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java @@ -24,6 +24,7 @@ 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.ColorGradientFormatting; +import org.apache.poi.hssf.record.cf.DataBarFormatting; import org.apache.poi.hssf.record.cf.FontFormatting; import org.apache.poi.hssf.record.cf.IconMultiStateFormatting; import org.apache.poi.hssf.record.cf.PatternFormatting; @@ -69,8 +70,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin return (CFRule12Record)cfRuleRecord; } - private HSSFFontFormatting getFontFormatting(boolean create) - { + private HSSFFontFormatting getFontFormatting(boolean create) { FontFormatting fontFormatting = cfRuleRecord.getFontFormatting(); if ( fontFormatting != null) { @@ -92,8 +92,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin /** * @return - font formatting object if defined, null otherwise */ - public HSSFFontFormatting getFontFormatting() - { + public HSSFFontFormatting getFontFormatting() { return getFontFormatting(false); } /** @@ -101,13 +100,11 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin * otherwise just return existing object. * @return - font formatting object, never returns null. */ - public HSSFFontFormatting createFontFormatting() - { + public HSSFFontFormatting createFontFormatting() { return getFontFormatting(true); } - private HSSFBorderFormatting getBorderFormatting(boolean create) - { + private HSSFBorderFormatting getBorderFormatting(boolean create) { BorderFormatting borderFormatting = cfRuleRecord.getBorderFormatting(); if ( borderFormatting != null) { @@ -128,8 +125,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin /** * @return - border formatting object if defined, null otherwise */ - public HSSFBorderFormatting getBorderFormatting() - { + public HSSFBorderFormatting getBorderFormatting() { return getBorderFormatting(false); } /** @@ -137,8 +133,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin * otherwise just return existing object. * @return - border formatting object, never returns null. */ - public HSSFBorderFormatting createBorderFormatting() - { + public HSSFBorderFormatting createBorderFormatting() { return getBorderFormatting(true); } @@ -179,6 +174,37 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin return getPatternFormatting(true); } + private HSSFDataBarFormatting getDataBarFormatting(boolean create) { + CFRule12Record cfRule12Record = getCFRule12Record(create); + DataBarFormatting databarFormatting = cfRule12Record.getDataBarFormatting(); + if (databarFormatting != null) + { + return new HSSFDataBarFormatting(cfRule12Record, sheet); + } + else if( create ) + { + databarFormatting = cfRule12Record.createDataBarFormatting(); + return new HSSFDataBarFormatting(cfRule12Record, sheet); + } + else + { + return null; + } + } + /** + * @return databar / data-bar formatting object if defined, null otherwise + */ + public HSSFDataBarFormatting getDataBarFormatting() { + return getDataBarFormatting(false); + } + /** + * create a new databar / data-bar formatting object if it does not exist, + * otherwise just return the existing object. + */ + public HSSFDataBarFormatting createDataBarFormatting() { + return getDataBarFormatting(true); + } + private HSSFIconMultiStateFormatting getMultiStateFormatting(boolean create) { CFRule12Record cfRule12Record = getCFRule12Record(create); IconMultiStateFormatting iconFormatting = cfRule12Record.getMultiStateFormatting(); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFDataBarFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFDataBarFormatting.java new file mode 100644 index 0000000000..f796dd80dd --- /dev/null +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFDataBarFormatting.java @@ -0,0 +1,86 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.usermodel; + +import org.apache.poi.hssf.record.CFRule12Record; +import org.apache.poi.hssf.record.cf.DataBarFormatting; +import org.apache.poi.hssf.record.cf.DataBarThreshold; +import org.apache.poi.ss.usermodel.Color; + +/** + * High level representation for DataBar / Data-Bar Formatting + * component of Conditional Formatting settings + */ +public final class HSSFDataBarFormatting implements org.apache.poi.ss.usermodel.DataBarFormatting { + private final HSSFSheet sheet; + private final CFRule12Record cfRule12Record; + private final DataBarFormatting databarFormatting; + + protected HSSFDataBarFormatting(CFRule12Record cfRule12Record, HSSFSheet sheet) { + this.sheet = sheet; + this.cfRule12Record = cfRule12Record; + this.databarFormatting = this.cfRule12Record.getDataBarFormatting(); + } + + public boolean isLeftToRight() { + return !databarFormatting.isReversed(); + } + public void setLeftToRight(boolean ltr) { + databarFormatting.setReversed(!ltr); + } + + public int getWidthMin() { + return databarFormatting.getPercentMin(); + } + public void setWidthMin(int width) { + databarFormatting.setPercentMin((byte)width); + } + + public int getWidthMax() { + return databarFormatting.getPercentMax(); + } + public void setWidthMax(int width) { + databarFormatting.setPercentMax((byte)width); + } + + public HSSFExtendedColor getColor() { + return new HSSFExtendedColor(databarFormatting.getColor()); + } + public void setColor(Color color) { + HSSFExtendedColor hcolor = (HSSFExtendedColor)color; + databarFormatting.setColor(hcolor.getExtendedColor()); + } + + public HSSFConditionalFormattingThreshold getMinThreshold() { + return new HSSFConditionalFormattingThreshold(databarFormatting.getThresholdMin(), sheet); + } + public HSSFConditionalFormattingThreshold getMaxThreshold() { + return new HSSFConditionalFormattingThreshold(databarFormatting.getThresholdMax(), sheet); + } + + public boolean isIconOnly() { + return databarFormatting.isIconOnly(); + } + public void setIconOnly(boolean only) { + databarFormatting.setIconOnly(only); + } + + public HSSFConditionalFormattingThreshold createThreshold() { + return new HSSFConditionalFormattingThreshold(new DataBarThreshold(), sheet); + } +} diff --git a/src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java b/src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java index 6f71b7b636..199e45e351 100644 --- a/src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java +++ b/src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java @@ -78,6 +78,11 @@ public interface ConditionalFormattingRule { */ PatternFormatting getPatternFormatting(); + /** + * @return - databar / data-bar formatting object if defined, null otherwise + */ + DataBarFormatting getDataBarFormatting(); + /** * @return - icon / multi-state formatting object if defined, null otherwise */ diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFConditionalFormattingRule.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFConditionalFormattingRule.java index 2523991403..dc2e6f6298 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFConditionalFormattingRule.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFConditionalFormattingRule.java @@ -173,6 +173,42 @@ public class XSSFConditionalFormattingRule implements ConditionalFormattingRule return new XSSFPatternFormatting(dxf.getFill()); } + public XSSFDataBarFormatting createDataBarFormatting(XSSFColor color) { + // Is it already there? + if (_cfRule.isSetDataBar() && _cfRule.getType() == STCfType.DATA_BAR) + return getDataBarFormatting(); + + // Mark it as being a Data Bar + _cfRule.setType(STCfType.DATA_BAR); + + // Ensure the right element + CTDataBar bar = null; + if (_cfRule.isSetDataBar()) { + bar = _cfRule.getDataBar(); + } else { + bar = _cfRule.addNewDataBar(); + } + // Set the color + bar.setColor(color.getCTColor()); + + // Add the default thresholds + CTCfvo min = bar.addNewCfvo(); + min.setType(STCfvoType.Enum.forString(RangeType.MIN.name)); + CTCfvo max = bar.addNewCfvo(); + max.setType(STCfvoType.Enum.forString(RangeType.MAX.name)); + + // Wrap and return + return new XSSFDataBarFormatting(bar); + } + public XSSFDataBarFormatting getDataBarFormatting() { + if (_cfRule.isSetDataBar()) { + CTDataBar bar = _cfRule.getDataBar(); + return new XSSFDataBarFormatting(bar); + } else { + return null; + } + } + public XSSFIconMultiStateFormatting createMultiStateFormatting(IconSet iconSet) { // Is it already there? if (_cfRule.isSetIconSet() && _cfRule.getType() == STCfType.ICON_SET) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java new file mode 100644 index 0000000000..bc7b9cf718 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java @@ -0,0 +1,83 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ +package org.apache.poi.xssf.usermodel; + +import org.apache.poi.ss.usermodel.Color; +import org.apache.poi.ss.usermodel.DataBarFormatting; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataBar; + +/** + * High level representation for DataBar / Data Bar Formatting + * component of Conditional Formatting settings + */ +public class XSSFDataBarFormatting implements DataBarFormatting { + CTDataBar _databar; + + /*package*/ XSSFDataBarFormatting(CTDataBar databar){ + _databar = databar; + } + + public boolean isIconOnly() { + if (_databar.isSetShowValue()) + return !_databar.getShowValue(); + return false; + } + public void setIconOnly(boolean only) { + _databar.setShowValue(!only); + } + + public boolean isLeftToRight() { + return true; + } + public void setLeftToRight(boolean ltr) { + // TODO How does XSSF encode this? + } + + public int getWidthMin() { + return 0; + } + public void setWidthMin(int width) { + // TODO How does XSSF encode this? + } + + public int getWidthMax() { + return 100; + } + public void setWidthMax(int width) { + // TODO How does XSSF encode this? + } + + public XSSFColor getColor() { + return new XSSFColor(_databar.getColor()); + } + public void setColor(Color color) { + _databar.setColor( ((XSSFColor)color).getCTColor() ); + } + + public XSSFConditionalFormattingThreshold getMinThreshold() { + return new XSSFConditionalFormattingThreshold(_databar.getCfvoArray(0)); + } + public XSSFConditionalFormattingThreshold getMaxThreshold() { + return new XSSFConditionalFormattingThreshold(_databar.getCfvoArray(1)); + } + + public XSSFConditionalFormattingThreshold createThreshold() { + return new XSSFConditionalFormattingThreshold(_databar.addNewCfvo()); + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java index 3c155c77e2..ca1530514f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java @@ -131,7 +131,13 @@ public class XSSFSheetConditionalFormatting implements SheetConditionalFormattin * {@link XSSFDataBarFormatting#getMaxThreshold()} */ public XSSFConditionalFormattingRule createConditionalFormattingRule(XSSFColor color) { - throw new IllegalStateException("Not Implemented Yet!"); // TODO Implement + XSSFConditionalFormattingRule rule = new XSSFConditionalFormattingRule(_sheet); + + // Have it setup, with suitable defaults + rule.createDataBarFormatting(color); + + // All done! + return rule; } public XSSFConditionalFormattingRule createConditionalFormattingRule(ExtendedColor color) { return createConditionalFormattingRule((XSSFColor)color); diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java index 241a2efcb0..d14b556f44 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java @@ -641,11 +641,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { cf = sheetCF.getConditionalFormattingAt(2); assertEquals(1, cf.getFormattingRanges().length); assertEquals("E2:E17", cf.getFormattingRanges()[0].formatAsString()); - - assertEquals(1, cf.getNumberOfRules()); - cr = cf.getRule(0); - assertEquals(ConditionType.DATA_BAR, cr.getConditionTypeType()); - // TODO Support Data Bars, then check the rest of this rule + assertDataBar(cf, "FF63C384"); // Colours Red->Yellow->Green - Column F @@ -774,6 +770,37 @@ public abstract class BaseTestConditionalFormatting extends TestCase { // TODO Support EXT formattings } + private void assertDataBar(ConditionalFormatting cf, String color) { + assertEquals(1, cf.getNumberOfRules()); + ConditionalFormattingRule cr = cf.getRule(0); + assertDataBar(cr, color); + } + private void assertDataBar(ConditionalFormattingRule cr, String color) { + assertEquals(ConditionType.DATA_BAR, cr.getConditionTypeType()); + assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation()); + assertEquals(null, cr.getFormula1()); + assertEquals(null, cr.getFormula2()); + + DataBarFormatting databar = cr.getDataBarFormatting(); + assertNotNull(databar); + assertEquals(false, databar.isIconOnly()); + assertEquals(true, databar.isLeftToRight()); + assertEquals(0, databar.getWidthMin()); + assertEquals(100, databar.getWidthMax()); + + assertColour(color, databar.getColor()); + + ConditionalFormattingThreshold th; + th = databar.getMinThreshold(); + assertEquals(RangeType.MIN, th.getRangeType()); + assertEquals(null, th.getValue()); + assertEquals(null, th.getFormula()); + th = databar.getMaxThreshold(); + assertEquals(RangeType.MAX, th.getRangeType()); + assertEquals(null, th.getValue()); + assertEquals(null, th.getFormula()); + } + private void assertIconSetPercentages(ConditionalFormatting cf, IconSet iconset, Double...vals) { assertEquals(1, cf.getNumberOfRules()); ConditionalFormattingRule cr = cf.getRule(0); -- 2.39.5