]> source.dussan.org Git - poi.git/commitdiff
Completed StylesTable initialization in order to create a new 'POIzed' file readable...
authorPaolo Mottadelli <paolo@apache.org>
Sun, 6 Apr 2008 13:05:13 +0000 (13:05 +0000)
committerPaolo Mottadelli <paolo@apache.org>
Sun, 6 Apr 2008 13:05:13 +0000 (13:05 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@645234 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/XSSFCellBorder.java
src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java
src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFColor.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java

index c26f2af3fe643e702897f379ae020b59e0564a54..e6965a91be56fee121aeb998739e1a1c45b1d76b 100644 (file)
@@ -35,15 +35,19 @@ 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;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorders;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellStyleXfs;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFills;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFonts;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmt;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmts;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
 
 
@@ -84,16 +88,8 @@ public class StylesTable implements StylesSource, XSSFModel {
     public StylesTable() {
        doc = StyleSheetDocument.Factory.newInstance();
        doc.addNewStyleSheet();
-       
-       // Add a single, default cell xf and cell style xf
-       // Excel seems to require these
-       CTXf[] ctxfs = new CTXf[2];
-       for (int i = 0; i < ctxfs.length; i++) {
-                       ctxfs[i] = CTXf.Factory.newInstance(); 
-                       ctxfs[i].setNumFmtId(0);
-               }
-       xfs.add(ctxfs[0]);
-       styleXfs.add(ctxfs[1]);
+       // Initialization required in order to make the document readable by MSExcel
+       initialize();
     }
 
     /**
@@ -279,18 +275,24 @@ public class StylesTable implements StylesSource, XSSFModel {
        doc.getStyleSheet().setNumFmts(formats);
        
        // Fonts
-       CTFonts fnts = CTFonts.Factory.newInstance();
-       fnts.setCount(fonts.size());
-       fnts.setFontArray(
+       CTFonts ctFonts = CTFonts.Factory.newInstance();
+       ctFonts.setCount(fonts.size());
+       ctFonts.setFontArray(
                        fonts.toArray(new CTFont[fonts.size()])
        );
-       doc.getStyleSheet().setFonts(fnts);
+       doc.getStyleSheet().setFonts(ctFonts);
        
        // Fills
-       // TODO
+       CTFills ctFills = CTFills.Factory.newInstance();
+       ctFills.setCount(fills.size());
+       ctFills.setFillArray(fills.toArray(new CTFill[fills.size()]));
+       doc.getStyleSheet().setFills(ctFills);
        
        // Borders
-       // TODO
+       CTBorders ctBorders = CTBorders.Factory.newInstance();
+       ctBorders.setCount(borders.size());
+       ctBorders.setBorderArray(borders.toArray(new CTBorder[borders.size()]));
+       doc.getStyleSheet().setBorders(ctBorders);
        
        // Xfs
        if(xfs.size() > 0) {
@@ -327,4 +329,51 @@ public class StylesTable implements StylesSource, XSSFModel {
     private long putFont(XSSFFont font, LinkedList<CTFont> fonts) {
        return font.putFont(fonts);
        }
+       private void initialize() {
+               CTFont ctFont = createDefaultFont();
+       fonts.add(ctFont);
+       
+       CTFill ctFill = createDefaultFill();
+       fills.add(ctFill);
+       
+       CTBorder ctBorder = createDefaultBorder();
+       borders.add(ctBorder);
+       
+       CTXf styleXf = createDefaultXf();
+       styleXfs.add(styleXf);
+       CTXf xf = createDefaultXf();
+       xf.setXfId(0);
+       xfs.add(xf);
+       }
+       private CTXf createDefaultXf() {
+               CTXf ctXf = CTXf.Factory.newInstance();
+       ctXf.setNumFmtId(0);
+       ctXf.setFontId(0);
+       ctXf.setFillId(0);
+       ctXf.setBorderId(0);
+       return ctXf;
+       }
+       private CTBorder createDefaultBorder() {
+               CTBorder ctBorder = CTBorder.Factory.newInstance();
+       ctBorder.addNewBottom();
+       ctBorder.addNewTop();
+       ctBorder.addNewLeft();
+       ctBorder.addNewRight();
+       ctBorder.addNewDiagonal();
+               return ctBorder;
+       }
+       private CTFill createDefaultFill() {
+               CTFill ctFill = CTFill.Factory.newInstance();
+       ctFill.addNewPatternFill().setPatternType(STPatternType.NONE);
+               return ctFill;
+       }
+       private CTFont createDefaultFont() {
+               CTFont ctFont = CTFont.Factory.newInstance();
+       ctFont.addNewSz().setVal(11);
+       ctFont.addNewColor().setTheme(1);
+       ctFont.addNewName().setVal("Calibri");
+       ctFont.addNewFamily().setVal(2);
+       ctFont.addNewScheme().setVal(STFontScheme.MINOR);
+               return ctFont;
+       }
 }
index f2a9ddcdeddd24b4e905c63f22eed1923ba8390b..4f8296d3bd9f2f12effccb49b0bf25d3ace4464b 100644 (file)
@@ -33,7 +33,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment.Enum;
 
 
 public class XSSFCellStyle implements CellStyle {
@@ -313,6 +312,14 @@ public class XSSFCellStyle implements CellStyle {
                getCellAlignment().setWrapText(wrapped);
        }
 
+       public XSSFColor getBorderColor(BorderSide side) {
+               return getCellBorder().getBorderColor(side);
+       }
+       
+       public void setBorderColor(BorderSide side, XSSFColor color) {
+               getCellBorder().setBorderColor(side, color);
+       }
+
        private XSSFCellBorder getCellBorder() {
                if (cellBorder == null) {
                        // TODO make a common Cell Border object
@@ -373,10 +380,6 @@ public class XSSFCellStyle implements CellStyle {
        private short getBorderColorIndexed(BorderSide side) {
                return (short) getBorderColor(side).getIndexed();
        }
-
-       private XSSFColor getBorderColor(BorderSide side) {
-               return getCellBorder().getBorderColor(side);
-       }
        
        private void setBorderColorIndexed(BorderSide side, long color) {
                getBorderColor(side).setIndexed(color);
index 485814be0991c11d3a328e4d00f0664606402744..57d476b0d2a76f22dba3c11c730556b099387484 100644 (file)
@@ -22,7 +22,6 @@ import java.util.LinkedList;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle.Enum;
 
 
 public class XSSFCellBorder {
@@ -69,6 +68,10 @@ public class XSSFCellBorder {
                }
                return new XSSFColor(getBorder(side).getColor());
        }
+
+       public void setBorderColor(BorderSide side, XSSFColor color) {
+               color.setToBorder(getBorder(side));
+       }
        
        private CTBorderPr getBorder(BorderSide side) {
                switch (side) {
index d848498b3de6625cf886f848992cd388fc095647..6540441ebd6920b0dd96eda851232c4d3e605602 100644 (file)
@@ -1,3 +1,19 @@
+/* ====================================================================\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
 package org.apache.poi.xssf.usermodel.extensions;\r
 \r
 import java.util.LinkedList;\r
index b8ee04a1dca5a3383fd909e17b9c0069ae5e1151..5ca0f552a49eec9af5513ab235c1dcaa89da41f7 100644 (file)
@@ -16,6 +16,7 @@
 ==================================================================== */
 package org.apache.poi.xssf.usermodel.extensions;
 
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
 
 public class XSSFColor {
@@ -65,4 +66,8 @@ public class XSSFColor {
        public void setTint(double tint) {
                color.setTint(tint);
        }
+
+       public void setToBorder(CTBorderPr border) {
+               border.setColor(this.color);
+       }
 }
index 98ebba8eb5a47ce6719c0bd4320d69bf1f4528f7..d368d3aaa29ff3cabbd5f15be4086d9e6e9ab201 100644 (file)
@@ -22,6 +22,8 @@ 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.apache.poi.xssf.usermodel.extensions.XSSFColor;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
@@ -136,6 +138,66 @@ public class TestXSSFCellStyle extends TestCase {
                assertEquals("hair", cellStyle.getBorderTopAsString());
        }
        
+       public void testGetSetBottomBorderColor() {
+               CTColor ctColor = ctBorderA.addNewBottom().addNewColor();
+               ctColor.setIndexed(2);
+               XSSFColor color = new XSSFColor(ctColor);
+               assertEquals((short)2, cellStyle.getBottomBorderColor());
+               CTColor anotherCtColor = CTColor.Factory.newInstance();
+               anotherCtColor.setIndexed(4);
+               anotherCtColor.setTheme(3);
+               anotherCtColor.setRgb("1234".getBytes());
+               XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+               cellStyle.setBorderColor(BorderSide.BOTTOM, anotherColor);
+               assertEquals((short)4, cellStyle.getBottomBorderColor());
+               assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.BOTTOM).getRgb()));
+       }
+       
+       public void testGetSetTopBorderColor() {
+               CTColor ctColor = ctBorderA.addNewTop().addNewColor();
+               ctColor.setIndexed(5);
+               XSSFColor color = new XSSFColor(ctColor);
+               assertEquals((short)5, cellStyle.getTopBorderColor());
+               CTColor anotherCtColor = CTColor.Factory.newInstance();
+               anotherCtColor.setIndexed(7);
+               anotherCtColor.setTheme(3);
+               anotherCtColor.setRgb("abcd".getBytes());
+               XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+               cellStyle.setBorderColor(BorderSide.TOP, anotherColor);
+               assertEquals((short)7, cellStyle.getTopBorderColor());
+               assertEquals(new String("abcd".getBytes()), new String(cellStyle.getBorderColor(BorderSide.TOP).getRgb()));
+       }
+       
+       public void testGetSetLeftBorderColor() {
+               CTColor ctColor = ctBorderA.addNewLeft().addNewColor();
+               ctColor.setIndexed(2);
+               XSSFColor color = new XSSFColor(ctColor);
+               assertEquals((short)2, cellStyle.getLeftBorderColor());
+               CTColor anotherCtColor = CTColor.Factory.newInstance();
+               anotherCtColor.setIndexed(4);
+               anotherCtColor.setTheme(3);
+               anotherCtColor.setRgb("1234".getBytes());
+               XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+               cellStyle.setBorderColor(BorderSide.LEFT, anotherColor);
+               assertEquals((short)4, cellStyle.getLeftBorderColor());
+               assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.LEFT).getRgb()));
+       }
+       
+       public void testGetSetRightBorderColor() {
+               CTColor ctColor = ctBorderA.addNewRight().addNewColor();
+               ctColor.setIndexed(8);
+               XSSFColor color = new XSSFColor(ctColor);
+               assertEquals((short)8, cellStyle.getRightBorderColor());
+               CTColor anotherCtColor = CTColor.Factory.newInstance();
+               anotherCtColor.setIndexed(14);
+               anotherCtColor.setTheme(3);
+               anotherCtColor.setRgb("af67".getBytes());
+               XSSFColor anotherColor = new XSSFColor(anotherCtColor);
+               cellStyle.setBorderColor(BorderSide.RIGHT, anotherColor);
+               assertEquals((short)14, cellStyle.getRightBorderColor());
+               assertEquals(new String("af67".getBytes()), new String(cellStyle.getBorderColor(BorderSide.RIGHT).getRgb()));
+       }
+       
        public void testGetFillBackgroundColor() {
                CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
                CTColor ctBgColor = ctPatternFill.addNewBgColor();