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.
*
* @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);
*
* @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();
}
*
* @param row the 0-based row of the cell that contains the comment
*/
+ @Override
public void setRow(int row) {
_note.setRow(row);
}
*
* @return the 0-based column of the cell that contains the comment
*/
+ @Override
public int getColumn() {
return _note.getColumn();
}
*
* @param col the 0-based column of the cell that contains the comment
*/
+ @Override
public void setColumn(int col) {
_note.setColumn(col);
}
*
* @return the name of the original author of the comment
*/
+ @Override
public String getAuthor() {
return _note.getAuthor();
}
*
* @param author the name of the original author of the comment
*/
+ @Override
public void setAuthor(String author) {
if (_note != null) _note.setAuthor(author);
}
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());
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 {
// 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
*
* @return Name of the original comment author. Default value is blank.
*/
+ @Override
public String getAuthor() {
return _comments.getAuthor((int) _comment.getAuthorId());
}
*
* @param author the name of the original author of the comment
*/
+ @Override
public void setAuthor(String author) {
_comment.setAuthorId(
_comments.findAuthor(author)
/**
* @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){
/**
* @param visible whether the comment is visible
*/
+ @Override
public void setVisible(boolean visible) {
if(_vmlShape != null){
String style;
_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
}
}
+ /**
+ * 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();
*
* @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");
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());
+ }
}