]> source.dussan.org Git - poi.git/commitdiff
try to make CommentsTable more extensible
authorPJ Fanning <fanningpj@apache.org>
Thu, 18 Nov 2021 11:51:24 +0000 (11:51 +0000)
committerPJ Fanning <fanningpj@apache.org>
Thu, 18 Nov 2021 11:51:24 +0000 (11:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895148 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/model/Comments.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java

index d40be0f5db013724d2bf5ae685a8aaa8f2b165cf..ff8fad93cecfc36d6e09868949bbcf1b5248b092 100644 (file)
@@ -46,6 +46,16 @@ public interface Comments {
      */
     XSSFComment findCellComment(CellAddress cellAddress);
 
+    /**
+     * Finds the cell comment at cellAddress, if one exists
+     *
+     * @param sheet the sheet that has the comment
+     * @param cellAddress the address of the cell to find a comment
+     * @return cell comment if one exists, otherwise returns null
+     * @since POI 5.2.0
+     */
+    public XSSFComment findCellComment(XSSFSheet sheet, CellAddress cellAddress);
+
     /**
      * Remove the comment at cellRef location, if one exists
      *
index a47557013a65cf42e738592770f772277860687c..f4a5493436a272b41104131f6372ca17edf4ae65 100644 (file)
@@ -197,6 +197,7 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
      * @return cell comment if one exists, otherwise returns null
      * @since POI 5.2.0
      */
+    @Override
     public XSSFComment findCellComment(XSSFSheet sheet, CellAddress cellAddress) {
         CTComment ctComment = getCTComment(cellAddress);
         if(ctComment == null) {
index 99a0bed281fd6291ef158c8cd64a371eda8151fa..ce57c967401fa453d20edaa5decfc052afb203c6 100644 (file)
@@ -45,7 +45,7 @@ import org.apache.poi.ss.usermodel.Drawing;
 import org.apache.poi.ss.util.ImageUtils;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.Units;
-import org.apache.poi.xssf.model.CommentsTable;
+import org.apache.poi.xssf.model.Comments;
 import org.apache.xmlbeans.XmlCursor;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
@@ -383,7 +383,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
         XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
         XSSFSheet sheet = getSheet();
 
-        CommentsTable comments = sheet.getCommentsTable(true);
+        Comments comments = sheet.getCommentsTable(true);
         return comments.createNewComment(getSheet(), ca);
     }
 
index 504324cdb2eb192125c2446fc26fba25d1420dc4..34fdf42c1d518e6e6e45d6461554ea9141ccf739 100644 (file)
@@ -56,6 +56,7 @@ import org.apache.poi.ss.util.SheetUtil;
 import org.apache.poi.util.Beta;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.Units;
+import org.apache.poi.xssf.model.Comments;
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.usermodel.XSSFPivotTable.PivotTableReferenceConfigurator;
 import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
@@ -95,7 +96,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
     private final SortedMap<Integer, XSSFRow> _rows = new TreeMap<>();
     private List<XSSFHyperlink> hyperlinks;
     private ColumnHelper columnHelper;
-    private CommentsTable sheetComments;
+    private Comments sheetComments;
     /**
      * cache of master shared formulas in this sheet.
      * Master shared formula is the first formula in a group of shared formulas is saved in the f element.
@@ -163,8 +164,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
         // Look for bits we're interested in
         for(RelationPart rp : getRelationParts()){
             POIXMLDocumentPart p = rp.getDocumentPart();
-            if(p instanceof CommentsTable) {
-                sheetComments = (CommentsTable)p;
+            if(p instanceof Comments) {
+                sheetComments = (Comments)p;
             }
             if(p instanceof XSSFTable) {
                 tables.put( rp.getRelationship().getId(), (XSSFTable)p );
@@ -3506,18 +3507,18 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
      *
      * @param create create a new comments table if it does not exist
      */
-    protected CommentsTable getCommentsTable(boolean create) {
+    protected Comments getCommentsTable(boolean create) {
         if(sheetComments == null && create){
             // Try to create a comments table with the same number as
             //  the sheet has (i.e. sheet 1 -> comments 1)
             try {
-                sheetComments = (CommentsTable)createRelationship(
+                sheetComments = (Comments)createRelationship(
                         XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), Math.toIntExact(sheet.getSheetId()));
             } catch(PartAlreadyExistsException e) {
                 // Technically a sheet doesn't need the same number as
-                //  it's comments, and clearly someone has already pinched
+                //  its comments, and clearly someone has already pinched
                 //  our number! Go for the next available one instead
-                sheetComments = (CommentsTable)createRelationship(
+                sheetComments = (Comments)createRelationship(
                         XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), -1);
             }
         }
index caea41f0d2b02088e165cb045594084b59d4af5b..44b663ce68caa5647e70b8c6130a02325262ab3a 100644 (file)
@@ -44,6 +44,7 @@ import org.apache.poi.ss.util.CellAddress;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.apache.poi.xssf.model.Comments;
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.apache.xmlbeans.XmlObject;
@@ -210,7 +211,7 @@ public final class TestXSSFComment extends BaseTestCellComment  {
             XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
 
             // create comments and vmlDrawing parts if they don't exist
-            CommentsTable comments = wb.getXSSFWorkbook()
+            Comments comments = wb.getXSSFWorkbook()
                     .getSheetAt(0).getCommentsTable(true);
             XSSFVMLDrawing vml = wb.getXSSFWorkbook()
                     .getSheetAt(0).getVMLDrawing(true);
@@ -223,7 +224,8 @@ public final class TestXSSFComment extends BaseTestCellComment  {
             }
 
             // create the comment in two different ways and verify that there is no difference
-            XSSFComment shape1 = new XSSFComment(comments, comments.newComment(CellAddress.A1), vmlShape1);
+            CommentsTable commentsTable = (CommentsTable)comments;
+            XSSFComment shape1 = new XSSFComment(comments, commentsTable.newComment(CellAddress.A1), vmlShape1);
             shape1.setColumn(ca.getCol1());
             shape1.setRow(ca.getRow1());
 
@@ -236,7 +238,7 @@ public final class TestXSSFComment extends BaseTestCellComment  {
             }
 
             CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
-            XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2);
+            XSSFComment shape2 = new XSSFComment(comments, commentsTable.newComment(ref), vmlShape2);
 
             assertEquals(shape1.getAuthor(), shape2.getAuthor());
             assertEquals(shape1.getClientAnchor(), shape2.getClientAnchor());
index 2b341c3520748282d68812faa8ee45ef90483f50..fea707e14824339a116114e70ab0edb038f21ba1 100644 (file)
@@ -74,6 +74,7 @@ import org.apache.poi.util.LocaleUtil;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.CalculationChain;
+import org.apache.poi.xssf.model.Comments;
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
@@ -267,7 +268,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
             XSSFComment comment = dg.createCellComment(new XSSFClientAnchor());
 
             Cell cell = sheet.createRow(0).createCell(0);
-            CommentsTable comments = sheet.getCommentsTable(false);
+            CommentsTable comments = (CommentsTable) sheet.getCommentsTable(false);
             CTComments ctComments = comments.getCTComments();
 
             cell.setCellComment(comment);
@@ -917,10 +918,10 @@ public final class TestXSSFSheet extends BaseTestXSheet {
     void commentsTable() throws IOException {
         try (XSSFWorkbook wb1 = new XSSFWorkbook()) {
             XSSFSheet sheet1 = wb1.createSheet();
-            CommentsTable comment1 = sheet1.getCommentsTable(false);
+            CommentsTable comment1 = (CommentsTable) sheet1.getCommentsTable(false);
             assertNull(comment1);
 
-            comment1 = sheet1.getCommentsTable(true);
+            comment1 = (CommentsTable) sheet1.getCommentsTable(true);
             assertNotNull(comment1);
             assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
 
@@ -928,10 +929,10 @@ public final class TestXSSFSheet extends BaseTestXSheet {
 
             //second sheet
             XSSFSheet sheet2 = wb1.createSheet();
-            CommentsTable comment2 = sheet2.getCommentsTable(false);
+            CommentsTable comment2 = (CommentsTable) sheet2.getCommentsTable(false);
             assertNull(comment2);
 
-            comment2 = sheet2.getCommentsTable(true);
+            comment2 = (CommentsTable) sheet2.getCommentsTable(true);
             assertNotNull(comment2);
 
             assertSame(comment2, sheet2.getCommentsTable(true));
@@ -944,7 +945,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
         //now test against a workbook containing cell comments
         try (XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx")) {
             XSSFSheet sheet1 = wb2.getSheetAt(0);
-            CommentsTable comment1 = sheet1.getCommentsTable(true);
+            CommentsTable comment1 = (CommentsTable) sheet1.getCommentsTable(true);
             assertNotNull(comment1);
             assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
             assertSame(comment1, sheet1.getCommentsTable(true));