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 d8bf3aae84
commit cec1989a51
4 changed files with 57 additions and 12 deletions

View File

@ -442,6 +442,20 @@ public final class HSLFSlideShow implements SlideShow {
* OutputStream
*/
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);
}

View File

@ -64,6 +64,8 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
private int shapeId;
private StyleTextProp9Atom styleTextProp9Atom;
private boolean _dirty = false;
/**
* Constructs a Text Run from a Unicode text block.
@ -265,7 +267,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
@Override
public void setLeftMargin(Double leftMargin) {
Integer val = (leftMargin == null) ? null : Units.pointsToMaster(leftMargin);
setPropVal(_paragraphStyle, "text.offset", val);
setParagraphTextPropVal("text.offset", val);
}
@Override
@ -288,7 +290,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
@Override
public void setIndent(Double indent) {
Integer val = (indent == null) ? null : Units.pointsToMaster(indent);
setPropVal(_paragraphStyle, "bullet.offset", val);
setParagraphTextPropVal("bullet.offset", val);
}
@Override
@ -327,7 +329,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
case JUSTIFY_LOW: alignInt = TextAlignmentProp.JUSTIFYLOW; break;
case THAI_DIST: alignInt = TextAlignmentProp.THAIDISTRIBUTED; break;
}
setPropVal(_paragraphStyle, "alignment", alignInt);
setParagraphTextPropVal("alignment", alignInt);
}
@Override
@ -455,7 +457,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
*/
public void setBulletChar(Character c) {
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) {
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();
int idx = fc.addFont(typeface);
setPropVal(_paragraphStyle, "bullet.font", idx);
setParagraphTextPropVal("bullet.font", idx);
setFlag(ParagraphFlagsTextProp.BULLET_HARDFONT_IDX, true);
}
@ -576,7 +578,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
if (dval != null) {
ival = (dval < 0) ? Units.pointsToMaster(dval) : dval.intValue();
}
setPropVal(_paragraphStyle, propName, ival);
setParagraphTextPropVal(propName, ival);
}
private boolean getFlag(int index) {
@ -587,6 +589,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
private void setFlag(int index, boolean value) {
BitMaskTextProp tp = (BitMaskTextProp)_paragraphStyle.addWithName(ParagraphFlagsTextProp.NAME);
tp.setSubValue(value, index);
setDirty();
}
/**
@ -813,6 +816,10 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFTextRun> {
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());
}
/**
* 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;
import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.getPropVal;
import static org.apache.poi.hslf.usermodel.HSLFTextParagraph.setPropVal;
import java.awt.Color;
@ -133,7 +132,10 @@ public final class HSLFTextRun implements TextRun {
*/
private void setCharFlagsTextPropVal(int index, boolean value) {
// 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
*/
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
*/
public void setSuperscript(int val) {
setPropVal(characterStyle, "superscript", val);
setCharTextPropVal("superscript", val);
}
@Override

View File

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