You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2010/09/20 22:10:14 UTC

svn commit: r999096 - in /poi/trunk/src: documentation/content/xdocs/ ooxml/java/org/apache/poi/xssf/model/ ooxml/java/org/apache/poi/xssf/usermodel/ ooxml/testcases/org/apache/poi/xssf/usermodel/

Author: nick
Date: Mon Sep 20 20:10:14 2010
New Revision: 999096

URL: http://svn.apache.org/viewvc?rev=999096&view=rev
Log:
Fix bug #47582 - XSSFCellStyle support for creating a style in one workbook based on a style from a different one

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

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=999096&r1=999095&r2=999096&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Mon Sep 20 20:10:14 2010
@@ -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>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java?rev=999096&r1=999095&r2=999096&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java Mon Sep 20 20:10:14 2010
@@ -183,6 +183,8 @@ public class StylesTable extends POIXMLD
 	 *  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 POIXMLD
 		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);

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java?rev=999096&r1=999095&r2=999096&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java Mon Sep 20 20:10:14 2010
@@ -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.ext
 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 Ce
     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");
         }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java?rev=999096&r1=999095&r2=999096&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java Mon Sep 20 20:10:14 2010
@@ -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 T
 	 * 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##"));
+   }
 }



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