]> source.dussan.org Git - poi.git/commitdiff
XSSFCellStyle fillColors support; XSSFCellFill class; tests
authorPaolo Mottadelli <paolo@apache.org>
Tue, 25 Mar 2008 13:19:27 +0000 (13:19 +0000)
committerPaolo Mottadelli <paolo@apache.org>
Tue, 25 Mar 2008 13:19:27 +0000 (13:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@640794 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java [new file with mode: 0644]
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java [new file with mode: 0644]

index fcb0503daaee8f38584d51dad3d87594132faa28..0e79149e6d0b3c97ee89013bf3d684b1e3b36ad0 100644 (file)
@@ -30,6 +30,7 @@ import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.StylesSource;
 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlOptions;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
@@ -206,7 +207,20 @@ public class StylesTable implements StylesSource, XSSFModel {
                borders.add(border);
                return borders.size() - 1;
        }
-       
+
+       public XSSFCellFill getFillAt(long idx) {
+               return new XSSFCellFill(fills.get((int) idx));
+       }
+       public long putFill(XSSFCellFill fill) {
+               return putFill(fill.getCTFill());
+       }
+       public synchronized long putFill(CTFill fill) {
+               if (fills.contains(fill)) {
+                       return fills.indexOf(fill);
+               }
+               fills.add(fill);
+               return fills.size() - 1;
+       }
        /**
      * For unit testing only
      */
index e234417239380763017c4791e6d0297b1b5f8eea..083dd3b3a557eba3d70392ac1f2125c5f06e8195 100644 (file)
@@ -23,6 +23,7 @@ import org.apache.poi.ss.usermodel.StylesSource;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSides;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle.Enum;
@@ -33,6 +34,7 @@ public class XSSFCellStyle implements CellStyle {
        private CTXf cellXf;
        private CTXf cellStyleXf;
        private XSSFCellBorder cellBorder;
+       private XSSFCellFill cellFill;
        
        /**
         * Creates a Cell Style from the supplied parts
@@ -120,18 +122,15 @@ public class XSSFCellStyle implements CellStyle {
        }
 
        public short getFillBackgroundColor() {
-               // TODO Auto-generated method stub
-               return 0;
+               return (short) getCellFill().getFillBackgroundColor().getIndexed();
        }
 
        public short getFillForegroundColor() {
-               // TODO Auto-generated method stub
-               return 0;
+               return (short) getCellFill().getFillForegroundColor().getIndexed();
        }
 
        public short getFillPattern() {
-               // TODO Auto-generated method stub
-               return 0;
+               return (short) getCellFill().getPatternType().intValue();
        }
 
        public Font getFont(Workbook parentWorkbook) {
@@ -304,6 +303,20 @@ public class XSSFCellStyle implements CellStyle {
                }
                return (int) cellStyleXf.getBorderId();
        }
+       
+       private XSSFCellFill getCellFill() {
+               if (cellFill == null) {
+                       cellFill = ((StylesTable)stylesSource).getFillAt(getFillId());
+               }
+               return cellFill;
+       }
+       
+       private int getFillId() {
+               if (cellXf.isSetFillId()) {
+                       return (int) cellXf.getFillId();
+               }
+               return (int) cellStyleXf.getFillId();
+       }
 
        private Enum getBorderStyle(BorderSides side) {
                return getCellBorder().getBorderStyle(side);
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java
new file mode 100644 (file)
index 0000000..8f8477e
--- /dev/null
@@ -0,0 +1,43 @@
+package org.apache.poi.xssf.usermodel.extensions;\r
+\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;\r
+\r
+public class XSSFCellFill {\r
+       \r
+       private CTFill fill;\r
+       \r
+       public XSSFCellFill(CTFill fill) {\r
+               this.fill = fill;\r
+       }\r
+       \r
+       public XSSFCellFill() {\r
+               this.fill = CTFill.Factory.newInstance();\r
+       }\r
+       \r
+       public XSSFColor getFillBackgroundColor() {\r
+               return new XSSFColor(getPatternFill().getBgColor());\r
+       }\r
+\r
+       public XSSFColor getFillForegroundColor() {\r
+               return new XSSFColor(getPatternFill().getFgColor());\r
+       }\r
+\r
+       public Enum getPatternType() {\r
+               return getPatternFill().getPatternType();\r
+       }\r
+\r
+       private CTPatternFill getPatternFill() {\r
+               CTPatternFill patternFill = fill.getPatternFill();\r
+               if (patternFill == null) {\r
+                       patternFill = fill.addNewPatternFill();\r
+               }\r
+               return patternFill;\r
+       }\r
+\r
+       public CTFill getCTFill() {\r
+               return this.fill;\r
+       }\r
+\r
+}\r
index 9f18dd6b9a9b5b1bc8d20a26474ad9925ae4b162..ace6b95c0a686d667fe82e7a4982e21c0451868e 100644 (file)
@@ -21,10 +21,15 @@ import junit.framework.TestCase;
 
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
 
 
 public class TestXSSFCellStyle extends TestCase {
@@ -32,6 +37,7 @@ public class TestXSSFCellStyle extends TestCase {
        private StylesTable stylesTable;
        private CTBorder ctBorderA;
        private CTBorder ctBorderB;
+       private CTFill ctFill;
        private CTXf cellStyleXf;
        private CTXf cellXf;
        private XSSFCellStyle cellStyle;
@@ -50,6 +56,11 @@ public class TestXSSFCellStyle extends TestCase {
                ctBorderB = borderB.getCTBorder();
                assertEquals(1, stylesTable.putBorder(borderB));
                
+               ctFill = CTFill.Factory.newInstance();
+               XSSFCellFill fill = new XSSFCellFill(ctFill);
+               long fillId = stylesTable.putFill(fill);
+               assertEquals(0, fillId);
+               
                cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
                cellStyleXf.setBorderId(0);
                cellXf = ctStylesheet.addNewCellXfs().addNewXf();
@@ -96,4 +107,24 @@ public class TestXSSFCellStyle extends TestCase {
                ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
                assertEquals("hair", cellStyle.getBorderTopAsString());
        }
+       
+       public void testGetFillBackgroundColor() {
+               CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+               CTColor ctBgColor = ctPatternFill.addNewBgColor();
+               ctBgColor.setIndexed(4);
+               assertEquals(4, cellStyle.getFillBackgroundColor());
+       }
+       
+       public void testGetFillForegroundColor() {
+               CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+               CTColor ctFgColor = ctPatternFill.addNewFgColor();
+               ctFgColor.setIndexed(5);
+               assertEquals(5, cellStyle.getFillForegroundColor());
+       }
+       
+       public void testGetFillPattern() {
+               CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+               ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
+               assertEquals(8, cellStyle.getFillPattern());
+       }
 }
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java
new file mode 100644 (file)
index 0000000..e69cd96
--- /dev/null
@@ -0,0 +1,58 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.xssf.usermodel.extensions;\r
+\r
+\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;\r
+\r
+import junit.framework.TestCase;\r
+\r
+\r
+public class TestXSSFCellFill extends TestCase {\r
+       \r
+       public void testGetFillBackgroundColor() {\r
+               CTFill ctFill = CTFill.Factory.newInstance();\r
+               XSSFCellFill cellFill = new XSSFCellFill(ctFill);\r
+               CTPatternFill ctPatternFill = ctFill.addNewPatternFill();\r
+               CTColor bgColor = ctPatternFill.addNewBgColor();\r
+               assertNotNull(cellFill.getFillBackgroundColor());\r
+               bgColor.setIndexed(2);\r
+               assertEquals(2, cellFill.getFillBackgroundColor().getIndexed());\r
+       }\r
+       \r
+       public void testGetFillForegroundColor() {\r
+               CTFill ctFill = CTFill.Factory.newInstance();\r
+               XSSFCellFill cellFill = new XSSFCellFill(ctFill);\r
+               CTPatternFill ctPatternFill = ctFill.addNewPatternFill();\r
+               CTColor fgColor = ctPatternFill.addNewFgColor();\r
+               assertNotNull(cellFill.getFillForegroundColor());\r
+               fgColor.setIndexed(8);\r
+               assertEquals(8, cellFill.getFillForegroundColor().getIndexed());\r
+       }\r
+       \r
+       public void testGetPatternType() {\r
+               CTFill ctFill = CTFill.Factory.newInstance();\r
+               XSSFCellFill cellFill = new XSSFCellFill(ctFill);\r
+               CTPatternFill ctPatternFill = ctFill.addNewPatternFill();\r
+               ctPatternFill.setPatternType(STPatternType.DARK_DOWN);\r
+               assertEquals(8, cellFill.getPatternType().intValue());\r
+       }\r
+}\r