]> source.dussan.org Git - poi.git/commitdiff
implemented removing of all shape types
authorEvgeniy Berlog <berlog@apache.org>
Thu, 19 Jul 2012 20:29:42 +0000 (20:29 +0000)
committerEvgeniy Berlog <berlog@apache.org>
Thu, 19 Jul 2012 20:29:42 +0000 (20:29 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/gsoc2012@1363524 13f79535-47bb-0310-9956-ffa450edef68

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/HSSFShapeGroup.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/TestDrawingShapes.java

index 8bafa3b50728b72ecf14ee6723116b3559769058..4922529e6dd12cf44197ea66a9f02cd2b7b1d1f2 100644 (file)
@@ -210,8 +210,7 @@ public class HSSFComment extends HSSFTextbox implements Comment {
     }
 
     public void afterRemove(HSSFPatriarch patriarch){
-        patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID));
-        patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));
+        super.afterRemove(patriarch);
         patriarch._getBoundAggregate().removeTailRecord(getNoteRecord());
     }
 
index 96d12adaa7f306bf08db2a51604fd0cf260b0cef..8f40651205c8c91df06df3ebedb875c3562b7d03 100644 (file)
@@ -88,10 +88,13 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
      *
      * @param shape to be removed
      */
-    public void removeShape(HSSFShape shape) {
-        _mainSpgrContainer.removeChildRecord(shape.getEscherContainer());
-        shape.afterRemove(this);
-        _shapes.remove(shape);
+    public boolean removeShape(HSSFShape shape) {
+        boolean  isRemoved = _mainSpgrContainer.removeChildRecord(shape.getEscherContainer());
+        if (isRemoved){
+            shape.afterRemove(this);
+            _shapes.remove(shape);
+        }
+        return isRemoved;
     }
 
     void afterCreate() {
index e145b9fe50ff50b16e1a81053e5ef574eb7472e6..0d15030c96a2941a7510e03c1ac472da7bc3628d 100644 (file)
@@ -114,6 +114,7 @@ public class HSSFPolygon  extends HSSFSimpleShape {
 
     @Override
     protected void afterRemove(HSSFPatriarch patriarch) {
+        patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID));
     }
 
     public int[] getXPoints() {
index 548b45b75d8391849fcf77f588bbe2eaa22f68cb..7cf6803fd00c58ef65477520bbaa17f675fde7ce 100644 (file)
@@ -124,6 +124,14 @@ public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer {
 
     @Override
     protected void afterRemove(HSSFPatriarch patriarch) {
+        patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildContainers().get(0)
+                .getChildById(EscherClientDataRecord.RECORD_ID));
+        for ( int i=0; i<shapes.size(); i++ ) {
+            HSSFShape shape = shapes.get(i);
+            removeShape(shape);
+            shape.afterRemove(_patriarch);
+        }
+        shapes.clear();
     }
 
     private void onCreate(HSSFShape shape){
@@ -360,4 +368,13 @@ public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer {
         }
         return group;
     }
+
+    public boolean removeShape(HSSFShape shape) {
+        boolean  isRemoved = getEscherContainer().removeChildRecord(shape.getEscherContainer());
+        if (isRemoved){
+            shape.afterRemove(this._patriarch);
+            shapes.remove(shape);
+        }
+        return isRemoved;
+    }
 }
index eb2e865e76f48c8922db7d469ab4591df5e252cf..258ee011a6fb8a8bb4142f0ee32ce97db432e82f 100644 (file)
@@ -152,6 +152,10 @@ public class HSSFSimpleShape extends HSSFShape
 
     @Override
     protected void afterRemove(HSSFPatriarch patriarch) {
+        patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID));
+        if (null != getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID)){
+            patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));
+        }
     }
 
     /**
index cfbbd9797c17e7670ed3d9f033b317180865598e..872a195228b87d2e23a5aa163f5ecd81c3fe2db8 100644 (file)
@@ -71,14 +71,15 @@ public class HSSFTextbox extends HSSFSimpleShape {
         ObjRecord obj = new ObjRecord();
         CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
         c.setObjectType(HSSFTextbox.OBJECT_TYPE_TEXT);
-        c.setLocked( true );
-        c.setPrintable( true );
-        c.setAutofill( true );
-        c.setAutoline( true );
+        c.setLocked(true);
+        c.setPrintable(true);
+        c.setAutofill(true);
+        c.setAutoline(true);
         EndSubRecord e = new EndSubRecord();
-        obj.addSubRecord( c );
-        obj.addSubRecord( e );
-        return obj;    }
+        obj.addSubRecord(c);
+        obj.addSubRecord(e);
+        return obj;
+    }
 
     @Override
     protected EscherContainerRecord createSpContainer() {
@@ -106,12 +107,12 @@ public class HSSFTextbox extends HSSFSimpleShape {
         opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TEXT__TEXTBOTTOM, 0));
 
         opt.setEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEDASHING, LINESTYLE_SOLID));
-        opt.setEscherProperty( new EscherBoolProperty( EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080008));
+        opt.setEscherProperty(new EscherBoolProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080008));
         opt.setEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEWIDTH, LINEWIDTH_DEFAULT));
         opt.setEscherProperty(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, FILL__FILLCOLOR_DEFAULT));
         opt.setEscherProperty(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, LINESTYLE__COLOR_DEFAULT));
         opt.setEscherProperty(new EscherBoolProperty(EscherProperties.FILL__NOFILLHITTEST, NO_FILLHITTEST_FALSE));
-        opt.setEscherProperty(new EscherBoolProperty( EscherProperties.GROUPSHAPE__PRINT, 0x080000));
+        opt.setEscherProperty(new EscherBoolProperty(EscherProperties.GROUPSHAPE__PRINT, 0x080000));
 
         EscherRecord anchor = getAnchor().getEscherAnchor();
         clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
@@ -137,7 +138,7 @@ public class HSSFTextbox extends HSSFSimpleShape {
     }
 
     @Override
-    void afterInsert(HSSFPatriarch patriarch){
+    void afterInsert(HSSFPatriarch patriarch) {
         EscherAggregate agg = patriarch._getBoundAggregate();
         agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
         agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID), getTextObjectRecord());
@@ -148,7 +149,7 @@ public class HSSFTextbox extends HSSFSimpleShape {
      */
     public int getMarginLeft() {
         EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.TEXT__TEXTLEFT);
-        return property == null ? 0: property.getPropertyValue();
+        return property == null ? 0 : property.getPropertyValue();
     }
 
     /**
@@ -163,7 +164,7 @@ public class HSSFTextbox extends HSSFSimpleShape {
      */
     public int getMarginRight() {
         EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.TEXT__TEXTRIGHT);
-        return property == null ? 0: property.getPropertyValue();
+        return property == null ? 0 : property.getPropertyValue();
     }
 
     /**
@@ -178,7 +179,7 @@ public class HSSFTextbox extends HSSFSimpleShape {
      */
     public int getMarginTop() {
         EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.TEXT__TEXTTOP);
-        return property == null ? 0: property.getPropertyValue();
+        return property == null ? 0 : property.getPropertyValue();
     }
 
     /**
@@ -193,7 +194,7 @@ public class HSSFTextbox extends HSSFSimpleShape {
      */
     public int getMarginBottom() {
         EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.TEXT__TEXTBOTTOM);
-        return property == null ? 0: property.getPropertyValue();
+        return property == null ? 0 : property.getPropertyValue();
     }
 
     /**
@@ -233,16 +234,22 @@ public class HSSFTextbox extends HSSFSimpleShape {
 
     @Override
     public void setShapeType(int shapeType) {
-        throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
+        throw new IllegalStateException("Shape type can not be changed in " + this.getClass().getSimpleName());
     }
 
     @Override
     public HSSFShape cloneShape() {
         TextObjectRecord txo = (TextObjectRecord) getTextObjectRecord().cloneViaReserialise();
         EscherContainerRecord spContainer = new EscherContainerRecord();
-        byte [] inSp = getEscherContainer().serialize();
+        byte[] inSp = getEscherContainer().serialize();
         spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
         ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise();
         return new HSSFTextbox(spContainer, obj, txo);
     }
+
+    @Override
+    protected void afterRemove(HSSFPatriarch patriarch) {
+        patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID));
+        patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));
+    }
 }
index e57a1b437242c1efd432e8786f93a2f266ee48f3..e31a6ecbf843b60d0f8e214e181fe6e0a1c18eb9 100644 (file)
@@ -7,6 +7,7 @@ import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
 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.ss.usermodel.Workbook;\r
 import org.apache.poi.util.HexDump;\r
 \r
 import java.io.IOException;\r
@@ -395,4 +396,144 @@ public class TestDrawingShapes extends TestCase {
         assertTrue(Arrays.equals(comboboxShape.getSpContainer().serialize(), combobox.getEscherContainer().serialize()));\r
         assertTrue(Arrays.equals(comboboxShape.getObjRecord().serialize(), combobox.getObjRecord().serialize()));\r
     }\r
+\r
+    public void testRemoveShapes(){\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        HSSFSheet sheet = wb.createSheet();\r
+        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();\r
+\r
+        HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor());\r
+        rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);\r
+\r
+        int idx = wb.addPicture(new byte[]{1,2,3}, Workbook.PICTURE_TYPE_JPEG);\r
+        patriarch.createPicture(new HSSFClientAnchor(), idx);\r
+\r
+        patriarch.createCellComment(new HSSFClientAnchor());\r
+\r
+        HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());\r
+        polygon.setPoints(new int[]{1,2}, new int[]{2,3});\r
+\r
+        patriarch.createTextbox(new HSSFClientAnchor());\r
+\r
+        HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());\r
+        group.createTextbox(new HSSFChildAnchor());\r
+        group.createPicture(new HSSFChildAnchor(), idx);\r
+\r
+        assertEquals(patriarch.getChildren().size(), 6);\r
+        assertEquals(group.getChildren().size(), 2);\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 12);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 12);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+\r
+        assertEquals(patriarch.getChildren().size(), 6);\r
+\r
+        group = (HSSFShapeGroup) patriarch.getChildren().get(5);\r
+        group.removeShape(group.getChildren().get(0));\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 10);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 10);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+\r
+        group = (HSSFShapeGroup) patriarch.getChildren().get(5);\r
+        patriarch.removeShape(group);\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 8);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 8);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+        assertEquals(patriarch.getChildren().size(), 5);\r
+\r
+        HSSFShape shape = patriarch.getChildren().get(0);\r
+        patriarch.removeShape(shape);\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 6);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+        assertEquals(patriarch.getChildren().size(), 4);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 6);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+        assertEquals(patriarch.getChildren().size(), 4);\r
+\r
+        HSSFPicture picture = (HSSFPicture) patriarch.getChildren().get(0);\r
+        patriarch.removeShape(picture);\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 5);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+        assertEquals(patriarch.getChildren().size(), 3);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 5);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);\r
+        assertEquals(patriarch.getChildren().size(), 3);\r
+\r
+        HSSFComment comment = (HSSFComment) patriarch.getChildren().get(0);\r
+        patriarch.removeShape(comment);\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 3);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);\r
+        assertEquals(patriarch.getChildren().size(), 2);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 3);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);\r
+        assertEquals(patriarch.getChildren().size(), 2);\r
+\r
+        polygon = (HSSFPolygon) patriarch.getChildren().get(0);\r
+        patriarch.removeShape(polygon);\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 2);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);\r
+        assertEquals(patriarch.getChildren().size(), 1);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 2);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);\r
+        assertEquals(patriarch.getChildren().size(), 1);\r
+\r
+        HSSFTextbox textbox = (HSSFTextbox) patriarch.getChildren().get(0);\r
+        patriarch.removeShape(textbox);\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 0);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);\r
+        assertEquals(patriarch.getChildren().size(), 0);\r
+\r
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        sheet = wb.getSheetAt(0);\r
+        patriarch = sheet.getDrawingPatriarch();\r
+\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 0);\r
+        assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);\r
+        assertEquals(patriarch.getChildren().size(), 0);\r
+    }\r
 }\r