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.hssf.record.TextObjectRecord;
import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment; 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. * 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 * @param visible <code>true</code> if the comment is visible, <code>false</code> otherwise
*/ */
@Override
public void setVisible(boolean visible) { public void setVisible(boolean visible) {
_note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN); _note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN);
setHidden(!visible); 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 * @return <code>true</code> if the comment is visible, <code>false</code> otherwise
*/ */
@Override
public boolean isVisible() { public boolean isVisible() {
return _note.getFlags() == NoteRecord.NOTE_VISIBLE; 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 row of the cell that contains the comment
* *
* @return the 0-based row of the cell that contains the comment * @return the 0-based row of the cell that contains the comment
*/ */
@Override
public int getRow() { public int getRow() {
return _note.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 * @param row the 0-based row of the cell that contains the comment
*/ */
@Override
public void setRow(int row) { public void setRow(int row) {
_note.setRow(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 * @return the 0-based column of the cell that contains the comment
*/ */
@Override
public int getColumn() { public int getColumn() {
return _note.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 * @param col the 0-based column of the cell that contains the comment
*/ */
@Override
public void setColumn(int col) { public void setColumn(int col) {
_note.setColumn(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 * @return the name of the original author of the comment
*/ */
@Override
public String getAuthor() { public String getAuthor() {
return _note.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 * @param author the name of the original author of the comment
*/ */
@Override
public void setAuthor(String author) { public void setAuthor(String author) {
if (_note != null) _note.setAuthor(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()); throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
} }
@Override
public void afterRemove(HSSFPatriarch patriarch){ public void afterRemove(HSSFPatriarch patriarch){
super.afterRemove(patriarch); super.afterRemove(patriarch);
patriarch.getBoundAggregate().removeTailRecord(getNoteRecord()); patriarch.getBoundAggregate().removeTailRecord(getNoteRecord());

View File

@ -17,6 +17,8 @@
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import org.apache.poi.ss.util.CellAddress;
public interface Comment { public interface Comment {
/** /**
@ -33,6 +35,28 @@ public interface Comment {
*/ */
boolean isVisible(); 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 * 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.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
import com.microsoft.schemas.office.excel.CTClientData;
import com.microsoft.schemas.vml.CTShape; import com.microsoft.schemas.vml.CTShape;
public class XSSFComment implements Comment { public class XSSFComment implements Comment {
@ -55,11 +56,9 @@ public class XSSFComment implements Comment {
// the same way as we do in setRow()/setColumn() // the same way as we do in setRow()/setColumn()
if(vmlShape != null && vmlShape.sizeOfClientDataArray() > 0) { if(vmlShape != null && vmlShape.sizeOfClientDataArray() > 0) {
CellReference ref = new CellReference(comment.getRef()); CellReference ref = new CellReference(comment.getRef());
vmlShape.getClientDataArray(0).setRowArray(0, CTClientData clientData = vmlShape.getClientDataArray(0);
new BigInteger(String.valueOf(ref.getRow()))); clientData.setRowArray(0, new BigInteger(String.valueOf(ref.getRow())));
clientData.setColumnArray(0, new BigInteger(String.valueOf(ref.getCol())));
vmlShape.getClientDataArray(0).setColumnArray(0,
new BigInteger(String.valueOf(ref.getCol())));
// There is a very odd xmlbeans bug when changing the row // There is a very odd xmlbeans bug when changing the row
// arrays which can lead to corrupt pointer // 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. * @return Name of the original comment author. Default value is blank.
*/ */
@Override
public String getAuthor() { public String getAuthor() {
return _comments.getAuthor((int) _comment.getAuthorId()); 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 * @param author the name of the original author of the comment
*/ */
@Override
public void setAuthor(String author) { public void setAuthor(String author) {
_comment.setAuthorId( _comment.setAuthorId(
_comments.findAuthor(author) _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. * @return the 0-based column of the cell that the comment is associated with.
*/ */
@Override
public int getColumn() { 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. * @return the 0-based row index of the cell that the comment is associated with.
*/ */
@Override
public int getRow() { public int getRow() {
return new CellReference(_comment.getRef()).getRow(); return getAddress().getRow();
} }
/** /**
* @return whether the comment is visible * @return whether the comment is visible
*/ */
@Override
public boolean isVisible() { public boolean isVisible() {
boolean visible = false; boolean visible = false;
if(_vmlShape != null){ if(_vmlShape != null){
@ -116,6 +120,7 @@ public class XSSFComment implements Comment {
/** /**
* @param visible whether the comment is visible * @param visible whether the comment is visible
*/ */
@Override
public void setVisible(boolean visible) { public void setVisible(boolean visible) {
if(_vmlShape != null){ if(_vmlShape != null){
String style; String style;
@ -125,22 +130,27 @@ public class XSSFComment implements Comment {
} }
} }
/** @Override
* Set the column of the cell that contains the comment public CellAddress getAddress() {
* return new CellAddress(_comment.getRef());
* @param col the 0-based column of the cell that contains the comment }
*/
public void setColumn(int col) { @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 oldRef = new CellAddress(_comment.getRef());
CellAddress ref = new CellAddress(getRow(), col); _comment.setRef(address.formatAsString());
_comment.setRef(ref.formatAsString());
_comments.referenceUpdated(oldRef, _comment); _comments.referenceUpdated(oldRef, _comment);
if(_vmlShape != null) { if (_vmlShape != null) {
_vmlShape.getClientDataArray(0).setColumnArray( CTClientData clientData = _vmlShape.getClientDataArray(0);
new BigInteger[] { new BigInteger(String.valueOf(col)) } 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 // There is a very odd xmlbeans bug when changing the column
// arrays which can lead to corrupt pointer // 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 * Set the row of the cell that contains the comment
* *
* @param row the 0-based 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) { public void setRow(int row) {
CellAddress oldRef = new CellAddress(_comment.getRef()); setAddress(row, getColumn());
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();
}
} }
/** /**
* @return the rich text string of the comment * @return the rich text string of the comment
*/ */
@Override
public XSSFRichTextString getString() { public XSSFRichTextString getString() {
if(_str == null) { if(_str == null) {
CTRst rst = _comment.getText(); CTRst rst = _comment.getText();
@ -188,6 +196,7 @@ public class XSSFComment implements Comment {
* *
* @param string the XSSFRichTextString used by this object. * @param string the XSSFRichTextString used by this object.
*/ */
@Override
public void setString(RichTextString string) { public void setString(RichTextString string) {
if(!(string instanceof XSSFRichTextString)){ if(!(string instanceof XSSFRichTextString)){
throw new IllegalArgumentException("Only XSSFRichTextString argument is supported"); throw new IllegalArgumentException("Only XSSFRichTextString argument is supported");

View File

@ -378,4 +378,38 @@ public abstract class BaseTestCellComment {
wb.close(); 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());
}
} }