]> source.dussan.org Git - poi.git/commitdiff
Use an enum to simplify the themes code and tests
authorNick Burch <nick@apache.org>
Tue, 4 Aug 2015 23:14:23 +0000 (23:14 +0000)
committerNick Burch <nick@apache.org>
Tue, 4 Aug 2015 23:14:23 +0000 (23:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1694125 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java

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