Implement Sheet.removeShape(Shape shape) in HSLF

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@642946 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-03-31 09:58:27 +00:00
parent 42c226d204
commit e9b9c591d0
5 changed files with 72 additions and 0 deletions

View 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>

View 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>
@ -440,6 +441,22 @@
</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>

View 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>

View 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 .
*/

View 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);
}
}