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
This commit is contained in:
Javen O'Neal 2016-05-09 05:09:41 +00:00
parent fe04abd31d
commit 70459401a3
4 changed files with 129 additions and 35 deletions

View File

@ -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 <code>true</code> if the comment is visible, <code>false</code> 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 <code>true</code> if the comment is visible, <code>false</code> 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());

View File

@ -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 <code>true</code> if the comment is visible, <code>false</code> 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

View File

@ -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");

View File

@ -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());
}
}