]> source.dussan.org Git - poi.git/commitdiff
try to make comments table more extensible
authorPJ Fanning <fanningpj@apache.org>
Wed, 17 Nov 2021 13:00:09 +0000 (13:00 +0000)
committerPJ Fanning <fanningpj@apache.org>
Wed, 17 Nov 2021 13:00:09 +0000 (13:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895104 13f79535-47bb-0310-9956-ffa450edef68

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/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java
poi/src/main/java9/module-info.class

index 016eb2c3b86ac0bdd20f07bbccac84b8150f48ff..876339f2fa347e7444ff8559b84d922d71a9fbdf 100644 (file)
@@ -25,11 +25,16 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
+import com.microsoft.schemas.vml.CTShape;
 import org.apache.poi.ooxml.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.ss.util.CellAddress;
 import org.apache.poi.util.Internal;
+import org.apache.poi.util.Units;
+import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
 import org.apache.poi.xssf.usermodel.XSSFComment;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFVMLDrawing;
 import org.apache.xmlbeans.XmlException;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
@@ -168,6 +173,37 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
         return commentRefs.keySet().iterator();
     }
 
+    /**
+     * Create a new comment and add to the CommentTable.
+     * @param sheet sheet to add comment to
+     * @param clientAnchor the anchor for this comment
+     * @return new XSSFComment
+     * @since POI 5.2.0
+     */
+    public XSSFComment createNewComment(XSSFSheet sheet, XSSFClientAnchor clientAnchor) {
+        XSSFVMLDrawing vml = sheet.getVMLDrawing(true);
+        com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape();
+        if (clientAnchor.isSet()) {
+            // convert offsets from emus to pixels since we get a
+            // DrawingML-anchor
+            // but create a VML Drawing
+            int dx1Pixels = clientAnchor.getDx1() / Units.EMU_PER_PIXEL;
+            int dy1Pixels = clientAnchor.getDy1() / Units.EMU_PER_PIXEL;
+            int dx2Pixels = clientAnchor.getDx2() / Units.EMU_PER_PIXEL;
+            int dy2Pixels = clientAnchor.getDy2() / Units.EMU_PER_PIXEL;
+            String position = clientAnchor.getCol1() + ", " + dx1Pixels + ", " + clientAnchor.getRow1() + ", " + dy1Pixels + ", " +
+                    clientAnchor.getCol2() + ", " + dx2Pixels + ", " + clientAnchor.getRow2() + ", " + dy2Pixels;
+            vmlShape.getClientDataArray(0).setAnchorArray(0, position);
+        }
+        CellAddress ref = new CellAddress(clientAnchor.getRow1(), clientAnchor.getCol1());
+
+        if (findCellComment(ref) != null) {
+            throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
+        }
+
+        return new XSSFComment(this, newComment(ref), vmlShape);
+    }
+
     /**
      * Refresh Map<CellAddress, CTComment> commentRefs cache,
      * Calls that use the commentRefs cache will perform in O(1)
index ee953ba50c21918feb19ea5bc85e71bd6ebbc76f..99a0bed281fd6291ef158c8cd64a371eda8151fa 100644 (file)
@@ -42,7 +42,6 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper;
 import org.apache.poi.openxml4j.opc.TargetMode;
 import org.apache.poi.ss.usermodel.ClientAnchor;
 import org.apache.poi.ss.usermodel.Drawing;
-import org.apache.poi.ss.util.CellAddress;
 import org.apache.poi.ss.util.ImageUtils;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.Units;
@@ -384,29 +383,8 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
         XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
         XSSFSheet sheet = getSheet();
 
-        // create comments and vmlDrawing parts if they don't exist
         CommentsTable comments = sheet.getCommentsTable(true);
-        XSSFVMLDrawing vml = sheet.getVMLDrawing(true);
-        com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape();
-        if (ca.isSet()) {
-            // convert offsets from emus to pixels since we get a
-            // DrawingML-anchor
-            // but create a VML Drawing
-            int dx1Pixels = ca.getDx1() / Units.EMU_PER_PIXEL;
-            int dy1Pixels = ca.getDy1() / Units.EMU_PER_PIXEL;
-            int dx2Pixels = ca.getDx2() / Units.EMU_PER_PIXEL;
-            int dy2Pixels = ca.getDy2() / Units.EMU_PER_PIXEL;
-            String position = ca.getCol1() + ", " + dx1Pixels + ", " + ca.getRow1() + ", " + dy1Pixels + ", " + ca
-                .getCol2() + ", " + dx2Pixels + ", " + ca.getRow2() + ", " + dy2Pixels;
-            vmlShape.getClientDataArray(0).setAnchorArray(0, position);
-        }
-        CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
-
-        if (comments.findCellComment(ref) != null) {
-            throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
-        }
-
-        return new XSSFComment(comments, comments.newComment(ref), vmlShape);
+        return comments.createNewComment(getSheet(), ca);
     }
 
     /**
index 67f86991fa237ec1286ff46de9ffd0ce2ccd1db5..e716283ef1077d3e6a7b18f55237d27c0fb7c7b3 100644 (file)
@@ -570,13 +570,14 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
     }
 
     /**
-     * Get VML drawing for this sheet (aka 'legacy' drawing)
+     * Get VML drawing for this sheet (aka 'legacy' drawing). This method is for internal POI use only.
      *
      * @param autoCreate if true, then a new VML drawing part is created
      *
      * @return the VML drawing of {@code null} if the drawing was not found and autoCreate=false
      */
-    protected XSSFVMLDrawing getVMLDrawing(boolean autoCreate) {
+    @Internal
+    public XSSFVMLDrawing getVMLDrawing(boolean autoCreate) {
         XSSFVMLDrawing drawing = null;
         CTLegacyDrawing ctDrawing = getCTLegacyDrawing();
         if(ctDrawing == null) {
index 59aa22985320bb58bf04c86a8a92a72d9ba51081..2adda367680120d777eb14a18b6e713c4a5dd9d4 100644 (file)
@@ -49,6 +49,7 @@ import com.microsoft.schemas.vml.STStrokeJoinStyle;
 import org.apache.poi.ooxml.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.schemas.vmldrawing.XmlDocument;
+import org.apache.poi.util.Internal;
 import org.apache.poi.util.ReplacingInputStream;
 import org.apache.xmlbeans.XmlCursor;
 import org.apache.xmlbeans.XmlException;
@@ -236,7 +237,11 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart {
         grpCur.dispose();
     }
 
-    protected CTShape newCommentShape(){
+    /**
+     * This method is for internal POI use only.
+     */
+    @Internal
+    public CTShape newCommentShape() {
         CTGroup grp = CTGroup.Factory.newInstance();
 
         CTShape shape = grp.addNewShape();
index a27868cdf4ab031ce77f7eff01d32dadd450c52a..7ca4ecd882a04d09234b79b8f0809aa79580adb5 100644 (file)
Binary files a/poi/src/main/java9/module-info.class and b/poi/src/main/java9/module-info.class differ