]> source.dussan.org Git - poi.git/commitdiff
added support for HSSFPicture in HSSFShapeFactory, fixed bugs, added tests
authorEvgeniy Berlog <berlog@apache.org>
Fri, 22 Jun 2012 09:37:17 +0000 (09:37 +0000)
committerEvgeniy Berlog <berlog@apache.org>
Fri, 22 Jun 2012 09:37:17 +0000 (09:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/gsoc2012@1352818 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ddf/EscherRGBProperty.java
src/java/org/apache/poi/ddf/EscherSimpleProperty.java
src/java/org/apache/poi/hssf/record/EscherAggregate.java
src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java
src/java/org/apache/poi/hssf/usermodel/HSSFShape.java
src/java/org/apache/poi/hssf/usermodel/drawing/HSSFShapeType.java
src/testcases/org/apache/poi/hssf/model/TestDrawingAggregate.java
src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java
src/testcases/org/apache/poi/hssf/model/TestHSSFAnchor.java
test-data/spreadsheet/drawings.xls

index e28b97b5006c9cfb9d0f7af231b8fc307035f31c..5d23addfb5773a6beccba8600d91579f18637b47 100644 (file)
@@ -38,10 +38,6 @@ public class EscherRGBProperty
         return propertyValue;
     }
 
-    public void setRgbColor(int color){
-        this.propertyValue = color;
-    }
-
     public byte getRed()
     {
         return (byte) ( propertyValue & 0xFF );
index efc392dcfacd6e2782821e22d91d6eaa5693ebaa..78fb6420342a633329ffcd309235d798536b0dcd 100644 (file)
@@ -80,10 +80,6 @@ public class EscherSimpleProperty extends EscherProperty
         return propertyValue;
     }
 
-    public void setPropertyValue(int propertyValue) {
-        this.propertyValue = propertyValue;
-    }
-
     /**
      * Returns true if one escher property is equal to another.
      */
index f1b470398f691d6f2123517c5002483fbbe1b91d..fb16037a378fd692a9e1fd01e4cb783aed9ea96d 100644 (file)
@@ -315,6 +315,10 @@ public final class EscherAggregate extends AbstractEscherHolderRecord {
      */
     private List<Record> tailRec = new ArrayList<Record>();
 
+    public EscherAggregate() {
+        buildBaseTree();
+    }
+
     public EscherAggregate(DrawingManager2 drawingManager) {
         this.drawingManager = drawingManager;
     }
@@ -634,7 +638,6 @@ public final class EscherAggregate extends AbstractEscherHolderRecord {
 
     public void setPatriarch(HSSFPatriarch patriarch) {
         this.patriarch = patriarch;
-        convertPatriarch(patriarch);
     }
 
     /**
@@ -1025,6 +1028,43 @@ public final class EscherAggregate extends AbstractEscherHolderRecord {
         throw new IllegalArgumentException("Can not find client data record");
     }
 
+    private void buildBaseTree(){
+        EscherContainerRecord dgContainer = new EscherContainerRecord();
+        EscherContainerRecord spgrContainer = new EscherContainerRecord();
+        EscherContainerRecord spContainer1 = new EscherContainerRecord();
+        EscherSpgrRecord spgr = new EscherSpgrRecord();
+        EscherSpRecord sp1 = new EscherSpRecord();
+        dgContainer.setRecordId(EscherContainerRecord.DG_CONTAINER);
+        dgContainer.setOptions((short) 0x000F);
+        EscherDgRecord dg = new EscherDgRecord();
+        dg.setRecordId( EscherDgRecord.RECORD_ID );
+        short dgId = 1;
+        dg.setOptions( (short) ( dgId << 4 ) );
+        dg.setNumShapes( 1 );
+        dg.setLastMSOSPID( 1024 );
+        drawingGroupId = dg.getDrawingGroupId();
+        spgrContainer.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
+        spgrContainer.setOptions((short) 0x000F);
+        spContainer1.setRecordId(EscherContainerRecord.SP_CONTAINER);
+        spContainer1.setOptions((short) 0x000F);
+        spgr.setRecordId(EscherSpgrRecord.RECORD_ID);
+        spgr.setOptions((short) 0x0001);    // version
+        spgr.setRectX1(0);
+        spgr.setRectY1(0);
+        spgr.setRectX2(1023);
+        spgr.setRectY2(255);
+        sp1.setRecordId(EscherSpRecord.RECORD_ID);
+        sp1.setOptions((short) 0x0002);
+        sp1.setShapeId(1024);
+        sp1.setFlags(EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_PATRIARCH);
+        dgContainer.addChildRecord(dg);
+        dgContainer.addChildRecord(spgrContainer);
+        spgrContainer.addChildRecord(spContainer1);
+        spContainer1.addChildRecord(spgr);
+        spContainer1.addChildRecord(sp1);
+        addEscherRecord(dgContainer);
+    }
+
     private void convertPatriarch(HSSFPatriarch patriarch) {
         EscherContainerRecord dgContainer = new EscherContainerRecord();
         EscherDgRecord dg;
index 5aeb40b188ea5d9cfdba3a0ff4dd092c2ccd692f..985bb8f177960c7ad72fe58be253574fd77d125e 100644 (file)
@@ -20,8 +20,8 @@ package org.apache.poi.hssf.usermodel;
 import java.awt.Dimension;
 import java.io.ByteArrayInputStream;
 
-import org.apache.poi.ddf.EscherBSERecord;
-import org.apache.poi.ddf.EscherBlipRecord;
+import org.apache.poi.ddf.*;
+import org.apache.poi.hssf.record.ObjRecord;
 import org.apache.poi.ss.usermodel.Picture;
 import org.apache.poi.ss.util.ImageUtils;
 import org.apache.poi.hssf.model.InternalWorkbook;
@@ -54,7 +54,9 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture {
      */
     private static final int PX_ROW = 15;
 
-    private int _pictureIndex;
+    public HSSFPicture(EscherContainerRecord spContainer, ObjRecord objRecord) {
+        super(spContainer, objRecord);
+    }
 
     /**
      * Constructs a picture object.
@@ -67,12 +69,16 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture {
 
     public int getPictureIndex()
     {
-        return _pictureIndex;
+        EscherSimpleProperty property = _optRecord.lookup(EscherProperties.BLIP__BLIPTODISPLAY);
+        if (null == property){
+            return -1;
+        }
+        return property.getPropertyValue();
     }
 
     public void setPictureIndex( int pictureIndex )
     {
-        this._pictureIndex = pictureIndex;
+        setPropertyValue(new EscherSimpleProperty( EscherProperties.BLIP__BLIPTODISPLAY, false, true, pictureIndex));
     }
 
     /**
@@ -215,7 +221,7 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture {
      * @return image dimension
      */
     public Dimension getImageDimension(){
-        EscherBSERecord bse = _patriarch._sheet._book.getBSERecord(_pictureIndex);
+        EscherBSERecord bse = _patriarch._sheet._book.getBSERecord(getPictureIndex());
         byte[] data = bse.getBlipRecord().getPicturedata();
         int type = bse.getBlipTypeWin32();
         return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type);
@@ -228,7 +234,7 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture {
      */
     public HSSFPictureData getPictureData(){
         InternalWorkbook iwb = _patriarch._sheet.getWorkbook().getWorkbook();
-       EscherBlipRecord blipRecord = iwb.getBSERecord(_pictureIndex).getBlipRecord();
+       EscherBlipRecord blipRecord = iwb.getBSERecord(getPictureIndex()).getBlipRecord();
        return new HSSFPictureData(blipRecord);
     }
 }
index c9bf922cb7f04eb015fb536f9c4ef3eaa88b52bb..33dfd68643e9541df86a28dbabd3c23c850f390f 100644 (file)
@@ -30,6 +30,7 @@ public abstract class HSSFShape {
     public static final int LINEWIDTH_DEFAULT = 9525;
     public static final int LINESTYLE__COLOR_DEFAULT = 0x08000040;
     public static final int FILL__FILLCOLOR_DEFAULT = 0x08000009;
+    public static final boolean NO_FILL_DEFAULT = true;
 
     public static final int LINESTYLE_SOLID = 0;              // Solid (continuous) pen
     public static final int LINESTYLE_DASHSYS = 1;            // PS_DASH system   dash style
@@ -44,6 +45,8 @@ public abstract class HSSFShape {
     public static final int LINESTYLE_LONGDASHDOTDOTGEL = 10; // long dash short dash short dash
     public static final int LINESTYLE_NONE = -1;
 
+    public static final int LINESTYLE_DEFAULT = LINESTYLE_NONE;
+
     // TODO - make all these fields private
     HSSFShape parent;
     HSSFAnchor anchor;
@@ -108,11 +111,20 @@ public abstract class HSSFShape {
      * @see HSSFClientAnchor
      */
     public void setAnchor(HSSFAnchor anchor) {
+        int i = 0;
+        int recordId = -1;
         if (parent == null) {
             if (anchor instanceof HSSFChildAnchor)
                 throw new IllegalArgumentException("Must use client anchors for shapes directly attached to sheet.");
             EscherClientAnchorRecord anch = _escherContainer.getChildById(EscherClientAnchorRecord.RECORD_ID);
             if (null != anch) {
+                for (i=0; i< _escherContainer.getChildRecords().size(); i++){
+                    if (_escherContainer.getChild(i).getRecordId() == EscherClientAnchorRecord.RECORD_ID){
+                        if (i != _escherContainer.getChildRecords().size() -1){
+                            recordId = _escherContainer.getChild(i+1).getRecordId();
+                        }
+                    }
+                }
                 _escherContainer.removeChildRecord(anch);
             }
         } else {
@@ -120,10 +132,21 @@ public abstract class HSSFShape {
                 throw new IllegalArgumentException("Must use child anchors for shapes attached to groups.");
             EscherChildAnchorRecord anch = _escherContainer.getChildById(EscherChildAnchorRecord.RECORD_ID);
             if (null != anch) {
+                for (i=0; i< _escherContainer.getChildRecords().size(); i++){
+                    if (_escherContainer.getChild(i).getRecordId() == EscherChildAnchorRecord.RECORD_ID){
+                        if (i != _escherContainer.getChildRecords().size() -1){
+                            recordId = _escherContainer.getChild(i+1).getRecordId();
+                        }
+                    }
+                }
                 _escherContainer.removeChildRecord(anch);
             }
         }
-        _escherContainer.addChildRecord(anchor.getEscherAnchor());
+        if (-1 == recordId){
+            _escherContainer.addChildRecord(anchor.getEscherAnchor());
+        } else {
+            _escherContainer.addChildBefore(anchor.getEscherAnchor(), recordId);
+        }
         this.anchor = anchor;
     }
 
@@ -139,13 +162,7 @@ public abstract class HSSFShape {
      * The color applied to the lines of this shape.
      */
     public void setLineStyleColor(int lineStyleColor) {
-        EscherRGBProperty rgbProperty = _optRecord.lookup(EscherProperties.LINESTYLE__COLOR);
-        if (null == rgbProperty) {
-            rgbProperty = new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, lineStyleColor);
-            _optRecord.addEscherProperty(rgbProperty);
-        } else {
-            rgbProperty.setRgbColor(lineStyleColor);
-        }
+        setPropertyValue(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, lineStyleColor));
     }
 
     /**
@@ -153,13 +170,7 @@ public abstract class HSSFShape {
      */
     public void setLineStyleColor(int red, int green, int blue) {
         int lineStyleColor = ((blue) << 16) | ((green) << 8) | red;
-        EscherRGBProperty rgbProperty = _optRecord.lookup(EscherProperties.LINESTYLE__COLOR);
-        if (null == rgbProperty) {
-            rgbProperty = new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, lineStyleColor);
-            _optRecord.addEscherProperty(rgbProperty);
-        } else {
-            rgbProperty.setRgbColor(lineStyleColor);
-        }
+        setPropertyValue(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, lineStyleColor));
     }
 
     /**
@@ -174,13 +185,7 @@ public abstract class HSSFShape {
      * The color used to fill this shape.
      */
     public void setFillColor(int fillColor) {
-        EscherRGBProperty rgbProperty = _optRecord.lookup(EscherProperties.FILL__FILLCOLOR);
-        if (null == rgbProperty) {
-            rgbProperty = new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, fillColor);
-            _optRecord.addEscherProperty(rgbProperty);
-        } else {
-            rgbProperty.setRgbColor(fillColor);
-        }
+        setPropertyValue(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, fillColor));
     }
 
     /**
@@ -188,13 +193,7 @@ public abstract class HSSFShape {
      */
     public void setFillColor(int red, int green, int blue) {
         int fillColor = ((blue) << 16) | ((green) << 8) | red;
-        EscherRGBProperty rgbProperty = _optRecord.lookup(EscherProperties.FILL__FILLCOLOR);
-        if (null == rgbProperty) {
-            rgbProperty = new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, fillColor);
-            _optRecord.addEscherProperty(rgbProperty);
-        } else {
-            rgbProperty.setRgbColor(fillColor);
-        }
+        setPropertyValue(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, fillColor));
     }
 
     /**
@@ -202,7 +201,7 @@ public abstract class HSSFShape {
      */
     public int getLineWidth() {
         EscherSimpleProperty property = _optRecord.lookup(EscherProperties.LINESTYLE__LINEWIDTH);
-        return property.getPropertyValue();
+        return property == null ? LINEWIDTH_DEFAULT: property.getPropertyValue();
     }
 
     /**
@@ -212,13 +211,7 @@ public abstract class HSSFShape {
      * @see HSSFShape#LINEWIDTH_ONE_PT
      */
     public void setLineWidth(int lineWidth) {
-        EscherSimpleProperty property = _optRecord.lookup(EscherProperties.LINESTYLE__LINEWIDTH);
-        if (null == property) {
-            property = new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEWIDTH, lineWidth);
-            _optRecord.addEscherProperty(property);
-        } else {
-            property.setPropertyValue(lineWidth);
-        }
+        setPropertyValue(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEWIDTH, lineWidth));
     }
 
     /**
@@ -227,7 +220,7 @@ public abstract class HSSFShape {
     public int getLineStyle() {
         EscherSimpleProperty property = _optRecord.lookup(EscherProperties.LINESTYLE__LINEDASHING);
         if (null == property){
-            return -1;
+            return LINESTYLE_DEFAULT;
         }
         return property.getPropertyValue();
     }
@@ -238,13 +231,7 @@ public abstract class HSSFShape {
      * @param lineStyle One of the constants in LINESTYLE_*
      */
     public void setLineStyle(int lineStyle) {
-        EscherSimpleProperty property = _optRecord.lookup(EscherProperties.LINESTYLE__LINEDASHING);
-        if (null == property) {
-            property = new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEDASHING, lineStyle);
-            _optRecord.addEscherProperty(property);
-        } else {
-            property.setPropertyValue(lineStyle);
-        }
+        setPropertyValue(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEDASHING, lineStyle));
     }
 
     /**
@@ -252,19 +239,29 @@ public abstract class HSSFShape {
      */
     public boolean isNoFill() {
         EscherBoolProperty property = _optRecord.lookup(EscherProperties.FILL__NOFILLHITTEST);
-        return property.isTrue();
+        return property == null ? NO_FILL_DEFAULT : property.isTrue();
     }
 
     /**
      * Sets whether this shape is filled or transparent.
      */
     public void setNoFill(boolean noFill) {
-        EscherBoolProperty property = _optRecord.lookup(EscherProperties.FILL__NOFILLHITTEST);
-        if (null == property) {
-            property = new EscherBoolProperty(EscherProperties.FILL__NOFILLHITTEST, noFill ? 1 : 0);
+        setPropertyValue(new EscherBoolProperty(EscherProperties.FILL__NOFILLHITTEST, noFill ? 1 : 0));
+    }
+
+    protected void setPropertyValue(EscherProperty property){
+        if (null == _optRecord.lookup(property.getId())){
             _optRecord.addEscherProperty(property);
         } else {
-            property.setPropertyValue(noFill ? 1 : 0);
+            int i=0;
+            for (EscherProperty prop: _optRecord.getEscherProperties()){
+                if (prop.getId() == property.getId()){
+                    _optRecord.getEscherProperties().remove(i);
+                    break;
+                }
+                i++;
+            }
+            _optRecord.addEscherProperty(property);
         }
     }
 
index 29741c90f8b8f40f93748e0c62e8dac4582400d5..c63a08b7e5564c3c3e5ab1a8380377245ee80884 100644 (file)
@@ -1,5 +1,6 @@
 package org.apache.poi.hssf.usermodel.drawing;\r
 \r
+import org.apache.poi.hssf.usermodel.HSSFPicture;\r
 import org.apache.poi.hssf.usermodel.HSSFSimpleShape;\r
 \r
 /**\r
@@ -9,6 +10,7 @@ import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
 public enum HSSFShapeType {\r
     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
     ROUND_RECTANGLE((short)0x2, null, null);\r
 \r
     private Short type;\r
index 43cdd8b856bbedc78ff95b34fdc2618124695562..18572d6ec1f2c2996683fc09c2b5eb85d9ebc302 100644 (file)
@@ -43,6 +43,8 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;\r
 import java.io.FilenameFilter;\r
 import java.io.IOException;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.lang.reflect.Method;\r
 import java.util.ArrayList;\r
 import java.util.Arrays;\r
 import java.util.HashMap;\r
@@ -192,6 +194,40 @@ public class TestDrawingAggregate extends TestCase {
         }\r
     }\r
 \r
+    public void testBuildBaseTree(){\r
+        EscherAggregate agg = new EscherAggregate();\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        HSSFSheet sheet = wb.createSheet();\r
+\r
+        HSSFPatriarch drawing = sheet.createDrawingPatriarch();\r
+        EscherAggregate agg1 = HSSFTestHelper.getEscherAggregate(drawing);\r
+        callConvertPatriarch(agg1);\r
+        agg1.setPatriarch(null);\r
+        \r
+        agg.setPatriarch(null);\r
+\r
+        byte[] aggS = agg.serialize();\r
+        byte []agg1S = agg1.serialize();\r
+\r
+        assertEquals(aggS.length, agg1S.length);\r
+        assertTrue(Arrays.equals(aggS, agg1S));\r
+    }\r
+\r
+    private static void callConvertPatriarch(EscherAggregate agg) {\r
+        Method method = null;\r
+        try {\r
+            method = agg.getClass().getDeclaredMethod("convertPatriarch", HSSFPatriarch.class);\r
+            method.setAccessible(true);\r
+            method.invoke(agg, agg.getPatriarch());\r
+        } catch (IllegalAccessException e) {\r
+            e.printStackTrace();\r
+        } catch (NoSuchMethodException e) {\r
+            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.\r
+        } catch (InvocationTargetException e) {\r
+            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.\r
+        }\r
+    }\r
+\r
     /**\r
      * when reading incomplete data ensure that the serialized bytes\r
      match the source\r
index f276cba39116fda80281f5dab823e4bbf9d03351..097d5d01ee4cba82d6ef5376e076a03798a2d353 100644 (file)
@@ -3,6 +3,9 @@ package org.apache.poi.hssf.model;
 import junit.framework.TestCase;\r
 import org.apache.poi.ddf.*;\r
 import org.apache.poi.hssf.HSSFTestDataSamples;\r
+import org.apache.poi.hssf.record.CommonObjectDataSubRecord;\r
+import org.apache.poi.hssf.record.EscherAggregate;\r
+import org.apache.poi.hssf.record.ObjRecord;\r
 import org.apache.poi.hssf.usermodel.*;\r
 import org.apache.poi.util.HexDump;\r
 \r
@@ -72,6 +75,38 @@ public class TestDrawingShapes extends TestCase{
         assertEquals(true,\r
                 ((EscherBoolProperty)opt.lookup(EscherProperties.GROUPSHAPE__PRINT)).isTrue());\r
     }\r
+\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
+        assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_SOLID);\r
+        assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);\r
+        assertEquals(picture.isNoFill(), false);\r
+        assertEquals(picture.getPictureIndex(), -1);//not set yet\r
+    }\r
+\r
+    /**\r
+     * No NullPointerException should appear\r
+     */\r
+    public void testDefaultSettingsWithEmptyContainer(){\r
+        EscherContainerRecord container = new EscherContainerRecord();\r
+        EscherOptRecord opt = new EscherOptRecord();\r
+        opt.setRecordId(EscherOptRecord.RECORD_ID);\r
+        container.addChildRecord(opt);\r
+        ObjRecord obj = new ObjRecord();\r
+        CommonObjectDataSubRecord cod = new CommonObjectDataSubRecord();\r
+        cod.setObjectType(HSSFSimpleShape.OBJECT_TYPE_PICTURE);\r
+        obj.addSubRecord(cod);\r
+        HSSFPicture picture = new HSSFPicture(container, obj);\r
+\r
+        assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);\r
+        assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT);\r
+        assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT);\r
+        assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);\r
+        assertEquals(picture.isNoFill(), HSSFShape.NO_FILL_DEFAULT);\r
+        assertEquals(picture.getPictureIndex(), -1);//not set yet\r
+    }\r
     /**\r
      * create a rectangle, save the workbook, read back and verify that all shape properties are there\r
      */\r
@@ -139,6 +174,28 @@ public class TestDrawingShapes extends TestCase{
         assertEquals(rectangle2.getAnchor().getDy1(), 4);\r
         assertEquals(rectangle2.getAnchor().getDy2(), 5);\r
         assertEquals(rectangle2.isNoFill(), false);\r
+\r
+        HSSFSimpleShape rect3 = drawing.createSimpleShape(new HSSFClientAnchor());\r
+        rect3.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+\r
+        drawing = wb.getSheetAt(0).getDrawingPatriarch();\r
+        assertEquals(drawing.getChildren().size(), 2);\r
+    }\r
+\r
+    public void testReadExistingImage(){\r
+        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");\r
+        HSSFSheet sheet = wb.getSheet("pictures");\r
+        HSSFPatriarch drawing = sheet.getDrawingPatriarch();\r
+        assertEquals(1, drawing.getChildren().size());\r
+        HSSFPicture picture = (HSSFPicture) drawing.getChildren().get(0);\r
+\r
+        assertEquals(picture.getPictureIndex(), 1);\r
+        assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);\r
+        assertEquals(picture.getFillColor(), 0x5DC943);\r
+        assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);\r
+        assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT);\r
+        assertEquals(picture.isNoFill(), true);\r
     }\r
 \r
 \r
@@ -157,4 +214,41 @@ public class TestDrawingShapes extends TestCase{
             assertEquals(shape.getLineWidth(), HSSFShape.LINEWIDTH_ONE_PT*2);\r
         }\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
+            patriarch1.createSimpleShape(new HSSFClientAnchor());\r
+        }\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet1 = wb.getSheetAt(0);\r
+        patriarch1 = sheet1.getDrawingPatriarch();\r
+\r
+        EscherAggregate agg1 = HSSFTestHelper.getEscherAggregate(patriarch1);\r
+        // last shape ID cached in EscherDgRecord\r
+        EscherDgRecord dg1 =\r
+                agg1.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);\r
+        assertEquals(1026, dg1.getLastMSOSPID());\r
+\r
+        // iterate over shapes and check shapeId\r
+        EscherContainerRecord spgrContainer =\r
+                agg1.getEscherContainer().getChildContainers().get(0);\r
+        // root spContainer + 2 spContainers for shapes\r
+        assertEquals(3, spgrContainer.getChildRecords().size());\r
+\r
+        EscherSpRecord sp0 =\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
+        assertEquals(1025, sp1.getShapeId());\r
+\r
+        EscherSpRecord sp2 =\r
+                ((EscherContainerRecord)spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID);\r
+        assertEquals(1026, sp2.getShapeId());\r
+    }\r
 }\r
index ef90f01f40d7e97d4031c1bb3a9a1230893bac68..248247266a4f67b948a5d66bdf178fd10ba18dea 100644 (file)
@@ -1,10 +1,10 @@
 package org.apache.poi.hssf.model;\r
 \r
 import junit.framework.TestCase;\r
-import org.apache.poi.ddf.EscherChildAnchorRecord;\r
-import org.apache.poi.ddf.EscherClientAnchorRecord;\r
-import org.apache.poi.ddf.EscherContainerRecord;\r
+import org.apache.poi.ddf.*;\r
+import org.apache.poi.hssf.HSSFTestDataSamples;\r
 import org.apache.poi.hssf.usermodel.*;\r
+import org.apache.poi.util.HexDump;\r
 \r
 /**\r
  * @author Evgeniy Berlog\r
@@ -12,6 +12,63 @@ import org.apache.poi.hssf.usermodel.*;
  */\r
 public class TestHSSFAnchor extends TestCase {\r
 \r
+    public void testDefaultValues(){\r
+        HSSFClientAnchor clientAnchor = new HSSFClientAnchor();\r
+        assertEquals(clientAnchor.getAnchorType(), 0);\r
+        assertEquals(clientAnchor.getCol1(), 0);\r
+        assertEquals(clientAnchor.getCol2(), 0);\r
+        assertEquals(clientAnchor.getDx1(), 0);\r
+        assertEquals(clientAnchor.getDx2(), 0);\r
+        assertEquals(clientAnchor.getDy1(), 0);\r
+        assertEquals(clientAnchor.getDy2(), 0);\r
+        assertEquals(clientAnchor.getRow1(), 0);\r
+        assertEquals(clientAnchor.getRow2(), 0);\r
+\r
+        clientAnchor = new HSSFClientAnchor(new EscherClientAnchorRecord());\r
+        assertEquals(clientAnchor.getAnchorType(), 0);\r
+        assertEquals(clientAnchor.getCol1(), 0);\r
+        assertEquals(clientAnchor.getCol2(), 0);\r
+        assertEquals(clientAnchor.getDx1(), 0);\r
+        assertEquals(clientAnchor.getDx2(), 0);\r
+        assertEquals(clientAnchor.getDy1(), 0);\r
+        assertEquals(clientAnchor.getDy2(), 0);\r
+        assertEquals(clientAnchor.getRow1(), 0);\r
+        assertEquals(clientAnchor.getRow2(), 0);\r
+\r
+        HSSFChildAnchor childAnchor = new HSSFChildAnchor();\r
+        assertEquals(childAnchor.getDx1(), 0);\r
+        assertEquals(childAnchor.getDx2(), 0);\r
+        assertEquals(childAnchor.getDy1(), 0);\r
+        assertEquals(childAnchor.getDy2(), 0);\r
+\r
+        childAnchor = new HSSFChildAnchor(new EscherChildAnchorRecord());\r
+        assertEquals(childAnchor.getDx1(), 0);\r
+        assertEquals(childAnchor.getDx2(), 0);\r
+        assertEquals(childAnchor.getDy1(), 0);\r
+        assertEquals(childAnchor.getDy2(), 0);\r
+    }\r
+\r
+    public void testCorrectOrderInSpContainer(){\r
+        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");\r
+        HSSFSheet sheet = wb.getSheet("pictures");\r
+        HSSFPatriarch drawing = sheet.getDrawingPatriarch();\r
+\r
+        HSSFSimpleShape rectangle = (HSSFSimpleShape) drawing.getChildren().get(0);\r
+        rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);\r
+\r
+        assertEquals(rectangle.getEscherContainer().getChild(0).getRecordId(), EscherSpRecord.RECORD_ID);\r
+        assertEquals(rectangle.getEscherContainer().getChild(1).getRecordId(), EscherOptRecord.RECORD_ID);\r
+        assertEquals("  " + HexDump.toHex(rectangle.getEscherContainer().getChild(2).getRecordId()) + "    ", rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);\r
+        assertEquals(rectangle.getEscherContainer().getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID);\r
+\r
+        rectangle.setAnchor(new HSSFClientAnchor());\r
+\r
+        assertEquals(rectangle.getEscherContainer().getChild(0).getRecordId(), EscherSpRecord.RECORD_ID);\r
+        assertEquals(rectangle.getEscherContainer().getChild(1).getRecordId(), EscherOptRecord.RECORD_ID);\r
+        assertEquals("  " + HexDump.toHex(rectangle.getEscherContainer().getChild(2).getRecordId()) + "    ", rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);\r
+        assertEquals(rectangle.getEscherContainer().getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID);\r
+    }\r
+\r
     public void testCreateClientAnchorFromContainer(){\r
         EscherContainerRecord container = new EscherContainerRecord();\r
         EscherClientAnchorRecord escher = new EscherClientAnchorRecord();\r
@@ -206,4 +263,116 @@ public class TestHSSFAnchor extends TestCase {
         assertEquals(anchor.getDy2(), 118);\r
         assertEquals(escher.getDy2(), 118);\r
     }\r
+\r
+    public void testEqualsToSelf(){\r
+        HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);\r
+        assertEquals(clientAnchor, clientAnchor);\r
+\r
+        HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3);\r
+        assertEquals(childAnchor, childAnchor);\r
+    }\r
+\r
+    public void testPassIncompatibleTypeIsFalse(){\r
+        HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);\r
+        assertNotSame(clientAnchor, "wrongType");\r
+\r
+        HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3);\r
+        assertNotSame(childAnchor, "wrongType");\r
+    }\r
+\r
+    public void testNullReferenceIsFalse() {\r
+        HSSFClientAnchor clientAnchor = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);\r
+        assertFalse("Passing null to equals should return false", clientAnchor.equals(null));\r
+\r
+        HSSFChildAnchor childAnchor = new HSSFChildAnchor(0, 1, 2, 3);\r
+        assertFalse("Passing null to equals should return false", childAnchor.equals(null));\r
+    }\r
+\r
+    public void testEqualsIsReflexiveIsSymmetric() {\r
+        HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);\r
+        HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);\r
+\r
+        assertTrue(clientAnchor1.equals(clientAnchor2));\r
+        assertTrue(clientAnchor1.equals(clientAnchor2));\r
+\r
+        HSSFChildAnchor childAnchor1 = new HSSFChildAnchor(0, 1, 2, 3);\r
+        HSSFChildAnchor childAnchor2 = new HSSFChildAnchor(0, 1, 2, 3);\r
+\r
+        assertTrue(childAnchor1.equals(childAnchor2));\r
+        assertTrue(childAnchor2.equals(childAnchor1));\r
+    }\r
+\r
+    public void testEqualsValues(){\r
+        HSSFClientAnchor clientAnchor1 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);\r
+        HSSFClientAnchor clientAnchor2 = new HSSFClientAnchor(0, 1, 2, 3, (short)4, 5, (short)6, 7);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        clientAnchor2.setDx1(10);\r
+        assertNotSame(clientAnchor1, clientAnchor2);\r
+        clientAnchor2.setDx1(0);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        clientAnchor2.setDy1(10);\r
+        assertNotSame(clientAnchor1, clientAnchor2);\r
+        clientAnchor2.setDy1(1);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        clientAnchor2.setDx2(10);\r
+        assertNotSame(clientAnchor1, clientAnchor2);\r
+        clientAnchor2.setDx2(2);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        clientAnchor2.setDy2(10);\r
+        assertNotSame(clientAnchor1, clientAnchor2);\r
+        clientAnchor2.setDy2(3);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        clientAnchor2.setCol1(10);\r
+        assertNotSame(clientAnchor1, clientAnchor2);\r
+        clientAnchor2.setCol1(4);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        clientAnchor2.setRow1(10);\r
+        assertNotSame(clientAnchor1, clientAnchor2);\r
+        clientAnchor2.setRow1(5);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        clientAnchor2.setCol2(10);\r
+        assertNotSame(clientAnchor1, clientAnchor2);\r
+        clientAnchor2.setCol2(6);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        clientAnchor2.setRow2(10);\r
+        assertNotSame(clientAnchor1, clientAnchor2);\r
+        clientAnchor2.setRow2(7);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        clientAnchor2.setAnchorType(3);\r
+        assertNotSame(clientAnchor1, clientAnchor2);\r
+        clientAnchor2.setAnchorType(0);\r
+        assertEquals(clientAnchor1, clientAnchor2);\r
+\r
+        HSSFChildAnchor childAnchor1 = new HSSFChildAnchor(0, 1, 2, 3);\r
+        HSSFChildAnchor childAnchor2 = new HSSFChildAnchor(0, 1, 2, 3);\r
+\r
+        childAnchor1.setDx1(10);\r
+        assertNotSame(childAnchor1, childAnchor2);\r
+        childAnchor1.setDx1(0);\r
+        assertEquals(childAnchor1, childAnchor2);\r
+\r
+        childAnchor2.setDy1(10);\r
+        assertNotSame(childAnchor1, childAnchor2);\r
+        childAnchor2.setDy1(1);\r
+        assertEquals(childAnchor1, childAnchor2);\r
+\r
+        childAnchor2.setDx2(10);\r
+        assertNotSame(childAnchor1, childAnchor2);\r
+        childAnchor2.setDx2(2);\r
+        assertEquals(childAnchor1, childAnchor2);\r
+\r
+        childAnchor2.setDy2(10);\r
+        assertNotSame(childAnchor1, childAnchor2);\r
+        childAnchor2.setDy2(3);\r
+        assertEquals(childAnchor1, childAnchor2);\r
+    }\r
 }\r
index ccbcc8cb4a10b6031f4b49711e404707fb947ba2..27de3b575db9c9d8b04567a1a410b869785263b3 100644 (file)
Binary files a/test-data/spreadsheet/drawings.xls and b/test-data/spreadsheet/drawings.xls differ