From: Yegor Kozlov Date: Sun, 1 Feb 2009 15:07:25 +0000 (+0000) Subject: added a method to remove slides X-Git-Tag: REL_3_5_BETA5~15 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f6fae6e2d1ab4f9dc08bdaa3059b5f040bb02f4e;p=poi.git added a method to remove slides git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@739775 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 0e2a141d68..17a02ee598 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 46635 - Added a method to remove slides 46520 - Fixed HSSFFont.applyFont() to properly apply font to overlapping regions 46545 - Fixed ObjRecord to ignore excessive padding written by previous POI versions 46613 - Fixed evaluator to perform case insensitive string comparisons diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 4bd2067fac..c4c22452d3 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 46635 - Added a method to remove slides 46520 - Fixed HSSFFont.applyFont() to properly apply font to overlapping regions 46545 - Fixed ObjRecord to ignore excessive padding written by previous POI versions 46613 - Fixed evaluator to perform case insensitive string comparisons diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java b/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java index 13ffa70752..03f5778139 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java @@ -163,6 +163,11 @@ public class SlideListWithText extends RecordContainer */ public SlideAtomsSet[] getSlideAtomsSets() { return slideAtomsSets; } + /** + * Get access to the SlideAtomsSets of the children of this record + */ + public void setSlideAtomsSets( SlideAtomsSet[] sas ) { slideAtomsSets = sas; } + /** * Return the value we were given at creation */ diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java index 7672923ad5..6414d177ce 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java @@ -569,7 +569,46 @@ public final class SlideShow { slwt.setChildRecord(r); } - /* =============================================================== + /** + * Removes the slide at the given index (0-based). + *

Shifts any subsequent slides to the left (subtracts one from their slide numbers).

+ * + * @param index the index of the slide to remove (0-based) + * @return the slide that was removed from the slide show. + */ + public Slide removeSlide(int index) { + int lastSlideIdx = _slides.length - 1; + if(index < 0 || index > lastSlideIdx) { + throw new IllegalArgumentException("Slide index (" + + index +") is out of range (0.." + lastSlideIdx + ")"); + } + + SlideListWithText slwt = _documentRecord.getSlideSlideListWithText(); + SlideAtomsSet[] sas = slwt.getSlideAtomsSets(); + + Slide removedSlide = null; + ArrayList records = new ArrayList(); + ArrayList sa = new ArrayList(); + ArrayList sl = new ArrayList(); + for (int i = 0, num = 0; i < _slides.length; i++){ + if(i != index) { + sl.add(_slides[i]); + sa.add(sas[i]); + _slides[i].setSlideNumber(num++); + records.add(sas[i].getSlidePersistAtom()); + records.addAll(Arrays.asList(sas[i].getSlideRecords())); + } else { + removedSlide = _slides[i]; + } + } + slwt.setSlideAtomsSets( sa.toArray(new SlideAtomsSet[sa.size()]) ); + slwt.setChildRecord(records.toArray(new Record[records.size()])); + _slides = sl.toArray(new Slide[sl.size()]); + + return removedSlide; + } + + /* =============================================================== * Addition Code * =============================================================== */ diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestAddingSlides.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestAddingSlides.java index 02c6beb53b..17f8fcb68c 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestAddingSlides.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestAddingSlides.java @@ -225,4 +225,48 @@ public final class TestAddingSlides extends TestCase { assertEquals(8, s3._getSheetRefId()); assertEquals(3, s3.getSlideNumber()); } + + /** + * Test SlideShow#removeSlide + */ + public void testRemoving() throws Exception { + SlideShow ppt = new SlideShow(); + Slide slide1 = ppt.createSlide(); + Slide slide2 = ppt.createSlide(); + + Slide[] s1 = ppt.getSlides(); + assertEquals(2, s1.length); + try { + ppt.removeSlide(-1); + fail("expected exception"); + } catch (Exception e){ + ; + } + + try { + ppt.removeSlide(2); + fail("expected exception"); + } catch (Exception e){ + ; + } + + assertEquals(1, slide1.getSlideNumber()); + + Slide removedSlide = ppt.removeSlide(0); + Slide[] s2 = ppt.getSlides(); + assertEquals(1, s2.length); + assertSame(slide1, removedSlide); + assertSame(slide2, s2[0]); + + assertEquals(0, slide2.getSlideNumber()); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ppt.write(out); + + ppt = new SlideShow(new ByteArrayInputStream(out.toByteArray())); + + Slide[] s3 = ppt.getSlides(); + assertEquals(1, s3.length); + } + }