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 84d9ed96c..4fe095ded 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -22,11 +22,17 @@ import org.apache.poi.ss.usermodel.Font; 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.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.BorderSides; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle.Enum; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment.Enum; public class XSSFCellStyle implements CellStyle { @@ -36,6 +42,7 @@ public class XSSFCellStyle implements CellStyle { private XSSFCellBorder cellBorder; private XSSFCellFill cellFill; private XSSFFont font; + private XSSFCellAlignment cellAlignment; /** * Creates a Cell Style from the supplied parts @@ -75,8 +82,11 @@ public class XSSFCellStyle implements CellStyle { } public short getAlignment() { - // TODO Auto-generated method stub - return 0; + return (short)getAlignmentEnum().intValue(); + } + + public STHorizontalAlignment.Enum getAlignmentEnum() { + return getCellAlignment().getHorizontal(); } public short getBorderBottom() { @@ -135,8 +145,7 @@ public class XSSFCellStyle implements CellStyle { } public Font getFont(Workbook parentWorkbook) { - // TODO Auto-generated method stub - return null; + return getFont(); } public Font getFont() { @@ -147,18 +156,15 @@ public class XSSFCellStyle implements CellStyle { } public short getFontIndex() { - // TODO Auto-generated method stub - return 0; + return (short) getFontId(); } public boolean getHidden() { - // TODO Auto-generated method stub - return false; + return getCellProtection().getHidden(); } public short getIndention() { - // TODO Auto-generated method stub - return 0; + return (short) getCellAlignment().getIndent(); } public short getIndex() { @@ -171,8 +177,7 @@ public class XSSFCellStyle implements CellStyle { } public boolean getLocked() { - // TODO Auto-generated method stub - return false; + return getCellProtection().getLocked(); } public short getRightBorderColor() { @@ -180,8 +185,7 @@ public class XSSFCellStyle implements CellStyle { } public short getRotation() { - // TODO Auto-generated method stub - return 0; + return (short) getCellAlignment().getTextRotation(); } public short getTopBorderColor() { @@ -189,18 +193,23 @@ public class XSSFCellStyle implements CellStyle { } public short getVerticalAlignment() { - // TODO Auto-generated method stub - return 0; + return (short) getVerticalAlignmentEnum().intValue(); + } + + public STVerticalAlignment.Enum getVerticalAlignmentEnum() { + return getCellAlignment().getVertical(); } public boolean getWrapText() { - // TODO Auto-generated method stub - return false; + return getCellAlignment().getWrapText(); } public void setAlignment(short align) { - // TODO Auto-generated method stub - + getCellAlignment().setHorizontal(STHorizontalAlignment.Enum.forInt(align)); + } + + public void setAlignementEnum(STHorizontalAlignment.Enum align) { + getCellAlignment().setHorizontal(align); } public void setBorderBottom(short border) { @@ -253,13 +262,11 @@ public class XSSFCellStyle implements CellStyle { } public void setHidden(boolean hidden) { - // TODO Auto-generated method stub - + getCellProtection().setHidden(hidden); } public void setIndention(short indent) { - // TODO Auto-generated method stub - + getCellAlignment().setIndent(indent); } public void setLeftBorderColor(short color) { @@ -268,8 +275,7 @@ public class XSSFCellStyle implements CellStyle { } public void setLocked(boolean locked) { - // TODO Auto-generated method stub - + getCellProtection().setLocked(locked); } public void setRightBorderColor(short color) { @@ -288,8 +294,11 @@ public class XSSFCellStyle implements CellStyle { } public void setVerticalAlignment(short align) { - // TODO Auto-generated method stub - + setVerticalAlignmentEnum(STVerticalAlignment.Enum.forInt(align)); + } + + public void setVerticalAlignmentEnum(STVerticalAlignment.Enum align) { + getCellAlignment().setVertical(align); } public void setWrapText(boolean wrapped) { @@ -326,7 +335,7 @@ public class XSSFCellStyle implements CellStyle { return (int) cellStyleXf.getFillId(); } - private Enum getBorderStyle(BorderSides side) { + private STBorderStyle.Enum getBorderStyle(BorderSides side) { return getCellBorder().getBorderStyle(side); } @@ -340,5 +349,26 @@ public class XSSFCellStyle implements CellStyle { } return (int) cellStyleXf.getFontId(); } + + private CTCellProtection getCellProtection() { + if (cellXf.getProtection() == null) { + CTCellProtection protection = cellXf.addNewProtection(); + } + return cellXf.getProtection(); + } + + private XSSFCellAlignment getCellAlignment() { + if (this.cellAlignment == null) { + this.cellAlignment = new XSSFCellAlignment(getCTCellAlignment()); + } + return this.cellAlignment; + } + + private CTCellAlignment getCTCellAlignment() { + if (cellXf.getAlignment() == null) { + cellXf.setAlignment(CTCellAlignment.Factory.newInstance()); + } + return cellXf.getAlignment(); + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java index 141383d20..18656b61e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java @@ -18,7 +18,7 @@ package org.apache.poi.xssf.usermodel.extensions; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment.Enum; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment; public class XSSFCellAlignment { @@ -29,7 +29,21 @@ public class XSSFCellAlignment { this.cellAlignement = cellAlignment; } - public Enum getHorizontal() { + public STVerticalAlignment.Enum getVertical() { + if (cellAlignement.getVertical() == null) { + cellAlignement.setVertical(STVerticalAlignment.TOP); + } + return cellAlignement.getVertical(); + } + + public void setVertical(STVerticalAlignment.Enum vertical) { + cellAlignement.setVertical(vertical); + } + + public STHorizontalAlignment.Enum getHorizontal() { + if (cellAlignement.getHorizontal() == null) { + cellAlignement.setHorizontal(STHorizontalAlignment.GENERAL); + } return cellAlignement.getHorizontal(); } @@ -44,4 +58,12 @@ public class XSSFCellAlignment { public void setIndent(long indent) { cellAlignement.setIndent(indent); } + + public long getTextRotation() { + return cellAlignement.getTextRotation(); + } + + public boolean getWrapText() { + return cellAlignement.getWrapText(); + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java index bd260dd17..37e67f674 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java @@ -53,7 +53,7 @@ public class XSSFCellBorder { return borders.size() - 1; } - public Enum getBorderStyle(BorderSides side) { + public org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle.Enum getBorderStyle(BorderSides side) { return getBorder(side).getStyle(); } 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 542e7a4bb..ef1bcf23a 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java @@ -23,6 +23,7 @@ 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.CTCellXfs; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; @@ -30,7 +31,9 @@ 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.STHorizontalAlignment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment; public class TestXSSFCellStyle extends TestCase { @@ -41,12 +44,14 @@ public class TestXSSFCellStyle extends TestCase { private CTFont ctFont; private CTXf cellStyleXf; private CTXf cellXf; + private CTCellXfs cellXfs; private XSSFCellStyle cellStyle; + private CTStylesheet ctStylesheet; public void setUp() { stylesTable = new StylesTable(); - CTStylesheet ctStylesheet = stylesTable._getRawStylesheet(); + ctStylesheet = stylesTable._getRawStylesheet(); // Until we do XSSFBorder properly, cheat ctBorderA = CTBorder.Factory.newInstance(); @@ -69,7 +74,8 @@ public class TestXSSFCellStyle extends TestCase { cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf(); cellStyleXf.setBorderId(0); - cellXf = ctStylesheet.addNewCellXfs().addNewXf(); + cellXfs = ctStylesheet.addNewCellXfs(); + cellXf = cellXfs.addNewXf(); cellXf.setXfId(0); cellStyle = new XSSFCellStyle(cellXf, cellStyleXf, stylesTable); } @@ -137,4 +143,44 @@ public class TestXSSFCellStyle extends TestCase { public void testGetFont() { assertNotNull(cellStyle.getFont()); } + + public void testGetSetHidden() { + assertFalse(cellStyle.getHidden()); + cellXf.getProtection().setHidden(true); + assertTrue(cellStyle.getHidden()); + cellStyle.setHidden(false); + assertFalse(cellStyle.getHidden()); + } + + public void testGetSetLocked() { + assertFalse(cellStyle.getLocked()); + cellXf.getProtection().setLocked(true); + assertTrue(cellStyle.getLocked()); + cellStyle.setLocked(false); + assertFalse(cellStyle.getLocked()); + } + + public void testGetSetIndent() { + assertEquals((short)0, cellStyle.getIndention()); + cellXf.getAlignment().setIndent(3); + assertEquals((short)3, cellStyle.getIndention()); + cellStyle.setIndention((short) 13); + assertEquals((short)13, cellXf.getAlignment().getIndent()); + } + + public void testGetSetAlignement() { + assertEquals(1, cellStyle.getAlignment()); + cellStyle.setAlignment((short)2); + assertEquals(STHorizontalAlignment.LEFT, cellStyle.getAlignmentEnum()); + cellStyle.setAlignementEnum(STHorizontalAlignment.JUSTIFY); + assertEquals((short)6, cellStyle.getAlignment()); + } + + public void testGetSetVerticalAlignment() { + assertEquals(1, cellStyle.getVerticalAlignment()); + cellStyle.setVerticalAlignment((short)2); + assertEquals(STVerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum()); + cellStyle.setVerticalAlignmentEnum(STVerticalAlignment.JUSTIFY); + assertEquals((short)4, cellStyle.getVerticalAlignment()); + } }