From: Yegor Kozlov Date: Mon, 31 Mar 2008 09:58:27 +0000 (+0000) Subject: Implement Sheet.removeShape(Shape shape) in HSLF X-Git-Tag: REL_3_0_3_BETA1~52 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e9b9c591d038499f4e6114b6c886d7271f52425a;p=poi.git Implement Sheet.removeShape(Shape shape) in HSLF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@642946 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index f5a8acfbd2..5722a7be45 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + Implement Sheet.removeShape(Shape shape) in HSLF Various fixes: Recognising var-arg built-in functions #44675, ExternalNameRecord serialisation bug #44695, PMT() bug #44691 30311 - More work on Conditional Formatting Move the Formula Evaluator code out of scratchpad diff --git a/src/documentation/content/xdocs/hslf/how-to-shapes.xml b/src/documentation/content/xdocs/hslf/how-to-shapes.xml index 36e4a11387..a94ab41faf 100644 --- a/src/documentation/content/xdocs/hslf/how-to-shapes.xml +++ b/src/documentation/content/xdocs/hslf/how-to-shapes.xml @@ -40,6 +40,7 @@
  • How to create bulleted lists
  • Hyperlinks
  • Tables
  • +
  • How to remove shapes
  • Features @@ -440,6 +441,22 @@
    + +
    How to remove shapes from a slide + + + Shape[] shape = slide.getShapes(); + for (int i = 0; i < shape.length; i++) { + + //remove the shape + boolean ok = slide.removeShape(shape[i]); + if(ok){ + //the shape was removed. Do something. + } + } + +
    + diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 2d44e6e644..9d35cb7fd1 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Implement Sheet.removeShape(Shape shape) in HSLF Various fixes: Recognising var-arg built-in functions #44675, ExternalNameRecord serialisation bug #44695, PMT() bug #44691 30311 - More work on Conditional Formatting Move the Formula Evaluator code out of scratchpad diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java b/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java index d8ddc7d2f2..135b625f1d 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java @@ -265,6 +265,31 @@ public abstract class Sheet { } } + /** + * Removes the specified shape from this sheet. + * + * @param shape shape to be removed from this sheet, if present. + * @return true if the shape was deleted. + */ + public boolean removeShape(Shape shape) { + PPDrawing ppdrawing = getPPDrawing(); + + EscherContainerRecord dg = (EscherContainerRecord) ppdrawing.getEscherRecords()[0]; + EscherContainerRecord spgr = null; + + for (Iterator it = dg.getChildRecords().iterator(); it.hasNext();) { + EscherRecord rec = (EscherRecord) it.next(); + if (rec.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) { + spgr = (EscherContainerRecord) rec; + break; + } + } + if(spgr == null) return false; + + List lst = spgr.getChildRecords(); + return lst.remove(shape.getSpContainer()); + } + /** * Return the master sheet . */ diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java index 6dd5d9be17..1fe8f7e5e6 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java @@ -26,6 +26,7 @@ import java.awt.Rectangle; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.File; +import java.io.IOException; import java.util.ArrayList; /** @@ -279,4 +280,31 @@ public class TestShapes extends TestCase { line = (Line)grshape[1]; assertEquals(new Rectangle(300, 300, 500, 0), line.getAnchor()); } + + /** + * Test functionality of Sheet.removeShape(Shape shape) + */ + public void testRemoveShapes() throws IOException { + String file = System.getProperty("HSLF.testdata.path")+ "/with_textbox.ppt"; + SlideShow ppt = new SlideShow(new HSLFSlideShow(file)); + Slide sl = ppt.getSlides()[0]; + Shape[] sh = sl.getShapes(); + assertEquals("expected four shaped in " + file, 4, sh.length); + //remove all + for (int i = 0; i < sh.length; i++) { + boolean ok = sl.removeShape(sh[i]); + assertTrue("Failed to delete shape #" + i, ok); + } + //now Slide.getShapes() should return an empty array + assertEquals("expected 0 shaped in " + file, 0, sl.getShapes().length); + + //serialize and read again. The file should be readable and contain no shapes + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ppt.write(out); + out.close(); + + ppt = new SlideShow(new ByteArrayInputStream(out.toByteArray())); + sl = ppt.getSlides()[0]; + assertEquals("expected 0 shaped in " + file, 0, sl.getShapes().length); + } }