Provide format-agnostic conditional formatting patter colour getters and setters

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691047 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-07-14 18:32:35 +00:00
parent e349269fcf
commit 2eb88db1b0
4 changed files with 91 additions and 24 deletions

View File

@ -135,13 +135,13 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
if ( patternFormatting != null) if ( patternFormatting != null)
{ {
cfRuleRecord.setPatternFormatting(patternFormatting); cfRuleRecord.setPatternFormatting(patternFormatting);
return new HSSFPatternFormatting(cfRuleRecord); return new HSSFPatternFormatting(cfRuleRecord, workbook);
} }
else if( create ) else if( create )
{ {
patternFormatting = new PatternFormatting(); patternFormatting = new PatternFormatting();
cfRuleRecord.setPatternFormatting(patternFormatting); cfRuleRecord.setPatternFormatting(patternFormatting);
return new HSSFPatternFormatting(cfRuleRecord); return new HSSFPatternFormatting(cfRuleRecord, workbook);
} }
else else
{ {

View File

@ -19,17 +19,21 @@ package org.apache.poi.hssf.usermodel;
import org.apache.poi.hssf.record.CFRuleBase; import org.apache.poi.hssf.record.CFRuleBase;
import org.apache.poi.hssf.record.cf.PatternFormatting; import org.apache.poi.hssf.record.cf.PatternFormatting;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Color;
/** /**
* High level representation for Conditional Formatting settings * High level representation for Conditional Formatting settings
*/ */
public class HSSFPatternFormatting implements org.apache.poi.ss.usermodel.PatternFormatting public class HSSFPatternFormatting implements org.apache.poi.ss.usermodel.PatternFormatting
{ {
private final HSSFWorkbook workbook;
private final CFRuleBase cfRuleRecord; private final CFRuleBase cfRuleRecord;
private final PatternFormatting patternFormatting; private final PatternFormatting patternFormatting;
protected HSSFPatternFormatting(CFRuleBase cfRuleRecord) protected HSSFPatternFormatting(CFRuleBase cfRuleRecord, HSSFWorkbook workbook)
{ {
this.workbook = workbook;
this.cfRuleRecord = cfRuleRecord; this.cfRuleRecord = cfRuleRecord;
this.patternFormatting = cfRuleRecord.getPatternFormatting(); this.patternFormatting = cfRuleRecord.getPatternFormatting();
} }
@ -39,6 +43,14 @@ public class HSSFPatternFormatting implements org.apache.poi.ss.usermodel.Patter
return patternFormatting; return patternFormatting;
} }
public HSSFColor getFillBackgroundColorColor() {
return workbook.getCustomPalette().getColor(getFillBackgroundColor());
}
public HSSFColor getFillForegroundColorColor() {
return workbook.getCustomPalette().getColor(getFillForegroundColor());
}
/** /**
* @see org.apache.poi.hssf.record.cf.PatternFormatting#getFillBackgroundColor() * @see org.apache.poi.hssf.record.cf.PatternFormatting#getFillBackgroundColor()
*/ */
@ -63,6 +75,30 @@ public class HSSFPatternFormatting implements org.apache.poi.ss.usermodel.Patter
return (short)patternFormatting.getFillPattern(); return (short)patternFormatting.getFillPattern();
} }
public void setFillBackgroundColor(Color bg) {
if (bg != null && !(bg instanceof HSSFColor)) {
throw new IllegalArgumentException("Only HSSFColor objects are supported");
}
HSSFColor hcolor = (HSSFColor)bg;
if (hcolor == null) {
setFillBackgroundColor((short)0);
} else {
setFillBackgroundColor(hcolor.getIndex());
}
}
public void setFillForegroundColor(Color fg) {
if (fg != null && !(fg instanceof HSSFColor)) {
throw new IllegalArgumentException("Only HSSFColor objects are supported");
}
HSSFColor hcolor = (HSSFColor)fg;
if (hcolor == null) {
setFillForegroundColor((short)0);
} else {
setFillForegroundColor(hcolor.getIndex());
}
}
/** /**
* @param bg * @param bg
* @see org.apache.poi.hssf.record.cf.PatternFormatting#setFillBackgroundColor(int) * @see org.apache.poi.hssf.record.cf.PatternFormatting#setFillBackgroundColor(int)

View File

@ -63,14 +63,16 @@ public interface PatternFormatting {
public final static short LEAST_DOTS = 18 ; public final static short LEAST_DOTS = 18 ;
short getFillBackgroundColor(); short getFillBackgroundColor();
short getFillForegroundColor(); short getFillForegroundColor();
Color getFillBackgroundColorColor();
Color getFillForegroundColorColor();
short getFillPattern(); short getFillPattern();
void setFillBackgroundColor(short bg); void setFillBackgroundColor(short bg);
void setFillForegroundColor(short fg); void setFillForegroundColor(short fg);
void setFillBackgroundColor(Color bg);
void setFillForegroundColor(Color fg);
void setFillPattern(short fp); void setFillPattern(short fp);
} }

View File

@ -18,6 +18,7 @@
*/ */
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.PatternFormatting; import org.apache.poi.ss.usermodel.PatternFormatting;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
@ -34,17 +35,14 @@ public class XSSFPatternFormatting implements PatternFormatting {
_fill = fill; _fill = fill;
} }
public short getFillBackgroundColor(){ public XSSFColor getFillBackgroundColorColor() {
if(!_fill.isSetPatternFill()) return 0; if(!_fill.isSetPatternFill()) return null;
return new XSSFColor(_fill.getPatternFill().getBgColor());
return (short)_fill.getPatternFill().getBgColor().getIndexed();
} }
public XSSFColor getFillForegroundColorColor() {
public short getFillForegroundColor(){
if(!_fill.isSetPatternFill() || ! _fill.getPatternFill().isSetFgColor()) if(!_fill.isSetPatternFill() || ! _fill.getPatternFill().isSetFgColor())
return 0; return null;
return new XSSFColor(_fill.getPatternFill().getFgColor());
return (short)_fill.getPatternFill().getFgColor().getIndexed();
} }
public short getFillPattern() { public short getFillPattern() {
@ -53,18 +51,49 @@ public class XSSFPatternFormatting implements PatternFormatting {
return (short)(_fill.getPatternFill().getPatternType().intValue() - 1); return (short)(_fill.getPatternFill().getPatternType().intValue() - 1);
} }
public void setFillBackgroundColor(short bg){ public short getFillBackgroundColor() {
CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill(); XSSFColor color = getFillBackgroundColorColor();
CTColor bgColor = CTColor.Factory.newInstance(); if (color == null) return 0;
bgColor.setIndexed(bg); return color.getIndexed();
ptrn.setBgColor(bgColor); }
public short getFillForegroundColor() {
XSSFColor color = getFillForegroundColorColor();
if (color == null) return 0;
return color.getIndexed();
} }
public void setFillForegroundColor(short fg){ public void setFillBackgroundColor(Color bg) {
if (bg != null && !(bg instanceof XSSFColor)) {
throw new IllegalArgumentException("Only XSSFColor objects are supported");
}
XSSFColor xcolor = (XSSFColor)bg;
setFillBackgroundColor(xcolor.getCTColor());
}
public void setFillBackgroundColor(short bg) {
CTColor bgColor = CTColor.Factory.newInstance();
bgColor.setIndexed(bg);
setFillBackgroundColor(bgColor);
}
public void setFillBackgroundColor(CTColor color) {
CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill(); CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill();
ptrn.setBgColor(color);
}
public void setFillForegroundColor(Color fg) {
if (fg != null && !(fg instanceof XSSFColor)) {
throw new IllegalArgumentException("Only XSSFColor objects are supported");
}
XSSFColor xcolor = (XSSFColor)fg;
setFillForegroundColor(xcolor.getCTColor());
}
public void setFillForegroundColor(short fg) {
CTColor fgColor = CTColor.Factory.newInstance(); CTColor fgColor = CTColor.Factory.newInstance();
fgColor.setIndexed(fg); fgColor.setIndexed(fg);
ptrn.setFgColor(fgColor); setFillForegroundColor(fgColor);
}
public void setFillForegroundColor(CTColor color) {
CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill();
ptrn.setFgColor(color);
} }
public void setFillPattern(short fp){ public void setFillPattern(short fp){