Use an enum to simplify the themes code and tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1694125 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-08-04 23:14:23 +00:00
parent c4e543d4fa
commit b2ab81ad81
2 changed files with 53 additions and 45 deletions

View File

@ -34,18 +34,31 @@ import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
* colors and fonts.
*/
public class ThemesTable extends POIXMLDocumentPart {
public static final int THEME_LT1 = 0;
public static final int THEME_DK1 =1;
public static final int THEME_LT2 = 2;
public static final int THEME_DK2 = 3;
public static final int THEME_ACCENT1 = 4;
public static final int THEME_ACCENT2 = 5;
public static final int THEME_ACCENT3 = 6;
public static final int THEME_ACCENT4 = 7;
public static final int THEME_ACCENT5 = 8;
public static final int THEME_ACCENT6 = 9;
public static final int THEME_HLINK = 10;
public static final int THEME_FOLHLINK = 11;
public enum ThemeElement {
LT1(0, "Lt1"),
DK1(1,"Dk1"),
LT2(2,"Lt2"),
DK2(3,"Dk2"),
ACCENT1(4,"Accent1"),
ACCENT2(5,"Accent2"),
ACCENT3(6,"Accent3"),
ACCENT4(7,"Accent4"),
ACCENT5(8,"Accent5"),
ACCENT6(9,"Accent6"),
HLINK(10,"Hlink"),
FOLHLINK(11,"FolHlink"),
UNKNOWN(-1,null);
public static ThemeElement byId(int idx) {
if (idx >= values().length || idx < 0) return UNKNOWN;
return values()[idx];
}
private ThemeElement(int idx, String name) {
this.idx = idx; this.name = name;
}
public final int idx;
public final String name;
};
private ThemeDocument theme;
@ -92,19 +105,19 @@ public class ThemesTable extends POIXMLDocumentPart {
// in theme1.xml. They are keys to a mapped color.
CTColorScheme colorScheme = theme.getTheme().getThemeElements().getClrScheme();
CTColor ctColor;
switch (idx) {
case THEME_LT1: ctColor = colorScheme.getLt1(); break;
case THEME_DK1: ctColor = colorScheme.getDk1(); break;
case THEME_LT2: ctColor = colorScheme.getLt2(); break;
case THEME_DK2: ctColor = colorScheme.getDk2(); break;
case THEME_ACCENT1: ctColor = colorScheme.getAccent1(); break;
case THEME_ACCENT2: ctColor = colorScheme.getAccent2(); break;
case THEME_ACCENT3: ctColor = colorScheme.getAccent3(); break;
case THEME_ACCENT4: ctColor = colorScheme.getAccent4(); break;
case THEME_ACCENT5: ctColor = colorScheme.getAccent5(); break;
case THEME_ACCENT6: ctColor = colorScheme.getAccent6(); break;
case THEME_HLINK: ctColor = colorScheme.getHlink(); break;
case THEME_FOLHLINK:ctColor = colorScheme.getFolHlink();break;
switch (ThemeElement.byId(idx)) {
case LT1: ctColor = colorScheme.getLt1(); break;
case DK1: ctColor = colorScheme.getDk1(); break;
case LT2: ctColor = colorScheme.getLt2(); break;
case DK2: ctColor = colorScheme.getDk2(); break;
case ACCENT1: ctColor = colorScheme.getAccent1(); break;
case ACCENT2: ctColor = colorScheme.getAccent2(); break;
case ACCENT3: ctColor = colorScheme.getAccent3(); break;
case ACCENT4: ctColor = colorScheme.getAccent4(); break;
case ACCENT5: ctColor = colorScheme.getAccent5(); break;
case ACCENT6: ctColor = colorScheme.getAccent6(); break;
case HLINK: ctColor = colorScheme.getHlink(); break;
case FOLHLINK:ctColor = colorScheme.getFolHlink();break;
default: return null;
}

View File

@ -28,6 +28,7 @@ import org.apache.commons.codec.binary.Hex;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.ThemesTable.ThemeElement;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
@ -36,27 +37,13 @@ import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
public class TestThemesTable {
private String testFileSimple = "Themes.xlsx";
private String testFileComplex = "Themes2.xlsx";
// TODO .xls version available too, add HSSF support then check
// What our theme names are
private static String[] themeEntries = {
"lt1",
"dk1",
"lt2",
"dk2",
"accent1",
"accent2",
"accent3",
"accent4",
"accent5",
"accent6",
"hlink",
"folhlink"
};
// What colours they should show up as
private static String rgbExpected[] = {
"ffffff", // Lt1
@ -93,7 +80,7 @@ public class TestThemesTable {
// workbooks.put("Re-Saved_" + testFileComplex, complexRS);
// Sanity check
assertEquals(themeEntries.length, rgbExpected.length);
assertEquals(rgbExpected.length, rgbExpected.length);
// For offline testing
boolean createFiles = false;
@ -106,25 +93,33 @@ public class TestThemesTable {
int startRN = 0;
if (whatWorkbook.endsWith(testFileComplex)) startRN++;
for (int rn=startRN; rn<themeEntries.length+startRN; rn++) {
for (int rn=startRN; rn<rgbExpected.length+startRN; rn++) {
XSSFRow row = sheet.getRow(rn);
assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);
String ref = (new CellReference(rn, 0)).formatAsString();
XSSFCell cell = row.getCell(0);
assertNotNull(
"Missing cell " + ref + " in " + whatWorkbook, cell);
ThemeElement themeElem = ThemeElement.byId(rn-startRN);
assertEquals(
"Wrong theme at " + ref + " in " + whatWorkbook,
themeEntries[rn], cell.getStringCellValue());
themeElem.name.toLowerCase(), cell.getStringCellValue());
// Fonts are theme-based in their colours
XSSFFont font = cell.getCellStyle().getFont();
XSSFColor color = font.getXSSFColor();
CTColor ctColor = font.getCTFont().getColorArray(0);
assertNotNull(ctColor);
assertEquals(true, ctColor.isSetTheme());
assertEquals(themeElem.idx, ctColor.getTheme());
// Get the colour, via the theme
XSSFColor color = font.getXSSFColor();
// Theme colours aren't tinted
assertEquals(false, color.hasTint());
// Check the RGB part (no tint)
assertEquals(
"Wrong theme colour " + themeEntries[rn] + " on " + whatWorkbook,
"Wrong theme colour " + themeElem.name + " on " + whatWorkbook,
rgbExpected[rn], Hex.encodeHexString(color.getRGB()));
// Check the Theme ID
int expectedThemeIdx = rn - startRN;