From 579c07e3bfa4e8ca7b37fae1831c21ab96a6f4f8 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 31 Jan 2007 16:14:23 +0000 Subject: [PATCH] changing attributes of existing cell comments is reflected after save git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@501875 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFCell.java | 2 +- .../poi/hssf/usermodel/HSSFComment.java | 27 ++++++++++-- .../poi/hssf/usermodel/TestHSSFComment.java | 41 +++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index ec81c0d614..7d41f6d135 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -986,7 +986,7 @@ public class HSSFCell NoteRecord note = (NoteRecord)rec; if (note.getRow() == record.getRow() && note.getColumn() == record.getColumn()){ TextObjectRecord txo = (TextObjectRecord)txshapes.get(new Integer(note.getShapeId())); - comment = new HSSFComment(null, null); + comment = new HSSFComment(note, txo); comment.setRow(note.getRow()); comment.setColumn(note.getColumn()); comment.setAuthor(note.getAuthor()); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java index d56db733a3..258f26e228 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java @@ -28,7 +28,7 @@ import java.util.Iterator; /** * Represents a cell comment - a sticky note associated with a cell. * - * @author Yegor Kolzlov + * @author Yegor Kozlov */ public class HSSFComment extends HSSFTextbox { @@ -36,6 +36,9 @@ public class HSSFComment extends HSSFTextbox { private short col, row; private String author; + private NoteRecord note = null; + private TextObjectRecord txo = null; + /** * Construct a new comment with the given parent and anchor. * @@ -56,6 +59,12 @@ public class HSSFComment extends HSSFTextbox { author = ""; } + protected HSSFComment( NoteRecord note, TextObjectRecord txo ) + { + this( (HSSFShape)null, (HSSFAnchor)null ); + this.txo = txo; + this.note = note; + } /** * Returns whether this comment is visible. @@ -63,6 +72,7 @@ public class HSSFComment extends HSSFTextbox { * @param visible true if the comment is visible, false otherwise */ public void setVisible(boolean visible){ + if(note != null) note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN); this.visible = visible; } @@ -90,6 +100,7 @@ public class HSSFComment extends HSSFTextbox { * @param row the 0-based row of the cell that contains the comment */ public void setRow(int row){ + if(note != null) note.setRow((short)row); this.row = (short)row; } @@ -108,6 +119,7 @@ public class HSSFComment extends HSSFTextbox { * @param col the 0-based column of the cell that contains the comment */ public void setColumn(short col){ + if(note != null) note.setColumn(col); this.col = col; } @@ -126,6 +138,7 @@ public class HSSFComment extends HSSFTextbox { * @param author the name of the original author of the comment */ public void setAuthor(String author){ + if(note != null) note.setAuthor(author); this.author = author; } @@ -134,10 +147,16 @@ public class HSSFComment extends HSSFTextbox { * * @param string Sets the rich text string used by this object. */ - public void setString( HSSFRichTextString string ) - { - //if font is not set we must set the default one implicitly + public void setString( HSSFRichTextString string ) { + //if font is not set we must set the default one if (string.numFormattingRuns() == 0) string.applyFont((short)0); + + if (txo != null) { + int frLength = ( string.numFormattingRuns() + 1 ) * 8; + txo.setFormattingRunLength( (short) frLength ); + txo.setTextLength( (short) string.length() ); + txo.setStr( string ); + } super.setString(string); } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java index 3f38747b02..918d1f8130 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java @@ -117,4 +117,45 @@ public class TestHSSFComment extends TestCase { } } + /** + * test that we can modify existing cell comments + */ + public static void testModifyComments() throws Exception { + + String dir = System.getProperty("HSSF.testdata.path"); + FileInputStream is = new FileInputStream(new File(dir, "SimpleWithComments.xls")); + HSSFWorkbook wb = new HSSFWorkbook(is); + is.close(); + + HSSFSheet sheet = wb.getSheetAt(0); + + HSSFCell cell; + HSSFRow row; + HSSFComment comment; + + for (int rownum = 0; rownum < 3; rownum++) { + row = sheet.getRow(rownum); + cell = row.getCell((short)1); + comment = cell.getCellComment(); + comment.setAuthor("Mofified["+rownum+"] by Yegor"); + comment.setString(new HSSFRichTextString("Modified comment at row " + rownum)); + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + wb.write(out); + out.close(); + + wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray())); + sheet = wb.getSheetAt(0); + + for (int rownum = 0; rownum < 3; rownum++) { + row = sheet.getRow(rownum); + cell = row.getCell((short)1); + comment = cell.getCellComment(); + + assertEquals("Mofified["+rownum+"] by Yegor", comment.getAuthor()); + assertEquals("Modified comment at row " + rownum, comment.getString().getString()); + } + + } } -- 2.39.5