From aa7fa3437615b3ae84ccd58418a5c6fcb7263af5 Mon Sep 17 00:00:00 2001 From: Paolo Mottadelli Date: Tue, 25 Mar 2008 13:19:27 +0000 Subject: [PATCH] XSSFCellStyle fillColors support; XSSFCellFill class; tests git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@640794 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/model/StylesTable.java | 16 ++++- .../poi/xssf/usermodel/XSSFCellStyle.java | 25 ++++++-- .../usermodel/extensions/XSSFCellFill.java | 43 ++++++++++++++ .../poi/xssf/usermodel/TestXSSFCellStyle.java | 31 ++++++++++ .../extensions/TestXSSFCellFill.java | 58 +++++++++++++++++++ 5 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java create mode 100644 src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index fcb0503da..0e79149e6 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -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 */ diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index e23441723..083dd3b3a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -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 index 000000000..8f8477e17 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellFill.java @@ -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; + } + +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java index 9f18dd6b9..ace6b95c0 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java @@ -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 index 000000000..e69cd968e --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/extensions/TestXSSFCellFill.java @@ -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()); + } +}