bug 59342: add sheet tab color getter and setter

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1739547 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-04-17 07:38:15 +00:00
parent 3ba74f8319
commit 4d4b54b7c8
4 changed files with 137 additions and 9 deletions

View File

@ -16,8 +16,11 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import java.util.Arrays;
import org.apache.poi.ss.usermodel.Color; import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.ExtendedColor; import org.apache.poi.ss.usermodel.ExtendedColor;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
@ -51,6 +54,11 @@ public class XSSFColor extends ExtendedColor {
ctColor.setRgb(rgb); ctColor.setRgb(rgb);
} }
public XSSFColor(IndexedColors indexedColor) {
this();
ctColor.setIndexed(indexedColor.index);
}
/** /**
* A boolean value indicating the ctColor is automatic and system ctColor dependent. * A boolean value indicating the ctColor is automatic and system ctColor dependent.
*/ */
@ -304,7 +312,17 @@ public class XSSFColor extends ExtendedColor {
return ctColor; return ctColor;
} }
/**
* Checked type cast <tt>color</tt> to an XSSFColor.
*
* @param color the color to type cast
* @return the type casted color
* @throws IllegalArgumentException if color is null or is not an instance of XSSFColor
*/
public static XSSFColor toXSSFColor(Color color) { public static XSSFColor toXSSFColor(Color color) {
// FIXME: this method would be more useful if it could convert any Color to an XSSFColor
// Currently the only benefit of this method is to throw an IllegalArgumentException
// instead of a ClassCastException.
if (color != null && !(color instanceof XSSFColor)) { if (color != null && !(color instanceof XSSFColor)) {
throw new IllegalArgumentException("Only XSSFColor objects are supported"); throw new IllegalArgumentException("Only XSSFColor objects are supported");
} }
@ -316,13 +334,62 @@ public class XSSFColor extends ExtendedColor {
return ctColor.toString().hashCode(); return ctColor.toString().hashCode();
} }
// Helper methods for {@link #equals(Object)}
private boolean sameIndexed(XSSFColor other) {
if (isIndexed() == other.isIndexed()) {
if (isIndexed()) {
return getIndexed() == other.getIndexed();
}
return true;
}
return false;
}
private boolean sameARGB(XSSFColor other) {
if (isRGB() == other.isRGB()) {
if (isRGB()) {
return Arrays.equals(getARGB(), other.getARGB());
}
return true;
}
return false;
}
private boolean sameTheme(XSSFColor other) {
if (isThemed() == other.isThemed()) {
if (isThemed()) {
return getTheme() == other.getTheme();
}
return true;
}
return false;
}
private boolean sameTint(XSSFColor other) {
if (hasTint() == other.hasTint()) {
if (hasTint()) {
return getTint() == other.getTint();
}
return true;
}
return false;
}
private boolean sameAuto(XSSFColor other) {
return isAuto() == other.isAuto();
}
@Override @Override
public boolean equals(Object o){ public boolean equals(Object o){
if(!(o instanceof XSSFColor)) { if(!(o instanceof XSSFColor)) {
return false; return false;
} }
XSSFColor cf = (XSSFColor)o; XSSFColor other = (XSSFColor)o;
return ctColor.toString().equals(cf.getCTColor().toString());
// Compare each field in ctColor.
// Cannot compare ctColor's XML string representation because equivalent
// colors may have different relation namespace URI's
return sameARGB(other)
&& sameTheme(other)
&& sameIndexed(other)
&& sameTint(other)
&& sameAuto(other);
} }
} }

View File

@ -63,6 +63,7 @@ import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Footer; import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header; import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.IgnoredErrorType; import org.apache.poi.ss.usermodel.IgnoredErrorType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.AreaReference;
@ -3848,18 +3849,42 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
} }
/** /**
* Set background color of the sheet tab * Get background color of the sheet tab.
* Returns <tt>null</tt> if no sheet tab color is set.
* *
* @param colorIndex the indexed color to set, must be a constant from {@link IndexedColors} * @return the background color of the sheet tab
*/ */
public void setTabColor(int colorIndex){ public XSSFColor getTabColor() {
CTSheetPr pr = worksheet.getSheetPr(); CTSheetPr pr = worksheet.getSheetPr();
if(pr == null) pr = worksheet.addNewSheetPr(); if(pr == null) pr = worksheet.addNewSheetPr();
CTColor color = CTColor.Factory.newInstance(); if (!pr.isSetTabColor()) {
color.setIndexed(colorIndex); return null;
pr.setTabColor(color); }
return new XSSFColor(pr.getTabColor());
} }
/**
* Set background color of the sheet tab
*
* @param colorIndex the indexed color to set, must be a constant from {@link org.apache.poi.ss.usermodel.IndexedColors}
* @deprecated 3.15-beta2. Removed in 3.17. Use {@link #setTabColor(XSSFColor)}.
*/
public void setTabColor(int colorIndex) {
IndexedColors indexedColor = IndexedColors.fromInt(colorIndex);
XSSFColor color = new XSSFColor(indexedColor);
setTabColor(color);
}
/**
* Set background color of the sheet tab
*
* @param color the color to set
*/
public void setTabColor(XSSFColor color) {
CTSheetPr pr = worksheet.getSheetPr();
if(pr == null) pr = worksheet.addNewSheetPr();
pr.setTabColor(color.getCTColor());
}
@Override @Override
public CellRangeAddress getRepeatingRows() { public CellRangeAddress getRepeatingRows() {

View File

@ -1911,7 +1911,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
try { try {
XSSFSheet sh = wb.createSheet(); XSSFSheet sh = wb.createSheet();
assertTrue(sh.getCTWorksheet().getSheetPr() == null || !sh.getCTWorksheet().getSheetPr().isSetTabColor()); assertTrue(sh.getCTWorksheet().getSheetPr() == null || !sh.getCTWorksheet().getSheetPr().isSetTabColor());
sh.setTabColor(IndexedColors.RED); sh.setTabColor(new XSSFColor(IndexedColors.RED));
assertTrue(sh.getCTWorksheet().getSheetPr().isSetTabColor()); assertTrue(sh.getCTWorksheet().getSheetPr().isSetTabColor());
assertEquals(IndexedColors.RED.index, assertEquals(IndexedColors.RED.index,
sh.getCTWorksheet().getSheetPr().getTabColor().getIndexed()); sh.getCTWorksheet().getSheetPr().getTabColor().getIndexed());
@ -1919,4 +1919,40 @@ public final class TestXSSFSheet extends BaseTestSheet {
wb.close(); wb.close();
} }
} }
@Test
public void getTabColor() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
try {
XSSFSheet sh = wb.createSheet();
assertTrue(sh.getCTWorksheet().getSheetPr() == null || !sh.getCTWorksheet().getSheetPr().isSetTabColor());
assertNull(sh.getTabColor());
sh.setTabColor(new XSSFColor(IndexedColors.RED));
XSSFColor expected = new XSSFColor(IndexedColors.RED);
assertEquals(expected, sh.getTabColor());
} finally {
wb.close();
}
}
// Test using an existing workbook saved by Excel
@Test
public void tabColor() throws IOException {
XSSFWorkbook wb = openSampleWorkbook("SheetTabColors.xlsx");
try {
// non-colored sheets do not have a color
assertNull(wb.getSheet("default").getTabColor());
// test indexed-colored sheet
XSSFColor expected = new XSSFColor(IndexedColors.RED);
assertEquals(expected, wb.getSheet("indexedRed").getTabColor());
// test regular-colored (non-indexed, ARGB) sheet
expected = new XSSFColor();
expected.setARGBHex("FF7F2700");
assertEquals(expected, wb.getSheet("customOrange").getTabColor());
} finally {
wb.close();
}
}
} }

Binary file not shown.