Introduce dirty flag for paragraphs and store them to records on save

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/common_sl@1691774 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2015-07-18 20:35:28 +00:00
parent f7b8e88bda
commit 9b09cb683a
4 changed files with 57 additions and 12 deletions

View File

@ -442,6 +442,20 @@ public final class HSLFSlideShow implements SlideShow {
* OutputStream * OutputStream
*/ */
public void write(OutputStream out) throws IOException { 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;
boolean isDirty = false;
for (HSLFTextParagraph p : hts.getTextParagraphs()) {
isDirty |= p.isDirty();
}
if (isDirty) hts.storeText();
}
}
_hslfSlideShow.write(out); _hslfSlideShow.write(out);
} }

View File

@ -65,6 +65,8 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
private StyleTextProp9Atom styleTextProp9Atom; private StyleTextProp9Atom styleTextProp9Atom;
private boolean _dirty = false;
/** /**
* Constructs a Text Run from a Unicode text block. * Constructs a Text Run from a Unicode text block.
* Either a {@link TextCharsAtom} or a {@link TextBytesAtom} needs to be provided. * Either a {@link TextCharsAtom} or a {@link TextBytesAtom} needs to be provided.
@ -265,7 +267,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
@Override @Override
public void setLeftMargin(Double leftMargin) { public void setLeftMargin(Double leftMargin) {
Integer val = (leftMargin == null) ? null : Units.pointsToMaster(leftMargin); Integer val = (leftMargin == null) ? null : Units.pointsToMaster(leftMargin);
setPropVal(_paragraphStyle, "text.offset", val); setParagraphTextPropVal("text.offset", val);
} }
@Override @Override
@ -288,7 +290,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
@Override @Override
public void setIndent(Double indent) { public void setIndent(Double indent) {
Integer val = (indent == null) ? null : Units.pointsToMaster(indent); Integer val = (indent == null) ? null : Units.pointsToMaster(indent);
setPropVal(_paragraphStyle, "bullet.offset", val); setParagraphTextPropVal("bullet.offset", val);
} }
@Override @Override
@ -327,7 +329,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
case JUSTIFY_LOW: alignInt = TextAlignmentProp.JUSTIFYLOW; break; case JUSTIFY_LOW: alignInt = TextAlignmentProp.JUSTIFYLOW; break;
case THAI_DIST: alignInt = TextAlignmentProp.THAIDISTRIBUTED; break; case THAI_DIST: alignInt = TextAlignmentProp.THAIDISTRIBUTED; break;
} }
setPropVal(_paragraphStyle, "alignment", alignInt); setParagraphTextPropVal("alignment", alignInt);
} }
@Override @Override
@ -455,7 +457,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
*/ */
public void setBulletChar(Character c) { public void setBulletChar(Character c) {
Integer val = (c == null) ? null : (int)c.charValue(); Integer val = (c == null) ? null : (int)c.charValue();
setPropVal(_paragraphStyle, "bullet.char", val); setParagraphTextPropVal("bullet.char", val);
} }
/** /**
@ -485,7 +487,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
*/ */
public void setBulletColor(Color color) { public void setBulletColor(Color color) {
Integer val = (color == null) ? null : new Color(color.getBlue(), color.getGreen(), color.getRed(), 254).getRGB(); Integer val = (color == null) ? null : new Color(color.getBlue(), color.getGreen(), color.getRed(), 254).getRGB();
setPropVal(_paragraphStyle, "bullet.color", val); setParagraphTextPropVal("bullet.color", val);
} }
/** /**
@ -513,7 +515,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
FontCollection fc = getSheet().getSlideShow().getFontCollection(); FontCollection fc = getSheet().getSlideShow().getFontCollection();
int idx = fc.addFont(typeface); int idx = fc.addFont(typeface);
setPropVal(_paragraphStyle, "bullet.font", idx); setParagraphTextPropVal("bullet.font", idx);
setFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX, true); setFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX, true);
} }
@ -576,7 +578,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
if (dval != null) { if (dval != null) {
ival = (dval < 0) ? Units.pointsToMaster(dval) : dval.intValue(); ival = (dval < 0) ? Units.pointsToMaster(dval) : dval.intValue();
} }
setPropVal(_paragraphStyle, propName, ival); setParagraphTextPropVal(propName, ival);
} }
private boolean getFlag(int index) { private boolean getFlag(int index) {
@ -587,6 +589,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
private void setFlag(int index, boolean value) { private void setFlag(int index, boolean value) {
BitMaskTextProp tp = (BitMaskTextProp)_paragraphStyle.addWithName(ParagraphFlagsTextProp.NAME); BitMaskTextProp tp = (BitMaskTextProp)_paragraphStyle.addWithName(ParagraphFlagsTextProp.NAME);
tp.setSubValue(value, index); tp.setSubValue(value, index);
setDirty();
} }
/** /**
@ -813,6 +816,10 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
throw new RuntimeException("failed dummy write", e); throw new RuntimeException("failed dummy write", e);
} }
} }
for (HSLFTextParagraph p : paragraphs) {
p._dirty = false;
}
} }
/** /**
@ -1260,4 +1267,25 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
} }
return new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed()); return new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
} }
/**
* Sets the value of the given Paragraph TextProp, add if required
* @param propName The name of the Paragraph TextProp
* @param val The value to set for the TextProp
*/
public void setParagraphTextPropVal(String propName, Integer val) {
setPropVal(_paragraphStyle, propName, val);
setDirty();
}
/**
* marks this paragraph dirty, so its records will be renewed on save
*/
public void setDirty() {
_dirty = true;
}
public boolean isDirty() {
return _dirty;
}
} }

View File

@ -18,7 +18,6 @@
package org.apache.poi.hslf.usermodel; package org.apache.poi.hslf.usermodel;
import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.getPropVal; import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.getPropVal;
import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.setPropVal;
import java.awt.Color; import java.awt.Color;
@ -133,7 +132,10 @@ public final class HSLFTextRun implements TextRun {
*/ */
private void setCharFlagsTextPropVal(int index, boolean value) { private void setCharFlagsTextPropVal(int index, boolean value) {
// TODO: check if paragraph/chars can be handled the same ... // TODO: check if paragraph/chars can be handled the same ...
if (getFlag(index) != value) setFlag(index, value); if (getFlag(index) != value) {
setFlag(index, value);
parentParagraph.setDirty();
}
} }
/** /**
@ -142,7 +144,8 @@ public final class HSLFTextRun implements TextRun {
* @param val The value to set for the TextProp * @param val The value to set for the TextProp
*/ */
public void setCharTextPropVal(String propName, Integer val) { public void setCharTextPropVal(String propName, Integer val) {
setPropVal(characterStyle, propName, val); HSLFTextParagraph.setPropVal(characterStyle, propName, val);
parentParagraph.setDirty();
} }
@ -248,7 +251,7 @@ public final class HSLFTextRun implements TextRun {
* @param val the percentage of the font size. If the value is positive, it is superscript, otherwise it is subscript * @param val the percentage of the font size. If the value is positive, it is superscript, otherwise it is subscript
*/ */
public void setSuperscript(int val) { public void setSuperscript(int val) {
setPropVal(characterStyle, "superscript", val); setCharTextPropVal("superscript", val);
} }
@Override @Override

View File

@ -525,7 +525,7 @@ public final class TestTextRun {
rt.setFontColor(Color.RED); rt.setFontColor(Color.RED);
} }
} }
tx.storeText(); // tx.storeText();
} }
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();