]> source.dussan.org Git - poi.git/commitdiff
implemented creating shapes in existing files
authorEvgeniy Berlog <berlog@apache.org>
Tue, 26 Jun 2012 11:21:13 +0000 (11:21 +0000)
committerEvgeniy Berlog <berlog@apache.org>
Tue, 26 Jun 2012 11:21:13 +0000 (11:21 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/gsoc2012@1353960 13f79535-47bb-0310-9956-ffa450edef68

17 files changed:
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/HSSFSimpleShape.java
src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java
src/java/org/apache/poi/hssf/usermodel/HSSFUnknownShape.java
src/java/org/apache/poi/hssf/usermodel/drawing/HSSFShapeType.java
src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java [new file with mode: 0644]
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/TestText.java [new file with mode: 0644]
test-data/spreadsheet/drawings.xls
test-data/spreadsheet/empty.xls [new file with mode: 0644]

index fb16037a378fd692a9e1fd01e4cb783aed9ea96d..ee75870bb831e60c0836a82f531166c04bf129f3 100644 (file)
@@ -323,6 +323,10 @@ public final class EscherAggregate extends AbstractEscherHolderRecord {
         this.drawingManager = drawingManager;
     }
 
+    public DrawingManager2 getDrawingManager() {
+        return drawingManager;
+    }
+
     /**
      * @return Returns the current sid.
      */
@@ -628,7 +632,7 @@ public final class EscherAggregate extends AbstractEscherHolderRecord {
     /**
      * Associates an escher record to an OBJ record or a TXO record.
      */
-    public Object associateShapeToObjRecord(EscherRecord r, ObjRecord objRecord) {
+    public Object associateShapeToObjRecord(EscherRecord r, Record objRecord) {
         return shapeToObj.put(r, objRecord);
     }
 
@@ -1039,9 +1043,9 @@ public final class EscherAggregate extends AbstractEscherHolderRecord {
         EscherDgRecord dg = new EscherDgRecord();
         dg.setRecordId( EscherDgRecord.RECORD_ID );
         short dgId = 1;
-        dg.setOptions( (short) ( dgId << 4 ) );
-        dg.setNumShapes( 1 );
-        dg.setLastMSOSPID( 1024 );
+        dg.setOptions((short) (dgId << 4));
+        dg.setNumShapes(1);
+        dg.setLastMSOSPID(1024);
         drawingGroupId = dg.getDrawingGroupId();
         spgrContainer.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
         spgrContainer.setOptions((short) 0x000F);
index 2216971e68d437a38792ace97fb5c81282c04a32..f949e59fb02509faae47498dfd1ab14b92455f69 100644 (file)
@@ -66,7 +66,7 @@ public class HSSFComment extends HSSFTextbox implements Comment {
     }
 
     protected HSSFComment(NoteRecord note, TextObjectRecord txo) {
-        this((HSSFShape) null, (HSSFAnchor) null);
+        this(null, new HSSFClientAnchor());
         _txo = txo;
         _note = note;
     }
@@ -185,7 +185,7 @@ public class HSSFComment extends HSSFTextbox implements Comment {
     /**
      * Returns the underlying Text record
      */
-    protected TextObjectRecord getTextObjectRecord() {
+    public TextObjectRecord getTextObjectRecord() {
            return _txo;
        }
 }
index 71549bbf312ea7782b70f63f9fe123029da0d185..137086d9b8b50a71f9e63ce260fc8506e4abf0f4 100644 (file)
@@ -21,12 +21,8 @@ import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.poi.ddf.EscherComplexProperty;
-import org.apache.poi.ddf.EscherContainerRecord;
-import org.apache.poi.ddf.EscherOptRecord;
-import org.apache.poi.ddf.EscherProperty;
-import org.apache.poi.ddf.EscherBSERecord;
-import org.apache.poi.ddf.EscherSpgrRecord;
+import org.apache.poi.ddf.*;
+import org.apache.poi.hssf.model.DrawingManager2;
 import org.apache.poi.hssf.record.EscherAggregate;
 import org.apache.poi.ss.usermodel.Chart;
 import org.apache.poi.util.StringUtil;
@@ -93,6 +89,8 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
         HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
         shape.anchor = anchor;
         addShape(shape);
+        //open existing file
+        onCreate(shape);
         return shape;
     }
 
@@ -109,6 +107,8 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
         shape.setPictureIndex( pictureIndex );
         shape.anchor = anchor;
         addShape(shape);
+        //open existing file
+        onCreate(shape);
 
         EscherBSERecord bse = _sheet.getWorkbook().getWorkbook().getBSERecord(pictureIndex);
         bse.setRef(bse.getRef() + 1);
@@ -147,6 +147,7 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
         HSSFTextbox shape = new HSSFTextbox(null, anchor);
         shape.anchor = anchor;
         addShape(shape);
+        onCreate(shape);
         return shape;
     }
 
@@ -200,6 +201,20 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
         _shapes.add(shape);
     }
 
+    private void onCreate(HSSFShape shape){
+        if(_boundAggregate.getPatriarch() == null){
+            EscherContainerRecord spgrContainer =
+                    _boundAggregate.getEscherContainer().getChildContainers().get(0);
+
+            EscherContainerRecord spContainer = shape.getEscherContainer();
+            int shapeId = newShapeId();
+            shape.setShapeId(shapeId);
+
+            spgrContainer.addChildRecord(spContainer);
+            shape.afterInsert(this);
+        }
+    }
+
     /**
      * Total count of all children and their children's children.
      */
@@ -222,6 +237,17 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
         _y2 = y2;
     }
 
+    int newShapeId() {
+        if (_boundAggregate.getEscherContainer() == null){
+            throw new IllegalStateException("We can use this method for only existing files");
+        }
+        DrawingManager2 dm = _boundAggregate.getDrawingManager();
+        EscherDgRecord dg =
+                _boundAggregate.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);
+        short drawingGroupId = dg.getDrawingGroupId();
+        return dm.allocateShapeId(drawingGroupId, dg);
+    }
+
     /**
      * Does this HSSFPatriarch contain a chart?
      * (Technically a reference to a chart, since they
index 6027fb8be78bc3d2ce95747c07a0885144862917..c6dc9d7f0aa9ebdae5775034e6435546a515b806 100644 (file)
@@ -17,6 +17,9 @@
 
 package org.apache.poi.hssf.usermodel;
 
+import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.hssf.record.ObjRecord;
+
 /**
  * @author Glen Stampoultzis  (glens at superlinksoftware.com)
  */
@@ -33,6 +36,16 @@ public class HSSFPolygon
         super( parent, anchor );
     }
 
+    @Override
+    protected EscherContainerRecord createSpContainer() {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    protected ObjRecord createObjRecord() {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
     public int[] getXPoints()
     {
         return xPoints;
index 33dfd68643e9541df86a28dbabd3c23c850f390f..975453ad01a028d1722b9310119bf205f860f216 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.hssf.usermodel;
 
 import org.apache.poi.ddf.*;
+import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
 import org.apache.poi.hssf.record.ObjRecord;
 
 /**
@@ -71,6 +72,7 @@ public abstract class HSSFShape {
         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));
@@ -78,6 +80,24 @@ public abstract class HSSFShape {
         _optRecord.addEscherProperty(new EscherBoolProperty(EscherProperties.FILL__NOFILLHITTEST, 0x0));
     }
 
+    protected abstract EscherContainerRecord createSpContainer();
+
+    protected abstract ObjRecord createObjRecord();
+
+    void setShapeId(int shapeId){
+        EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
+        spRecord.setShapeId(shapeId);
+        CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) _objRecord.getSubRecords().get(0);
+        cod.setObjectId((short) (shapeId-1024));
+    }
+    
+    int getShapeId(){
+        return ((EscherSpRecord)_escherContainer.getChildById(EscherSpRecord.RECORD_ID)).getShapeId();
+    }
+
+    void afterInsert(HSSFPatriarch patriarch){
+    }
+
     public EscherContainerRecord getEscherContainer() {
         return _escherContainer;
     }
index 68f410d173d6530abc0a1c473a0d6f3c90d18222..5c1be44b4d8a92eed073dd139058460352c77892 100644 (file)
@@ -115,19 +115,37 @@ public class HSSFShapeFactory {
                         break;\r
                 }\r
             }\r
-            if (null != objRecord){\r
-                HSSFShape shape = shapeCreator.createNewShape(spRecord.getShapeType(), container, objRecord);\r
-                out.addShape(shape);\r
+            CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) objRecord.getSubRecords().get(0);\r
+            HSSFShape shape = null;\r
+            switch (cmo.getObjectType()) {\r
+                case CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE:\r
+                    shape = new HSSFPicture(container, objRecord);\r
+                    break;\r
+                case CommonObjectDataSubRecord.OBJECT_TYPE_RECTANGLE:\r
+                    shape = new HSSFSimpleShape(container, objRecord);\r
+                    break;\r
+                case CommonObjectDataSubRecord.OBJECT_TYPE_TEXT:\r
+                    shape = new HSSFTextbox(container, objRecord, txtRecord);\r
+                    break;\r
+                default:\r
+                    shape = new HSSFSimpleShape(container, objRecord);\r
             }\r
-            if (null != txtRecord){\r
-                //TODO resolve textbox\r
-//                TextboxShape shape = new TextboxShape(container, txtRecord);\r
-//                out.a\r
+            if (null != shape){\r
+                out.addShape(shape);\r
             }\r
-//\r
-//            //TODO decide what shape to create based on ObjRecord / EscherSpRecord\r
-//            HSSFShape shape = new HSSFUnknownShape(container, objRecord);\r
-//            out.addShape(shape);\r
+//            if (null != objRecord){\r
+//                HSSFShape shape = shapeCreator.createNewShape(spRecord.getShapeType(), container, objRecord);\r
+//                out.addShape(shape);\r
+//            }\r
+//            if (null != txtRecord){\r
+//                //TODO resolve textbox\r
+////                TextboxShape shape = new TextboxShape(container, txtRecord);\r
+////                out.a\r
+//            }\r
+////\r
+////            //TODO decide what shape to create based on ObjRecord / EscherSpRecord\r
+////            HSSFShape shape = new HSSFUnknownShape(container, objRecord);\r
+////            out.addShape(shape);\r
         }\r
     }\r
 }\r
index b3209467aefb0ec41d4051a1cdc28c69907ea0e5..96b39daceb889671a0dc6456e05ed6554e355154 100644 (file)
@@ -74,6 +74,16 @@ public class HSSFShapeGroup
         _spgrRecord.setRectY2(255);
     }
 
+    @Override
+    protected EscherContainerRecord createSpContainer() {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    protected ObjRecord createObjRecord() {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
     /**
      * Create another group under this group.
      * @param anchor    the position of the new group.
index 6e0df0fd540b7c280beb5d100fc8890d892936fc..e7b2c718c68bb17c822439522b20726117fa5a86 100644 (file)
@@ -19,7 +19,13 @@ package org.apache.poi.hssf.usermodel;
 
 import org.apache.poi.ddf.*;
 import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
+import org.apache.poi.hssf.record.EndSubRecord;
+import org.apache.poi.hssf.record.EscherAggregate;
 import org.apache.poi.hssf.record.ObjRecord;
+import org.apache.poi.hssf.usermodel.drawing.HSSFShapeType;
+
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * Represents a simple shape such as a line, rectangle or oval.
@@ -56,17 +62,69 @@ public class HSSFSimpleShape
 
     int shapeType = OBJECT_TYPE_LINE;
 
+    private static final Map <Short, Short> objTypeToShapeType = new HashMap<Short, Short>();
+
+    static {
+        objTypeToShapeType.put(OBJECT_TYPE_RECTANGLE, HSSFShapeType.RECTANGLE.getType());
+        objTypeToShapeType.put(OBJECT_TYPE_PICTURE, HSSFShapeType.PICTURE.getType());
+        objTypeToShapeType.put(OBJECT_TYPE_LINE, HSSFShapeType.LINE.getType());
+    }
+
     public HSSFSimpleShape(EscherContainerRecord spContainer, ObjRecord objRecord) {
         super(spContainer, objRecord);
-        CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) objRecord.getSubRecords().get(0);
-        setShapeType(cod.getObjectType());
     }
 
     public HSSFSimpleShape( HSSFShape parent, HSSFAnchor anchor)
     {
         super( parent, anchor );
+        _escherContainer = createSpContainer();
+        _objRecord = createObjRecord();
+        setShapeType(OBJECT_TYPE_LINE);
+    }
+
+    @Override
+    protected EscherContainerRecord createSpContainer() {
+        EscherContainerRecord spContainer = new EscherContainerRecord();
+        spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
+        spContainer.setOptions( (short) 0x000F );
+
+        EscherSpRecord sp = new EscherSpRecord();
+        sp.setRecordId( EscherSpRecord.RECORD_ID );
+        sp.setFlags( EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE );
+
+        EscherClientDataRecord clientData = new EscherClientDataRecord();
+        clientData.setRecordId( EscherClientDataRecord.RECORD_ID );
+        clientData.setOptions( (short) 0x0000 );
+
+        spContainer.addChildRecord(sp);
+        spContainer.addChildRecord(_optRecord);
+        spContainer.addChildRecord(anchor.getEscherAnchor());
+        spContainer.addChildRecord(clientData);
+        return spContainer;
     }
 
+    @Override
+    protected ObjRecord createObjRecord() {
+        ObjRecord obj = new ObjRecord();
+        CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
+        c.setLocked(true);
+        c.setPrintable(true);
+        c.setAutofill(true);
+        c.setAutoline(true);
+        EndSubRecord e = new EndSubRecord();
+
+        obj.addSubRecord(c);
+        obj.addSubRecord(e);
+        return obj;
+    }
+
+    @Override
+    void afterInsert(HSSFPatriarch patriarch){
+        EscherAggregate agg = patriarch._getBoundAggregate();
+        agg.associateShapeToObjRecord(_escherContainer.getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
+    }
+
+
     /**
      * Gets the shape type.
      * @return  One of the OBJECT_TYPE_* constants.
@@ -77,7 +135,10 @@ public class HSSFSimpleShape
      * @see #OBJECT_TYPE_PICTURE
      * @see #OBJECT_TYPE_COMMENT
      */
-    public int getShapeType() { return shapeType; }
+    public int getShapeType() {
+        CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) _objRecord.getSubRecords().get(0);
+        return cod.getObjectType();
+    }
 
     /**
      * Sets the shape types.
@@ -90,6 +151,14 @@ public class HSSFSimpleShape
      * @see #OBJECT_TYPE_PICTURE
      * @see #OBJECT_TYPE_COMMENT
      */
-    public void setShapeType( int shapeType ){ this.shapeType = shapeType; }
-
+    public void setShapeType( int shapeType ){
+        CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) _objRecord.getSubRecords().get(0);
+        cod.setObjectType((short) shapeType);
+        EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
+        if (null == objTypeToShapeType.get((short)shapeType)){
+            System.out.println("Unknown shape type: "+shapeType);
+            return;
+        }
+        spRecord.setShapeType(objTypeToShapeType.get((short)shapeType));
+    }
 }
index 2548bf5114065070da4d3606070b587f2d49340e..00dfe9590cfb2fc71437134529145a1ca0858b8e 100644 (file)
@@ -17,6 +17,8 @@
 
 package org.apache.poi.hssf.usermodel;
 
+import org.apache.poi.ddf.*;
+import org.apache.poi.hssf.record.*;
 import org.apache.poi.ss.usermodel.RichTextString;
 
 /**
@@ -24,163 +26,233 @@ import org.apache.poi.ss.usermodel.RichTextString;
  *
  * @author Glen Stampoultzis (glens at apache.org)
  */
-public class HSSFTextbox
-        extends HSSFSimpleShape
-{
-    public final static short       OBJECT_TYPE_TEXT               = 6;
+public class HSSFTextbox extends HSSFSimpleShape {
+    public final static short OBJECT_TYPE_TEXT = 6;
 
     /**
      * How to align text horizontally
      */
-    public final static short  HORIZONTAL_ALIGNMENT_LEFT = 1;
-    public final static short  HORIZONTAL_ALIGNMENT_CENTERED = 2;
-    public final static short  HORIZONTAL_ALIGNMENT_RIGHT = 3;
-    public final static short  HORIZONTAL_ALIGNMENT_JUSTIFIED = 4;
-    public final static short  HORIZONTAL_ALIGNMENT_DISTRIBUTED = 7;
+    public final static short HORIZONTAL_ALIGNMENT_LEFT = 1;
+    public final static short HORIZONTAL_ALIGNMENT_CENTERED = 2;
+    public final static short HORIZONTAL_ALIGNMENT_RIGHT = 3;
+    public final static short HORIZONTAL_ALIGNMENT_JUSTIFIED = 4;
+    public final static short HORIZONTAL_ALIGNMENT_DISTRIBUTED = 7;
 
     /**
      * How to align text vertically
      */
-    public final static short  VERTICAL_ALIGNMENT_TOP    = 1;
-    public final static short  VERTICAL_ALIGNMENT_CENTER = 2;
-    public final static short  VERTICAL_ALIGNMENT_BOTTOM = 3;
-    public final static short  VERTICAL_ALIGNMENT_JUSTIFY = 4;
-    public final static short  VERTICAL_ALIGNMENT_DISTRIBUTED= 7;
+    public final static short VERTICAL_ALIGNMENT_TOP = 1;
+    public final static short VERTICAL_ALIGNMENT_CENTER = 2;
+    public final static short VERTICAL_ALIGNMENT_BOTTOM = 3;
+    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;
+
+    public HSSFTextbox(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord textObjectRecord) {
+        super(spContainer, objRecord);
+        this._textObjectRecord = textObjectRecord;
+    }
+
     HSSFRichTextString string = new HSSFRichTextString("");
 
     /**
      * Construct a new textbox with the given parent and anchor.
+     *
      * @param parent
-     * @param anchor  One of HSSFClientAnchor or HSSFChildAnchor
+     * @param anchor One of HSSFClientAnchor or HSSFChildAnchor
      */
-    public HSSFTextbox( HSSFShape parent, HSSFAnchor anchor )
-    {
-        super( parent, anchor );
-        setShapeType(OBJECT_TYPE_TEXT);
+    public HSSFTextbox(HSSFShape parent, HSSFAnchor anchor) {
+        super(parent, anchor);
+        _textObjectRecord = createTextObjRecord();
+        setHorizontalAlignment(HORIZONTAL_ALIGNMENT_LEFT);
+        setVerticalAlignment(VERTICAL_ALIGNMENT_TOP);
+        setString(new HSSFRichTextString(""));
+
+    }
+
+    protected TextObjectRecord createTextObjRecord(){
+        TextObjectRecord obj = new TextObjectRecord();
+        obj.setTextLocked(true);
+        obj.setTextOrientation(TextObjectRecord.TEXT_ORIENTATION_NONE);
+        return obj;
+    }
+
+    @Override
+    protected ObjRecord createObjRecord() {
+        ObjRecord obj = new ObjRecord();
+        CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
+        c.setObjectType(OBJECT_TYPE_TEXT);
+        c.setLocked( true );
+        c.setPrintable( true );
+        c.setAutofill( true );
+        c.setAutoline( true );
+        EndSubRecord e = new EndSubRecord();
+        obj.addSubRecord( c );
+        obj.addSubRecord( e );
+        return obj;
+    }
+
+    @Override
+    protected EscherContainerRecord createSpContainer() {
+        EscherContainerRecord spContainer = new EscherContainerRecord();
+        EscherSpRecord sp = new EscherSpRecord();
+        EscherOptRecord opt = new EscherOptRecord();
+        EscherClientDataRecord clientData = new EscherClientDataRecord();
+        EscherTextboxRecord escherTextbox = new EscherTextboxRecord();
+
+        spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
+        spContainer.setOptions((short) 0x000F);
+        sp.setRecordId(EscherSpRecord.RECORD_ID);
+        sp.setOptions((short) ((EscherAggregate.ST_TEXTBOX << 4) | 0x2));
 
-        halign = HORIZONTAL_ALIGNMENT_LEFT;
-        valign = VERTICAL_ALIGNMENT_TOP;
+        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));
+
+        EscherRecord anchor = getAnchor().getEscherAnchor();
+        clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
+        clientData.setOptions((short) 0x0000);
+        escherTextbox.setRecordId(EscherTextboxRecord.RECORD_ID);
+        escherTextbox.setOptions((short) 0x0000);
+
+        spContainer.addChildRecord(sp);
+        spContainer.addChildRecord(opt);
+        spContainer.addChildRecord(anchor);
+        spContainer.addChildRecord(clientData);
+        spContainer.addChildRecord(escherTextbox);
+
+        return spContainer;
+    }
+
+    @Override
+    void afterInsert(HSSFPatriarch patriarch){
+        EscherAggregate agg = patriarch._getBoundAggregate();
+        agg.associateShapeToObjRecord(_escherContainer.getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
+        agg.associateShapeToObjRecord(_escherContainer.getChildById(EscherTextboxRecord.RECORD_ID), getTextObjectRecord());
     }
 
     /**
-     * @return  the rich text string for this textbox.
+     * @return the rich text string for this textbox.
      */
-    public HSSFRichTextString getString()
-    {
-        return string;
+    public HSSFRichTextString getString() {
+        return _textObjectRecord.getStr();
     }
 
     /**
-     * @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 rtr = (HSSFRichTextString)string;
-
+    public void setString(RichTextString string) {
+        HSSFRichTextString rtr = (HSSFRichTextString) string;
         // If font is not set we must set the default one
-        if (rtr.numFormattingRuns() == 0) rtr.applyFont((short)0);
-
-        this.string = rtr;
+        if (rtr.numFormattingRuns() == 0) rtr.applyFont((short) 0);
+        _textObjectRecord.setStr(rtr);
     }
 
     /**
-     * @return  Returns the left margin within the textbox.
+     * @return Returns the left margin within the textbox.
      */
-    public int getMarginLeft()
-    {
-        return marginLeft;
+    public int getMarginLeft() {
+        EscherSimpleProperty property = _optRecord.lookup(EscherProperties.TEXT__TEXTLEFT);
+        return property == null ? 0: property.getPropertyValue();
     }
 
     /**
      * Sets the left margin within the textbox.
      */
-    public void setMarginLeft( int marginLeft )
-    {
-        this.marginLeft = marginLeft;
+    public void setMarginLeft(int marginLeft) {
+        setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTLEFT, marginLeft));
     }
 
     /**
-     * @return    returns the right margin within the textbox.
+     * @return returns the right margin within the textbox.
      */
-    public int getMarginRight()
-    {
-        return marginRight;
+    public int getMarginRight() {
+        EscherSimpleProperty property = _optRecord.lookup(EscherProperties.TEXT__TEXTRIGHT);
+        return property == null ? 0: property.getPropertyValue();
     }
 
     /**
      * Sets the right margin within the textbox.
      */
-    public void setMarginRight( int marginRight )
-    {
-        this.marginRight = marginRight;
+    public void setMarginRight(int marginRight) {
+        setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTRIGHT, marginRight));
     }
 
     /**
-     * @return  returns the top margin within the textbox.
+     * @return returns the top margin within the textbox.
      */
-    public int getMarginTop()
-    {
-        return marginTop;
+    public int getMarginTop() {
+        EscherSimpleProperty property = _optRecord.lookup(EscherProperties.TEXT__TEXTTOP);
+        return property == null ? 0: property.getPropertyValue();
     }
 
     /**
      * Sets the top margin within the textbox.
      */
-    public void setMarginTop( int marginTop )
-    {
-        this.marginTop = marginTop;
+    public void setMarginTop(int marginTop) {
+        setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTTOP, marginTop));
     }
 
     /**
      * Gets the bottom margin within the textbox.
      */
-    public int getMarginBottom()
-    {
-        return marginBottom;
+    public int getMarginBottom() {
+        EscherSimpleProperty property = _optRecord.lookup(EscherProperties.TEXT__TEXTBOTTOM);
+        return property == null ? 0: property.getPropertyValue();
     }
 
     /**
      * Sets the bottom margin within the textbox.
      */
-    public void setMarginBottom( int marginBottom )
-    {
-        this.marginBottom = marginBottom;
+    public void setMarginBottom(int marginBottom) {
+        setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTBOTTOM, marginBottom));
     }
 
     /**
      * Gets the horizontal alignment.
      */
-    public short getHorizontalAlignment()
-    {
-        return halign;
+    public short getHorizontalAlignment() {
+        return (short) _textObjectRecord.getHorizontalTextAlignment();
     }
 
     /**
      * Sets the horizontal alignment.
      */
-    public void setHorizontalAlignment( short align )
-    {
-        this.halign = align;
+    public void setHorizontalAlignment(short align) {
+        _textObjectRecord.setHorizontalTextAlignment(align);
     }
 
     /**
      * Gets the vertical alignment.
      */
-    public short getVerticalAlignment()
-    {
-        return valign;
+    public short getVerticalAlignment() {
+        return (short) _textObjectRecord.getVerticalTextAlignment();
     }
 
     /**
      * Sets the vertical alignment.
      */
-    public void setVerticalAlignment( short align )
-    {
-        this.valign = align;
+    public void setVerticalAlignment(short align) {
+        _textObjectRecord.setVerticalTextAlignment(align);
     }
+
+    public TextObjectRecord getTextObjectRecord() {
+        return _textObjectRecord;
+    }
+
+    @Override
+    public void setShapeType(int shapeType) {/**DO NOTHING**/}
 }
index d4cac9be9a5ab5b70fef85ffddaaaa4ed0988fd0..406dfe721988f3aef62049cb5cda07ffa4028d6c 100644 (file)
@@ -30,4 +30,14 @@ public class HSSFUnknownShape extends HSSFShape {
     public HSSFUnknownShape(EscherRecord spContainer, ObjRecord objRecord) {\r
         super((EscherContainerRecord) spContainer, objRecord);\r
     }\r
+\r
+    @Override\r
+    protected EscherContainerRecord createSpContainer() {\r
+        return null;  //To change body of implemented methods use File | Settings | File Templates.\r
+    }\r
+\r
+    @Override\r
+    protected ObjRecord createObjRecord() {\r
+        return null;  //To change body of implemented methods use File | Settings | File Templates.\r
+    }\r
 }\r
index c63a08b7e5564c3c3e5ab1a8380377245ee80884..2e2b64eeb49c27e7d01bfb8add06f2f6684f62d6 100644 (file)
@@ -2,6 +2,7 @@ package org.apache.poi.hssf.usermodel.drawing;
 \r
 import org.apache.poi.hssf.usermodel.HSSFPicture;\r
 import org.apache.poi.hssf.usermodel.HSSFSimpleShape;\r
+import org.apache.poi.hssf.usermodel.HSSFTextbox;\r
 \r
 /**\r
  * @author Evgeniy Berlog\r
@@ -11,16 +12,18 @@ public enum HSSFShapeType {
     NOT_PRIMITIVE((short)0x0, null, (short)0),\r
     RECTANGLE((short)0x1, HSSFSimpleShape.class, HSSFSimpleShape.OBJECT_TYPE_RECTANGLE),\r
     PICTURE((short)0x004B, HSSFPicture.class, HSSFSimpleShape.OBJECT_TYPE_PICTURE),\r
+    LINE((short)0x14, HSSFSimpleShape.class, HSSFSimpleShape.OBJECT_TYPE_LINE),\r
+    TEXT((short)202, HSSFTextbox.class, HSSFTextbox.OBJECT_TYPE_TEXT),\r
     ROUND_RECTANGLE((short)0x2, null, null);\r
 \r
     private Short type;\r
     private Class shape;\r
-    private Short objectId;\r
+    private Short objectType;\r
 \r
-    private HSSFShapeType(Short type, Class shape, Short objectId) {\r
+    private HSSFShapeType(Short type, Class shape, Short objectType) {\r
         this.type = type;\r
         this.shape = shape;\r
-        this.objectId = objectId;\r
+        this.objectType = objectType;\r
     }\r
 \r
     public Short getType() {\r
@@ -30,4 +33,8 @@ public enum HSSFShapeType {
     public Class getShape() {\r
         return shape;\r
     }\r
+\r
+    public Short getObjectType() {\r
+        return objectType;\r
+    }\r
 }\r
diff --git a/src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java b/src/testcases/org/apache/poi/hssf/model/HSSFTestModelHelper.java
new file mode 100644 (file)
index 0000000..cca8f2d
--- /dev/null
@@ -0,0 +1,13 @@
+package org.apache.poi.hssf.model;\r
+\r
+import org.apache.poi.hssf.usermodel.HSSFTextbox;\r
+\r
+/**\r
+ * @author Evgeniy Berlog\r
+ * @date 25.06.12\r
+ */\r
+public class HSSFTestModelHelper {\r
+    public static TextboxShape createTextboxShape(int shapeId, HSSFTextbox textbox){\r
+        return new TextboxShape(textbox, shapeId);\r
+    }\r
+}\r
index 097d5d01ee4cba82d6ef5376e076a03798a2d353..1beeabe94bf3ec9e1e68bde63d0c40515a3ec048 100644 (file)
@@ -251,4 +251,41 @@ public class TestDrawingShapes extends TestCase{
                 ((EscherContainerRecord)spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID);\r
         assertEquals(1026, sp2.getShapeId());\r
     }\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
+        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("empty.xls");\r
+        HSSFSheet sheet = wb.getSheetAt(0);\r
+        HSSFPatriarch patriarch = sheet.getDrawingPatriarch();\r
+\r
+        /**\r
+         * 2048 - main SpContainer id\r
+         * 2049 - existing shape id\r
+         */\r
+        assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 2050);\r
+        assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 2051);\r
+        assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 2052);\r
+\r
+        sheet = wb.getSheetAt(1);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        /**\r
+         * 3072 - main SpContainer id\r
+         * 3073 - existing shape id\r
+         */\r
+        assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 3074);\r
+        assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 3075);\r
+        assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 3076);\r
+\r
+\r
+        sheet = wb.getSheetAt(2);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1026);\r
+        assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1027);\r
+        assertEquals(HSSFTestHelper.allocateNewShapeId(patriarch), 1028);\r
+    }\r
 }\r
index 2d6b14b79a0c42a4ebce751b71972cbdf288399b..496139eaab1999b1a5b45a467f210c53d8332084 100644 (file)
@@ -43,4 +43,8 @@ public class HSSFTestHelper {
     public static EscherAggregate getEscherAggregate(HSSFPatriarch patriarch){
         return patriarch._getBoundAggregate();
     }
+
+    public static int allocateNewShapeId(HSSFPatriarch patriarch){
+        return patriarch.newShapeId();
+    }
 }
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestText.java b/src/testcases/org/apache/poi/hssf/usermodel/TestText.java
new file mode 100644 (file)
index 0000000..213c5be
--- /dev/null
@@ -0,0 +1,186 @@
+package org.apache.poi.hssf.usermodel;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.hssf.HSSFTestDataSamples;\r
+import org.apache.poi.hssf.model.HSSFTestModelHelper;\r
+import org.apache.poi.hssf.model.TextboxShape;\r
+import org.apache.poi.hssf.record.CommonObjectDataSubRecord;\r
+import org.apache.poi.hssf.record.ObjRecord;\r
+import org.apache.poi.hssf.record.TextObjectRecord;\r
+\r
+import java.util.Arrays;\r
+\r
+/**\r
+ * @author Evgeniy Berlog\r
+ * @date 25.06.12\r
+ */\r
+public class TestText extends TestCase {\r
+\r
+    public void testResultEqualsToAbstractShape() {\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        HSSFSheet sh = wb.createSheet();\r
+        HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+        HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());\r
+        TextboxShape textboxShape = HSSFTestModelHelper.createTextboxShape(0, textbox);\r
+\r
+        assertEquals(textbox.getEscherContainer().getChildRecords().size(), 5);\r
+        assertEquals(textboxShape.getSpContainer().getChildRecords().size(), 5);\r
+\r
+        //sp record\r
+        byte[] expected = textboxShape.getSpContainer().getChild(0).serialize();\r
+        byte[] actual = textbox.getEscherContainer().getChild(0).serialize();\r
+\r
+        assertEquals(expected.length, actual.length);\r
+        assertTrue(Arrays.equals(expected, actual));\r
+\r
+        expected = textboxShape.getSpContainer().getChild(2).serialize();\r
+        actual = textbox.getEscherContainer().getChild(2).serialize();\r
+\r
+        assertEquals(expected.length, actual.length);\r
+        assertTrue(Arrays.equals(expected, actual));\r
+\r
+        expected = textboxShape.getSpContainer().getChild(3).serialize();\r
+        actual = textbox.getEscherContainer().getChild(3).serialize();\r
+\r
+        assertEquals(expected.length, actual.length);\r
+        assertTrue(Arrays.equals(expected, actual));\r
+\r
+        expected = textboxShape.getSpContainer().getChild(4).serialize();\r
+        actual = textbox.getEscherContainer().getChild(4).serialize();\r
+\r
+        assertEquals(expected.length, actual.length);\r
+        assertTrue(Arrays.equals(expected, actual));\r
+\r
+        ObjRecord obj = textbox.getObjRecord();\r
+        ((CommonObjectDataSubRecord) obj.getSubRecords().get(0)).setObjectId(-1024);\r
+        ObjRecord objShape = textboxShape.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 = textbox.getTextObjectRecord();\r
+        TextObjectRecord torShape = textboxShape.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
+\r
+    public void testAddTextToExistingFile() {\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        HSSFSheet sh = wb.createSheet();\r
+        HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+        HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());\r
+        textbox.setString(new HSSFRichTextString("just for test"));\r
+        HSSFTextbox textbox2 = patriarch.createTextbox(new HSSFClientAnchor());\r
+        textbox2.setString(new HSSFRichTextString("just for test2"));\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
+        HSSFTextbox text3 = patriarch.createTextbox(new HSSFClientAnchor());\r
+        text3.setString(new HSSFRichTextString("text3"));\r
+        assertEquals(patriarch.getChildren().size(), 3);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sh = wb.getSheetAt(0);\r
+        patriarch = sh.getDrawingPatriarch();\r
+\r
+        assertEquals(patriarch.getChildren().size(), 3);\r
+        assertEquals(((HSSFTextbox) patriarch.getChildren().get(0)).getString().getString(), "just for test");\r
+        assertEquals(((HSSFTextbox) patriarch.getChildren().get(1)).getString().getString(), "just for test2");\r
+        assertEquals(((HSSFTextbox) patriarch.getChildren().get(2)).getString().getString(), "text3");\r
+    }\r
+\r
+    public void testSetGetProperties() {\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        HSSFSheet sh = wb.createSheet();\r
+        HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+        HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());\r
+        textbox.setString(new HSSFRichTextString("test"));\r
+        assertEquals(textbox.getString().getString(), "test");\r
+\r
+        textbox.setHorizontalAlignment((short) 5);\r
+        assertEquals(textbox.getHorizontalAlignment(), 5);\r
+\r
+        textbox.setVerticalAlignment((short) 6);\r
+        assertEquals(textbox.getVerticalAlignment(), (short) 6);\r
+\r
+        textbox.setMarginBottom(7);\r
+        assertEquals(textbox.getMarginBottom(), 7);\r
+\r
+        textbox.setMarginLeft(8);\r
+        assertEquals(textbox.getMarginLeft(), 8);\r
+\r
+        textbox.setMarginRight(9);\r
+        assertEquals(textbox.getMarginRight(), 9);\r
+\r
+        textbox.setMarginTop(10);\r
+        assertEquals(textbox.getMarginTop(), 10);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sh = wb.getSheetAt(0);\r
+        patriarch = sh.getDrawingPatriarch();\r
+        textbox = (HSSFTextbox) patriarch.getChildren().get(0);\r
+        assertEquals(textbox.getString().getString(), "test");\r
+        assertEquals(textbox.getHorizontalAlignment(), 5);\r
+        assertEquals(textbox.getVerticalAlignment(), (short) 6);\r
+        assertEquals(textbox.getMarginBottom(), 7);\r
+        assertEquals(textbox.getMarginLeft(), 8);\r
+        assertEquals(textbox.getMarginRight(), 9);\r
+        assertEquals(textbox.getMarginTop(), 10);\r
+\r
+        textbox.setString(new HSSFRichTextString("test1"));\r
+        textbox.setHorizontalAlignment(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED);\r
+        textbox.setVerticalAlignment(HSSFTextbox.VERTICAL_ALIGNMENT_TOP);\r
+        textbox.setMarginBottom(71);\r
+        textbox.setMarginLeft(81);\r
+        textbox.setMarginRight(91);\r
+        textbox.setMarginTop(101);\r
+\r
+        assertEquals(textbox.getString().getString(), "test1");\r
+        assertEquals(textbox.getHorizontalAlignment(), HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED);\r
+        assertEquals(textbox.getVerticalAlignment(), HSSFTextbox.VERTICAL_ALIGNMENT_TOP);\r
+        assertEquals(textbox.getMarginBottom(), 71);\r
+        assertEquals(textbox.getMarginLeft(), 81);\r
+        assertEquals(textbox.getMarginRight(), 91);\r
+        assertEquals(textbox.getMarginTop(), 101);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sh = wb.getSheetAt(0);\r
+        patriarch = sh.getDrawingPatriarch();\r
+        textbox = (HSSFTextbox) patriarch.getChildren().get(0);\r
+\r
+        assertEquals(textbox.getString().getString(), "test1");\r
+        assertEquals(textbox.getHorizontalAlignment(), HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED);\r
+        assertEquals(textbox.getVerticalAlignment(), HSSFTextbox.VERTICAL_ALIGNMENT_TOP);\r
+        assertEquals(textbox.getMarginBottom(), 71);\r
+        assertEquals(textbox.getMarginLeft(), 81);\r
+        assertEquals(textbox.getMarginRight(), 91);\r
+        assertEquals(textbox.getMarginTop(), 101);\r
+    }\r
+\r
+    public void testExistingFileWithText(){\r
+        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");\r
+        HSSFSheet sheet = wb.getSheet("text");\r
+        HSSFPatriarch drawing = sheet.getDrawingPatriarch();\r
+        assertEquals(1, drawing.getChildren().size());\r
+        HSSFTextbox textbox = (HSSFTextbox) drawing.getChildren().get(0);\r
+        assertEquals(textbox.getHorizontalAlignment(), HSSFTextbox.HORIZONTAL_ALIGNMENT_LEFT);\r
+        assertEquals(textbox.getVerticalAlignment(), HSSFTextbox.VERTICAL_ALIGNMENT_TOP);\r
+        assertEquals(textbox.getMarginTop(), 0);\r
+        assertEquals(textbox.getMarginBottom(), 3600000);\r
+        assertEquals(textbox.getMarginLeft(), 3600000);\r
+        assertEquals(textbox.getMarginRight(), 0);\r
+        assertEquals(textbox.getString().getString(), "teeeeesssstttt");\r
+    }\r
+}\r
index 27de3b575db9c9d8b04567a1a410b869785263b3..8ea863f59a6eb022c8a5cfa3946adec55f443e46 100644 (file)
Binary files a/test-data/spreadsheet/drawings.xls and b/test-data/spreadsheet/drawings.xls differ
diff --git a/test-data/spreadsheet/empty.xls b/test-data/spreadsheet/empty.xls
new file mode 100644 (file)
index 0000000..97732d4
Binary files /dev/null and b/test-data/spreadsheet/empty.xls differ