diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java index b87d8813c..a9bf30180 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java @@ -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 { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFontFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFontFormatting.java index f07965c7f..6447c33e3 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFFontFormatting.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFontFormatting.java @@ -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 diff --git a/src/java/org/apache/poi/ss/usermodel/FontFormatting.java b/src/java/org/apache/poi/ss/usermodel/FontFormatting.java index 2298d7963..0e6b7aba8 100644 --- a/src/java/org/apache/poi/ss/usermodel/FontFormatting.java +++ b/src/java/org/apache/poi/ss/usermodel/FontFormatting.java @@ -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 diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java index a5d8bdd40..40ce715f4 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFontFormatting.java @@ -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 * diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java index 26a04ef18..af3e07a8d 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java @@ -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;