poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r645547 - in /poi/branches/ooxml/src/ooxml: interfaces-jdk15/org/apache/poi/ss/usermodel/ java/org/apache/poi/xssf/model/ java/org/apache/poi/xssf/usermodel/ testcases/org/apache/poi/xssf/model/ testcases/org/apache/poi/xssf/usermodel/
Date Mon, 07 Apr 2008 14:55:56 GMT
Author: nick
Date: Mon Apr  7 07:55:50 2008
New Revision: 645547

URL: http://svn.apache.org/viewvc?rev=645547&view=rev
Log:
Fix the xssfcomments support, so we don't double-add the underlying ctcomment objects

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

Modified: poi/branches/ooxml/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java?rev=645547&r1=645546&r2=645547&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java
(original)
+++ poi/branches/ooxml/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java
Mon Apr  7 07:55:50 2008
@@ -32,9 +32,5 @@
 	
 	public Comment findCellComment(String cellRef);
 	
-	public void setCellComment (int row, int column, Comment comment);
-	
-	public void setCellComment (String cellRef, Comment comment);
-
 	public Comment addComment();
 }

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java?rev=645547&r1=645546&r2=645547&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java Mon Apr
 7 07:55:50 2008
@@ -20,7 +20,6 @@
 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 @@
 		}
 		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());
 	}

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java?rev=645547&r1=645546&r2=645547&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java Mon Apr
 7 07:55:50 2008
@@ -29,15 +29,17 @@
 	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 void setVisible(boolean visible) {
 		// TODO Auto-generated method stub
-
 	}
 }

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=645547&r1=645546&r2=645547&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Mon Apr
 7 07:55:50 2008
@@ -905,7 +905,10 @@
     }
     
     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) {
@@ -927,6 +930,10 @@
 	public boolean hasComments() {
 		if(sheetComments == null) { return false; }
 		return (sheetComments.getNumberOfComments() > 0);
+	}
+	protected int getNumberOfComments() {
+		if(sheetComments == null) { return 0; }
+		return sheetComments.getNumberOfComments();
 	}
 
 	private CTSelection getSheetTypeSelection() {

Modified: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java?rev=645547&r1=645546&r2=645547&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java
(original)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java
Mon Apr  7 07:55:50 2008
@@ -84,15 +84,17 @@
 		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());

Modified: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java?rev=645547&r1=645546&r2=645547&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java
(original)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFComment.java
Mon Apr  7 07:55:50 2008
@@ -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 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 @@
 		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());
+	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message