XSSFCellStyle fillColors support; XSSFCellFill class; tests

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@640794 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Paolo Mottadelli 2008-03-25 13:19:27 +00:00
parent 0da28ed66c
commit aa7fa34376
5 changed files with 166 additions and 7 deletions

View File

@ -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
*/

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}