]> source.dussan.org Git - poi.git/commitdiff
Provide a Conditional Formatting type class, and deprecate the byte-based types,...
authorNick Burch <nick@apache.org>
Mon, 13 Jul 2015 19:47:21 +0000 (19:47 +0000)
committerNick Burch <nick@apache.org>
Mon, 13 Jul 2015 19:47:21 +0000 (19:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1690803 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java
src/java/org/apache/poi/ss/usermodel/ComparisonOperator.java
src/java/org/apache/poi/ss/usermodel/ConditionType.java [new file with mode: 0644]
src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java
src/java/org/apache/poi/ss/usermodel/SheetConditionalFormatting.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFConditionalFormattingRule.java

index 93af503de384010c7655ca3593a6cddd203327a6..b87d8813ca8615c8cce1c32a9c6fd24d47846956 100644 (file)
@@ -25,6 +25,7 @@ import org.apache.poi.hssf.record.cf.BorderFormatting;
 import org.apache.poi.hssf.record.cf.FontFormatting;
 import org.apache.poi.hssf.record.cf.PatternFormatting;
 import org.apache.poi.ss.formula.ptg.Ptg;
+import org.apache.poi.ss.usermodel.ConditionType;
 import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
 
 /**
@@ -33,8 +34,7 @@ import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
  * It allows to specify formula based conditions for the Conditional Formatting
  * and the formatting settings such as font, border and pattern.
  */
-public final class HSSFConditionalFormattingRule implements ConditionalFormattingRule
-{
+public final class HSSFConditionalFormattingRule implements ConditionalFormattingRule {
        private static final byte CELL_COMPARISON = CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS;
 
        private final CFRuleBase cfRuleRecord;
@@ -172,6 +172,12 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
        public byte getConditionType() {
                return cfRuleRecord.getConditionType();
        }
+    /**
+     * @return -  the conditiontype for the cfrule
+     */
+       public ConditionType getConditionTypeType() {
+           return ConditionType.forId(getConditionType());
+       }
 
        /**
         * @return - the comparisionoperatation for the cfrule
index 7e29cbf4c9ea1929726ce29f3992941ac8e2d04a..7eee789037cecf471b052ae87e7fd30ef9c5cfea 100644 (file)
@@ -24,9 +24,6 @@ package org.apache.poi.ss.usermodel;
  * <p>\r
  * For example, "highlight cells that begin with "M2" and contain "Mountain Gear".\r
  * </p>\r
- *\r
- * @author Dmitriy Kumshayev\r
- * @author Yegor Kozlov\r
  */\r
 public final class ComparisonOperator {\r
     public static final byte NO_COMPARISON = 0;\r
diff --git a/src/java/org/apache/poi/ss/usermodel/ConditionType.java b/src/java/org/apache/poi/ss/usermodel/ConditionType.java
new file mode 100644 (file)
index 0000000..4f8de4f
--- /dev/null
@@ -0,0 +1,88 @@
+/*\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
+\r
+package org.apache.poi.ss.usermodel;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+/**\r
+ * Represents a type of a conditional formatting rule\r
+ */\r
+public class ConditionType {\r
+    private static Map<Integer,ConditionType> lookup = new HashMap<Integer, ConditionType>();\r
+    \r
+    /**\r
+     * This conditional formatting rule compares a cell value\r
+     * to a formula calculated result, using an operator\r
+     */\r
+    public static final ConditionType CELL_VALUE_IS = \r
+            new ConditionType(1, "cellIs");\r
+\r
+    /**\r
+     * This conditional formatting rule contains a formula to evaluate.\r
+     * When the formula result is true, the cell is highlighted.\r
+     */\r
+    public static final ConditionType FORMULA = \r
+            new ConditionType(2, "expression");\r
+    \r
+    /**\r
+     * This conditional formatting rule contains a color scale,\r
+     * with the cell background set according to a gradient.\r
+     */\r
+    public static final ConditionType COLOR_SCALE = \r
+            new ConditionType(3, "colorScale");\r
+    \r
+    /**\r
+     * This conditional formatting rule sets a data bar, with the\r
+     *  cell populated with bars based on their values\r
+     */\r
+    public static final ConditionType DATA_BAR = \r
+            new ConditionType(4, "dataBar");\r
+    \r
+    /**\r
+     * This conditional formatting rule that files the values\r
+     */\r
+    public static final ConditionType FILTER = \r
+            new ConditionType(5, null);\r
+    \r
+    /**\r
+     * This conditional formatting rule sets a data bar, with the\r
+     *  cell populated with bars based on their values\r
+     */\r
+    public static final ConditionType ICON_SET = \r
+            new ConditionType(6, "iconSet");\r
+    \r
+    \r
+    public final byte id;\r
+    public final String type;\r
+\r
+    \r
+    public static ConditionType forId(byte id) {\r
+        return forId((int)id);\r
+    }\r
+    public static ConditionType forId(int id) {\r
+        return lookup.get(id);\r
+    }\r
+        \r
+    private ConditionType(int id, String type) {\r
+        this.id = (byte)id; this.type = type;\r
+        lookup.put(id, this);\r
+    }\r
+}\r
index 2e2e3d734c75e3f22c16e743104e3cb3b3f201b1..04c04a269d2fa35a5e90ac5f8364f99662e61063 100644 (file)
@@ -19,6 +19,8 @@
 \r
 package org.apache.poi.ss.usermodel;\r
 \r
+import static org.apache.poi.ss.usermodel.ConditionType.*;\r
+\r
 /**\r
  * Represents a description of a conditional formatting rule\r
  */\r
@@ -26,14 +28,16 @@ public interface ConditionalFormattingRule {
     /**\r
      * This conditional formatting rule compares a cell value\r
      * to a formula calculated result, using an operator\r
+     * @deprecated Use {@link ConditionType#CELL_VALUE_IS}\r
      */\r
-    public static final byte CONDITION_TYPE_CELL_VALUE_IS = 1;\r
+    public static final byte CONDITION_TYPE_CELL_VALUE_IS = CELL_VALUE_IS.id;\r
 \r
     /**\r
      *  This conditional formatting rule contains a formula to evaluate.\r
      *  When the formula result is true, the cell is highlighted.\r
+     * @deprecated Use {@link ConditionType#FORMULA}\r
      */\r
-    public static final byte CONDITION_TYPE_FORMULA = 2;\r
+    public static final byte CONDITION_TYPE_FORMULA = FORMULA.id;\r
 \r
     /**\r
      * Create a new border formatting structure if it does not exist,\r
@@ -77,12 +81,20 @@ public interface ConditionalFormattingRule {
     /**\r
      * Type of conditional formatting rule.\r
      * <p>\r
-     * MUST be either {@link #CONDITION_TYPE_CELL_VALUE_IS} or  {@link #CONDITION_TYPE_FORMULA}\r
+     * MUST be one of the IDs of a {@link ConditionType}\r
      * </p>\r
      *\r
      * @return the type of condition\r
+     * @deprecated Use {@link #getConditionTypeType()}\r
      */\r
     byte getConditionType();\r
+    \r
+    /**\r
+     * Type of conditional formatting rule.\r
+     *\r
+     * @return the type of condition\r
+     */\r
+    ConditionType getConditionTypeType();\r
 \r
     /**\r
      * The comparison function used when the type of conditional formatting is set to\r
index 6d8c328c0c17c87a6f86936f1465cf9e7964685e..7f54f301582c7078b528ad4f4bc77db95590cc0b 100644 (file)
@@ -83,7 +83,7 @@ public interface SheetConditionalFormatting {
      * <p>\r
      * The created conditional formatting rule compares a cell value\r
      * to a formula calculated result, using the specified operator.\r
-     * The type  of the created condition is {@link ConditionalFormattingRule#CONDITION_TYPE_CELL_VALUE_IS}\r
+     * The type  of the created condition is {@link ConditionalFormattingRule#CONDITION_CELL_VALUE_IS}\r
      * </p>\r
      *\r
      * @param comparisonOperation - MUST be a constant value from\r
@@ -112,7 +112,7 @@ public interface SheetConditionalFormatting {
      * Create a conditional formatting rule that compares a cell value\r
      * to a formula calculated result, using an operator     *\r
      * <p>\r
-      * The type  of the created condition is {@link ConditionalFormattingRule#CONDITION_TYPE_CELL_VALUE_IS}\r
+      * The type  of the created condition is {@link ConditionalFormattingRule#CONDITION_CELL_VALUE_IS}\r
      * </p>\r
      *\r
      * @param comparisonOperation  MUST be a constant value from\r
@@ -129,7 +129,7 @@ public interface SheetConditionalFormatting {
      *  When the formula result is true, the cell is highlighted.\r
      *\r
      * <p>\r
-     *  The type of the created format condition is  {@link ConditionalFormattingRule#CONDITION_TYPE_FORMULA}\r
+     *  The type of the created format condition is  {@link ConditionalFormattingRule#CONDITION_FORMULA}\r
      * </p>\r
      * @param formula   the formula to evaluate. MUST be a Boolean function.\r
      */\r
index 64e49f9472a90ea7ff4dc6807ea090b4d9a2ea5e..0b4a05d096efd20f0c9e7e71ae358de30c25073c 100644 (file)
@@ -19,6 +19,9 @@
 \r
 package org.apache.poi.xssf.usermodel;\r
 \r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
 import org.apache.poi.ss.usermodel.*;\r
 import org.apache.poi.xssf.usermodel.XSSFFontFormatting;\r
 import org.apache.poi.xssf.model.StylesTable;\r
@@ -36,6 +39,30 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
 public class XSSFConditionalFormattingRule implements ConditionalFormattingRule {\r
     private final CTCfRule _cfRule;\r
     private XSSFSheet _sh;\r
+    \r
+    private static Map<STCfType.Enum, ConditionType> typeLookup = new HashMap<STCfType.Enum, ConditionType>();\r
+    static {\r
+        typeLookup.put(STCfType.CELL_IS, ConditionType.CELL_VALUE_IS);\r
+        typeLookup.put(STCfType.EXPRESSION, ConditionType.FORMULA);\r
+        typeLookup.put(STCfType.COLOR_SCALE, ConditionType.COLOR_SCALE);\r
+        typeLookup.put(STCfType.DATA_BAR, ConditionType.DATA_BAR);\r
+        typeLookup.put(STCfType.ICON_SET, ConditionType.ICON_SET);\r
+        \r
+        // These are all subtypes of Filter, we think...\r
+        typeLookup.put(STCfType.TOP_10, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.UNIQUE_VALUES, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.DUPLICATE_VALUES, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.CONTAINS_TEXT, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.NOT_CONTAINS_TEXT, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.BEGINS_WITH, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.ENDS_WITH, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.CONTAINS_BLANKS, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.NOT_CONTAINS_BLANKS, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.CONTAINS_ERRORS, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.NOT_CONTAINS_ERRORS, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.TIME_PERIOD, ConditionType.FILTER);\r
+        typeLookup.put(STCfType.ABOVE_AVERAGE, ConditionType.FILTER);\r
+    }\r
 \r
     /*package*/ XSSFConditionalFormattingRule(XSSFSheet sh){\r
         _cfRule = CTCfRule.Factory.newInstance();\r
@@ -153,19 +180,23 @@ public class XSSFConditionalFormattingRule implements ConditionalFormattingRule
     /**\r
      * Type of conditional formatting rule.\r
      * <p>\r
-     * MUST be either {@link ConditionalFormattingRule#CONDITION_TYPE_CELL_VALUE_IS}\r
-     * or  {@link ConditionalFormattingRule#CONDITION_TYPE_FORMULA}\r
+     * MUST be one of the IDs of a {@link ConditionType}\r
      * </p>\r
      *\r
      * @return the type of condition\r
      */\r
     public byte getConditionType(){\r
-        switch (_cfRule.getType().intValue()){\r
-            case STCfType.INT_EXPRESSION: return ConditionalFormattingRule.CONDITION_TYPE_FORMULA;\r
-            case STCfType.INT_CELL_IS: return ConditionalFormattingRule.CONDITION_TYPE_CELL_VALUE_IS;\r
-        }\r
+        ConditionType type = getConditionTypeType();\r
+        if (type != null) return type.id;\r
         return 0;\r
     }\r
+    \r
+    /**\r
+     * Type of conditional formatting rule.\r
+     */\r
+    public ConditionType getConditionTypeType() {\r
+        return typeLookup.get(_cfRule.getType());\r
+    }\r
 \r
     /**\r
      * The comparison function used when the type of conditional formatting is set to\r