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
This commit is contained in:
Nick Burch 2015-07-13 19:47:21 +00:00
parent 705c3fe1f9
commit 9217c91425
6 changed files with 151 additions and 17 deletions

View 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.FontFormatting;
import org.apache.poi.hssf.record.cf.PatternFormatting; import org.apache.poi.hssf.record.cf.PatternFormatting;
import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.ConditionType;
import org.apache.poi.ss.usermodel.ConditionalFormattingRule; 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 * It allows to specify formula based conditions for the Conditional Formatting
* and the formatting settings such as font, border and pattern. * 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 static final byte CELL_COMPARISON = CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS;
private final CFRuleBase cfRuleRecord; private final CFRuleBase cfRuleRecord;
@ -172,6 +172,12 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
public byte getConditionType() { public byte getConditionType() {
return cfRuleRecord.getConditionType(); return cfRuleRecord.getConditionType();
} }
/**
* @return - the conditiontype for the cfrule
*/
public ConditionType getConditionTypeType() {
return ConditionType.forId(getConditionType());
}
/** /**
* @return - the comparisionoperatation for the cfrule * @return - the comparisionoperatation for the cfrule

View File

@ -24,9 +24,6 @@ package org.apache.poi.ss.usermodel;
* <p> * <p>
* For example, "highlight cells that begin with "M2" and contain "Mountain Gear". * For example, "highlight cells that begin with "M2" and contain "Mountain Gear".
* </p> * </p>
*
* @author Dmitriy Kumshayev
* @author Yegor Kozlov
*/ */
public final class ComparisonOperator { public final class ComparisonOperator {
public static final byte NO_COMPARISON = 0; public static final byte NO_COMPARISON = 0;

View File

@ -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<Integer,ConditionType> lookup = new HashMap<Integer, ConditionType>();
/**
* 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);
}
}

View File

@ -19,6 +19,8 @@
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import static org.apache.poi.ss.usermodel.ConditionType.*;
/** /**
* Represents a description of a conditional formatting rule * Represents a description of a conditional formatting rule
*/ */
@ -26,14 +28,16 @@ public interface ConditionalFormattingRule {
/** /**
* This conditional formatting rule compares a cell value * This conditional formatting rule compares a cell value
* to a formula calculated result, using an operator * 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. * This conditional formatting rule contains a formula to evaluate.
* When the formula result is true, the cell is highlighted. * 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, * Create a new border formatting structure if it does not exist,
@ -77,12 +81,20 @@ public interface ConditionalFormattingRule {
/** /**
* Type of conditional formatting rule. * Type of conditional formatting rule.
* <p> * <p>
* MUST be either {@link #CONDITION_TYPE_CELL_VALUE_IS} or {@link #CONDITION_TYPE_FORMULA} * MUST be one of the IDs of a {@link ConditionType}
* </p> * </p>
* *
* @return the type of condition * @return the type of condition
* @deprecated Use {@link #getConditionTypeType()}
*/ */
byte getConditionType(); 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 * The comparison function used when the type of conditional formatting is set to

View File

@ -83,7 +83,7 @@ public interface SheetConditionalFormatting {
* <p> * <p>
* The created conditional formatting rule compares a cell value * The created conditional formatting rule compares a cell value
* to a formula calculated result, using the specified operator. * 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}
* </p> * </p>
* *
* @param comparisonOperation - MUST be a constant value from * @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 * Create a conditional formatting rule that compares a cell value
* to a formula calculated result, using an operator * * to a formula calculated result, using an operator *
* <p> * <p>
* 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}
* </p> * </p>
* *
* @param comparisonOperation MUST be a constant value from * @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. * When the formula result is true, the cell is highlighted.
* *
* <p> * <p>
* 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}
* </p> * </p>
* @param formula the formula to evaluate. MUST be a Boolean function. * @param formula the formula to evaluate. MUST be a Boolean function.
*/ */

View File

@ -19,6 +19,9 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFontFormatting; import org.apache.poi.xssf.usermodel.XSSFFontFormatting;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
@ -36,6 +39,30 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
public class XSSFConditionalFormattingRule implements ConditionalFormattingRule { public class XSSFConditionalFormattingRule implements ConditionalFormattingRule {
private final CTCfRule _cfRule; private final CTCfRule _cfRule;
private XSSFSheet _sh; private XSSFSheet _sh;
private static Map<STCfType.Enum, ConditionType> typeLookup = new HashMap<STCfType.Enum, ConditionType>();
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){ /*package*/ XSSFConditionalFormattingRule(XSSFSheet sh){
_cfRule = CTCfRule.Factory.newInstance(); _cfRule = CTCfRule.Factory.newInstance();
@ -153,19 +180,23 @@ public class XSSFConditionalFormattingRule implements ConditionalFormattingRule
/** /**
* Type of conditional formatting rule. * Type of conditional formatting rule.
* <p> * <p>
* MUST be either {@link ConditionalFormattingRule#CONDITION_TYPE_CELL_VALUE_IS} * MUST be one of the IDs of a {@link ConditionType}
* or {@link ConditionalFormattingRule#CONDITION_TYPE_FORMULA}
* </p> * </p>
* *
* @return the type of condition * @return the type of condition
*/ */
public byte getConditionType(){ public byte getConditionType(){
switch (_cfRule.getType().intValue()){ ConditionType type = getConditionTypeType();
case STCfType.INT_EXPRESSION: return ConditionalFormattingRule.CONDITION_TYPE_FORMULA; if (type != null) return type.id;
case STCfType.INT_CELL_IS: return ConditionalFormattingRule.CONDITION_TYPE_CELL_VALUE_IS;
}
return 0; 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 * The comparison function used when the type of conditional formatting is set to