]> source.dussan.org Git - poi.git/commitdiff
#58130 CF DataBar support
authorNick Burch <nick@apache.org>
Mon, 20 Jul 2015 03:52:50 +0000 (03:52 +0000)
committerNick Burch <nick@apache.org>
Mon, 20 Jul 2015 03:52:50 +0000 (03:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691868 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/CFRule12Record.java
src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java
src/java/org/apache/poi/hssf/usermodel/HSSFDataBarFormatting.java [new file with mode: 0644]
src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFConditionalFormattingRule.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataBarFormatting.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java

index 6cb977433a01c1f4ad3c26ecdaf7b7e91b02febd..0f29305c5c28f69aae6fa1b62fbfc6f54d27f413 100644 (file)
@@ -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);
index a6cdbcfd0340ac7322166464edbd965a22ab3d1f..79545327bab7cca88518add6fe360e508884c465 100644 (file)
@@ -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,  <code>null</code> 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 <code>null</code>.
      */
-    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,  <code>null</code> 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 <code>null</code>.
      */
-    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, <code>null</code> 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 (file)
index 0000000..f796dd8
--- /dev/null
@@ -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);
+    }
+}
index 6f71b7b636abe7a293029b8d4c4e1394f706c042..199e45e35189e680fd592b3e2c711fbdc45ca6a4 100644 (file)
@@ -78,6 +78,11 @@ public interface ConditionalFormattingRule {
      */\r
     PatternFormatting getPatternFormatting();\r
 \r
+    /**\r
+     * @return - databar / data-bar formatting object if defined, <code>null</code> otherwise\r
+     */\r
+    DataBarFormatting getDataBarFormatting();\r
+    \r
     /**\r
      * @return - icon / multi-state formatting object if defined, <code>null</code> otherwise\r
      */\r
index 25239914039667c854abea8c70d106e5682c3050..dc2e6f62988b0d095afb6a043db5b479d7e540c9 100644 (file)
@@ -173,6 +173,42 @@ public class XSSFConditionalFormattingRule implements ConditionalFormattingRule
         return new XSSFPatternFormatting(dxf.getFill());\r
     }\r
     \r
+    public XSSFDataBarFormatting createDataBarFormatting(XSSFColor color) {\r
+        // Is it already there?\r
+        if (_cfRule.isSetDataBar() && _cfRule.getType() == STCfType.DATA_BAR)\r
+            return getDataBarFormatting();\r
+        \r
+        // Mark it as being a Data Bar\r
+        _cfRule.setType(STCfType.DATA_BAR);\r
+\r
+        // Ensure the right element\r
+        CTDataBar bar = null;\r
+        if (_cfRule.isSetDataBar()) {\r
+            bar = _cfRule.getDataBar();\r
+        } else {\r
+            bar = _cfRule.addNewDataBar();\r
+        }\r
+        // Set the color\r
+        bar.setColor(color.getCTColor());\r
+        \r
+        // Add the default thresholds\r
+        CTCfvo min = bar.addNewCfvo();\r
+        min.setType(STCfvoType.Enum.forString(RangeType.MIN.name));\r
+        CTCfvo max = bar.addNewCfvo();\r
+        max.setType(STCfvoType.Enum.forString(RangeType.MAX.name));\r
+        \r
+        // Wrap and return\r
+        return new XSSFDataBarFormatting(bar);\r
+    }\r
+    public XSSFDataBarFormatting getDataBarFormatting() {\r
+        if (_cfRule.isSetDataBar()) {\r
+            CTDataBar bar = _cfRule.getDataBar();\r
+            return new XSSFDataBarFormatting(bar);\r
+        } else {\r
+            return null;\r
+        }\r
+    }\r
+    \r
     public XSSFIconMultiStateFormatting createMultiStateFormatting(IconSet iconSet) {\r
         // Is it already there?\r
         if (_cfRule.isSetIconSet() && _cfRule.getType() == STCfType.ICON_SET)\r
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 (file)
index 0000000..bc7b9cf
--- /dev/null
@@ -0,0 +1,83 @@
+/*\r
+ *  ====================================================================\r
+ *    Licensed to the Apache Software Foundation (ASF) under one or more\r
+ *    contributor license agreements.  See the NOTICE file distributed with\r
+ *    this work for additional information regarding copyright ownership.\r
+ *    The ASF licenses this file to You under the Apache License, Version 2.0\r
+ *    (the "License"); you may not use this file except in compliance with\r
+ *    the License.  You may obtain a copy of the License at\r
+ *\r
+ *        http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *    Unless required by applicable law or agreed to in writing, software\r
+ *    distributed under the License is distributed on an "AS IS" BASIS,\r
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *    See the License for the specific language governing permissions and\r
+ *    limitations under the License.\r
+ * ====================================================================\r
+ */\r
+package org.apache.poi.xssf.usermodel;\r
+\r
+import org.apache.poi.ss.usermodel.Color;\r
+import org.apache.poi.ss.usermodel.DataBarFormatting;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataBar;\r
+\r
+/**\r
+ * High level representation for DataBar / Data Bar Formatting \r
+ *  component of Conditional Formatting settings\r
+ */\r
+public class XSSFDataBarFormatting implements DataBarFormatting {\r
+    CTDataBar _databar;\r
+\r
+    /*package*/ XSSFDataBarFormatting(CTDataBar databar){\r
+        _databar = databar;\r
+    }\r
+\r
+    public boolean isIconOnly() {\r
+        if (_databar.isSetShowValue())\r
+            return !_databar.getShowValue();\r
+        return false;\r
+    }\r
+    public void setIconOnly(boolean only) {\r
+        _databar.setShowValue(!only);\r
+    }\r
+\r
+    public boolean isLeftToRight() {\r
+        return true;\r
+    }\r
+    public void setLeftToRight(boolean ltr) {\r
+        // TODO How does XSSF encode this?\r
+    }\r
+\r
+    public int getWidthMin() {\r
+        return 0;\r
+    }\r
+    public void setWidthMin(int width) {\r
+        // TODO How does XSSF encode this?\r
+    }\r
+\r
+    public int getWidthMax() {\r
+        return 100;\r
+    }\r
+    public void setWidthMax(int width) {\r
+        // TODO How does XSSF encode this?\r
+    }\r
+\r
+    public XSSFColor getColor() {\r
+        return new XSSFColor(_databar.getColor());\r
+    }\r
+    public void setColor(Color color) {\r
+        _databar.setColor( ((XSSFColor)color).getCTColor() );\r
+    }\r
+\r
+    public XSSFConditionalFormattingThreshold getMinThreshold() {\r
+        return new XSSFConditionalFormattingThreshold(_databar.getCfvoArray(0));\r
+    }\r
+    public XSSFConditionalFormattingThreshold getMaxThreshold() {\r
+        return new XSSFConditionalFormattingThreshold(_databar.getCfvoArray(1));\r
+    }\r
+\r
+    public XSSFConditionalFormattingThreshold createThreshold() {\r
+        return new XSSFConditionalFormattingThreshold(_databar.addNewCfvo());\r
+    }\r
+}\r
index 3c155c77e280a235044f44a004902f9fbf51b038..ca1530514fe8419fba8e6d29f025ed73e23fda74 100644 (file)
@@ -131,7 +131,13 @@ public class XSSFSheetConditionalFormatting implements SheetConditionalFormattin
      *  {@link XSSFDataBarFormatting#getMaxThreshold()}\r
      */\r
     public XSSFConditionalFormattingRule createConditionalFormattingRule(XSSFColor color) {\r
-        throw new IllegalStateException("Not Implemented Yet!"); // TODO Implement\r
+        XSSFConditionalFormattingRule rule = new XSSFConditionalFormattingRule(_sheet);\r
+        \r
+        // Have it setup, with suitable defaults\r
+        rule.createDataBarFormatting(color);\r
+        \r
+        // All done!\r
+        return rule;\r
     }\r
     public XSSFConditionalFormattingRule createConditionalFormattingRule(ExtendedColor color) {\r
         return createConditionalFormattingRule((XSSFColor)color);\r
index 241a2efcb0b381ea6bed4cdb6a33decf1aa3ea70..d14b556f440dd6a8269c5c6b057a96e56eda9f24 100644 (file)
@@ -641,11 +641,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
         cf = sheetCF.getConditionalFormattingAt(2);\r
         assertEquals(1, cf.getFormattingRanges().length);\r
         assertEquals("E2:E17", cf.getFormattingRanges()[0].formatAsString());\r
-        \r
-        assertEquals(1, cf.getNumberOfRules());\r
-        cr = cf.getRule(0);\r
-        assertEquals(ConditionType.DATA_BAR, cr.getConditionTypeType());\r
-        // TODO Support Data Bars, then check the rest of this rule\r
+        assertDataBar(cf, "FF63C384");\r
         \r
         \r
         // Colours Red->Yellow->Green - Column F\r
@@ -774,6 +770,37 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
         // TODO Support EXT formattings\r
     }\r
     \r
+    private void assertDataBar(ConditionalFormatting cf, String color) {\r
+        assertEquals(1, cf.getNumberOfRules());\r
+        ConditionalFormattingRule cr = cf.getRule(0);\r
+        assertDataBar(cr, color);\r
+    }\r
+    private void assertDataBar(ConditionalFormattingRule cr, String color) {\r
+        assertEquals(ConditionType.DATA_BAR, cr.getConditionTypeType());\r
+        assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation());\r
+        assertEquals(null, cr.getFormula1());\r
+        assertEquals(null, cr.getFormula2());\r
+        \r
+        DataBarFormatting databar = cr.getDataBarFormatting();\r
+        assertNotNull(databar);\r
+        assertEquals(false, databar.isIconOnly());\r
+        assertEquals(true, databar.isLeftToRight());\r
+        assertEquals(0, databar.getWidthMin());\r
+        assertEquals(100, databar.getWidthMax());\r
+        \r
+        assertColour(color, databar.getColor());\r
+        \r
+        ConditionalFormattingThreshold th;\r
+        th = databar.getMinThreshold();\r
+        assertEquals(RangeType.MIN, th.getRangeType());\r
+        assertEquals(null, th.getValue());\r
+        assertEquals(null, th.getFormula());\r
+        th = databar.getMaxThreshold();\r
+        assertEquals(RangeType.MAX, th.getRangeType());\r
+        assertEquals(null, th.getValue());\r
+        assertEquals(null, th.getFormula());\r
+    }\r
+    \r
     private void assertIconSetPercentages(ConditionalFormatting cf, IconSet iconset, Double...vals) {\r
         assertEquals(1, cf.getNumberOfRules());\r
         ConditionalFormattingRule cr = cf.getRule(0);\r