Author: nick
Date: Fri Apr  8 15:07:35 2011
New Revision: 1090289

URL: http://svn.apache.org/viewvc?rev=1090289&view=rev
Log:
Add support for adding a table to a XSSFSheet

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Table.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.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=1090289&r1=1090288&r2=1090289&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Fri Apr  8 15:07:35 
2011
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta3" date="2011-??-??">
+           <action dev="poi-developers" type="add">Support for adding a table 
to a XSSFSheet</action>
            <action dev="poi-developers" type="add">Improve HSMF MAPIMessage 
access to the HTML and RTF versions of the message body (where 
available)</action>
            <action dev="poi-developers" type="add">Add new method to HSMF of 
MAPIMessage.has7BitEncodingStrings() to make it easier to decide when encoding 
guessing is needed</action>
            <action dev="poi-developers" type="fix">OutlookTextExtractor now 
requests 7 bit encoding guessing</action>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Table.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Table.java?rev=1090289&r1=1090288&r2=1090289&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Table.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Table.java Fri Apr  8 
15:07:35 2011
@@ -192,6 +192,17 @@ public class Table extends POIXMLDocumen
        public String getName() {
           return ctTable.getName();
        }
+       
+       /**
+        * Changes the name of the Table
+        */
+       public void setName(String name) {
+          if(name == null) {
+             ctTable.unsetName();
+             return;
+          }
+          ctTable.setName(name);
+       }
 
    /**
     * @return the display name of the Table, if set
@@ -200,6 +211,13 @@ public class Table extends POIXMLDocumen
       return ctTable.getDisplayName();
    }
 
+   /**
+    * Changes the display name of the Table
+    */
+   public void setDisplayName(String name) {
+      ctTable.setDisplayName(name);
+   }
+
        /**
         * @return  the number of mapped table columns (see Open Office XML 
Part 4: chapter 3.5.1.4)
         */

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1090289&r1=1090288&r2=1090289&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Fri 
Apr  8 15:07:35 2011
@@ -88,6 +88,7 @@ public class XSSFSheet extends POIXMLDoc
      * Master shared formula is the first formula in a group of shared 
formulas is saved in the f element.
      */
     private Map<Integer, CTCellFormula> sharedFormulas;
+    private TreeMap<String,Table> tables;
     private List<CellRangeAddress> arrayFormulas;
     private XSSFDataValidationHelper dataValidationHelper;    
 
@@ -151,6 +152,9 @@ public class XSSFSheet extends POIXMLDoc
                sheetComments = (CommentsTable)p;
                break;
             }
+            if(p instanceof Table) {
+               tables.put( p.getPackageRelationship().getId(), (Table)p );
+            }
         }
         
         // Process external hyperlinks for the sheet, if there are any
@@ -171,6 +175,7 @@ public class XSSFSheet extends POIXMLDoc
     @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are 
deprecated in xmlbeans with JDK 1.5 support
     private void initRows(CTWorksheet worksheet) {
         _rows = new TreeMap<Integer, XSSFRow>();
+        tables = new TreeMap<String, Table>();
         sharedFormulas = new HashMap<Integer, CTCellFormula>();
         arrayFormulas = new ArrayList<CellRangeAddress>();
         for (CTRow row : worksheet.getSheetData().getRowArray()) {
@@ -2957,16 +2962,31 @@ public class XSSFSheet extends POIXMLDoc
     }
     
     /**
+     * Creates a new Table, and associates it with this Sheet
+     */
+    public Table createTable() {
+       if(! worksheet.isSetTableParts()) {
+          worksheet.addNewTableParts();
+       }
+       
+       CTTableParts tblParts = worksheet.getTableParts();
+       CTTablePart tbl = tblParts.addNewTablePart();
+       
+       Table table = (Table)createRelationship(XSSFRelation.TABLE, 
XSSFFactory.getInstance(), tblParts.sizeOfTablePartArray());
+       tbl.setId(table.getPackageRelationship().getId());
+       
+       tables.put(tbl.getId(), table);
+       
+       return table;
+    }
+    
+    /**
      * Returns any tables associated with this Sheet
      */
     public List<Table> getTables() {
-       List<Table> tables = new ArrayList<Table>();
-       for(POIXMLDocumentPart p : getRelations()) {
-          if 
(p.getPackageRelationship().getRelationshipType().equals(XSSFRelation.TABLE.getRelation()))
 {
-             Table table = (Table) p;
-             tables.add(table);
-          }
-       }
-       return tables;
+       List<Table> tableList = new ArrayList<Table>(
+             tables.values()
+       );
+       return tableList;
     }
 }

Modified: 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1090289&r1=1090288&r2=1090289&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java 
(original)
+++ 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java 
Fri Apr  8 15:07:35 2011
@@ -892,6 +892,38 @@ public final class TestXSSFBugs extends 
        assertEquals("Tabella1", t.getName());
        assertEquals("Tabella1", t.getDisplayName());
        assertEquals("A1:C3", t.getCTTable().getRef());
+
+       
+       // Add some more tables, and check
+       t = s2.createTable();
+       t.setName("New 2");
+       t.setDisplayName("New 2");
+       t = s3.createTable();
+       t.setName("New 3");
+       t.setDisplayName("New 3");
+       
+       wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
+       s1 = wb.getSheetAt(0);
+       s2 = wb.getSheetAt(1);
+       s3 = wb.getSheetAt(2);
+       s4 = wb.getSheetAt(3);
+       assertEquals(0, s1.getTables().size());
+       assertEquals(2, s2.getTables().size());
+       assertEquals(1, s3.getTables().size());
+       assertEquals(0, s4.getTables().size());
+       
+       t = s2.getTables().get(0);
+       assertEquals("Tabella1", t.getName());
+       assertEquals("Tabella1", t.getDisplayName());
+       assertEquals("A1:C3", t.getCTTable().getRef());
+       
+       t = s2.getTables().get(1);
+       assertEquals("New 2", t.getName());
+       assertEquals("New 2", t.getDisplayName());
+       
+       t = s3.getTables().get(0);
+       assertEquals("New 3", t.getName());
+       assertEquals("New 3", t.getDisplayName());
     }
     
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to