From 2e33f41e903d664037367fb4c8588e2ff96f9f74 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Tue, 12 Aug 2008 07:15:26 +0000 Subject: [PATCH] Properly update the array of Slide's text runs in HSLF when new text shapes are added git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@685064 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../src/org/apache/poi/hslf/model/Sheet.java | 11 +++- .../src/org/apache/poi/hslf/model/Slide.java | 12 +++++ .../apache/poi/hslf/model/SlideMaster.java | 34 +++---------- .../org/apache/poi/hslf/model/TextShape.java | 4 ++ .../apache/poi/hslf/model/TestTextRun.java | 51 +++++++++++++++++++ 7 files changed, 87 insertions(+), 27 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 985db6ab9..e290d73df 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + Properly update the array of Slide's text runs in HSLF when new text shapes are added 45590 - Fix for Header/footer extraction for .ppt files saved in Office 2007 Big improvement in how HWPF handles unicode text, and more sanity checking of text ranges within HWPF Include headers and footers int he extracted text from HWPF's WordExtractor diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index d762f1e5a..9a6810660 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Properly update the array of Slide's text runs in HSLF when new text shapes are added 45590 - Fix for Header/footer extraction for .ppt files saved in Office 2007 Big improvement in how HWPF handles unicode text, and more sanity checking of text ranges within HWPF Include headers and footers int he extracted text from HWPF's WordExtractor 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 ccbc03829..c4d542aa0 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java @@ -363,6 +363,16 @@ public abstract class Sheet { } + /** + * Subclasses should call this method and update the array of text runs + * when a text shape is added + * + * @param shape + */ + protected void onAddTextShape(TextShape shape) { + + } + /** * Return placeholder by text type * @@ -440,5 +450,4 @@ public abstract class Sheet { } - } diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java b/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java index c524f31f7..8acbca5ac 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java @@ -460,4 +460,16 @@ public class Slide extends Sheet return new HeadersFooters(hdd, this, newRecord, ppt2007); } } + + protected void onAddTextShape(TextShape shape) { + TextRun run = shape.getTextRun(); + + if(_runs == null) _runs = new TextRun[]{run}; + else { + TextRun[] tmp = new TextRun[_runs.length + 1]; + System.arraycopy(_runs, 0, tmp, 0, _runs.length); + tmp[tmp.length-1] = run; + _runs = tmp; + } + } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java b/src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java index b48edfc1a..b30a46e38 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java @@ -129,33 +129,15 @@ public class SlideMaster extends MasterSheet { } } - /** - * Checks if the shape is a placeholder. - * (placeholders aren't normal shapes, they are visible only in the Edit Master mode) - * - * - * @return true if the shape is a placeholder - */ - public static boolean isPlaceholder(Shape shape){ - if(!(shape instanceof TextShape)) return false; - - TextShape tx = (TextShape)shape; - TextRun run = tx.getTextRun(); - if(run == null) return false; - - Record[] records = run._records; - for (int i = 0; i < records.length; i++) { - int type = (int)records[i].getRecordType(); - if (type == RecordTypes.OEPlaceholderAtom.typeID || - type == RecordTypes.SlideNumberMCAtom.typeID || - type == RecordTypes.DateTimeMCAtom.typeID || - type == RecordTypes.GenericDateMCAtom.typeID || - type == RecordTypes.FooterMCAtom.typeID ){ - return true; - - } + protected void onAddTextShape(TextShape shape) { + TextRun run = shape.getTextRun(); + if(_runs == null) _runs = new TextRun[]{run}; + else { + TextRun[] tmp = new TextRun[_runs.length + 1]; + System.arraycopy(_runs, 0, tmp, 0, _runs.length); + tmp[tmp.length-1] = run; + _runs = tmp; } - return false; } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/TextShape.java b/src/scratchpad/src/org/apache/poi/hslf/model/TextShape.java index a9975ab37..de78b6265 100755 --- a/src/scratchpad/src/org/apache/poi/hslf/model/TextShape.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/TextShape.java @@ -199,6 +199,10 @@ public abstract class TextShape extends SimpleShape { } if(getAnchor().equals(new Rectangle()) && !"".equals(getText())) resizeToFitText(); } + if(_txtrun != null) { + _txtrun.setShapeId(getShapeId()); + sh.onAddTextShape(this); + } } protected EscherTextboxWrapper getEscherTextboxWrapper(){ diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestTextRun.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestTextRun.java index 7b75c3676..6463da4c3 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestTextRun.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestTextRun.java @@ -445,4 +445,55 @@ public class TestTextRun extends TestCase { assertEquals("Sdfsdfs\n" + "Sdfsdf\n", rt[1].getText()); } + + /** + * Test creation of TextRun objects. + */ + public void testAddTextRun() throws Exception{ + SlideShow ppt = new SlideShow(); + Slide slide = ppt.createSlide(); + + assertNull(slide.getTextRuns()); + + TextBox shape1 = new TextBox(); + TextRun run1 = shape1.getTextRun(); + assertSame(run1, shape1.createTextRun()); + run1.setText("Text 1"); + slide.addShape(shape1); + + //The array of Slide's text runs must be updated when new text shapes are added. + TextRun[] runs = slide.getTextRuns(); + assertNotNull(runs); + assertSame(run1, runs[0]); + + TextBox shape2 = new TextBox(); + TextRun run2 = shape2.getTextRun(); + assertSame(run2, shape2.createTextRun()); + run2.setText("Text 2"); + slide.addShape(shape2); + + runs = slide.getTextRuns(); + assertEquals(2, runs.length); + + assertSame(run1, runs[0]); + assertSame(run2, runs[1]); + + //as getShapes() + Shape[] sh = slide.getShapes(); + assertEquals(2, sh.length); + assertTrue(sh[0] instanceof TextBox); + TextBox box1 = (TextBox)sh[0]; + assertSame(run1, box1.getTextRun()); + TextBox box2 = (TextBox)sh[1]; + assertSame(run2, box2.getTextRun()); + + //test Table - a complex group of shapes containing text objects + Slide slide2 = ppt.createSlide(); + assertNull(slide2.getTextRuns()); + Table table = new Table(2, 2); + slide2.addShape(table); + runs = slide2.getTextRuns(); + assertNotNull(runs); + assertEquals(4, runs.length); + } }