poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1694125 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/model/ThemesTable.java testcases/org/apache/poi/xssf/model/TestThemesTable.java
Date Tue, 04 Aug 2015 23:14:23 GMT
Author: nick
Date: Tue Aug  4 23:14:23 2015
New Revision: 1694125

URL: http://svn.apache.org/r1694125
Log:
Use an enum to simplify the themes code and tests

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

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java?rev=1694125&r1=1694124&r2=1694125&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java Tue Aug  4 23:14:23
2015
@@ -34,18 +34,31 @@ import org.openxmlformats.schemas.drawin
  * 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 POIXMLD
         // 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;
         }
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java?rev=1694125&r1=1694124&r2=1694125&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java Tue Aug 
4 23:14:23 2015
@@ -28,6 +28,7 @@ import org.apache.commons.codec.binary.H
 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.XSS
 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;



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message