From 1d9948f0906136d8d541defba4fe091f6341330e Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 17 Nov 2021 18:41:16 +0000 Subject: [PATCH] try to make comments table more extensible git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895113 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/model/CommentsTable.java | 49 +++++++++++++------ .../apache/poi/xssf/usermodel/XSSFSheet.java | 9 +--- 2 files changed, 35 insertions(+), 23 deletions(-) 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 c3b122ad9a..9d93633b7c 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 @@ -164,6 +164,25 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { 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 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) { + CTComment ctComment = getCTComment(cellAddress); + if(ctComment == null) { + return null; + } + + XSSFVMLDrawing vml = sheet.getVMLDrawing(false); + 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 @@ -221,21 +240,6 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { return new XSSFComment(this, newComment(ref), vmlShape); } - - /** - * Refresh Map commentRefs cache, - * Calls that use the commentRefs cache will perform in O(1) - * time rather than O(n) lookup time for List comments. - */ - private void prepareCTCommentCache() { - // Create the cache if needed - if(commentRefs == null) { - commentRefs = new HashMap<>(); - for (CTComment comment : comments.getCommentList().getCommentArray()) { - commentRefs.put(new CellAddress(comment.getRef()), comment); - } - } - } /** * Create a new comment located at cell address @@ -304,4 +308,19 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { public CTComments getCTComments(){ return comments; } + + /** + * Refresh Map commentRefs cache, + * Calls that use the commentRefs cache will perform in O(1) + * time rather than O(n) lookup time for List comments. + */ + private void prepareCTCommentCache() { + // Create the cache if needed + if(commentRefs == null) { + commentRefs = new HashMap<>(); + for (CTComment comment : comments.getCommentList().getCommentArray()) { + commentRefs.put(new CellAddress(comment.getRef()), comment); + } + } + } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index e716283ef1..f876ef9dfb 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -774,14 +774,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { return null; } - CTComment ctComment = sheetComments.getCTComment(address); - if(ctComment == null) { - return null; - } - - XSSFVMLDrawing vml = getVMLDrawing(false); - return new XSSFComment(sheetComments, ctComment, - vml == null ? null : vml.findCommentShape(address.getRow(), address.getColumn())); + return sheetComments.findCellComment(this, address); } /** -- 2.39.5