]> source.dussan.org Git - poi.git/commitdiff
implemented creating comments in existing files, bugfixes
authorEvgeniy Berlog <berlog@apache.org>
Thu, 28 Jun 2012 10:56:55 +0000 (10:56 +0000)
committerEvgeniy Berlog <berlog@apache.org>
Thu, 28 Jun 2012 10:56:55 +0000 (10:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/gsoc2012@1354910 13f79535-47bb-0310-9956-ffa450edef68

17 files changed:
src/java/org/apache/poi/ddf/AbstractEscherOptRecord.java
src/java/org/apache/poi/hssf/record/EscherAggregate.java
src/java/org/apache/poi/hssf/usermodel/HSSFComment.java
src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java
src/java/org/apache/poi/hssf/usermodel/HSSFPolygon.java
src/java/org/apache/poi/hssf/usermodel/HSSFShape.java
src/java/org/apache/poi/hssf/usermodel/HSSFShapeFactory.java
src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java
src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java
src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java
src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java
src/testcases/org/apache/poi/hssf/usermodel/TestComment.java [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
test-data/spreadsheet/drawings.xls

index a86fbda669bc4be8cce39c7fae657fa0a5bcbc27..dbf4a6dad23e555fe17721ea70b2458c11aef1ff 100644 (file)
 ==================================================================== */
 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;
@@ -139,6 +136,24 @@ public abstract class AbstractEscherOptRecord extends EscherRecord
         } );
     }
 
+    /**
+     * 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.
      */
index ee75870bb831e60c0836a82f531166c04bf129f3..b5bb2121a06bcd009f0ddd020680dee766638810 100644 (file)
@@ -1152,4 +1152,15 @@ public final class EscherAggregate extends AbstractEscherHolderRecord {
     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;
+    }
 }
index f949e59fb02509faae47498dfd1ab14b92455f69..23a792f365e6d2c7cf847ba5bf7f41c767787a20 100644 (file)
@@ -16,8 +16,8 @@
 ==================================================================== */
 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;
 
@@ -28,59 +28,89 @@ 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);
     }
 
     /**
@@ -89,7 +119,7 @@ public class HSSFComment extends HSSFTextbox implements Comment {
      * @return <code>true</code> if the comment is visible, <code>false</code> otherwise
      */
     public boolean isVisible() {
-        return _visible;
+        return _note.getFlags() == NoteRecord.NOTE_VISIBLE;
     }
 
     /**
@@ -98,7 +128,7 @@ public class HSSFComment extends HSSFTextbox implements Comment {
      * @return the 0-based row of the cell that contains the comment
      */
     public int getRow() {
-        return _row;
+        return _note.getRow();
     }
 
     /**
@@ -107,10 +137,7 @@ public class HSSFComment extends HSSFTextbox implements Comment {
      * @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);
     }
 
     /**
@@ -118,8 +145,8 @@ public class HSSFComment extends HSSFTextbox implements Comment {
      *
      * @return the 0-based column of the cell that contains the comment
      */
-    public int getColumn(){
-        return _col;
+    public int getColumn() {
+        return _note.getColumn();
     }
 
     /**
@@ -128,17 +155,15 @@ public class HSSFComment extends HSSFTextbox implements Comment {
      * @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);
     }
 
     /**
@@ -147,7 +172,7 @@ public class HSSFComment extends HSSFTextbox implements Comment {
      * @return the name of the original author of the comment
      */
     public String getAuthor() {
-        return _author;
+        return _note.getAuthor();
     }
 
     /**
@@ -155,37 +180,35 @@ public class HSSFComment extends HSSFTextbox implements Comment {
      *
      * @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;
+    }
 }
index 137086d9b8b50a71f9e63ce260fc8506e4abf0f4..bc3eebc1b374b4537211b13e90c11fa94105d43d 100644 (file)
@@ -163,6 +163,7 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
         HSSFComment shape = new HSSFComment(null, anchor);
         shape.anchor = anchor;
         addShape(shape);
+        onCreate(shape);
         return shape;
     }
 
index c6dc9d7f0aa9ebdae5775034e6435546a515b806..33f17d7e4808aa669d350432cdb9623d897318cf 100644 (file)
@@ -18,6 +18,7 @@
 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;
 
 /**
@@ -38,12 +39,18 @@ public class HSSFPolygon
 
     @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()
index 975453ad01a028d1722b9310119bf205f860f216..9c2775373f79f1f9efb6060db212fa9d7728b9be 100644 (file)
@@ -53,8 +53,8 @@ public abstract class HSSFShape {
     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) {
@@ -70,14 +70,11 @@ public abstract class HSSFShape {
     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();
@@ -90,6 +87,14 @@ public abstract class HSSFShape {
         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();
@@ -270,19 +275,7 @@ public abstract class HSSFShape {
     }
 
     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);
     }
 
     /**
index 5c1be44b4d8a92eed073dd139058460352c77892..5358f4868fd0f91a7c5270168fc8bf05e2ed8832 100644 (file)
@@ -127,6 +127,9 @@ public class HSSFShapeFactory {
                 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
index 96b39daceb889671a0dc6456e05ed6554e355154..d98471e33a3cc5dd749754362ff1578040fb8c82 100644 (file)
 
 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;
@@ -76,7 +72,13 @@ public class HSSFShapeGroup
 
     @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
index da7a404b28dcab3a4b5fe2d2d46f78a5cd105a67..cbfb1807d11312a6ac3d5f6b47981121823aa565 100644 (file)
@@ -2007,4 +2007,27 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
         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;
+    }
 }
index e7b2c718c68bb17c822439522b20726117fa5a86..2b64faaa5abef9348870490e48fc9b2eb067cdd7 100644 (file)
@@ -60,8 +60,6 @@ public class HSSFSimpleShape
     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 {
@@ -77,8 +75,6 @@ public class HSSFSimpleShape
     public HSSFSimpleShape( HSSFShape parent, HSSFAnchor anchor)
     {
         super( parent, anchor );
-        _escherContainer = createSpContainer();
-        _objRecord = createObjRecord();
         setShapeType(OBJECT_TYPE_LINE);
     }
 
@@ -96,8 +92,11 @@ public class HSSFSimpleShape
         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;
@@ -159,6 +158,6 @@ public class HSSFSimpleShape
             System.out.println("Unknown shape type: "+shapeType);
             return;
         }
-        spRecord.setShapeType(objTypeToShapeType.get((short)shapeType));
+        spRecord.setShapeType(objTypeToShapeType.get((short) shapeType));
     }
 }
index 00dfe9590cfb2fc71437134529145a1ca0858b8e..42385a87ed0de7ccac1efa67087bdef406463564 100644 (file)
@@ -47,11 +47,7 @@ public class HSSFTextbox extends HSSFSimpleShape {
     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);
@@ -113,15 +109,16 @@ public class HSSFTextbox extends HSSFSimpleShape {
         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);
index cca8f2d63572f82b8ec69bbddbffd26068435899..507561e7e4e7831ebf1ebc072ef1a54798e2c82c 100644 (file)
@@ -1,5 +1,6 @@
 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
@@ -10,4 +11,8 @@ public class HSSFTestModelHelper {
     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
index 1beeabe94bf3ec9e1e68bde63d0c40515a3ec048..4e120a7cb83b7e225c0fbd96e0006a44733e27a1 100644 (file)
@@ -17,7 +17,7 @@ import static junit.framework.Assert.assertEquals;
  * @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
@@ -33,7 +33,7 @@ public class TestDrawingShapes extends TestCase{
      * ----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
@@ -47,7 +47,7 @@ public class TestDrawingShapes extends TestCase{
     }\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
@@ -61,22 +61,22 @@ public class TestDrawingShapes extends TestCase{
 \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
@@ -89,7 +89,7 @@ public class TestDrawingShapes extends TestCase{
     /**\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
@@ -107,6 +107,7 @@ public class TestDrawingShapes extends TestCase{
         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
@@ -116,7 +117,7 @@ public class TestDrawingShapes extends TestCase{
         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
@@ -139,7 +140,7 @@ public class TestDrawingShapes extends TestCase{
         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
@@ -163,7 +164,7 @@ public class TestDrawingShapes extends TestCase{
         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
@@ -183,7 +184,7 @@ public class TestDrawingShapes extends TestCase{
         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
@@ -196,6 +197,9 @@ public class TestDrawingShapes extends TestCase{
         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
@@ -206,20 +210,19 @@ public class TestDrawingShapes extends TestCase{
         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
@@ -240,15 +243,15 @@ public class TestDrawingShapes extends TestCase{
         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
@@ -256,7 +259,7 @@ public class TestDrawingShapes extends TestCase{
      * 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
@@ -288,4 +291,37 @@ public class TestDrawingShapes extends TestCase{
         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
index 496139eaab1999b1a5b45a467f210c53d8332084..9b2273c59080a66ec63a8c65570b8c07f146b840 100644 (file)
@@ -16,6 +16,7 @@
 ==================================================================== */
 
 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;
@@ -47,4 +48,8 @@ public class HSSFTestHelper {
     public static int allocateNewShapeId(HSSFPatriarch patriarch){
         return patriarch.newShapeId();
     }
+
+    public static EscherOptRecord getOptRecord(HSSFShape shape){
+        return shape._optRecord;
+    }
 }
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestComment.java b/src/testcases/org/apache/poi/hssf/usermodel/TestComment.java
new file mode 100644 (file)
index 0000000..91b1a7c
--- /dev/null
@@ -0,0 +1,241 @@
+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
index 7d04110b12ebde51c2323b0b4d1b5e7a3c977110..b49f725baaa704ab9ecc2ead6897406d229497fd 100644 (file)
@@ -32,7 +32,7 @@ public final class TestHSSFComment extends BaseTestCellComment {
     }
 
     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());
     }
 
index 8ea863f59a6eb022c8a5cfa3946adec55f443e46..24684cb6ccc0880829ab8de111fe6263a07ebcad 100644 (file)
Binary files a/test-data/spreadsheet/drawings.xls and b/test-data/spreadsheet/drawings.xls differ