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
This commit is contained in:
Nick Burch 2008-04-07 14:55:50 +00:00
parent ec795c8b4a
commit 5da68800de
6 changed files with 89 additions and 31 deletions

View File

@ -32,9 +32,5 @@ public interface CommentsSource {
public Comment findCellComment(String cellRef); public Comment findCellComment(String cellRef);
public void setCellComment (int row, int column, Comment comment);
public void setCellComment (String cellRef, Comment comment);
public Comment addComment(); public Comment addComment();
} }

View File

@ -20,7 +20,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CommentsSource; import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFComment; import org.apache.poi.xssf.usermodel.XSSFComment;
@ -103,21 +102,10 @@ public class CommentsTable implements CommentsSource, XSSFModel {
return null; return null;
} }
public void setCellComment (int row, int column, Comment comment) { /**
XSSFComment current = findCellComment(row, column); * Generates a new XSSFComment, associated with the
if (current == null) { * current comments list.
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);
}
public XSSFComment addComment() { public XSSFComment addComment() {
return new XSSFComment(this, getCommentsList().addNewComment()); return new XSSFComment(this, getCommentsList().addNewComment());
} }

View File

@ -29,15 +29,17 @@ public class XSSFComment implements Comment {
private CTComment comment; private CTComment comment;
private CommentsSource comments; 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) { public XSSFComment(CommentsSource comments, CTComment comment) {
this.comment = comment; this.comment = comment;
this.comments = comments; this.comments = comments;
} }
public XSSFComment(CommentsSource sheetComments) {
this(sheetComments, CTComment.Factory.newInstance());
}
public String getAuthor() { public String getAuthor() {
return comments.getAuthor(comment.getAuthorId()); return comments.getAuthor(comment.getAuthorId());
} }
@ -97,6 +99,5 @@ public class XSSFComment implements Comment {
public void setVisible(boolean visible) { public void setVisible(boolean visible) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
} }

View File

@ -905,7 +905,10 @@ public class XSSFSheet implements Sheet {
} }
public void setCellComment(String cellRef, XSSFComment comment) { 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) { public void setCellHyperlink(XSSFHyperlink hyperlink) {
@ -928,6 +931,10 @@ public class XSSFSheet implements Sheet {
if(sheetComments == null) { return false; } if(sheetComments == null) { return false; }
return (sheetComments.getNumberOfComments() > 0); return (sheetComments.getNumberOfComments() > 0);
} }
protected int getNumberOfComments() {
if(sheetComments == null) { return 0; }
return sheetComments.getNumberOfComments();
}
private CTSelection getSheetTypeSelection() { private CTSelection getSheetTypeSelection() {
if (getSheetTypeSheetView().sizeOfSelectionArray() == 0) { if (getSheetTypeSheetView().sizeOfSelectionArray() == 0) {

View File

@ -84,15 +84,17 @@ public class TestCommentsTable extends TestCase {
assertNull(sheetComments.findCellComment(2, 0)); assertNull(sheetComments.findCellComment(2, 0));
} }
public void testSetCellComment() { public void testAddCellComment() {
CTComments comments = CTComments.Factory.newInstance(); CTComments comments = CTComments.Factory.newInstance();
CommentsTable sheetComments = new CommentsTable(comments); CommentsTable sheetComments = new CommentsTable(comments);
CTCommentList commentList = comments.addNewCommentList(); CTCommentList commentList = comments.addNewCommentList();
assertEquals(0, commentList.sizeOfCommentArray()); assertEquals(0, commentList.sizeOfCommentArray());
XSSFComment comment = new XSSFComment(sheetComments);
comment.setAuthor("test A1 author");
sheetComments.setCellComment("A1", comment); XSSFComment comment = sheetComments.addComment();
comment.setAuthor("test A1 author");
comment.setRow(0);
comment.setColumn((short)0);
assertEquals(1, commentList.sizeOfCommentArray()); assertEquals(1, commentList.sizeOfCommentArray());
assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId())); assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId()));
assertEquals("test A1 author", comment.getAuthor()); assertEquals("test A1 author", comment.getAuthor());

View File

@ -17,10 +17,17 @@
package org.apache.poi.xssf.usermodel; 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.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.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.model.CommentsTable; 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.CTAuthors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
@ -35,7 +42,7 @@ public class TestXSSFComment extends TestCase {
public void testConstructors() { public void testConstructors() {
CommentsTable sheetComments = new CommentsTable(); CommentsTable sheetComments = new CommentsTable();
XSSFComment comment = new XSSFComment(sheetComments); XSSFComment comment = sheetComments.addComment();
assertNotNull(comment); assertNotNull(comment);
CTComment ctComment = CTComment.Factory.newInstance(); CTComment ctComment = CTComment.Factory.newInstance();
@ -121,4 +128,61 @@ public class TestXSSFComment extends TestCase {
assertEquals(TEST_RICHTEXTSTRING, ctComment.getText().getT()); 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());
}
} }