/*
* ====================================================================
* 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 java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType;
import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
import org.apache.poi.xssf.usermodel.XSSFFontFormatting;
import org.apache.poi.xssf.model.StylesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
/**
* XSSF support for Conditional Formatting rules
*/
public class XSSFConditionalFormattingRule implements ConditionalFormattingRule {
private final CTCfRule _cfRule;
private XSSFSheet _sh;
private static Mapnull
.
*/
public XSSFBorderFormatting createBorderFormatting(){
CTDxf dxf = getDxf(true);
CTBorder border;
if(!dxf.isSetBorder()) {
border = dxf.addNewBorder();
} else {
border = dxf.getBorder();
}
return new XSSFBorderFormatting(border);
}
/**
* @return - border formatting object if defined, null
otherwise
*/
public XSSFBorderFormatting getBorderFormatting(){
CTDxf dxf = getDxf(false);
if(dxf == null || !dxf.isSetBorder()) return null;
return new XSSFBorderFormatting(dxf.getBorder());
}
/**
* Create a new font formatting structure if it does not exist,
* otherwise just return existing object.
*
* @return - font formatting object, never returns null
.
*/
public XSSFFontFormatting createFontFormatting(){
CTDxf dxf = getDxf(true);
CTFont font;
if(!dxf.isSetFont()) {
font = dxf.addNewFont();
} else {
font = dxf.getFont();
}
return new XSSFFontFormatting(font);
}
/**
* @return - font formatting object if defined, null
otherwise
*/
public XSSFFontFormatting getFontFormatting(){
CTDxf dxf = getDxf(false);
if(dxf == null || !dxf.isSetFont()) return null;
return new XSSFFontFormatting(dxf.getFont());
}
/**
* Create a new pattern formatting structure if it does not exist,
* otherwise just return existing object.
*
* @return - pattern formatting object, never returns null
.
*/
public XSSFPatternFormatting createPatternFormatting(){
CTDxf dxf = getDxf(true);
CTFill fill;
if(!dxf.isSetFill()) {
fill = dxf.addNewFill();
} else {
fill = dxf.getFill();
}
return new XSSFPatternFormatting(fill);
}
/**
* @return - pattern formatting object if defined, null
otherwise
*/
public XSSFPatternFormatting getPatternFormatting(){
CTDxf dxf = getDxf(false);
if(dxf == null || !dxf.isSetFill()) return null;
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)
return getMultiStateFormatting();
// Mark it as being an Icon Set
_cfRule.setType(STCfType.ICON_SET);
// Ensure the right element
CTIconSet icons = null;
if (_cfRule.isSetIconSet()) {
icons = _cfRule.getIconSet();
} else {
icons = _cfRule.addNewIconSet();
}
// Set the type of the icon set
if (iconSet.name != null) {
STIconSetType.Enum xIconSet = STIconSetType.Enum.forString(iconSet.name);
icons.setIconSet(xIconSet);
}
// Add a default set of thresholds
int jump = 100 / iconSet.num;
STCfvoType.Enum type = STCfvoType.Enum.forString(RangeType.PERCENT.name);
for (int i=0; i
* If the condition type is {@link ConditionType#CELL_VALUE_IS}, * this field is the first operand of the comparison. * If type is {@link ConditionType#FORMULA}, this formula is used * to determine if the conditional formatting is applied. *
** If comparison type is {@link ConditionType#FORMULA} the formula MUST be a Boolean function *
* * @return the first formula */ public String getFormula1(){ return _cfRule.sizeOfFormulaArray() > 0 ? _cfRule.getFormulaArray(0) : null; } /** * The formula used to evaluate the second operand of the comparison when * comparison type is {@link ConditionType#CELL_VALUE_IS} and operator * is either {@link org.apache.poi.ss.usermodel.ComparisonOperator#BETWEEN} or {@link org.apache.poi.ss.usermodel.ComparisonOperator#NOT_BETWEEN} * * @return the second formula */ public String getFormula2(){ return _cfRule.sizeOfFormulaArray() == 2 ? _cfRule.getFormulaArray(1) : null; } }