From 86fce0ebe8e2fc428ca92651fe01d29435e34ced Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 7 Apr 2008 14:55:50 +0000 Subject: [PATCH] Fix the xssfcomments support, so we don't double-add the underlying ctcomment objects git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@645547 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/usermodel/CommentsSource.java | 4 -- .../apache/poi/xssf/model/CommentsTable.java | 20 ++---- .../poi/xssf/usermodel/XSSFComment.java | 11 ++-- .../apache/poi/xssf/usermodel/XSSFSheet.java | 9 ++- .../poi/xssf/model/TestCommentsTable.java | 8 ++- .../poi/xssf/usermodel/TestXSSFComment.java | 66 ++++++++++++++++++- 6 files changed, 88 insertions(+), 30 deletions(-) diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java index 88e0971e76..34cb1bdf96 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java @@ -32,9 +32,5 @@ public interface CommentsSource { public Comment findCellComment(String cellRef); - public void setCellComment (int row, int column, Comment comment); - - public void setCellComment (String cellRef, Comment comment); - public Comment addComment(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java index b9c919e0a9..b4705d207c 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.CommentsSource; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFComment; @@ -102,22 +101,11 @@ public class CommentsTable implements CommentsSource, XSSFModel { } return null; } - - public void setCellComment (int row, int column, Comment comment) { - XSSFComment current = findCellComment(row, column); - if (current == null) { - current = addComment(); - } - current = (XSSFComment)comment; - current.setRow(row); - current.setColumn((short) column); - } - - public void setCellComment (String cellRef, Comment comment) { - CellReference cellReference = new CellReference(cellRef); - setCellComment(cellReference.getRow(), cellReference.getCol(), comment); - } + /** + * Generates a new XSSFComment, associated with the + * current comments list. + */ public XSSFComment addComment() { return new XSSFComment(this, getCommentsList().addNewComment()); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java index 0e77f41efa..1a3d391d07 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java @@ -29,15 +29,17 @@ public class XSSFComment implements Comment { private CTComment comment; private CommentsSource comments; + /** + * Creates a new XSSFComment, associated with a given + * low level comment object. + * If, as an end user, you want a new XSSFComment + * object, the please ask your sheet for one. + */ public XSSFComment(CommentsSource comments, CTComment comment) { this.comment = comment; this.comments = comments; } - public XSSFComment(CommentsSource sheetComments) { - this(sheetComments, CTComment.Factory.newInstance()); - } - public String getAuthor() { return comments.getAuthor(comment.getAuthorId()); } @@ -97,6 +99,5 @@ public class XSSFComment implements Comment { public void setVisible(boolean visible) { // TODO Auto-generated method stub - } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 1976e79b87..9ffa8947ba 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -905,7 +905,10 @@ public class XSSFSheet implements Sheet { } public void setCellComment(String cellRef, XSSFComment comment) { - getComments().setCellComment(cellRef, comment); + CellReference cellReference = new CellReference(cellRef); + + comment.setRow(cellReference.getRow()); + comment.setColumn((short)cellReference.getCol()); } public void setCellHyperlink(XSSFHyperlink hyperlink) { @@ -928,6 +931,10 @@ public class XSSFSheet implements Sheet { if(sheetComments == null) { return false; } return (sheetComments.getNumberOfComments() > 0); } + protected int getNumberOfComments() { + if(sheetComments == null) { return 0; } + return sheetComments.getNumberOfComments(); + } private CTSelection getSheetTypeSelection() { if (getSheetTypeSheetView().sizeOfSelectionArray() == 0) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java index 10cbd91ca2..a30dbdba66 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java @@ -84,15 +84,17 @@ public class TestCommentsTable extends TestCase { assertNull(sheetComments.findCellComment(2, 0)); } - public void testSetCellComment() { + public void testAddCellComment() { CTComments comments = CTComments.Factory.newInstance(); CommentsTable sheetComments = new CommentsTable(comments); CTCommentList commentList = comments.addNewCommentList(); assertEquals(0, commentList.sizeOfCommentArray()); - XSSFComment comment = new XSSFComment(sheetComments); + + XSSFComment comment = sheetComments.addComment(); comment.setAuthor("test A1 author"); + comment.setRow(0); + comment.setColumn((short)0); - sheetComments.setCellComment("A1", comment); assertEquals(1, commentList.sizeOfCommentArray()); assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId())); assertEquals("test A1 author", comment.getAuthor()); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java index 2ac7409fc2..4a943597e6 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java @@ -17,10 +17,17 @@ package org.apache.poi.xssf.usermodel; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + import org.apache.poi.hssf.usermodel.HSSFRichTextString; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.model.CommentsTable; +import org.openxml4j.opc.Package; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAuthors; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; @@ -35,7 +42,7 @@ public class TestXSSFComment extends TestCase { public void testConstructors() { CommentsTable sheetComments = new CommentsTable(); - XSSFComment comment = new XSSFComment(sheetComments); + XSSFComment comment = sheetComments.addComment(); assertNotNull(comment); CTComment ctComment = CTComment.Factory.newInstance(); @@ -121,4 +128,61 @@ public class TestXSSFComment extends TestCase { assertEquals(TEST_RICHTEXTSTRING, ctComment.getText().getT()); } + /** + * Tests that we can add comments to a new + * file, save, load, and still see them + * @throws Exception + */ + public void testCreateSave() throws Exception { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet s1 = (XSSFSheet)wb.createSheet(); + Row r1 = s1.createRow(0); + Cell r1c1 = r1.createCell(0); + r1c1.setCellValue(2.2); + + assertEquals(0, s1.getNumberOfComments()); + + Comment c1 = s1.createComment(); + c1.setAuthor("Author 1"); + c1.setString(new XSSFRichTextString("Comment 1")); + r1c1.setCellComment(c1); + + assertEquals(1, s1.getNumberOfComments()); + + // Save and re-load + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + wb.write(baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + + wb = new XSSFWorkbook(Package.open(bais)); + s1 = (XSSFSheet)wb.getSheetAt(0); + + assertEquals(1, s1.getNumberOfComments()); + assertNotNull(s1.getRow(0).getCell(0).getCellComment()); + assertEquals("Author 1", s1.getRow(0).getCell(0).getCellComment().getAuthor()); + assertEquals("Comment 1", s1.getRow(0).getCell(0).getCellComment().getString().getString()); + + // Now add an orphaned one + Comment c2 = s1.createComment(); + c2.setAuthor("Author 2"); + c2.setString(new XSSFRichTextString("Second Comment")); + c2.setRow(0); + c2.setColumn((short)1); + assertEquals(2, s1.getNumberOfComments()); + + // Save and re-load + baos = new ByteArrayOutputStream(); + wb.write(baos); + bais = new ByteArrayInputStream(baos.toByteArray()); + + wb = new XSSFWorkbook(Package.open(bais)); + s1 = (XSSFSheet)wb.getSheetAt(0); + + assertEquals(2, s1.getNumberOfComments()); + assertNotNull(s1.getCellComment(0, 0)); + assertNotNull(s1.getCellComment(0, 1)); + + assertEquals("Author 1", s1.getCellComment(0, 0).getAuthor()); + assertEquals("Author 2", s1.getCellComment(0, 1).getAuthor()); + } } -- 2.39.5