]> source.dussan.org Git - poi.git/commitdiff
#45908 - RichTextRun.setBullet(false) doesn't work, bullets still here
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 1 Nov 2015 22:52:49 +0000 (22:52 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 1 Nov 2015 22:52:49 +0000 (22:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1711858 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShow.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextRun.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java
test-data/slideshow/bug45908.ppt [new file with mode: 0644]

index 1162cbaa487b9eaa64502c2f0f1ab4e7e05cccf6..0ba0fd87ece09f9487fcbef9116f145ebbbee0c6 100644 (file)
@@ -90,7 +90,12 @@ import org.apache.poi.util.Units;
  * @author Yegor kozlov
  */
 public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagraph>, Closeable {
-       // What we're based on
+    enum LoadSavePhase {
+        INIT, LOADED;
+    }
+    private static ThreadLocal<LoadSavePhase> loadSavePhase = new ThreadLocal<LoadSavePhase>();
+    
+    // What we're based on
        private HSLFSlideShowImpl _hslfSlideShow;
 
        // Pointers to the most recent versions of the core records
@@ -127,6 +132,8 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
         * @param hslfSlideShow the HSLFSlideShow to base on
         */
        public HSLFSlideShow(HSLFSlideShowImpl hslfSlideShow) {
+           loadSavePhase.set(LoadSavePhase.INIT);
+           
            // Get useful things from our base slideshow
            _hslfSlideShow = hslfSlideShow;
 
@@ -142,6 +149,8 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
 
                // Build up the model level Slides and Notes
                buildSlidesAndNotes();
+               
+               loadSavePhase.set(LoadSavePhase.LOADED);
        }
 
        /**
@@ -154,13 +163,15 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
        /**
         * Constructs a Powerpoint document from an input stream.
         */
-       public HSLFSlideShow(InputStream inputStream) throws IOException {
+       @SuppressWarnings("resource")
+    public HSLFSlideShow(InputStream inputStream) throws IOException {
                this(new HSLFSlideShowImpl(inputStream));
        }
 
     /**
      * Constructs a Powerpoint document from an POIFSFileSystem.
      */
+    @SuppressWarnings("resource")
     public HSLFSlideShow(NPOIFSFileSystem npoifs) throws IOException {
         this(new HSLFSlideShowImpl(npoifs));
     }
@@ -168,10 +179,18 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
     /**
      * Constructs a Powerpoint document from an DirectoryNode.
      */
+    @SuppressWarnings("resource")
     public HSLFSlideShow(DirectoryNode root) throws IOException {
         this(new HSLFSlideShowImpl(root));
     }
 
+    /**
+     * @return the current loading/saving phase
+     */
+    protected static LoadSavePhase getLoadSavePhase() {
+        return loadSavePhase.get();
+    }
+    
        /**
         * Use the PersistPtrHolder entries to figure out what is the "most recent"
         * version of all the core records (Document, Notes, Slide etc), and save a
@@ -454,19 +473,25 @@ public final class HSLFSlideShow implements SlideShow<HSLFShape,HSLFTextParagrap
        public void write(OutputStream out) throws IOException {
            // check for text paragraph modifications
            for (HSLFSlide sl : getSlides()) {
-               for (HSLFShape sh : sl.getShapes()) {
-                   if (!(sh instanceof HSLFTextShape)) continue;
-                   HSLFTextShape hts = (HSLFTextShape)sh;
+               writeDirtyParagraphs(sl);
+           }
+           
+               _hslfSlideShow.write(out);
+       }
+       
+       private void writeDirtyParagraphs(HSLFShapeContainer container) {
+        for (HSLFShape sh : container.getShapes()) {
+            if (sh instanceof HSLFShapeContainer) {
+                writeDirtyParagraphs((HSLFShapeContainer)sh);
+            } else if (sh instanceof HSLFTextShape) {
+                HSLFTextShape hts = (HSLFTextShape)sh;
                 boolean isDirty = false;
                 for (HSLFTextParagraph p : hts.getTextParagraphs()) {
                     isDirty |= p.isDirty();
                 }
-                   if (isDirty) hts.storeText();
-               }
-           }
-           
-           
-               _hslfSlideShow.write(out);
+                if (isDirty) hts.storeText();
+            }
+        }
        }
 
        /*
index e1f2ab24d6d311c0e1b2776538d861e89552f5d2..c3e7da1ba4dd2673b70b84e1cff8e918994fc87d 100644 (file)
@@ -21,6 +21,7 @@ import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.getPropVal;
 
 import java.awt.Color;
 
+import org.apache.poi.hslf.exceptions.HSLFException;
 import org.apache.poi.hslf.model.textproperties.BitMaskTextProp;
 import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp;
 import org.apache.poi.hslf.model.textproperties.TextProp;
@@ -97,7 +98,16 @@ public final class HSLFTextRun implements TextRun {
         * Change the text
         */
        public void setText(String text) {
-           _runText = HSLFTextParagraph.toInternalString(text);
+           if (text == null) {
+               throw new HSLFException("text must not be null");
+           }
+           String newText = HSLFTextParagraph.toInternalString(text);
+           if (!newText.equals(_runText)) {
+               _runText = newText;
+               if (HSLFSlideShow.getLoadSavePhase() == HSLFSlideShow.LoadSavePhase.LOADED) {
+                   parentParagraph.setDirty();
+               }
+           }
        }
 
        // --------------- Internal helpers on rich text properties -------
@@ -275,7 +285,8 @@ public final class HSLFTextRun implements TextRun {
        @Override
        public void setFontFamily(String fontFamily) {
            HSLFSheet sheet = parentParagraph.getSheet();
-           HSLFSlideShow slideShow = (sheet == null) ? null : sheet.getSlideShow();
+           @SuppressWarnings("resource")
+        HSLFSlideShow slideShow = (sheet == null) ? null : sheet.getSlideShow();
                if (sheet == null || slideShow == null) {
                        //we can't set font since slideshow is not assigned yet
                        _fontFamily = fontFamily;
@@ -289,6 +300,7 @@ public final class HSLFTextRun implements TextRun {
        @Override
        public String getFontFamily() {
         HSLFSheet sheet = parentParagraph.getSheet();
+        @SuppressWarnings("resource")
         HSLFSlideShow slideShow = (sheet == null) ? null : sheet.getSlideShow();
                if (sheet == null || slideShow == null) {
                        return _fontFamily;
index 820f099eefa14a65e6ae940418913ad4676774b0..22d750857a0f07609bf0488cf71b790ff635c485 100644 (file)
@@ -26,7 +26,6 @@ import java.awt.Color;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -49,7 +48,6 @@ import org.apache.poi.hslf.record.Document;
 import org.apache.poi.hslf.record.Record;
 import org.apache.poi.hslf.record.SlideListWithText;
 import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
-import org.apache.poi.hslf.record.StyleTextPropAtom;
 import org.apache.poi.hslf.record.TextHeaderAtom;
 import org.apache.poi.sl.draw.DrawPaint;
 import org.apache.poi.sl.usermodel.PaintStyle;
@@ -719,4 +717,75 @@ public final class TestBugs {
         assertEquals(textExp, textAct);
         ppt2.close();
     }
+    
+    @Test
+    public void bug45908() throws IOException {
+        HSLFSlideShow ppt1 = (HSLFSlideShow)SlideShowFactory.create(_slTests.getFile("bug45908.ppt"));
+
+        HSLFSlide slide = ppt1.getSlides().get(0);
+        HSLFAutoShape styleShape = (HSLFAutoShape)slide.getShapes().get(1);
+        HSLFTextParagraph tp0 = styleShape.getTextParagraphs().get(0);
+        HSLFTextRun tr0 = tp0.getTextRuns().get(0);
+
+
+        int rows = 5;
+        int cols = 2;
+        HSLFTable table = slide.createTable(rows, cols);
+        for (int i = 0; i < rows; i++) {
+            for (int j = 0; j < cols; j++) {
+
+                HSLFTableCell cell = table.getCell(i, j);
+                cell.setText("Test");
+
+                HSLFTextParagraph tp = cell.getTextParagraphs().get(0);
+                tp.setBulletStyle('%', tp0.getBulletColor(), tp0.getBulletFont(), tp0.getBulletSize());
+                tp.setIndent(tp0.getIndent());
+                tp.setAlignment(tp0.getTextAlign());
+                tp.setIndentLevel(tp0.getIndentLevel());
+                tp.setSpaceAfter(tp0.getSpaceAfter());
+                tp.setSpaceBefore(tp0.getSpaceBefore());
+                tp.setBulletStyle();
+                
+                HSLFTextRun tr = tp.getTextRuns().get(0);
+                tr.setBold(tr0.isBold());
+                // rt.setEmbossed();
+                tr.setFontColor(Color.BLACK);
+                tr.setFontFamily(tr0.getFontFamily());
+                tr.setFontSize(tr0.getFontSize());
+                tr.setItalic(tr0.isItalic());
+                tr.setShadowed(tr0.isShadowed());
+                tr.setStrikethrough(tr0.isStrikethrough());
+                tr.setUnderlined(tr0.isUnderlined());
+            }
+        }
+
+        table.moveTo(100, 100);
+
+        HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
+        ppt1.close();
+
+        HSLFTable tab = (HSLFTable)ppt2.getSlides().get(0).getShapes().get(2);
+        HSLFTableCell c2 = tab.getCell(0, 0);
+        HSLFTextParagraph tp1 = c2.getTextParagraphs().get(0);
+        HSLFTextRun tr1 = tp1.getTextRuns().get(0);
+        assertFalse(tp1.isBullet());
+        assertEquals(tp0.getBulletColor(), tp1.getBulletColor());
+        assertEquals(tp0.getBulletFont(), tp1.getBulletFont());
+        assertEquals(tp0.getBulletSize(), tp1.getBulletSize());
+        assertEquals(tp0.getIndent(), tp1.getIndent());
+        assertEquals(tp0.getTextAlign(), tp1.getTextAlign());
+        assertEquals(tp0.getIndentLevel(), tp1.getIndentLevel());
+        assertEquals(tp0.getSpaceAfter(), tp1.getSpaceAfter());
+        assertEquals(tp0.getSpaceBefore(), tp1.getSpaceBefore());
+        assertEquals(tr0.isBold(), tr1.isBold());
+        assertEquals(Color.black, DrawPaint.applyColorTransform(tr1.getFontColor().getSolidColor()));
+        assertEquals(tr0.getFontFamily(), tr1.getFontFamily());
+        assertEquals(tr0.getFontSize(), tr1.getFontSize());
+        assertEquals(tr0.isItalic(), tr1.isItalic());
+        assertEquals(tr0.isShadowed(), tr1.isShadowed());
+        assertEquals(tr0.isStrikethrough(), tr1.isStrikethrough());
+        assertEquals(tr0.isUnderlined(), tr1.isUnderlined());
+        
+        ppt2.close();
+    }
 }
diff --git a/test-data/slideshow/bug45908.ppt b/test-data/slideshow/bug45908.ppt
new file mode 100644 (file)
index 0000000..2bd1b1d
Binary files /dev/null and b/test-data/slideshow/bug45908.ppt differ