From d3da8a2ea7da292fd9d0aadfe8c2446299ed3183 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 13 Jul 2015 19:47:21 +0000 Subject: [PATCH] Provide a Conditional Formatting type class, and deprecate the byte-based types, to better work with the wider range git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1690803 13f79535-47bb-0310-9956-ffa450edef68 --- .../HSSFConditionalFormattingRule.java | 10 ++- .../poi/ss/usermodel/ComparisonOperator.java | 3 - .../poi/ss/usermodel/ConditionType.java | 88 +++++++++++++++++++ .../usermodel/ConditionalFormattingRule.java | 18 +++- .../usermodel/SheetConditionalFormatting.java | 6 +- .../XSSFConditionalFormattingRule.java | 43 +++++++-- 6 files changed, 151 insertions(+), 17 deletions(-) create mode 100644 src/java/org/apache/poi/ss/usermodel/ConditionType.java diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java index 93af503de3..b87d8813ca 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java @@ -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 diff --git a/src/java/org/apache/poi/ss/usermodel/ComparisonOperator.java b/src/java/org/apache/poi/ss/usermodel/ComparisonOperator.java index 7e29cbf4c9..7eee789037 100644 --- a/src/java/org/apache/poi/ss/usermodel/ComparisonOperator.java +++ b/src/java/org/apache/poi/ss/usermodel/ComparisonOperator.java @@ -24,9 +24,6 @@ package org.apache.poi.ss.usermodel; *

* For example, "highlight cells that begin with "M2" and contain "Mountain Gear". *

- * - * @author Dmitriy Kumshayev - * @author Yegor Kozlov */ public final class ComparisonOperator { public static final byte NO_COMPARISON = 0; 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 index 0000000000..4f8de4f675 --- /dev/null +++ b/src/java/org/apache/poi/ss/usermodel/ConditionType.java @@ -0,0 +1,88 @@ +/* + * ==================================================================== + * 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.ss.usermodel; + +import java.util.HashMap; +import java.util.Map; + +/** + * Represents a type of a conditional formatting rule + */ +public class ConditionType { + private static Map lookup = new HashMap(); + + /** + * This conditional formatting rule compares a cell value + * to a formula calculated result, using an operator + */ + public static final ConditionType CELL_VALUE_IS = + new ConditionType(1, "cellIs"); + + /** + * This conditional formatting rule contains a formula to evaluate. + * When the formula result is true, the cell is highlighted. + */ + public static final ConditionType FORMULA = + new ConditionType(2, "expression"); + + /** + * This conditional formatting rule contains a color scale, + * with the cell background set according to a gradient. + */ + public static final ConditionType COLOR_SCALE = + new ConditionType(3, "colorScale"); + + /** + * This conditional formatting rule sets a data bar, with the + * cell populated with bars based on their values + */ + public static final ConditionType DATA_BAR = + new ConditionType(4, "dataBar"); + + /** + * This conditional formatting rule that files the values + */ + public static final ConditionType FILTER = + new ConditionType(5, null); + + /** + * This conditional formatting rule sets a data bar, with the + * cell populated with bars based on their values + */ + public static final ConditionType ICON_SET = + new ConditionType(6, "iconSet"); + + + public final byte id; + public final String type; + + + public static ConditionType forId(byte id) { + return forId((int)id); + } + public static ConditionType forId(int id) { + return lookup.get(id); + } + + private ConditionType(int id, String type) { + this.id = (byte)id; this.type = type; + lookup.put(id, this); + } +} diff --git a/src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java b/src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java index 2e2e3d734c..04c04a269d 100644 --- a/src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java +++ b/src/java/org/apache/poi/ss/usermodel/ConditionalFormattingRule.java @@ -19,6 +19,8 @@ package org.apache.poi.ss.usermodel; +import static org.apache.poi.ss.usermodel.ConditionType.*; + /** * Represents a description of a conditional formatting rule */ @@ -26,14 +28,16 @@ public interface ConditionalFormattingRule { /** * This conditional formatting rule compares a cell value * to a formula calculated result, using an operator + * @deprecated Use {@link ConditionType#CELL_VALUE_IS} */ - public static final byte CONDITION_TYPE_CELL_VALUE_IS = 1; + public static final byte CONDITION_TYPE_CELL_VALUE_IS = CELL_VALUE_IS.id; /** * This conditional formatting rule contains a formula to evaluate. * When the formula result is true, the cell is highlighted. + * @deprecated Use {@link ConditionType#FORMULA} */ - public static final byte CONDITION_TYPE_FORMULA = 2; + public static final byte CONDITION_TYPE_FORMULA = FORMULA.id; /** * Create a new border formatting structure if it does not exist, @@ -77,12 +81,20 @@ public interface ConditionalFormattingRule { /** * Type of conditional formatting rule. *

- * MUST be either {@link #CONDITION_TYPE_CELL_VALUE_IS} or {@link #CONDITION_TYPE_FORMULA} + * MUST be one of the IDs of a {@link ConditionType} *

* * @return the type of condition + * @deprecated Use {@link #getConditionTypeType()} */ byte getConditionType(); + + /** + * Type of conditional formatting rule. + * + * @return the type of condition + */ + ConditionType getConditionTypeType(); /** * The comparison function used when the type of conditional formatting is set to diff --git a/src/java/org/apache/poi/ss/usermodel/SheetConditionalFormatting.java b/src/java/org/apache/poi/ss/usermodel/SheetConditionalFormatting.java index 6d8c328c0c..7f54f30158 100644 --- a/src/java/org/apache/poi/ss/usermodel/SheetConditionalFormatting.java +++ b/src/java/org/apache/poi/ss/usermodel/SheetConditionalFormatting.java @@ -83,7 +83,7 @@ public interface SheetConditionalFormatting { *

* The created conditional formatting rule compares a cell value * to a formula calculated result, using the specified operator. - * The type of the created condition is {@link ConditionalFormattingRule#CONDITION_TYPE_CELL_VALUE_IS} + * The type of the created condition is {@link ConditionalFormattingRule#CONDITION_CELL_VALUE_IS} *

* * @param comparisonOperation - MUST be a constant value from @@ -112,7 +112,7 @@ public interface SheetConditionalFormatting { * Create a conditional formatting rule that compares a cell value * to a formula calculated result, using an operator * *

- * The type of the created condition is {@link ConditionalFormattingRule#CONDITION_TYPE_CELL_VALUE_IS} + * The type of the created condition is {@link ConditionalFormattingRule#CONDITION_CELL_VALUE_IS} *

* * @param comparisonOperation MUST be a constant value from @@ -129,7 +129,7 @@ public interface SheetConditionalFormatting { * When the formula result is true, the cell is highlighted. * *

- * The type of the created format condition is {@link ConditionalFormattingRule#CONDITION_TYPE_FORMULA} + * The type of the created format condition is {@link ConditionalFormattingRule#CONDITION_FORMULA} *

* @param formula the formula to evaluate. MUST be a Boolean function. */ 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 64e49f9472..0b4a05d096 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFConditionalFormattingRule.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFConditionalFormattingRule.java @@ -19,6 +19,9 @@ package org.apache.poi.xssf.usermodel; +import java.util.HashMap; +import java.util.Map; + import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFFontFormatting; import org.apache.poi.xssf.model.StylesTable; @@ -36,6 +39,30 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder; public class XSSFConditionalFormattingRule implements ConditionalFormattingRule { private final CTCfRule _cfRule; private XSSFSheet _sh; + + private static Map typeLookup = new HashMap(); + static { + typeLookup.put(STCfType.CELL_IS, ConditionType.CELL_VALUE_IS); + typeLookup.put(STCfType.EXPRESSION, ConditionType.FORMULA); + typeLookup.put(STCfType.COLOR_SCALE, ConditionType.COLOR_SCALE); + typeLookup.put(STCfType.DATA_BAR, ConditionType.DATA_BAR); + typeLookup.put(STCfType.ICON_SET, ConditionType.ICON_SET); + + // These are all subtypes of Filter, we think... + typeLookup.put(STCfType.TOP_10, ConditionType.FILTER); + typeLookup.put(STCfType.UNIQUE_VALUES, ConditionType.FILTER); + typeLookup.put(STCfType.DUPLICATE_VALUES, ConditionType.FILTER); + typeLookup.put(STCfType.CONTAINS_TEXT, ConditionType.FILTER); + typeLookup.put(STCfType.NOT_CONTAINS_TEXT, ConditionType.FILTER); + typeLookup.put(STCfType.BEGINS_WITH, ConditionType.FILTER); + typeLookup.put(STCfType.ENDS_WITH, ConditionType.FILTER); + typeLookup.put(STCfType.CONTAINS_BLANKS, ConditionType.FILTER); + typeLookup.put(STCfType.NOT_CONTAINS_BLANKS, ConditionType.FILTER); + typeLookup.put(STCfType.CONTAINS_ERRORS, ConditionType.FILTER); + typeLookup.put(STCfType.NOT_CONTAINS_ERRORS, ConditionType.FILTER); + typeLookup.put(STCfType.TIME_PERIOD, ConditionType.FILTER); + typeLookup.put(STCfType.ABOVE_AVERAGE, ConditionType.FILTER); + } /*package*/ XSSFConditionalFormattingRule(XSSFSheet sh){ _cfRule = CTCfRule.Factory.newInstance(); @@ -153,19 +180,23 @@ public class XSSFConditionalFormattingRule implements ConditionalFormattingRule /** * Type of conditional formatting rule. *

- * MUST be either {@link ConditionalFormattingRule#CONDITION_TYPE_CELL_VALUE_IS} - * or {@link ConditionalFormattingRule#CONDITION_TYPE_FORMULA} + * MUST be one of the IDs of a {@link ConditionType} *

* * @return the type of condition */ public byte getConditionType(){ - switch (_cfRule.getType().intValue()){ - case STCfType.INT_EXPRESSION: return ConditionalFormattingRule.CONDITION_TYPE_FORMULA; - case STCfType.INT_CELL_IS: return ConditionalFormattingRule.CONDITION_TYPE_CELL_VALUE_IS; - } + ConditionType type = getConditionTypeType(); + if (type != null) return type.id; return 0; } + + /** + * Type of conditional formatting rule. + */ + public ConditionType getConditionTypeType() { + return typeLookup.get(_cfRule.getType()); + } /** * The comparison function used when the type of conditional formatting is set to -- 2.39.5