diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/TableRowAlign.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TableRowAlign.java new file mode 100644 index 000000000..0345ecafa --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TableRowAlign.java @@ -0,0 +1,54 @@ +/* ==================================================================== + 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.xwpf.usermodel; + +import java.util.HashMap; +import java.util.Map; + +/** + * Sets alignment values allowed for Tables and Table Rows + */ +public enum TableRowAlign { + + LEFT(1), + CENTER(2), + RIGHT(3); + + private static Map imap = new HashMap<>(); + + static { + for (TableRowAlign p : values()) { + imap.put(p.getValue(), p); + } + } + + private final int value; + + private TableRowAlign(int val) { + value = val; + } + + public static TableRowAlign valueOf(int type) { + TableRowAlign err = imap.get(type); + if (err == null) throw new IllegalArgumentException("Unknown table row alignment: " + type); + return err; + } + + public int getValue() { + return value; + } +} diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java index 8cae81c3e..7ff279fad 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTable.java @@ -29,6 +29,7 @@ import org.apache.poi.util.NotImplemented; import org.apache.poi.util.Removal; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString; @@ -39,6 +40,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; /** @@ -410,6 +412,39 @@ public class XWPFTable implements IBodyElement, ISDTContents { : null; } + /** + * Returns the current table alignment or NULL + * + * @return Table Alignment as a {@link TableRowAlign} enum + */ + public TableRowAlign getTableAlignment() { + CTTblPr tPr = getTblPr(false); + return tPr == null ? null + : tPr.isSetJc() ? TableRowAlign.valueOf(tPr.getJc().getVal().intValue()) + : null; + } + + /** + * Set table alignment to specified {@link TableRowAlign} + * + * @param ha {@link TableRowAlign} to set + */ + public void setTableAlignment(TableRowAlign tra) { + CTTblPr tPr = getTblPr(true); + CTJc jc = tPr.isSetJc() ? tPr.getJc() : tPr.addNewJc(); + jc.setVal(STJc.Enum.forInt(tra.getValue())); + } + + /** + * Removes the table alignment attribute from a table + */ + public void removeTableAlignment() { + CTTblPr tPr = getTblPr(false); + if (tPr != null && tPr.isSetJc()) { + tPr.unsetJc(); + } + } + private void addColumn(XWPFTableRow tabRow, int sizeCol) { if (sizeCol > 0) { for (int i = 0; i < sizeCol; i++) { @@ -1172,6 +1207,6 @@ public class XWPFTable implements IBodyElement, ISDTContents { THIN_THICK_MEDIUM_GAP, THICK_THIN_MEDIUM_GAP, THIN_THICK_THIN_MEDIUM_GAP, THIN_THICK_LARGE_GAP, THICK_THIN_LARGE_GAP, THIN_THICK_THIN_LARGE_GAP, WAVE, DOUBLE_WAVE, DASH_SMALL_GAP, DASH_DOT_STROKED, THREE_D_EMBOSS, THREE_D_ENGRAVE, - OUTSET, INSET + OUTSET, INSET; } } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java index e264702c8..96e7b2271 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTable.java @@ -16,13 +16,18 @@ ==================================================================== */ package org.apache.poi.xwpf.usermodel; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + import java.io.IOException; import java.math.BigInteger; import java.util.List; -import junit.framework.TestCase; import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType; +import org.junit.Test; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; @@ -34,20 +39,13 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder; + /** * Tests for XWPF Tables */ -public class TestXWPFTable extends TestCase { - @Override - protected void setUp() { - /* - XWPFDocument doc = new XWPFDocument(); - p = doc.createParagraph(); - - this.ctRun = CTR.Factory.newInstance(); - */ - } +public class TestXWPFTable { + @Test public void testConstructor() { XWPFDocument doc = new XWPFDocument(); CTTbl ctTable = CTTbl.Factory.newInstance(); @@ -70,6 +68,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testTblGrid() { XWPFDocument doc = new XWPFDocument(); CTTbl ctTable = CTTbl.Factory.newInstance(); @@ -87,6 +86,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testGetText() { XWPFDocument doc = new XWPFDocument(); CTTbl table = CTTbl.Factory.newInstance(); @@ -106,7 +106,7 @@ public class TestXWPFTable extends TestCase { } } - + @Test public void testCreateRow() { XWPFDocument doc = new XWPFDocument(); @@ -142,7 +142,7 @@ public class TestXWPFTable extends TestCase { } } - + @Test public void testSetGetWidth() { XWPFDocument doc = new XWPFDocument(); @@ -162,6 +162,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetHeight() { XWPFDocument doc = new XWPFDocument(); @@ -178,6 +179,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetMargins() { // instantiate the following class so it'll get picked up by // the XmlBean process and added to the jar file. it's required @@ -206,6 +208,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetHBorders() { // instantiate the following classes so they'll get picked up by // the XmlBean process and added to the jar file. they are required @@ -278,6 +281,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetVBorders() { // create a table XWPFDocument doc = new XWPFDocument(); @@ -347,6 +351,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetTopBorders() { // create a table XWPFDocument doc = new XWPFDocument(); @@ -389,6 +394,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetBottomBorders() { // create a table XWPFDocument doc = new XWPFDocument(); @@ -431,6 +437,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetLeftBorders() { // create a table XWPFDocument doc = new XWPFDocument(); @@ -473,6 +480,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetRightBorders() { // create a table XWPFDocument doc = new XWPFDocument(); @@ -515,6 +523,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetRowBandSize() { XWPFDocument doc = new XWPFDocument(); CTTbl ctTable = CTTbl.Factory.newInstance(); @@ -529,6 +538,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testSetGetColBandSize() { XWPFDocument doc = new XWPFDocument(); CTTbl ctTable = CTTbl.Factory.newInstance(); @@ -543,6 +553,7 @@ public class TestXWPFTable extends TestCase { } } + @Test public void testCreateTable() throws Exception { // open an empty document XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); @@ -572,4 +583,23 @@ public class TestXWPFTable extends TestCase { fail("Unable to close doc"); } } + + @Test + public void testSetGetTableAlignment() { + XWPFDocument doc = new XWPFDocument(); + XWPFTable tbl = doc.createTable(1, 1); + tbl.setTableAlignment(TableRowAlign.LEFT); + assertEquals(TableRowAlign.LEFT, tbl.getTableAlignment()); + tbl.setTableAlignment(TableRowAlign.CENTER); + assertEquals(TableRowAlign.CENTER, tbl.getTableAlignment()); + tbl.setTableAlignment(TableRowAlign.RIGHT); + assertEquals(TableRowAlign.RIGHT, tbl.getTableAlignment()); + tbl.removeTableAlignment(); + assertNull(tbl.getTableAlignment()); + try { + doc.close(); + } catch (IOException e) { + fail("Unable to close doc"); + } + } } \ No newline at end of file