From 1f50363e472136faffb4fa813ec593278ed740bf Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 21 Nov 2021 13:46:42 +0000 Subject: [PATCH] set sheet on comments table git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895230 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/xssf/model/Comments.java | 25 +++++++--------- .../apache/poi/xssf/model/CommentsTable.java | 30 +++++++------------ .../poi/xssf/usermodel/XSSFDrawing.java | 3 +- .../apache/poi/xssf/usermodel/XSSFSheet.java | 10 ++++--- .../poi/xssf/usermodel/TestXSSFComment.java | 6 ++-- 5 files changed, 32 insertions(+), 42 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/Comments.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/Comments.java index 6cbf6f6ccc..c927af27e8 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/Comments.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/Comments.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.model; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellAddress; +import org.apache.poi.util.Internal; import org.apache.poi.xssf.usermodel.XSSFComment; import java.util.Iterator; @@ -30,6 +31,15 @@ import java.util.Iterator; */ public interface Comments { + /** + * This method is for internal POI use only. POI uses it to link the sheet and comments table. + * This method will not move comments from one sheet to another (if a user tries to use this method for that purpose). + * @param sheet the sheet that this comments table is associated with + * @since POI 5.2.0 + */ + @Internal + void setSheet(Sheet sheet); + int getNumberOfComments(); int getNumberOfAuthors(); @@ -47,18 +57,6 @@ public interface Comments { */ XSSFComment findCellComment(CellAddress cellAddress); - /** - * Finds the cell comment at cellAddress, if one exists - * - * @param sheet the sheet to check for comments (used to find drawing/shape data for comments) - set to null - * if you don't need the drawing/shape data - * @param cellAddress the address of the cell to find a comment - * @return cell comment if one exists, otherwise returns null - * @see #findCellComment(CellAddress) - * @since POI 5.2.0 - */ - public XSSFComment findCellComment(Sheet sheet, CellAddress cellAddress); - /** * Remove the comment at cellRef location, if one exists * @@ -76,12 +74,11 @@ public interface Comments { /** * 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 */ - XSSFComment createNewComment(Sheet sheet, ClientAnchor clientAnchor); + XSSFComment createNewComment(ClientAnchor clientAnchor); /** * Called after the reference is updated, so that diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java index 3cf33f22cf..dfd3320795 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java @@ -51,6 +51,8 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { public static final String DEFAULT_AUTHOR = ""; public static final int DEFAULT_AUTHOR_ID = 0; + private Sheet sheet; + /** * Underlying XML Beans CTComment list. */ @@ -92,6 +94,12 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { doc.save(out, DEFAULT_XML_OPTIONS); } + @Override + @Internal + public void setSheet(Sheet sheet) { + this.sheet = sheet; + } + @Override protected void commit() throws IOException { PackagePart part = getPackagePart(); @@ -175,26 +183,9 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { * * @param cellAddress the address of the cell to find a comment * @return cell comment if one exists, otherwise returns null - * @see #findCellComment(Sheet, CellAddress) */ @Override public XSSFComment findCellComment(CellAddress cellAddress) { - CTComment ct = getCTComment(cellAddress); - return ct == null ? null : new XSSFComment(this, ct, null); - } - - /** - * Finds the cell comment at cellAddress, if one exists - * - * @param sheet the sheet to check for comments (used to find drawing/shape data for comments) - set to null - * if you don't need the drawing/shape data - * @param cellAddress the address of the cell to find a comment - * @return cell comment if one exists, otherwise returns null - * @see #findCellComment(CellAddress) - * @since POI 5.2.0 - */ - @Override - public XSSFComment findCellComment(Sheet sheet, CellAddress cellAddress) { CTComment ctComment = getCTComment(cellAddress); if(ctComment == null) { return null; @@ -204,7 +195,7 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { return new XSSFComment(this, ctComment, vml == null ? null : vml.findCommentShape(cellAddress.getRow(), cellAddress.getColumn())); } - + /** * Get the underlying CTComment xmlbean for a comment located at cellRef, if it exists * @@ -233,13 +224,12 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { /** * 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 */ @Override - public XSSFComment createNewComment(Sheet sheet, ClientAnchor clientAnchor) { + public XSSFComment createNewComment(ClientAnchor clientAnchor) { XSSFVMLDrawing vml = getVMLDrawing(sheet, true); CTShape vmlShape = vml == null ? null : vml.newCommentShape(); if (vmlShape != null && clientAnchor instanceof XSSFClientAnchor && ((XSSFClientAnchor)clientAnchor).isSet()) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java index eed308d422..46fef0784b 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java @@ -380,11 +380,10 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing