Provide format-agnostic conditional formatting font colour getter and setter

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691045 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-07-14 18:05:28 +00:00
parent 892d7fa4d0
commit 777806d350
5 changed files with 63 additions and 14 deletions

View File

@ -62,13 +62,13 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
if ( fontFormatting != null)
{
cfRuleRecord.setFontFormatting(fontFormatting);
return new HSSFFontFormatting(cfRuleRecord);
return new HSSFFontFormatting(cfRuleRecord, workbook);
}
else if( create )
{
fontFormatting = new FontFormatting();
cfRuleRecord.setFontFormatting(fontFormatting);
return new HSSFFontFormatting(cfRuleRecord);
return new HSSFFontFormatting(cfRuleRecord, workbook);
}
else
{

View File

@ -19,6 +19,8 @@ package org.apache.poi.hssf.usermodel;
import org.apache.poi.hssf.record.CFRuleBase;
import org.apache.poi.hssf.record.cf.FontFormatting;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Color;
/**
* High level representation for Font Formatting component
* of Conditional Formatting settings
@ -37,10 +39,12 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
public final static byte U_DOUBLE_ACCOUNTING = FontFormatting.U_DOUBLE_ACCOUNTING;
private final FontFormatting fontFormatting;
private final HSSFWorkbook workbook;
protected HSSFFontFormatting(CFRuleBase cfRuleRecord)
protected HSSFFontFormatting(CFRuleBase cfRuleRecord, HSSFWorkbook workbook)
{
this.fontFormatting = cfRuleRecord.getFontFormatting();
this.workbook = workbook;
}
protected FontFormatting getFontFormattingBlock()
@ -69,7 +73,26 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
return fontFormatting.getFontColorIndex();
}
/**
public HSSFColor getFontColor() {
return workbook.getCustomPalette().getColor(
getFontColorIndex()
);
}
public void setFontColor(Color color) {
if (color != null && !(color instanceof HSSFColor)) {
throw new IllegalArgumentException("Only HSSFColor objects are supported");
}
HSSFColor hcolor = (HSSFColor)color;
if (hcolor == null) {
fontFormatting.setFontColorIndex((short)0);
} else {
fontFormatting.setFontColorIndex(hcolor.getIndex());
}
}
/**
* gets the height of the font in 1/20th point units
*
* @return fontheight (in points/20); or -1 if not modified

View File

@ -66,15 +66,26 @@ public interface FontFormatting {
void setEscapementType(short escapementType);
/**
* @return font color index
* @return font colour index, or 0 if not indexed (XSSF only)
*/
short getFontColorIndex();
/**
* @param color font color index
* Sets the indexed colour to use
* @param color font colour index
*/
void setFontColorIndex(short color);
/**
* @return The colour of the font, or null if no colour applied
*/
Color getFontColor();
/**
* Sets the colour to use
* @param color font colour to use
*/
void setFontColor(Color color);
/**
* gets the height of the font in 1/20th point units

View File

@ -80,7 +80,6 @@ public class XSSFFontFormatting implements FontFormatting {
return (short)idx;
}
/**
* @param color font color index
*/
@ -91,16 +90,32 @@ public class XSSFFontFormatting implements FontFormatting {
}
}
/**
*
* @return xssf color wrapper or null if color info is missing
*/
public XSSFColor getXSSFColor(){
public XSSFColor getFontColor() {
if(_font.sizeOfColorArray() == 0) return null;
return new XSSFColor(_font.getColorArray(0));
}
public void setFontColor(Color color) {
if (color != null && !(color instanceof XSSFColor)) {
throw new IllegalArgumentException("Only XSSFColor objects are supported");
}
XSSFColor xcolor = (XSSFColor)color;
if (xcolor == null) {
_font.getColorList().clear();
} else {
_font.setColorArray(0, xcolor.getCTColor());
}
}
/**
* @deprecated use {@link #getFontColor()}
*/
public XSSFColor getXSSFColor(){
return getFontColor();
}
/**
* gets the height of the font in 1/20th point units
*

View File

@ -40,7 +40,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STConditionalFormatti
*/
public class XSSFSheetConditionalFormatting implements SheetConditionalFormatting {
/** Office 2010 Conditional Formatting extensions namespace */
protected static final CF_EXT_2009_NS_X14 = "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main";
protected static final String CF_EXT_2009_NS_X14 = "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main";
private final XSSFSheet _sheet;