==================================================================== */
package org.apache.poi.ddf;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
+import java.util.*;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
} );
}
+ /**
+ * Set an escher property. If a property with given propId already
+ exists it is replaced.
+ *
+ * @param value the property to set.
+ */
+ public void setEscherProperty(EscherProperty value){
+ for ( Iterator<EscherProperty> iterator =
+ properties.iterator(); iterator.hasNext(); ) {
+ EscherProperty prop = iterator.next();
+ if (prop.getId() == value.getId()){
+ iterator.remove();
+ }
+ }
+ properties.add( value );
+ sortProperties();
+ }
+
/**
* Retrieve the string representation of this record.
*/
public List<Record> getTailRecords(){
return Collections.unmodifiableList(tailRec);
}
+
+ public NoteRecord getNoteRecordByObj(ObjRecord obj){
+ for (Record rec: tailRec){
+ NoteRecord note = (NoteRecord) rec;
+ CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) obj.getSubRecords().get(0);
+ if (note.getShapeId() == cod.getObjectId()){
+ return note;
+ }
+ }
+ return null;
+ }
}
==================================================================== */
package org.apache.poi.hssf.usermodel;
-import org.apache.poi.hssf.record.NoteRecord;
-import org.apache.poi.hssf.record.TextObjectRecord;
+import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.hssf.record.*;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.RichTextString;
*/
public class HSSFComment extends HSSFTextbox implements Comment {
- /*
- * TODO - make HSSFComment more consistent when created vs read from file.
- * Currently HSSFComment has two main forms (corresponding to the two constructors). There
- * are certain operations that only work on comment objects in one of the forms (e.g. deleting
- * comments).
- * POI is also deficient in its management of RowRecord fields firstCol and lastCol. Those
- * fields are supposed to take comments into account, but POI does not do this yet (feb 2009).
- * It seems like HSSFRow should manage a collection of local HSSFComments
- */
-
+ /*
+ * TODO - make HSSFComment more consistent when created vs read from file.
+ * Currently HSSFComment has two main forms (corresponding to the two constructors). There
+ * are certain operations that only work on comment objects in one of the forms (e.g. deleting
+ * comments).
+ * POI is also deficient in its management of RowRecord fields firstCol and lastCol. Those
+ * fields are supposed to take comments into account, but POI does not do this yet (feb 2009).
+ * It seems like HSSFRow should manage a collection of local HSSFComments
+ */
+
private boolean _visible;
private int _row;
private int _col;
private String _author;
private NoteRecord _note;
- private TextObjectRecord _txo;
+
+ public HSSFComment(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord textObjectRecord, NoteRecord _note) {
+ super(spContainer, objRecord, textObjectRecord);
+ this._note = _note;
+ }
/**
* Construct a new comment with the given parent and anchor.
*
* @param parent
- * @param anchor defines position of this anchor in the sheet
+ * @param anchor defines position of this anchor in the sheet
*/
public HSSFComment(HSSFShape parent, HSSFAnchor anchor) {
super(parent, anchor);
- setShapeType(OBJECT_TYPE_COMMENT);
-
+ _note = createNoteRecord();
//default color for comments
setFillColor(0x08000050);
//by default comments are hidden
- _visible = false;
-
- _author = "";
+ setVisible(false);
+ setAuthor("");
}
protected HSSFComment(NoteRecord note, TextObjectRecord txo) {
this(null, new HSSFClientAnchor());
- _txo = txo;
+ _textObjectRecord = txo;
_note = note;
}
+ @Override
+ protected ObjRecord createObjRecord() {
+ ObjRecord obj = new ObjRecord();
+ CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
+ c.setObjectType(OBJECT_TYPE_COMMENT);
+ c.setLocked(true);
+ c.setPrintable(true);
+ c.setAutofill(false);
+ c.setAutoline(true);
+
+ NoteStructureSubRecord u = new NoteStructureSubRecord();
+ EndSubRecord e = new EndSubRecord();
+ obj.addSubRecord(c);
+ obj.addSubRecord(u);
+ obj.addSubRecord(e);
+ return obj;
+ }
+
+ private NoteRecord createNoteRecord(){
+ NoteRecord note = new NoteRecord();
+ note.setFlags(NoteRecord.NOTE_VISIBLE);
+ note.setAuthor("");
+ return note;
+ }
+
+ @Override
+ void setShapeId(int shapeId) {
+ super.setShapeId(shapeId);
+ _note.setShapeId(shapeId-1024);
+ }
+
/**
* Returns whether this comment is visible.
*
* @param visible <code>true</code> if the comment is visible, <code>false</code> otherwise
*/
- public void setVisible(boolean visible){
- if(_note != null) {
- _note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN);
- }
- _visible = visible;
+ public void setVisible(boolean visible) {
+ _note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN);
}
/**
* @return <code>true</code> if the comment is visible, <code>false</code> otherwise
*/
public boolean isVisible() {
- return _visible;
+ return _note.getFlags() == NoteRecord.NOTE_VISIBLE;
}
/**
* @return the 0-based row of the cell that contains the comment
*/
public int getRow() {
- return _row;
+ return _note.getRow();
}
/**
* @param row the 0-based row of the cell that contains the comment
*/
public void setRow(int row) {
- if(_note != null) {
- _note.setRow(row);
- }
- _row = row;
+ _note.setRow(row);
}
/**
*
* @return the 0-based column of the cell that contains the comment
*/
- public int getColumn(){
- return _col;
+ public int getColumn() {
+ return _note.getColumn();
}
/**
* @param col the 0-based column of the cell that contains the comment
*/
public void setColumn(int col) {
- if(_note != null) {
- _note.setColumn(col);
- }
- _col = col;
+ _note.setColumn(col);
}
+
/**
* @deprecated (Nov 2009) use {@link HSSFComment#setColumn(int)} }
*/
@Deprecated
public void setColumn(short col) {
- setColumn((int)col);
+ setColumn((int) col);
}
/**
* @return the name of the original author of the comment
*/
public String getAuthor() {
- return _author;
+ return _note.getAuthor();
}
/**
*
* @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;
+ public void setAuthor(String author) {
+ if (_note != null) _note.setAuthor(author);
}
-
+
/**
* Sets the rich text string used by this comment.
*
- * @param string Sets the rich text string used by this object.
+ * @param string Sets the rich text string used by this object.
*/
public void setString(RichTextString string) {
HSSFRichTextString hstring = (HSSFRichTextString) string;
//if font is not set we must set the default one
- if (hstring.numFormattingRuns() == 0) hstring.applyFont((short)0);
+ if (hstring.numFormattingRuns() == 0) hstring.applyFont((short) 0);
- if (_txo != null) {
- _txo.setStr(hstring);
- }
+ _textObjectRecord.setStr(hstring);
super.setString(string);
}
-
+
/**
* Returns the underlying Note record
*/
protected NoteRecord getNoteRecord() {
- return _note;
- }
+ return _note;
+ }
+
/**
* Returns the underlying Text record
*/
public TextObjectRecord getTextObjectRecord() {
- return _txo;
- }
+ return _textObjectRecord;
+ }
}
HSSFComment shape = new HSSFComment(null, anchor);
shape.anchor = anchor;
addShape(shape);
+ onCreate(shape);
return shape;
}
package org.apache.poi.hssf.usermodel;
import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.ddf.EscherOptRecord;
import org.apache.poi.hssf.record.ObjRecord;
/**
@Override
protected EscherContainerRecord createSpContainer() {
- return null; //To change body of implemented methods use File | Settings | File Templates.
+ EscherContainerRecord spContainer = new EscherContainerRecord();
+ spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
+ spContainer.setOptions( (short) 0x000F );
+ EscherOptRecord optRecord = new EscherOptRecord();
+ optRecord.setRecordId(EscherOptRecord.RECORD_ID);
+ spContainer.addChildRecord(optRecord);
+ return spContainer;
}
@Override
protected ObjRecord createObjRecord() {
- return null; //To change body of implemented methods use File | Settings | File Templates.
+ return null;
}
public int[] getXPoints()
HSSFAnchor anchor;
HSSFPatriarch _patriarch;
- protected EscherContainerRecord _escherContainer;
- protected ObjRecord _objRecord;
+ protected final EscherContainerRecord _escherContainer;
+ protected final ObjRecord _objRecord;
protected final EscherOptRecord _optRecord;
public HSSFShape(EscherContainerRecord spContainer, ObjRecord objRecord) {
public HSSFShape(HSSFShape parent, HSSFAnchor anchor) {
this.parent = parent;
this.anchor = anchor;
- this._escherContainer = new EscherContainerRecord();
- _optRecord = new EscherOptRecord();
- _optRecord.setRecordId( EscherOptRecord.RECORD_ID );
- _optRecord.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEDASHING, LINESTYLE_SOLID));
- _optRecord.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEWIDTH, LINEWIDTH_DEFAULT));
- _optRecord.addEscherProperty(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, FILL__FILLCOLOR_DEFAULT));
- _optRecord.addEscherProperty(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, LINESTYLE__COLOR_DEFAULT));
- _optRecord.addEscherProperty(new EscherBoolProperty(EscherProperties.FILL__NOFILLHITTEST, 0x0));
+ this._escherContainer = createSpContainer();
+ _optRecord = _escherContainer.getChildById(EscherOptRecord.RECORD_ID);
+ addStandardOptions(_optRecord);
+ _objRecord = createObjRecord();
+
}
protected abstract EscherContainerRecord createSpContainer();
CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) _objRecord.getSubRecords().get(0);
cod.setObjectId((short) (shapeId-1024));
}
+
+ private void addStandardOptions(EscherOptRecord optRecord){
+ setPropertyValue(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEDASHING, LINESTYLE_SOLID));
+ setPropertyValue(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEWIDTH, LINEWIDTH_DEFAULT));
+ setPropertyValue(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, FILL__FILLCOLOR_DEFAULT));
+ setPropertyValue(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, LINESTYLE__COLOR_DEFAULT));
+ setPropertyValue(new EscherBoolProperty(EscherProperties.FILL__NOFILLHITTEST, 0x0));
+ }
int getShapeId(){
return ((EscherSpRecord)_escherContainer.getChildById(EscherSpRecord.RECORD_ID)).getShapeId();
}
protected void setPropertyValue(EscherProperty property){
- if (null == _optRecord.lookup(property.getId())){
- _optRecord.addEscherProperty(property);
- } else {
- int i=0;
- for (EscherProperty prop: _optRecord.getEscherProperties()){
- if (prop.getId() == property.getId()){
- _optRecord.getEscherProperties().remove(i);
- break;
- }
- i++;
- }
- _optRecord.addEscherProperty(property);
- }
+ _optRecord.setEscherProperty(property);
}
/**
case CommonObjectDataSubRecord.OBJECT_TYPE_TEXT:\r
shape = new HSSFTextbox(container, objRecord, txtRecord);\r
break;\r
+ case CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT:\r
+ shape = new HSSFComment(container, objRecord, txtRecord, agg.getNoteRecordByObj(objRecord));\r
+ break;\r
default:\r
shape = new HSSFSimpleShape(container, objRecord);\r
}\r
package org.apache.poi.hssf.usermodel;
-import org.apache.poi.ddf.EscherChildAnchorRecord;
-import org.apache.poi.ddf.EscherClientAnchorRecord;
-import org.apache.poi.ddf.EscherContainerRecord;
-import org.apache.poi.ddf.EscherRecord;
-import org.apache.poi.ddf.EscherSpgrRecord;
+import org.apache.poi.ddf.*;
import org.apache.poi.hssf.model.TextboxShape;
import org.apache.poi.hssf.record.EscherAggregate;
import org.apache.poi.hssf.record.ObjRecord;
@Override
protected EscherContainerRecord createSpContainer() {
- return null; //To change body of implemented methods use File | Settings | File Templates.
+ EscherContainerRecord spContainer = new EscherContainerRecord();
+ spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
+ spContainer.setOptions( (short) 0x000F );
+ EscherOptRecord optRecord = new EscherOptRecord();
+ optRecord.setRecordId(EscherOptRecord.RECORD_ID);
+ spContainer.addChildRecord(optRecord);
+ return spContainer;
}
@Override
return new HSSFAutoFilter(this);
}
+ protected HSSFComment findCellComment(int row, int column) {
+ return lookForComment(getDrawingPatriarch(), row, column);
+ }
+
+ private HSSFComment lookForComment(HSSFShapeContainer container, int row, int column){
+ for (Object object: container.getChildren()){
+ HSSFShape shape = (HSSFShape) object;
+ if (shape instanceof HSSFShapeGroup){
+ HSSFShape res = lookForComment((HSSFShapeContainer) shape, row, column);
+ if (null != res){
+ return (HSSFComment) res;
+ }
+ continue;
+ }
+ if (shape instanceof HSSFComment){
+ HSSFComment comment = (HSSFComment) shape;
+ if (comment.getColumn() == column && comment.getRow() == row){
+ return comment;
+ }
+ }
+ }
+ return null;
+ }
}
public final static short OBJECT_TYPE_COMMENT = 25;
// public final static short OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING = 30;
- int shapeType = OBJECT_TYPE_LINE;
-
private static final Map <Short, Short> objTypeToShapeType = new HashMap<Short, Short>();
static {
public HSSFSimpleShape( HSSFShape parent, HSSFAnchor anchor)
{
super( parent, anchor );
- _escherContainer = createSpContainer();
- _objRecord = createObjRecord();
setShapeType(OBJECT_TYPE_LINE);
}
clientData.setRecordId( EscherClientDataRecord.RECORD_ID );
clientData.setOptions( (short) 0x0000 );
+ EscherOptRecord optRecord = new EscherOptRecord();
+ optRecord.setRecordId( EscherOptRecord.RECORD_ID );
+
spContainer.addChildRecord(sp);
- spContainer.addChildRecord(_optRecord);
+ spContainer.addChildRecord(optRecord);
spContainer.addChildRecord(anchor.getEscherAnchor());
spContainer.addChildRecord(clientData);
return spContainer;
System.out.println("Unknown shape type: "+shapeType);
return;
}
- spRecord.setShapeType(objTypeToShapeType.get((short)shapeType));
+ spRecord.setShapeType(objTypeToShapeType.get((short) shapeType));
}
}
public final static short VERTICAL_ALIGNMENT_JUSTIFY = 4;
public final static short VERTICAL_ALIGNMENT_DISTRIBUTED = 7;
-
- int marginLeft, marginRight, marginTop, marginBottom;
- short halign, valign;
-
- private TextObjectRecord _textObjectRecord;
+ protected TextObjectRecord _textObjectRecord;
public HSSFTextbox(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord textObjectRecord) {
super(spContainer, objRecord);
sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE);
opt.setRecordId(EscherOptRecord.RECORD_ID);
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTID, 0));
- opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTLEFT, getMarginLeft()));
- opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTRIGHT, getMarginRight()));
- opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTBOTTOM, getMarginBottom()));
- opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTTOP, getMarginTop()));
-
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__WRAPTEXT, 0));
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__ANCHORTEXT, 0));
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GROUPSHAPE__PRINT, 0x00080000));
+ opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTLEFT, 0));
+ opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTRIGHT, 0));
+ opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTTOP, 0));
+ opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTBOTTOM, 0));
+
+
EscherRecord anchor = getAnchor().getEscherAnchor();
clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
clientData.setOptions((short) 0x0000);
package org.apache.poi.hssf.model;\r
\r
+import org.apache.poi.hssf.usermodel.HSSFComment;\r
import org.apache.poi.hssf.usermodel.HSSFTextbox;\r
\r
/**\r
public static TextboxShape createTextboxShape(int shapeId, HSSFTextbox textbox){\r
return new TextboxShape(textbox, shapeId);\r
}\r
+\r
+ public static CommentShape createCommentShape(int shapeId, HSSFComment comment){\r
+ return new CommentShape(comment, shapeId);\r
+ }\r
}\r
* @author Evgeniy Berlog\r
* date: 12.06.12\r
*/\r
-public class TestDrawingShapes extends TestCase{\r
+public class TestDrawingShapes extends TestCase {\r
\r
/**\r
* HSSFShape tree bust be built correctly\r
* ----shape\r
* ----shape\r
*/\r
- public void testDrawingGroups(){\r
+ public void testDrawingGroups() {\r
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");\r
HSSFSheet sheet = wb.getSheet("groups");\r
HSSFPatriarch patriarch = sheet.getDrawingPatriarch();\r
}\r
\r
public void testHSSFShapeCompatibility() {\r
- HSSFShape shape = new HSSFSimpleShape(null, new HSSFClientAnchor());\r
+ HSSFShape shape = new HSSFSimpleShape(null, new HSSFClientAnchor());\r
assertEquals(0x08000040, shape.getLineStyleColor());\r
assertEquals(0x08000009, shape.getFillColor());\r
assertEquals(HSSFShape.LINEWIDTH_DEFAULT, shape.getLineWidth());\r
\r
assertEquals(7, opt.getEscherProperties().size());\r
assertEquals(true,\r
- ((EscherBoolProperty)opt.lookup(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE)).isTrue());\r
+ ((EscherBoolProperty) opt.lookup(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE)).isTrue());\r
assertEquals(0x00000004,\r
- ((EscherSimpleProperty)opt.lookup(EscherProperties.GEOMETRY__SHAPEPATH)).getPropertyValue());\r
+ ((EscherSimpleProperty) opt.lookup(EscherProperties.GEOMETRY__SHAPEPATH)).getPropertyValue());\r
assertEquals(0x08000009,\r
- ((EscherSimpleProperty)opt.lookup(EscherProperties.FILL__FILLCOLOR)).getPropertyValue());\r
+ ((EscherSimpleProperty) opt.lookup(EscherProperties.FILL__FILLCOLOR)).getPropertyValue());\r
assertEquals(true,\r
- ((EscherBoolProperty)opt.lookup(EscherProperties.FILL__NOFILLHITTEST)).isTrue());\r
+ ((EscherBoolProperty) opt.lookup(EscherProperties.FILL__NOFILLHITTEST)).isTrue());\r
assertEquals(0x08000040,\r
- ((EscherSimpleProperty)opt.lookup(EscherProperties.LINESTYLE__COLOR)).getPropertyValue());\r
+ ((EscherSimpleProperty) opt.lookup(EscherProperties.LINESTYLE__COLOR)).getPropertyValue());\r
assertEquals(true,\r
- ((EscherBoolProperty)opt.lookup(EscherProperties.LINESTYLE__NOLINEDRAWDASH)).isTrue());\r
+ ((EscherBoolProperty) opt.lookup(EscherProperties.LINESTYLE__NOLINEDRAWDASH)).isTrue());\r
assertEquals(true,\r
- ((EscherBoolProperty)opt.lookup(EscherProperties.GROUPSHAPE__PRINT)).isTrue());\r
+ ((EscherBoolProperty) opt.lookup(EscherProperties.GROUPSHAPE__PRINT)).isTrue());\r
}\r
\r
- public void testDefaultPictureSettings(){\r
+ public void testDefaultPictureSettings() {\r
HSSFPicture picture = new HSSFPicture(null, new HSSFClientAnchor());\r
assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);\r
assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT);\r
/**\r
* No NullPointerException should appear\r
*/\r
- public void testDefaultSettingsWithEmptyContainer(){\r
+ public void testDefaultSettingsWithEmptyContainer() {\r
EscherContainerRecord container = new EscherContainerRecord();\r
EscherOptRecord opt = new EscherOptRecord();\r
opt.setRecordId(EscherOptRecord.RECORD_ID);\r
assertEquals(picture.isNoFill(), HSSFShape.NO_FILL_DEFAULT);\r
assertEquals(picture.getPictureIndex(), -1);//not set yet\r
}\r
+\r
/**\r
* create a rectangle, save the workbook, read back and verify that all shape properties are there\r
*/\r
HSSFSheet sheet = wb.createSheet();\r
\r
HSSFPatriarch drawing = sheet.createDrawingPatriarch();\r
- HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 200, 200, (short)2, 2, (short)15, 15);\r
+ HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 200, 200, (short) 2, 2, (short) 15, 15);\r
anchor.setAnchorType(2);\r
assertEquals(anchor.getAnchorType(), 2);\r
\r
assertEquals(1, drawing.getChildren().size());\r
\r
HSSFSimpleShape rectangle2 =\r
- (HSSFSimpleShape)drawing.getChildren().get(0);\r
+ (HSSFSimpleShape) drawing.getChildren().get(0);\r
assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE,\r
rectangle2.getShapeType());\r
assertEquals(10000, rectangle2.getLineWidth());\r
sheet = wb.getSheetAt(0);\r
drawing = sheet.getDrawingPatriarch();\r
assertEquals(1, drawing.getChildren().size());\r
- rectangle2 = (HSSFSimpleShape)drawing.getChildren().get(0);\r
+ rectangle2 = (HSSFSimpleShape) drawing.getChildren().get(0);\r
assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, rectangle2.getShapeType());\r
assertEquals(77, rectangle2.getLineWidth());\r
assertEquals(9, rectangle2.getLineStyle());\r
assertEquals(drawing.getChildren().size(), 2);\r
}\r
\r
- public void testReadExistingImage(){\r
+ public void testReadExistingImage() {\r
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");\r
HSSFSheet sheet = wb.getSheet("pictures");\r
HSSFPatriarch drawing = sheet.getDrawingPatriarch();\r
assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);\r
assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT);\r
assertEquals(picture.isNoFill(), true);\r
+\r
+ picture.setPictureIndex(2);\r
+ assertEquals(picture.getPictureIndex(), 2);\r
}\r
\r
\r
HSSFPatriarch drawing = sheet.getDrawingPatriarch();\r
assertEquals(1, drawing.getChildren().size());\r
\r
- for(HSSFShape shape : drawing.getChildren()){\r
- assertEquals(shape.isNoFill(), true);\r
- assertEquals(shape.getLineStyle(), HSSFShape.LINESTYLE_DASHDOTGEL);\r
- assertEquals(shape.getLineStyleColor(), 0x616161);\r
- assertEquals(HexDump.toHex(shape.getFillColor()), shape.getFillColor(), 0x2CE03D);\r
- assertEquals(shape.getLineWidth(), HSSFShape.LINEWIDTH_ONE_PT*2);\r
- }\r
+ HSSFSimpleShape shape = (HSSFSimpleShape) drawing.getChildren().get(0);\r
+ assertEquals(shape.isNoFill(), true);\r
+ assertEquals(shape.getLineStyle(), HSSFShape.LINESTYLE_DASHDOTGEL);\r
+ assertEquals(shape.getLineStyleColor(), 0x616161);\r
+ assertEquals(HexDump.toHex(shape.getFillColor()), shape.getFillColor(), 0x2CE03D);\r
+ assertEquals(shape.getLineWidth(), HSSFShape.LINEWIDTH_ONE_PT * 2);\r
}\r
\r
public void testShapeIds() {\r
HSSFWorkbook wb = new HSSFWorkbook();\r
HSSFSheet sheet1 = wb.createSheet();\r
HSSFPatriarch patriarch1 = sheet1.createDrawingPatriarch();\r
- for(int i = 0; i < 2; i++) {\r
+ for (int i = 0; i < 2; i++) {\r
patriarch1.createSimpleShape(new HSSFClientAnchor());\r
}\r
\r
assertEquals(3, spgrContainer.getChildRecords().size());\r
\r
EscherSpRecord sp0 =\r
- ((EscherContainerRecord)spgrContainer.getChild(0)).getChildById(EscherSpRecord.RECORD_ID);\r
+ ((EscherContainerRecord) spgrContainer.getChild(0)).getChildById(EscherSpRecord.RECORD_ID);\r
assertEquals(1024, sp0.getShapeId());\r
\r
EscherSpRecord sp1 =\r
- ((EscherContainerRecord)spgrContainer.getChild(1)).getChildById(EscherSpRecord.RECORD_ID);\r
+ ((EscherContainerRecord) spgrContainer.getChild(1)).getChildById(EscherSpRecord.RECORD_ID);\r
assertEquals(1025, sp1.getShapeId());\r
\r
EscherSpRecord sp2 =\r
- ((EscherContainerRecord)spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID);\r
+ ((EscherContainerRecord) spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID);\r
assertEquals(1026, sp2.getShapeId());\r
}\r
\r
* Test get new id for shapes from existing file\r
* File already have for 1 shape on each sheet, because document must contain EscherDgRecord for each sheet\r
*/\r
- public void testAllocateNewIds(){\r
+ public void testAllocateNewIds() {\r
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("empty.xls");\r
HSSFSheet sheet = wb.getSheetAt(0);\r
HSSFPatriarch patriarch = sheet.getDrawingPatriarch();\r
assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1027);\r
assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1028);\r
}\r
+\r
+ public void testOpt() throws Exception {\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+\r
+ // create a sheet with a text box\r
+ HSSFSheet sheet = wb.createSheet();\r
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();\r
+\r
+ HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());\r
+ EscherOptRecord opt1 = HSSFTestHelper.getOptRecord(textbox);\r
+ EscherOptRecord opt2 = textbox.getEscherContainer().getChildById(EscherOptRecord.RECORD_ID);\r
+ assertSame(opt1, opt2);\r
+ }\r
+ \r
+ public void testCorrectOrderInOptRecord(){\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sheet = wb.createSheet();\r
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();\r
+\r
+ HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());\r
+ EscherOptRecord opt = HSSFTestHelper.getOptRecord(textbox); \r
+ \r
+ String opt1Str = opt.toXml();\r
+\r
+ textbox.setFillColor(textbox.getFillColor());\r
+ assertEquals(opt1Str, textbox.getEscherContainer().getChildById(EscherOptRecord.RECORD_ID).toXml());\r
+ textbox.setLineStyle(textbox.getLineStyle());\r
+ assertEquals(opt1Str, textbox.getEscherContainer().getChildById(EscherOptRecord.RECORD_ID).toXml());\r
+ textbox.setLineWidth(textbox.getLineWidth());\r
+ assertEquals(opt1Str, textbox.getEscherContainer().getChildById(EscherOptRecord.RECORD_ID).toXml());\r
+ textbox.setLineStyleColor(textbox.getLineStyleColor());\r
+ assertEquals(opt1Str, textbox.getEscherContainer().getChildById(EscherOptRecord.RECORD_ID).toXml());\r
+ }\r
}\r
==================================================================== */
package org.apache.poi.hssf.usermodel;
+import org.apache.poi.ddf.EscherOptRecord;
import org.apache.poi.hssf.model.InternalSheet;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.EscherAggregate;
public static int allocateNewShapeId(HSSFPatriarch patriarch){
return patriarch.newShapeId();
}
+
+ public static EscherOptRecord getOptRecord(HSSFShape shape){
+ return shape._optRecord;
+ }
}
--- /dev/null
+package org.apache.poi.hssf.usermodel;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.ddf.EscherSpRecord;\r
+import org.apache.poi.hssf.HSSFTestDataSamples;\r
+import org.apache.poi.hssf.model.CommentShape;\r
+import org.apache.poi.hssf.model.HSSFTestModelHelper;\r
+import org.apache.poi.hssf.record.CommonObjectDataSubRecord;\r
+import org.apache.poi.hssf.record.NoteRecord;\r
+import org.apache.poi.hssf.record.ObjRecord;\r
+import org.apache.poi.hssf.record.TextObjectRecord;\r
+\r
+import java.io.IOException;\r
+import java.util.Arrays;\r
+\r
+/**\r
+ * @author Evgeniy Berlog\r
+ * @date 26.06.12\r
+ */\r
+public class TestComment extends TestCase {\r
+\r
+ public void testResultEqualsToAbstractShape() {\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sh = wb.createSheet();\r
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+\r
+ HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+ HSSFRow row = sh.createRow(0);\r
+ HSSFCell cell = row.createCell(0);\r
+ cell.setCellComment(comment);\r
+\r
+ CommentShape commentShape = HSSFTestModelHelper.createCommentShape(0, comment);\r
+\r
+ assertEquals(comment.getEscherContainer().getChildRecords().size(), 5);\r
+ assertEquals(commentShape.getSpContainer().getChildRecords().size(), 5);\r
+\r
+ //sp record\r
+ byte[] expected = commentShape.getSpContainer().getChild(0).serialize();\r
+ byte[] actual = comment.getEscherContainer().getChild(0).serialize();\r
+\r
+ assertEquals(expected.length, actual.length);\r
+ assertTrue(Arrays.equals(expected, actual));\r
+\r
+ expected = commentShape.getSpContainer().getChild(2).serialize();\r
+ actual = comment.getEscherContainer().getChild(2).serialize();\r
+\r
+ assertEquals(expected.length, actual.length);\r
+ assertTrue(Arrays.equals(expected, actual));\r
+\r
+ expected = commentShape.getSpContainer().getChild(3).serialize();\r
+ actual = comment.getEscherContainer().getChild(3).serialize();\r
+\r
+ assertEquals(expected.length, actual.length);\r
+ assertTrue(Arrays.equals(expected, actual));\r
+\r
+ expected = commentShape.getSpContainer().getChild(4).serialize();\r
+ actual = comment.getEscherContainer().getChild(4).serialize();\r
+\r
+ assertEquals(expected.length, actual.length);\r
+ assertTrue(Arrays.equals(expected, actual));\r
+\r
+ ObjRecord obj = comment.getObjRecord();\r
+ ObjRecord objShape = commentShape.getObjRecord();\r
+\r
+ expected = obj.serialize();\r
+ actual = objShape.serialize();\r
+\r
+ assertEquals(expected.length, actual.length);\r
+ assertTrue(Arrays.equals(expected, actual));\r
+\r
+ TextObjectRecord tor = comment.getTextObjectRecord();\r
+ TextObjectRecord torShape = commentShape.getTextObjectRecord();\r
+\r
+ expected = tor.serialize();\r
+ actual = torShape.serialize();\r
+\r
+ assertEquals(expected.length, actual.length);\r
+ assertTrue(Arrays.equals(expected, actual));\r
+\r
+ NoteRecord note = comment.getNoteRecord();\r
+ NoteRecord noteShape = commentShape.getNoteRecord();\r
+\r
+ expected = note.serialize();\r
+ actual = noteShape.serialize();\r
+\r
+ assertEquals(expected.length, actual.length);\r
+ assertTrue(Arrays.equals(expected, actual));\r
+ }\r
+\r
+ public void testAddToExistingFile() {\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sh = wb.createSheet();\r
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+\r
+ HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+ comment.setString(new HSSFRichTextString("comment1"));\r
+ comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+ comment.setString(new HSSFRichTextString("comment2"));\r
+\r
+ assertEquals(patriarch.getChildren().size(), 2);\r
+\r
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+ sh = wb.getSheetAt(0);\r
+ patriarch = sh.getDrawingPatriarch();\r
+\r
+ assertEquals(patriarch.getChildren().size(), 2);\r
+ comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+ comment.setString(new HSSFRichTextString("comment3"));\r
+\r
+ assertEquals(patriarch.getChildren().size(), 3);\r
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+ sh = wb.getSheetAt(0);\r
+ patriarch = sh.getDrawingPatriarch();\r
+ assertEquals(patriarch.getChildren().size(), 3);\r
+ assertEquals(((HSSFComment) patriarch.getChildren().get(0)).getString().getString(), "comment1");\r
+ assertEquals(((HSSFComment) patriarch.getChildren().get(1)).getString().getString(), "comment2");\r
+ assertEquals(((HSSFComment) patriarch.getChildren().get(2)).getString().getString(), "comment3");\r
+ }\r
+\r
+ public void testSetGetProperties() throws IOException {\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sh = wb.createSheet();\r
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+\r
+ HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+ comment.setString(new HSSFRichTextString("comment1"));\r
+ assertEquals(comment.getString().getString(), "comment1");\r
+\r
+ comment.setAuthor("poi");\r
+ assertEquals(comment.getAuthor(), "poi");\r
+\r
+ comment.setColumn(3);\r
+ assertEquals(comment.getColumn(), 3);\r
+\r
+ comment.setRow(4);\r
+ assertEquals(comment.getRow(), 4);\r
+\r
+ comment.setVisible(false);\r
+ assertEquals(comment.isVisible(), false);\r
+\r
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+ sh = wb.getSheetAt(0);\r
+ patriarch = sh.getDrawingPatriarch();\r
+\r
+ comment = (HSSFComment) patriarch.getChildren().get(0);\r
+\r
+ assertEquals(comment.getString().getString(), "comment1");\r
+ assertEquals("poi", comment.getAuthor());\r
+ assertEquals(comment.getColumn(), 3);\r
+ assertEquals(comment.getRow(), 4);\r
+ assertEquals(comment.isVisible(), false);\r
+\r
+ comment.setString(new HSSFRichTextString("comment12"));\r
+ comment.setAuthor("poi2");\r
+ comment.setColumn(32);\r
+ comment.setRow(42);\r
+ comment.setVisible(true);\r
+\r
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+ sh = wb.getSheetAt(0);\r
+ patriarch = sh.getDrawingPatriarch();\r
+ comment = (HSSFComment) patriarch.getChildren().get(0);\r
+\r
+ assertEquals(comment.getString().getString(), "comment12");\r
+ assertEquals("poi2", comment.getAuthor());\r
+ assertEquals(comment.getColumn(), 32);\r
+ assertEquals(comment.getRow(), 42);\r
+ assertEquals(comment.isVisible(), true);\r
+ }\r
+\r
+ public void testExistingFileWithComment(){\r
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");\r
+ HSSFSheet sheet = wb.getSheet("comments");\r
+ HSSFPatriarch drawing = sheet.getDrawingPatriarch();\r
+ assertEquals(1, drawing.getChildren().size());\r
+ HSSFComment comment = (HSSFComment) drawing.getChildren().get(0);\r
+ assertEquals(comment.getAuthor(), "evgeniy");\r
+ assertEquals(comment.getString().getString(), "evgeniy:\npoi test");\r
+ assertEquals(comment.getColumn(), 1);\r
+ assertEquals(comment.getRow(), 2);\r
+ }\r
+\r
+ public void testFindComments(){\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sh = wb.createSheet();\r
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+\r
+ HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+ HSSFRow row = sh.createRow(5);\r
+ HSSFCell cell = row.createCell(4);\r
+ cell.setCellComment(comment);\r
+\r
+ HSSFTestModelHelper.createCommentShape(0, comment);\r
+\r
+ assertNotNull(sh.findCellComment(5, 4));\r
+ assertNull(sh.findCellComment(5, 5));\r
+\r
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+ sh = wb.getSheetAt(0);\r
+\r
+ assertNotNull(sh.findCellComment(5, 4));\r
+ assertNull(sh.findCellComment(5, 5));\r
+ }\r
+\r
+ public void testInitState(){\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sh = wb.createSheet();\r
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+\r
+ HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+\r
+ HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor());\r
+\r
+ //5 properties of HSSFShape + 8 of HSSFTextbox\r
+ assertEquals(comment._optRecord.getEscherProperties().size(), 13);\r
+ }\r
+\r
+ public void testShapeId(){\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sh = wb.createSheet();\r
+ HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+\r
+ HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+\r
+ comment.setShapeId(2024);\r
+ /**\r
+ * SpRecord.id == shapeId\r
+ * ObjRecord.id == shapeId - 1024\r
+ * NoteRecord.id == ObjectRecord.id == shapeId - 1024\r
+ */\r
+\r
+ assertEquals(comment.getShapeId(), 2024);\r
+\r
+ CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) comment.getObjRecord().getSubRecords().get(0);\r
+ assertEquals(cod.getObjectId(), 1000);\r
+ EscherSpRecord spRecord = (EscherSpRecord) comment.getEscherContainer().getChild(0);\r
+ assertEquals(spRecord.getShapeId(), 2024);\r
+ assertEquals(comment.getShapeId(), 2024);\r
+ assertEquals(comment.getNoteRecord().getShapeId(), 1000);\r
+ }\r
+}\r
}
public void testDefaultShapeType() {
- HSSFComment comment = new HSSFComment((HSSFShape)null, (HSSFAnchor)null);
+ HSSFComment comment = new HSSFComment((HSSFShape)null, new HSSFClientAnchor());
assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment.getShapeType());
}