]> source.dussan.org Git - poi.git/commitdiff
Fix bug #47582 - XSSFCellStyle support for creating a style in one workbook based...
authorNick Burch <nick@apache.org>
Mon, 20 Sep 2010 20:10:14 +0000 (20:10 +0000)
committerNick Burch <nick@apache.org>
Mon, 20 Sep 2010 20:10:14 +0000 (20:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@999096 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java

index cad9c5d4e140ebab800c3a016cc3e389031db46e..736f3549b4694f079b5eb979adc0c8d11cab1101 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta3" date="2010-??-??">
+           <action dev="poi-developers" type="add">47582 - XSSFCellStyle support for creating a style in one workbook based on a style from a different one</action>
            <action dev="poi-developers" type="fix">49931 - Avoid concurrency problems when re-ordering multiple HSSF header records for a PageSettingsBlock</action>
            <action dev="poi-developers" type="fix">49765 - Fix XWPFDocument.addPicture so that it correctly sets up relationships</action>
            <action dev="poi-developers" type="fix">48018 - Improve HWPF handling of lists in documents read and then saved, by preserving order better</action>
index 0230d660fdcd270491d51392dd6d77e81e3f6256..6a4a7678cb8e54533e7117c29a63f15cf18e6b96 100644 (file)
@@ -183,6 +183,8 @@ public class StylesTable extends POIXMLDocumentPart {
         *  registration is requested.
         * This allows people to create several fonts
         *  then customise them later.
+        * Note - End Users probably want to call
+        *  {@link XSSFFont#registerTo(StylesTable)}
         */
        public int putFont(XSSFFont font, boolean forceRegistration) {
                int idx = -1;
@@ -193,8 +195,10 @@ public class StylesTable extends POIXMLDocumentPart {
                if (idx != -1) {
                        return idx;
                }
+               
+               idx = fonts.size();
                fonts.add(font);
-               return fonts.size() - 1;
+               return idx;
        }
        public int putFont(XSSFFont font) {
                return putFont(font, false);
index d2d17bd57b08cf269305c28b31d31de11d2ebcf0..6358168c67a74cbf013102319649755a4686d4fb 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import org.apache.poi.POIXMLException;
 import org.apache.poi.ss.usermodel.BorderStyle;
 import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.FillPatternType;
@@ -31,11 +32,13 @@ import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
+import org.apache.xmlbeans.XmlException;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
@@ -132,8 +135,44 @@ public class XSSFCellStyle implements CellStyle {
     public void cloneStyleFrom(CellStyle source) {
         if(source instanceof XSSFCellStyle) {
             XSSFCellStyle src = (XSSFCellStyle)source;
-            _cellXf.set(src.getCoreXf());
-            _cellStyleXf.set(src.getStyleXf());
+            
+            // Is it on our Workbook?
+            if(src._stylesSource == _stylesSource) {
+               // Nice and easy
+               _cellXf.set(src.getCoreXf());
+               _cellStyleXf.set(src.getStyleXf());
+            } else {
+               // Copy the style
+               try {
+                  _cellXf = CTXf.Factory.parse(
+                        src.getCoreXf().toString()
+                  );
+               } catch(XmlException e) {
+                  throw new POIXMLException(e);
+               }
+               
+               // Copy the format
+               String fmt = src.getDataFormatString();
+               setDataFormat(
+                     (new XSSFDataFormat(_stylesSource)).getFormat(fmt)
+               );
+               
+               // Copy the font
+               try {
+                  CTFont ctFont = CTFont.Factory.parse(
+                        src.getFont().getCTFont().toString()
+                  );
+                  XSSFFont font = new XSSFFont(ctFont);
+                  font.registerTo(_stylesSource);
+                  setFont(font);
+               } catch(XmlException e) {
+                  throw new POIXMLException(e);
+               }
+            }
+            
+            // Clear out cached details
+            _font = null;
+            _cellAlignment = null;
         } else {
             throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
         }
index f6c63c96a9e38cab61980a364a8cc9f4235e422d..02b34ae85ef2dd4ba42414a94229aff710a9e1a2 100644 (file)
@@ -19,16 +19,25 @@ package org.apache.poi.xssf.usermodel;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.VerticalAlignment;
-import org.apache.poi.ss.usermodel.IndexedColors;
 import org.apache.poi.ss.usermodel.HorizontalAlignment;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.VerticalAlignment;
 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.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.hssf.usermodel.HSSFCellStyle;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
+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.STHorizontalAlignment;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
 
 
 public class TestXSSFCellStyle extends TestCase {
@@ -586,12 +595,85 @@ public class TestXSSFCellStyle extends TestCase {
         * Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
         */
        public void testCloneStyleSameWB() {
-               // TODO
+      XSSFWorkbook wb = new XSSFWorkbook();
+      assertEquals(1, wb.getNumberOfFonts());
+      
+      XSSFFont fnt = wb.createFont();
+      fnt.setFontName("TestingFont");
+      assertEquals(2, wb.getNumberOfFonts());
+      
+      XSSFCellStyle orig = wb.createCellStyle();
+      orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
+      orig.setFont(fnt);
+      orig.setDataFormat((short)18);
+      
+      assertTrue(HSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
+      assertTrue(fnt == orig.getFont());
+      assertTrue(18 == orig.getDataFormat());
+      
+      XSSFCellStyle clone = wb.createCellStyle();
+      assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
+      assertFalse(fnt == clone.getFont());
+      assertFalse(18 == clone.getDataFormat());
+      
+      clone.cloneStyleFrom(orig);
+      assertTrue(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
+      assertTrue(fnt == clone.getFont());
+      assertTrue(18 == clone.getDataFormat());
+      assertEquals(2, wb.getNumberOfFonts());
        }
        /**
         * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
         */
        public void testCloneStyleDiffWB() {
-               // TODO
-       }
+       XSSFWorkbook wbOrig = new XSSFWorkbook();
+       assertEquals(1, wbOrig.getNumberOfFonts());
+       assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
+       
+       XSSFFont fnt = wbOrig.createFont();
+       fnt.setFontName("TestingFont");
+       assertEquals(2, wbOrig.getNumberOfFonts());
+       assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
+       
+       XSSFDataFormat fmt = wbOrig.createDataFormat();
+       fmt.getFormat("MadeUpOne");
+       fmt.getFormat("MadeUpTwo");
+       
+       XSSFCellStyle orig = wbOrig.createCellStyle();
+       orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
+       orig.setFont(fnt);
+       orig.setDataFormat(fmt.getFormat("Test##"));
+       
+       assertTrue(XSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
+       assertTrue(fnt == orig.getFont());
+       assertTrue(fmt.getFormat("Test##") == orig.getDataFormat());
+       
+       assertEquals(2, wbOrig.getNumberOfFonts());
+       assertEquals(3, wbOrig.getStylesSource().getNumberFormats().size());
+       
+       
+       // Now a style on another workbook
+       XSSFWorkbook wbClone = new XSSFWorkbook();
+       assertEquals(1, wbClone.getNumberOfFonts());
+       assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
+       
+       XSSFDataFormat fmtClone = wbClone.createDataFormat();
+       XSSFCellStyle clone = wbClone.createCellStyle();
+       
+       assertEquals(1, wbClone.getNumberOfFonts());
+       assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
+       
+       assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
+       assertFalse("TestingFont" == clone.getFont().getFontName());
+       
+       clone.cloneStyleFrom(orig);
+       
+       assertEquals(2, wbClone.getNumberOfFonts());
+       assertEquals(1, wbClone.getStylesSource().getNumberFormats().size());
+       
+       assertEquals(HSSFCellStyle.ALIGN_RIGHT, clone.getAlignment());
+       assertEquals("TestingFont", clone.getFont().getFontName());
+       assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat());
+       assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
+   }
 }