]> source.dussan.org Git - poi.git/commitdiff
Fix the xssfcomments support, so we don't double-add the underlying ctcomment objects
authorNick Burch <nick@apache.org>
Mon, 7 Apr 2008 14:55:50 +0000 (14:55 +0000)
committerNick Burch <nick@apache.org>
Mon, 7 Apr 2008 14:55:50 +0000 (14:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@645547 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java
src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java

index 88e0971e76b6cd03317a90ccba5bb3dbc4458512..34cb1bdf96d132fb58fbec093cdfa2cb01890d23 100644 (file)
@@ -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();
 }
index b9c919e0a99402822e079f38fd730ad293b9307d..b4705d207c5da91a5d14c649e08040c0994fcd05 100644 (file)
@@ -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());
        }
index 0e77f41efa37ddb39699ee5f3b4ed23450cedb37..1a3d391d07601e1b664781b1cec61e270217ed31 100644 (file)
@@ -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
-
        }
 }
index 1976e79b8745def983e3968f9810c39e5d33f5e6..9ffa8947bad24a6a9b760e912ef3337c72873031 100644 (file)
@@ -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) {
index 10cbd91ca29f0b22216a1d144f0139eabbfe9b3f..a30dbdba6664d0771f788b640898d1da2c28bc92 100644 (file)
@@ -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());
index 2ac7409fc240ea58c6f947a2163efe737014938b..4a943597e6a70f1a300b3e003740f139ac179ee6 100644 (file)
 
 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());
+       }
 }