]> source.dussan.org Git - poi.git/commitdiff
Implement Sheet.removeShape(Shape shape) in HSLF
authorYegor Kozlov <yegor@apache.org>
Mon, 31 Mar 2008 09:58:27 +0000 (09:58 +0000)
committerYegor Kozlov <yegor@apache.org>
Mon, 31 Mar 2008 09:58:27 +0000 (09:58 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@642946 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/hslf/how-to-shapes.xml
src/documentation/content/xdocs/status.xml
src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java
src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java

index f5a8acfbd2b34f1f948387b77e477d0a2c13bbd9..5722a7be458761b55cc06067a0d218ca06dbd004 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.0.3-beta1" date="2008-04-??">
+           <action dev="POI-DEVELOPERS" type="add">Implement Sheet.removeShape(Shape shape) in HSLF</action>
            <action dev="POI-DEVELOPERS" type="add">Various fixes: Recognising var-arg built-in functions #44675, ExternalNameRecord serialisation bug #44695, PMT() bug #44691</action>
            <action dev="POI-DEVELOPERS" type="add">30311 - More work on Conditional Formatting</action>
            <action dev="POI-DEVELOPERS" type="add">Move the Formula Evaluator code out of scratchpad</action>
index 36e4a1138733b8e94c2f9ed89d7be8a90278c5f0..a94ab41fafa735d7368f24f4d5976228ed2cf6db 100644 (file)
@@ -40,6 +40,7 @@
                     <li><link href="#Bullets">How to create bulleted lists</link></li>
                     <li><link href="#Hyperlinks">Hyperlinks</link></li>
                     <li><link href="#Tables">Tables</link></li>
+                    <li><link href="#RemoveShape">How to remove shapes</link></li>
                 </ul>
             </section>
             <section><title>Features</title>
                     </source>
                 </section>
                   
+                <anchor id="RemoveShape"/>
+                <section><title>How to remove shapes from a slide</title>
+                  <source>
+
+        Shape[] shape = slide.getShapes();
+        for (int i = 0; i &lt; shape.length; i++) {
+    
+            //remove the shape
+            boolean ok = slide.removeShape(shape[i]);
+            if(ok){
+              //the shape was removed. Do something.
+            }
+        }
+                    </source>
+                  </section>
+
             </section>
         </section>
     </body>
index 2d44e6e644f8a053ca7abb72108fefc398d88bcd..9d35cb7fd10d8c89485891087d5932ce8469f9b6 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.0.3-beta1" date="2008-04-??">
+           <action dev="POI-DEVELOPERS" type="add">Implement Sheet.removeShape(Shape shape) in HSLF</action>
            <action dev="POI-DEVELOPERS" type="add">Various fixes: Recognising var-arg built-in functions #44675, ExternalNameRecord serialisation bug #44695, PMT() bug #44691</action>
            <action dev="POI-DEVELOPERS" type="add">30311 - More work on Conditional Formatting</action>
            <action dev="POI-DEVELOPERS" type="add">Move the Formula Evaluator code out of scratchpad</action>
index d8ddc7d2f2e9013b6c7cde4cf9d04cd1edba782e..135b625f1d7ba7640ebfb7805cec41fe0bfcdb5c 100644 (file)
@@ -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 <tt>true</tt> 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 .
      */
index 6dd5d9be1700827f7178d769efa97087bc0a5ff4..1fe8f7e5e6d88d135c6edecabd2cabc31c20e19b 100644 (file)
@@ -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);
+    }
 }