You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2012/02/07 10:41:53 UTC

svn commit: r1241393 - in /poi/trunk/src: documentation/content/xdocs/status.xml ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java

Author: yegor
Date: Tue Feb  7 09:41:52 2012
New Revision: 1241393

URL: http://svn.apache.org/viewvc?rev=1241393&view=rev
Log:
Bugzilla 52566: added methods to set/get vertical alignment and color in XWPFTableCell

Added:
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java
Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1241393&r1=1241392&r2=1241393&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Tue Feb  7 09:41:52 2012
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta6" date="2012-??-??">
+           <action dev="poi-developers" type="add">52566 - Added methods to set/get vertical alignment and color in XWPFTableCell</action>
            <action dev="poi-developers" type="add">52562 - Added methods to get/set a table row's Can't Split and Repeat Header attributes  in XWPF</action>
            <action dev="poi-developers" type="add">52561 - Added methods to set table inside borders and cell margins in XWPF</action>
            <action dev="poi-developers" type="add">52569 - Support DConRefRecord in HSSF</action>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java?rev=1241393&r1=1241392&r2=1241393&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java Tue Feb  7 09:41:52 2012
@@ -18,6 +18,8 @@ package org.apache.poi.xwpf.usermodel;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.EnumMap;
+import java.util.HashMap;
 import java.util.List;
 
 import org.apache.poi.POIXMLDocumentPart;
@@ -26,10 +28,21 @@ import org.apache.xmlbeans.XmlCursor;
 import org.apache.xmlbeans.XmlObject;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
-
-
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
+
+/**
+ * XWPFTableCell class.
+ *
+ * @author Gregg Morris (gregg dot morris at gmail dot com) - added XWPFVertAlign enum,
+ *         setColor(),
+ *         setVerticalAlignment()
+ */
 public class XWPFTableCell implements IBody {
     private final CTTc ctTc;
     protected List<XWPFParagraph> paragraphs = null;
@@ -37,6 +50,28 @@ public class XWPFTableCell implements IB
     protected List<IBodyElement> bodyElements = null;
     protected IBody part;
     private XWPFTableRow tableRow = null;
+    // Create a map from this XWPF-level enum to the STVerticalJc.Enum values
+    public static enum XWPFVertAlign { TOP, CENTER, BOTH, BOTTOM };
+    private static EnumMap<XWPFVertAlign, STVerticalJc.Enum> alignMap;
+    // Create a map from the STVerticalJc.Enum values to the XWPF-level enums
+    private static HashMap<Integer, XWPFVertAlign> stVertAlignTypeMap;
+
+    static {
+        // populate enum maps
+        alignMap = new EnumMap<XWPFVertAlign, STVerticalJc.Enum>(XWPFVertAlign.class);
+        alignMap.put(XWPFVertAlign.TOP, STVerticalJc.Enum.forInt(STVerticalJc.INT_TOP));
+        alignMap.put(XWPFVertAlign.CENTER, STVerticalJc.Enum.forInt(STVerticalJc.INT_CENTER));
+        alignMap.put(XWPFVertAlign.BOTH, STVerticalJc.Enum.forInt(STVerticalJc.INT_BOTH));
+        alignMap.put(XWPFVertAlign.BOTTOM, STVerticalJc.Enum.forInt(STVerticalJc.INT_BOTTOM));
+
+        stVertAlignTypeMap = new HashMap<Integer, XWPFVertAlign>();
+        stVertAlignTypeMap.put(STVerticalJc.INT_TOP, XWPFVertAlign.TOP);
+        stVertAlignTypeMap.put(STVerticalJc.INT_CENTER, XWPFVertAlign.CENTER);
+        stVertAlignTypeMap.put(STVerticalJc.INT_BOTH, XWPFVertAlign.BOTH);
+        stVertAlignTypeMap.put(STVerticalJc.INT_BOTTOM, XWPFVertAlign.BOTTOM);
+
+    }
+
     /**
      * If a table cell does not include at least one block-level element, then this document shall be considered corrupt
      */
@@ -152,6 +187,59 @@ public class XWPFTableCell implements IB
     }
     
     /**
+     * Set cell color. This sets some associated values; for finer control
+     * you may want to access these elements individually.
+     * @param rgbStr - the desired cell color, in the hex form "RRGGBB".
+     */
+    public void setColor(String rgbStr) {
+        CTTcPr tcpr = ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr();
+        CTShd ctshd = tcpr.isSetShd() ? tcpr.getShd() : tcpr.addNewShd();
+        ctshd.setColor("auto");
+        ctshd.setVal(STShd.CLEAR);
+        ctshd.setFill(rgbStr);
+    }
+
+    /**
+     * Get cell color. Note that this method only returns the "fill" value.
+     * @return RGB string of cell color
+     */
+    public String getColor() {
+    	String color = null;
+        CTTcPr tcpr = ctTc.getTcPr();
+        if (tcpr != null) {
+        	CTShd ctshd = tcpr.getShd();
+        	if (ctshd != null) {
+        		color = ctshd.xgetFill().getStringValue();
+        	}
+        }
+    	return color;
+    }
+
+    /**
+     * Set the vertical alignment of the cell.
+     * @param vAlign - the desired alignment enum value
+     */
+    public void setVerticalAlignment(XWPFVertAlign vAlign) {
+        CTTcPr tcpr = ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr();
+    	CTVerticalJc va = tcpr.addNewVAlign();
+    	va.setVal(alignMap.get(vAlign));
+    }
+
+    /**
+     * Get the vertical alignment of the cell.
+     * @return the cell alignment enum value
+     */
+    public XWPFVertAlign getVerticalAlignment() {
+    	XWPFVertAlign vAlign = null;
+        CTTcPr tcpr = ctTc.getTcPr();
+        if (ctTc != null) {
+        	CTVerticalJc va = tcpr.getVAlign();
+        	vAlign = stVertAlignTypeMap.get(va.getVal().intValue());
+        }
+        return vAlign;
+    }
+
+    /**
      * add a new paragraph at position of the cursor
      * @param cursor
      * @return the inserted paragraph
@@ -346,7 +434,7 @@ public class XWPFTableCell implements IB
 			return null;
 		}
 		XWPFTableRow tableRow = table.getRow(row);
-		if(row == null){
+		if (tableRow == null) {
 			return null;
 		}
 		return tableRow.getTableCell(cell);

Added: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java?rev=1241393&view=auto
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java (added)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java Tue Feb  7 09:41:52 2012
@@ -0,0 +1,87 @@
+/*
+ *  ====================================================================
+ *    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 junit.framework.TestCase;
+
+import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
+
+public class TestXWPFTableCell extends TestCase {
+
+	@Override
+	protected void setUp() throws Exception {
+		super.setUp();
+	}
+
+	public void testSetGetVertAlignment() throws Exception {
+    	// instantiate the following classes so they'll get picked up by
+    	// the XmlBean process and added to the jar file. they are required
+    	// for the following XWPFTableCell methods.
+		CTShd ctShd = CTShd.Factory.newInstance();
+		assertNotNull(ctShd);
+		CTVerticalJc ctVjc = CTVerticalJc.Factory.newInstance();
+		assertNotNull(ctVjc);
+		STShd stShd = STShd.Factory.newInstance();
+		assertNotNull(stShd);
+		STVerticalJc stVjc = STVerticalJc.Factory.newInstance();
+		assertNotNull(stVjc);
+
+    	// create a table
+        XWPFDocument doc = new XWPFDocument();
+    	CTTbl ctTable = CTTbl.Factory.newInstance();
+        XWPFTable table = new XWPFTable(ctTable, doc);
+        // table has a single row by default; grab it
+        XWPFTableRow tr = table.getRow(0);
+        assertNotNull(tr);
+        // row has a single cell by default; grab it
+        XWPFTableCell cell = tr.getCell(0);
+
+        cell.setVerticalAlignment(XWPFVertAlign.BOTH);
+        XWPFVertAlign al = cell.getVerticalAlignment();
+        assertEquals(XWPFVertAlign.BOTH, al);
+	}
+
+	public void testSetGetColor() throws Exception {
+    	// create a table
+        XWPFDocument doc = new XWPFDocument();
+    	CTTbl ctTable = CTTbl.Factory.newInstance();
+        XWPFTable table = new XWPFTable(ctTable, doc);
+        // table has a single row by default; grab it
+        XWPFTableRow tr = table.getRow(0);
+        assertNotNull(tr);
+        // row has a single cell by default; grab it
+        XWPFTableCell cell = tr.getCell(0);
+
+        cell.setColor("F0000F");
+        String clr = cell.getColor();
+        assertEquals("F0000F", clr);
+	}
+
+	@Override
+	protected void tearDown() throws Exception {
+		super.tearDown();
+	}
+
+}



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