You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by pa...@apache.org on 2008/03/25 14:19:34 UTC

svn commit: r640794 - in /poi/branches/ooxml/src/ooxml: java/org/apache/poi/xssf/model/ java/org/apache/poi/xssf/usermodel/ java/org/apache/poi/xssf/usermodel/extensions/ testcases/org/apache/poi/xssf/usermodel/ testcases/org/apache/poi/xssf/usermodel/...

Author: paolo
Date: Tue Mar 25 06:19:27 2008
New Revision: 640794

URL: http://svn.apache.org/viewvc?rev=640794&view=rev
Log:
XSSFCellStyle fillColors support; XSSFCellFill class; tests

Added:
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java
    poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java
Modified:
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
    poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java?rev=640794&r1=640793&r2=640794&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java Tue Mar 25 06:19:27 2008
@@ -30,6 +30,7 @@
 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 @@
 		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
      */

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java?rev=640794&r1=640793&r2=640794&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java Tue Mar 25 06:19:27 2008
@@ -23,6 +23,7 @@
 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 @@
 	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 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) {
@@ -303,6 +302,20 @@
 			return (int) cellXf.getBorderId();
 		}
 		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) {

Added: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java?rev=640794&view=auto
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java (added)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java Tue Mar 25 06:19:27 2008
@@ -0,0 +1,43 @@
+package org.apache.poi.xssf.usermodel.extensions;
+
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
+
+public class XSSFCellFill {
+	
+	private CTFill fill;
+	
+	public XSSFCellFill(CTFill fill) {
+		this.fill = fill;
+	}
+	
+	public XSSFCellFill() {
+		this.fill = CTFill.Factory.newInstance();
+	}
+	
+	public XSSFColor getFillBackgroundColor() {
+		return new XSSFColor(getPatternFill().getBgColor());
+	}
+
+	public XSSFColor getFillForegroundColor() {
+		return new XSSFColor(getPatternFill().getFgColor());
+	}
+
+	public Enum getPatternType() {
+		return getPatternFill().getPatternType();
+	}
+
+	private CTPatternFill getPatternFill() {
+		CTPatternFill patternFill = fill.getPatternFill();
+		if (patternFill == null) {
+			patternFill = fill.addNewPatternFill();
+		}
+		return patternFill;
+	}
+
+	public CTFill getCTFill() {
+		return this.fill;
+	}
+
+}

Modified: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java?rev=640794&r1=640793&r2=640794&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java (original)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java Tue Mar 25 06:19:27 2008
@@ -21,10 +21,15 @@
 
 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 @@
 	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 @@
 		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();
@@ -95,5 +106,25 @@
 	public void testGetTopBottomAsString() {
 		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());
 	}
 }

Added: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java?rev=640794&view=auto
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java (added)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java Tue Mar 25 06:19:27 2008
@@ -0,0 +1,58 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.extensions;
+
+
+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.STPatternType;
+
+import junit.framework.TestCase;
+
+
+public class TestXSSFCellFill extends TestCase {
+	
+	public void testGetFillBackgroundColor() {
+		CTFill ctFill = CTFill.Factory.newInstance();
+		XSSFCellFill cellFill = new XSSFCellFill(ctFill);
+		CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+		CTColor bgColor = ctPatternFill.addNewBgColor();
+		assertNotNull(cellFill.getFillBackgroundColor());
+		bgColor.setIndexed(2);
+		assertEquals(2, cellFill.getFillBackgroundColor().getIndexed());
+	}
+	
+	public void testGetFillForegroundColor() {
+		CTFill ctFill = CTFill.Factory.newInstance();
+		XSSFCellFill cellFill = new XSSFCellFill(ctFill);
+		CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+		CTColor fgColor = ctPatternFill.addNewFgColor();
+		assertNotNull(cellFill.getFillForegroundColor());
+		fgColor.setIndexed(8);
+		assertEquals(8, cellFill.getFillForegroundColor().getIndexed());
+	}
+	
+	public void testGetPatternType() {
+		CTFill ctFill = CTFill.Factory.newInstance();
+		XSSFCellFill cellFill = new XSSFCellFill(ctFill);
+		CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
+		ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
+		assertEquals(8, cellFill.getPatternType().intValue());
+	}
+}



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