From 38e137f9a0c33e8dd35a1d1205acfcce95fef7b4 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Mon, 9 May 2016 05:09:41 +0000 Subject: [PATCH] bug 59443: add get/setAddress methods on cell comment class git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1742879 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/usermodel/HSSFComment.java | 27 +++++++ .../org/apache/poi/ss/usermodel/Comment.java | 24 ++++++ .../poi/xssf/usermodel/XSSFComment.java | 79 +++++++++++-------- .../poi/ss/usermodel/BaseTestCellComment.java | 34 ++++++++ 4 files changed, 129 insertions(+), 35 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java index 928f1c9b20..23126d451f 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java @@ -30,6 +30,7 @@ import org.apache.poi.hssf.record.ObjRecord; import org.apache.poi.hssf.record.TextObjectRecord; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.Comment; +import org.apache.poi.ss.util.CellAddress; /** * Represents a cell comment - a sticky note associated with a cell. @@ -146,6 +147,7 @@ public class HSSFComment extends HSSFTextbox implements Comment { * * @param visible true if the comment is visible, false otherwise */ + @Override public void setVisible(boolean visible) { _note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN); setHidden(!visible); @@ -156,15 +158,34 @@ public class HSSFComment extends HSSFTextbox implements Comment { * * @return true if the comment is visible, false otherwise */ + @Override public boolean isVisible() { return _note.getFlags() == NoteRecord.NOTE_VISIBLE; } + + @Override + public CellAddress getAddress() { + return new CellAddress(getRow(), getColumn()); + } + + @Override + public void setAddress(CellAddress address) { + setRow(address.getRow()); + setColumn(address.getColumn()); + } + + @Override + public void setAddress(int row, int col) { + setRow(row); + setColumn(col); + } /** * Return the row of the cell that contains the comment * * @return the 0-based row of the cell that contains the comment */ + @Override public int getRow() { return _note.getRow(); } @@ -174,6 +195,7 @@ public class HSSFComment extends HSSFTextbox implements Comment { * * @param row the 0-based row of the cell that contains the comment */ + @Override public void setRow(int row) { _note.setRow(row); } @@ -183,6 +205,7 @@ public class HSSFComment extends HSSFTextbox implements Comment { * * @return the 0-based column of the cell that contains the comment */ + @Override public int getColumn() { return _note.getColumn(); } @@ -192,6 +215,7 @@ public class HSSFComment extends HSSFTextbox implements Comment { * * @param col the 0-based column of the cell that contains the comment */ + @Override public void setColumn(int col) { _note.setColumn(col); } @@ -201,6 +225,7 @@ public class HSSFComment extends HSSFTextbox implements Comment { * * @return the name of the original author of the comment */ + @Override public String getAuthor() { return _note.getAuthor(); } @@ -210,6 +235,7 @@ public class HSSFComment extends HSSFTextbox implements Comment { * * @param author the name of the original author of the comment */ + @Override public void setAuthor(String author) { if (_note != null) _note.setAuthor(author); } @@ -246,6 +272,7 @@ public class HSSFComment extends HSSFTextbox implements Comment { throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName()); } + @Override public void afterRemove(HSSFPatriarch patriarch){ super.afterRemove(patriarch); patriarch.getBoundAggregate().removeTailRecord(getNoteRecord()); diff --git a/src/java/org/apache/poi/ss/usermodel/Comment.java b/src/java/org/apache/poi/ss/usermodel/Comment.java index 356e55c7c9..1fbda29fac 100644 --- a/src/java/org/apache/poi/ss/usermodel/Comment.java +++ b/src/java/org/apache/poi/ss/usermodel/Comment.java @@ -17,6 +17,8 @@ package org.apache.poi.ss.usermodel; +import org.apache.poi.ss.util.CellAddress; + public interface Comment { /** @@ -32,6 +34,28 @@ public interface Comment { * @return true if the comment is visible, false otherwise */ boolean isVisible(); + + /** + * Get the address of the cell that this comment is attached to + * + * @return comment cell address + */ + CellAddress getAddress(); + + /** + * Set the address of the cell that this comment is attached to + * + * @param addr + */ + void setAddress(CellAddress addr); + + /** + * Set the address of the cell that this comment is attached to + * + * @param row + * @param col + */ + void setAddress(int row, int col); /** * Return the row of the cell that contains the comment 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 367c2f24bc..f71cbcb955 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java @@ -29,6 +29,7 @@ import org.apache.poi.xssf.model.CommentsTable; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; +import com.microsoft.schemas.office.excel.CTClientData; import com.microsoft.schemas.vml.CTShape; public class XSSFComment implements Comment { @@ -55,11 +56,9 @@ public class XSSFComment implements Comment { // the same way as we do in setRow()/setColumn() if(vmlShape != null && vmlShape.sizeOfClientDataArray() > 0) { CellReference ref = new CellReference(comment.getRef()); - vmlShape.getClientDataArray(0).setRowArray(0, - new BigInteger(String.valueOf(ref.getRow()))); - - vmlShape.getClientDataArray(0).setColumnArray(0, - new BigInteger(String.valueOf(ref.getCol()))); + CTClientData clientData = vmlShape.getClientDataArray(0); + clientData.setRowArray(0, new BigInteger(String.valueOf(ref.getRow()))); + clientData.setColumnArray(0, new BigInteger(String.valueOf(ref.getCol()))); // There is a very odd xmlbeans bug when changing the row // arrays which can lead to corrupt pointer @@ -72,6 +71,7 @@ public class XSSFComment implements Comment { * * @return Name of the original comment author. Default value is blank. */ + @Override public String getAuthor() { return _comments.getAuthor((int) _comment.getAuthorId()); } @@ -81,6 +81,7 @@ public class XSSFComment implements Comment { * * @param author the name of the original author of the comment */ + @Override public void setAuthor(String author) { _comment.setAuthorId( _comments.findAuthor(author) @@ -90,20 +91,23 @@ public class XSSFComment implements Comment { /** * @return the 0-based column of the cell that the comment is associated with. */ + @Override public int getColumn() { - return new CellReference(_comment.getRef()).getCol(); + return getAddress().getColumn(); } /** * @return the 0-based row index of the cell that the comment is associated with. */ + @Override public int getRow() { - return new CellReference(_comment.getRef()).getRow(); + return getAddress().getRow(); } /** * @return whether the comment is visible */ + @Override public boolean isVisible() { boolean visible = false; if(_vmlShape != null){ @@ -116,6 +120,7 @@ public class XSSFComment implements Comment { /** * @param visible whether the comment is visible */ + @Override public void setVisible(boolean visible) { if(_vmlShape != null){ String style; @@ -124,23 +129,28 @@ public class XSSFComment implements Comment { _vmlShape.setStyle(style); } } - - /** - * Set the column of the cell that contains the comment - * - * @param col the 0-based column of the cell that contains the comment - */ - public void setColumn(int col) { + + @Override + public CellAddress getAddress() { + return new CellAddress(_comment.getRef()); + } + + @Override + public void setAddress(int row, int col) { + setAddress(new CellAddress(row, col)); + } + + @Override + public void setAddress(CellAddress address) { CellAddress oldRef = new CellAddress(_comment.getRef()); - CellAddress ref = new CellAddress(getRow(), col); - _comment.setRef(ref.formatAsString()); + _comment.setRef(address.formatAsString()); _comments.referenceUpdated(oldRef, _comment); - if(_vmlShape != null) { - _vmlShape.getClientDataArray(0).setColumnArray( - new BigInteger[] { new BigInteger(String.valueOf(col)) } - ); + if (_vmlShape != null) { + CTClientData clientData = _vmlShape.getClientDataArray(0); + clientData.setRowArray(0, new BigInteger(String.valueOf(address.getRow()))); + clientData.setColumnArray(0, new BigInteger(String.valueOf(address.getColumn()))); // There is a very odd xmlbeans bug when changing the column // arrays which can lead to corrupt pointer @@ -149,32 +159,30 @@ public class XSSFComment implements Comment { } } + /** + * Set the column of the cell that contains the comment + * + * @param col the 0-based column of the cell that contains the comment + */ + @Override + public void setColumn(int col) { + setAddress(getRow(), col); + } + /** * Set the row of the cell that contains the comment * * @param row the 0-based row of the cell that contains the comment */ + @Override public void setRow(int row) { - CellAddress oldRef = new CellAddress(_comment.getRef()); - - CellAddress ref = new CellAddress(row, getColumn()); - _comment.setRef(ref.formatAsString()); - _comments.referenceUpdated(oldRef, _comment); - - if(_vmlShape != null) { - _vmlShape.getClientDataArray(0).setRowArray(0, - new BigInteger(String.valueOf(row))); - - // There is a very odd xmlbeans bug when changing the row - // arrays which can lead to corrupt pointer - // This call seems to fix them again... See bug #50795 - _vmlShape.getClientDataList().toString(); - } + setAddress(row, getColumn()); } /** * @return the rich text string of the comment */ + @Override public XSSFRichTextString getString() { if(_str == null) { CTRst rst = _comment.getText(); @@ -188,6 +196,7 @@ public class XSSFComment implements Comment { * * @param string the XSSFRichTextString used by this object. */ + @Override public void setString(RichTextString string) { if(!(string instanceof XSSFRichTextString)){ throw new IllegalArgumentException("Only XSSFRichTextString argument is supported"); diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCellComment.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCellComment.java index 844c95f825..dc11e6cb5f 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCellComment.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCellComment.java @@ -378,4 +378,38 @@ public abstract class BaseTestCellComment { wb.close(); } } + + @Test + public void getAddress() { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sh = wb.createSheet(); + CreationHelper factory = wb.getCreationHelper(); + Drawing patriarch = sh.createDrawingPatriarch(); + Comment comment = patriarch.createCellComment(factory.createClientAnchor()); + + assertEquals(CellAddress.A1, comment.getAddress()); + Cell C2 = sh.createRow(1).createCell(2); + C2.setCellComment(comment); + assertEquals(new CellAddress("C2"), comment.getAddress()); + } + + @Test + public void setAddress() { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sh = wb.createSheet(); + CreationHelper factory = wb.getCreationHelper(); + Drawing patriarch = sh.createDrawingPatriarch(); + Comment comment = patriarch.createCellComment(factory.createClientAnchor()); + + assertEquals(CellAddress.A1, comment.getAddress()); + CellAddress C2 = new CellAddress("C2"); + assertEquals("C2", C2.formatAsString()); + comment.setAddress(C2); + assertEquals(C2, comment.getAddress()); + + CellAddress E10 = new CellAddress(9, 4); + assertEquals("E10", E10.formatAsString()); + comment.setAddress(9, 4); + assertEquals(E10, comment.getAddress()); + } } -- 2.39.5