From 22c5121d982bf873fb08daf2079b65e728c7216e Mon Sep 17 00:00:00 2001 From: Evgeniy Berlog Date: Mon, 23 Jul 2012 09:06:56 +0000 Subject: [PATCH] added few improvements git-svn-id: https://svn.apache.org/repos/asf/poi/branches/gsoc2012@1364547 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/model/AbstractShape.java | 1 + .../apache/poi/hssf/model/ComboboxShape.java | 1 + .../apache/poi/hssf/model/CommentShape.java | 1 + .../org/apache/poi/hssf/model/LineShape.java | 1 + .../apache/poi/hssf/model/PolygonShape.java | 1 + .../poi/hssf/model/SimpleFilledShape.java | 1 + .../apache/poi/hssf/model/TextboxShape.java | 1 + .../apache/poi/hssf/usermodel/HSSFCell.java | 57 --------- .../poi/hssf/usermodel/HSSFObjectData.java | 24 +--- .../poi/hssf/usermodel/HSSFPatriarch.java | 46 ++++--- .../poi/hssf/usermodel/HSSFPicture.java | 22 ++-- .../apache/poi/hssf/usermodel/HSSFShape.java | 52 +++++++- .../hssf/usermodel/HSSFShapeContainer.java | 13 +- .../poi/hssf/usermodel/HSSFShapeGroup.java | 22 ++-- .../poi/hssf/usermodel/HSSFSimpleShape.java | 3 + .../poi/hssf/usermodel/HSSFTextbox.java | 8 -- .../poi/hssf/model/TestDrawingShapes.java | 114 ++++++++++++++++++ .../poi/hssf/usermodel/TestHSSFPicture.java | 10 +- .../poi/hssf/usermodel/TestShapeGroup.java | 33 +++++ test-data/spreadsheet/drawings.xls | Bin 851968 -> 851968 bytes 20 files changed, 280 insertions(+), 131 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/AbstractShape.java b/src/java/org/apache/poi/hssf/model/AbstractShape.java index 3fd8883d6..3441ba9f4 100644 --- a/src/java/org/apache/poi/hssf/model/AbstractShape.java +++ b/src/java/org/apache/poi/hssf/model/AbstractShape.java @@ -26,6 +26,7 @@ import org.apache.poi.hssf.usermodel.*; * * @author Glen Stampoultzis (glens at apache.org) */ +@Deprecated public abstract class AbstractShape { /** diff --git a/src/java/org/apache/poi/hssf/model/ComboboxShape.java b/src/java/org/apache/poi/hssf/model/ComboboxShape.java index 674fc46f0..ffca90a79 100644 --- a/src/java/org/apache/poi/hssf/model/ComboboxShape.java +++ b/src/java/org/apache/poi/hssf/model/ComboboxShape.java @@ -27,6 +27,7 @@ import org.apache.poi.hssf.usermodel.*; * * @author Yegor Kozlov */ +@Deprecated public class ComboboxShape extends AbstractShape { private EscherContainerRecord spContainer; diff --git a/src/java/org/apache/poi/hssf/model/CommentShape.java b/src/java/org/apache/poi/hssf/model/CommentShape.java index f739a44d7..19e83fff6 100644 --- a/src/java/org/apache/poi/hssf/model/CommentShape.java +++ b/src/java/org/apache/poi/hssf/model/CommentShape.java @@ -39,6 +39,7 @@ import org.apache.poi.hssf.usermodel.HSSFShape; * * @author Yegor Kozlov */ +@Deprecated public final class CommentShape extends TextboxShape { private NoteRecord _note; diff --git a/src/java/org/apache/poi/hssf/model/LineShape.java b/src/java/org/apache/poi/hssf/model/LineShape.java index 2480d4260..e4517e4d7 100644 --- a/src/java/org/apache/poi/hssf/model/LineShape.java +++ b/src/java/org/apache/poi/hssf/model/LineShape.java @@ -27,6 +27,7 @@ import org.apache.poi.hssf.usermodel.*; * * @author Glen Stampoultzis (glens at apache.org) */ +@Deprecated public class LineShape extends AbstractShape { diff --git a/src/java/org/apache/poi/hssf/model/PolygonShape.java b/src/java/org/apache/poi/hssf/model/PolygonShape.java index 22212c88d..1543e2df8 100644 --- a/src/java/org/apache/poi/hssf/model/PolygonShape.java +++ b/src/java/org/apache/poi/hssf/model/PolygonShape.java @@ -27,6 +27,7 @@ import org.apache.poi.hssf.usermodel.HSSFShape; import org.apache.poi.hssf.usermodel.HSSFPolygon; import org.apache.poi.util.LittleEndian; +@Deprecated public class PolygonShape extends AbstractShape { diff --git a/src/java/org/apache/poi/hssf/model/SimpleFilledShape.java b/src/java/org/apache/poi/hssf/model/SimpleFilledShape.java index 739e1ce32..1763b9d56 100644 --- a/src/java/org/apache/poi/hssf/model/SimpleFilledShape.java +++ b/src/java/org/apache/poi/hssf/model/SimpleFilledShape.java @@ -25,6 +25,7 @@ import org.apache.poi.hssf.record.EndSubRecord; import org.apache.poi.hssf.usermodel.HSSFSimpleShape; import org.apache.poi.hssf.usermodel.HSSFShape; +@Deprecated public class SimpleFilledShape extends AbstractShape { diff --git a/src/java/org/apache/poi/hssf/model/TextboxShape.java b/src/java/org/apache/poi/hssf/model/TextboxShape.java index 1ea79d094..1b2a154a1 100644 --- a/src/java/org/apache/poi/hssf/model/TextboxShape.java +++ b/src/java/org/apache/poi/hssf/model/TextboxShape.java @@ -28,6 +28,7 @@ import org.apache.poi.hssf.usermodel.*; * * @author Glen Stampoultzis (glens at apache.org) */ +@Deprecated public class TextboxShape extends AbstractShape { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 67fb64f22..1ad05587e 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -1041,63 +1041,6 @@ public class HSSFCell implements Cell { _sheet.getDrawingPatriarch().removeShape(comment); } - /** - * Cell comment finder. - * Returns cell comment for the specified sheet, row and column. - * - * @return cell comment or null if not found - */ - protected static HSSFComment findCellComment(InternalSheet sheet, int row, int column) { - // TODO - optimise this code by searching backwards, find NoteRecord first, quit if not found. Find one TXO by id - HSSFComment comment = null; - Map noteTxo = - new HashMap(); - int i = 0; - for (Iterator it = sheet.getRecords().iterator(); it.hasNext();) { - RecordBase rec = it.next(); - if (rec instanceof NoteRecord) { - NoteRecord note = (NoteRecord) rec; - if (note.getRow() == row && note.getColumn() == column) { - if(i < noteTxo.size()) { - TextObjectRecord txo = noteTxo.get(note.getShapeId()); - if(txo != null){ - comment = new HSSFComment(note, txo); - comment.setRow(note.getRow()); - comment.setColumn(note.getColumn()); - comment.setAuthor(note.getAuthor()); - comment.setVisible(note.getFlags() == NoteRecord.NOTE_VISIBLE); - comment.setString(txo.getStr()); - } else{ - log.log(POILogger.WARN, "Failed to match NoteRecord and TextObjectRecord, row: " + row + ", column: " + column); - } - } else { - log.log(POILogger.WARN, "Failed to match NoteRecord and TextObjectRecord, row: " + row + ", column: " + column); - } - break; - } - i++; - } else if (rec instanceof ObjRecord) { - ObjRecord obj = (ObjRecord) rec; - SubRecord sub = obj.getSubRecords().get(0); - if (sub instanceof CommonObjectDataSubRecord) { - CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) sub; - if (cmo.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT) { - //map ObjectId and corresponding TextObjectRecord, - //it will be used to match NoteRecord and TextObjectRecord - while (it.hasNext()) { - rec = it.next(); - if (rec instanceof TextObjectRecord) { - noteTxo.put(cmo.getObjectId(), (TextObjectRecord) rec); - break; - } - } - } - } - } - } - return comment; - } - /** * @return hyperlink associated with this cell or null if not found */ diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java b/src/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java index 565c89e78..dfe2b76a7 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java @@ -34,7 +34,7 @@ import org.apache.poi.util.HexDump; * * @author Daniel Noll */ -public final class HSSFObjectData extends HSSFShape { +public final class HSSFObjectData extends HSSFPicture { /** * Reference to the filesystem root, required for retrieving the object data. */ @@ -131,7 +131,7 @@ public final class HSSFObjectData extends HSSFShape { EscherAggregate agg = patriarch._getBoundAggregate(); agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord()); EscherBSERecord bse = - patriarch._sheet.getWorkbook().getWorkbook().getBSERecord(getPictureIndex()); + patriarch.getSheet().getWorkbook().getWorkbook().getBSERecord(getPictureIndex()); bse.setRef(bse.getRef() + 1); } @@ -143,24 +143,4 @@ public final class HSSFObjectData extends HSSFShape { ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise(); return new HSSFObjectData(spContainer, obj, _root); } - - public int getPictureIndex() { - EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.BLIP__BLIPTODISPLAY); - if (null == property) { - return -1; - } - return property.getPropertyValue(); - } - - public void setPictureIndex(int pictureIndex) { - setPropertyValue(new EscherSimpleProperty(EscherProperties.BLIP__BLIPTODISPLAY, false, true, pictureIndex)); - } - - @Override - void setShapeId(int shapeId) { - EscherSpRecord spRecord = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); - spRecord.setShapeId(shapeId); - CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) getObjRecord().getSubRecords().get(0); - cod.setObjectId((short) (shapeId % 1024)); - } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java index 8f4065120..9240998c2 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java @@ -18,6 +18,7 @@ package org.apache.poi.hssf.usermodel; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -48,7 +49,7 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing { * and building up our shapes from the records) */ private EscherAggregate _boundAggregate; - final HSSFSheet _sheet; // TODO make private + private final HSSFSheet _sheet; /** * Creates the patriarch. @@ -132,13 +133,6 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing { addShape(shape); //open existing file onCreate(shape); - EscherSpRecord sp = shape.getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); - if (shape.anchor.isHorizontallyFlipped()) { - sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ); - } - if (shape.anchor.isVerticallyFlipped()) { - sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPVERT); - } return shape; } @@ -155,14 +149,6 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing { addShape(shape); //open existing file onCreate(shape); - - EscherSpRecord sp = shape.getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); - if (shape.anchor.isHorizontallyFlipped()) { - sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ); - } - if (shape.anchor.isVerticallyFlipped()) { - sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPVERT); - } return shape; } @@ -232,7 +218,7 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing { * Returns a list of all shapes contained by the patriarch. */ public List getChildren() { - return _shapes; + return Collections.unmodifiableList(_shapes); } /** @@ -254,6 +240,7 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing { spgrContainer.addChildRecord(spContainer); shape.afterInsert(this); + setFlipFlags(shape); } /** @@ -279,6 +266,13 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing { _spgrRecord.setRectX2(x2); } + public void clear() { + ArrayList copy = new ArrayList(_shapes); + for (HSSFShape shape: copy){ + removeShape(shape); + } + } + int newShapeId() { DrawingManager2 dm = _sheet.getWorkbook().getWorkbook().getDrawingManager(); EscherDgRecord dg = @@ -395,4 +389,22 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing { } } } + + private void setFlipFlags(HSSFShape shape){ + EscherSpRecord sp = shape.getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); + if (shape.anchor.isHorizontallyFlipped()) { + sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ); + } + if (shape.anchor.isVerticallyFlipped()) { + sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPVERT); + } + } + + public Iterator iterator() { + return _shapes.iterator(); + } + + protected HSSFSheet getSheet() { + return _sheet; + } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java index 6bc713366..7d8618ba7 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java @@ -34,7 +34,7 @@ import org.apache.poi.hssf.model.InternalWorkbook; * @author Glen Stampoultzis * @author Yegor Kozlov (yegor at apache.org) */ -public final class HSSFPicture extends HSSFSimpleShape implements Picture { +public class HSSFPicture extends HSSFSimpleShape implements Picture { public static final int PICTURE_TYPE_EMF = HSSFWorkbook.PICTURE_TYPE_EMF; // Windows Enhanced Metafile public static final int PICTURE_TYPE_WMF = HSSFWorkbook.PICTURE_TYPE_WMF; // Windows Metafile public static final int PICTURE_TYPE_PICT = HSSFWorkbook.PICTURE_TYPE_PICT; // Macintosh PICT @@ -203,7 +203,7 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture { private float getColumnWidthInPixels(int column){ - int cw = _patriarch._sheet.getColumnWidth(column); + int cw = _patriarch.getSheet().getColumnWidth(column); float px = getPixelWidth(column); return cw/px; @@ -211,18 +211,18 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture { private float getRowHeightInPixels(int i){ - HSSFRow row = _patriarch._sheet.getRow(i); + HSSFRow row = _patriarch.getSheet().getRow(i); float height; if(row != null) height = row.getHeight(); - else height = _patriarch._sheet.getDefaultRowHeight(); + else height = _patriarch.getSheet().getDefaultRowHeight(); return height/PX_ROW; } private float getPixelWidth(int column){ - int def = _patriarch._sheet.getDefaultColumnWidth()*256; - int cw = _patriarch._sheet.getColumnWidth(column); + int def = _patriarch.getSheet().getDefaultColumnWidth()*256; + int cw = _patriarch.getSheet().getColumnWidth(column); return cw == def ? PX_DEFAULT : PX_MODIFIED; } @@ -233,7 +233,7 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture { * @return image dimension */ public Dimension getImageDimension(){ - EscherBSERecord bse = _patriarch._sheet._book.getBSERecord(getPictureIndex()); + EscherBSERecord bse = _patriarch.getSheet()._book.getBSERecord(getPictureIndex()); byte[] data = bse.getBlipRecord().getPicturedata(); int type = bse.getBlipTypeWin32(); return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type); @@ -245,7 +245,7 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture { * @return picture data for this shape */ public HSSFPictureData getPictureData(){ - InternalWorkbook iwb = _patriarch._sheet.getWorkbook().getWorkbook(); + InternalWorkbook iwb = _patriarch.getSheet().getWorkbook().getWorkbook(); EscherBlipRecord blipRecord = iwb.getBSERecord(getPictureIndex()).getBlipRecord(); return new HSSFPictureData(blipRecord); } @@ -255,14 +255,14 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture { EscherAggregate agg = patriarch._getBoundAggregate(); agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord()); EscherBSERecord bse = - patriarch._sheet.getWorkbook().getWorkbook().getBSERecord(getPictureIndex()); + patriarch.getSheet().getWorkbook().getWorkbook().getBSERecord(getPictureIndex()); bse.setRef(bse.getRef() + 1); } /** * The color applied to the lines of this shape. */ - public String getAdditionalData() { + public String getFileName() { EscherComplexProperty propFile = (EscherComplexProperty) getOptRecord().lookup( EscherProperties.BLIP__BLIPFILENAME); try { @@ -275,7 +275,7 @@ public final class HSSFPicture extends HSSFSimpleShape implements Picture { } } - public void setAdditionalData(String data){ + public void setFileName(String data){ try { EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, true, data.getBytes("UTF-16LE")); setPropertyValue(prop); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFShape.java b/src/java/org/apache/poi/hssf/usermodel/HSSFShape.java index 137ead3c7..ba7a86670 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFShape.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFShape.java @@ -20,7 +20,10 @@ package org.apache.poi.hssf.usermodel; import org.apache.poi.ddf.*; import org.apache.poi.hssf.record.CommonObjectDataSubRecord; import org.apache.poi.hssf.record.ObjRecord; +import org.apache.poi.util.LittleEndian; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.Iterator; /** @@ -100,7 +103,7 @@ public abstract class HSSFShape { EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID); spRecord.setShapeId(shapeId); CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) _objRecord.getSubRecords().get(0); - cod.setObjectId((short) (shapeId-1024)); + cod.setObjectId((short) (shapeId%1024)); } int getShapeId(){ @@ -295,6 +298,53 @@ public abstract class HSSFShape { protected void setPropertyValue(EscherProperty property){ _optRecord.setEscherProperty(property); } + + public void setFlipVertical(boolean value){ + EscherSpRecord sp = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); + if (value){ + sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPVERT); + } else { + sp.setFlags(sp.getFlags() & (Integer.MAX_VALUE - EscherSpRecord.FLAG_FLIPVERT)); + } + } + + public void setFlipHorizontal(boolean value){ + EscherSpRecord sp = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); + if (value){ + sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ); + } else { + sp.setFlags(sp.getFlags() & (Integer.MAX_VALUE - EscherSpRecord.FLAG_FLIPHORIZ)); + } + } + + public boolean isFlipVertical(){ + EscherSpRecord sp = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); + return (sp.getFlags() & EscherSpRecord.FLAG_FLIPVERT) != 0; + } + + public boolean isFlipHorizontal(){ + EscherSpRecord sp = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID); + return (sp.getFlags() & EscherSpRecord.FLAG_FLIPHORIZ) != 0; + } + + public int getRotationDegree(){ + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.TRANSFORM__ROTATION); + if (null == property){ + return 0; + } + try { + LittleEndian.putInt(property.getPropertyValue(), bos); + return LittleEndian.getShort(bos.toByteArray(), 2); + } catch (IOException e) { + e.printStackTrace(); + return 0; + } + } + + public void setRotationDegree(short value){ + setPropertyValue(new EscherSimpleProperty(EscherProperties.TRANSFORM__ROTATION , (value << 16))); + } /** * Count of all children and their children's children. diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFShapeContainer.java b/src/java/org/apache/poi/hssf/usermodel/HSSFShapeContainer.java index 181b3d17a..1d7748edc 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFShapeContainer.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFShapeContainer.java @@ -24,12 +24,12 @@ import java.util.List; * * @author Glen Stampoultzis (glens at apache.org) */ -public interface HSSFShapeContainer +public interface HSSFShapeContainer extends Iterable { /** * @return Any children contained by this shape. */ - List getChildren(); + List getChildren(); /** * add shape to the list of child records @@ -42,4 +42,13 @@ public interface HSSFShapeContainer */ void setCoordinates( int x1, int y1, int x2, int y2 ); + void clear(); + + public int getX1(); + + public int getY1(); + + public int getX2(); + + public int getY2(); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java b/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java index 7cf6803fd..1c8ac95df 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java @@ -18,10 +18,10 @@ package org.apache.poi.hssf.usermodel; import org.apache.poi.ddf.*; -import org.apache.poi.hssf.model.TextboxShape; import org.apache.poi.hssf.record.*; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Iterator; @@ -32,7 +32,7 @@ import java.util.Iterator; * @author Glen Stampoultzis (glens at apache.org) */ public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer { - List shapes = new ArrayList(); + private final List shapes = new ArrayList(); private EscherSpgrRecord _spgrRecord; public HSSFShapeGroup(EscherContainerRecord spgrContainer, ObjRecord objRecord) { @@ -172,11 +172,6 @@ public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer { shapes.add(shape); } - public void addTextBox(TextboxShape textboxShape) { -// HSSFTextbox shape = new HSSFTextbox(this, textboxShape.geanchor); -// shapes.add(textboxShape); - } - /** * Create a new simple shape under this group. * @@ -258,7 +253,7 @@ public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer { * Return all children contained by this shape. */ public List getChildren() { - return shapes; + return Collections.unmodifiableList(shapes); } /** @@ -272,6 +267,13 @@ public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer { _spgrRecord.setRectY2(y2); } + public void clear() { + ArrayList copy = new ArrayList(shapes); + for (HSSFShape shape: copy){ + removeShape(shape); + } + } + /** * The top left x coordinate of this group. */ @@ -377,4 +379,8 @@ public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer { } return isRemoved; } + + public Iterator iterator() { + return shapes.iterator(); + } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java index 258ee011a..77d23f3f1 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSimpleShape.java @@ -177,6 +177,9 @@ public class HSSFSimpleShape extends HSSFShape // If font is not set we must set the default one if (rtr.numFormattingRuns() == 0) rtr.applyFont((short) 0); _textObjectRecord.setStr(rtr); + if (string.getString() != null){ + setPropertyValue(new EscherSimpleProperty(EscherProperties.TEXT__TEXTID, string.getString().hashCode())); + } } @Override diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java b/src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java index 872a19522..9f859ee3a 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java @@ -129,14 +129,6 @@ public class HSSFTextbox extends HSSFSimpleShape { return spContainer; } - @Override - public void setString(RichTextString string) { - HSSFRichTextString rtr = (HSSFRichTextString) string; - // If font is not set we must set the default one - if (rtr.numFormattingRuns() == 0) rtr.applyFont((short) 0); - getTextObjectRecord().setStr(rtr); - } - @Override void afterInsert(HSSFPatriarch patriarch) { EscherAggregate agg = patriarch._getBoundAggregate(); diff --git a/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java b/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java index e31a6ecbf..23dc0526c 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java +++ b/src/testcases/org/apache/poi/hssf/model/TestDrawingShapes.java @@ -136,6 +136,8 @@ public class TestDrawingShapes extends TestCase { rectangle.setNoFill(true); rectangle.setString(new HSSFRichTextString("teeeest")); assertEquals(rectangle.getLineStyleColor(), 1111); + assertEquals(((EscherSimpleProperty)((EscherOptRecord)rectangle.getEscherContainer().getChildById(EscherOptRecord.RECORD_ID)) + .lookup(EscherProperties.TEXT__TEXTID)).getPropertyValue(), "teeeest".hashCode()); assertEquals(rectangle.isNoFill(), true); assertEquals(rectangle.getString().getString(), "teeeest"); @@ -225,6 +227,7 @@ public class TestDrawingShapes extends TestCase { assertEquals(HexDump.toHex(shape.getFillColor()), shape.getFillColor(), 0x2CE03D); assertEquals(shape.getLineWidth(), HSSFShape.LINEWIDTH_ONE_PT * 2); assertEquals(shape.getString().getString(), "POItest"); + assertEquals(shape.getRotationDegree(), 27); } public void testShapeIds() { @@ -536,4 +539,115 @@ public class TestDrawingShapes extends TestCase { assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0); assertEquals(patriarch.getChildren().size(), 0); } + + public void testShapeFlip(){ + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor()); + rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); + + assertEquals(rectangle.isFlipVertical(), false); + assertEquals(rectangle.isFlipHorizontal(), false); + + rectangle.setFlipVertical(true); + assertEquals(rectangle.isFlipVertical(), true); + rectangle.setFlipHorizontal(true); + assertEquals(rectangle.isFlipHorizontal(), true); + + wb = HSSFTestDataSamples.writeOutAndReadBack(wb); + sheet = wb.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + + rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0); + + assertEquals(rectangle.isFlipHorizontal(), true); + rectangle.setFlipHorizontal(false); + assertEquals(rectangle.isFlipHorizontal(), false); + + assertEquals(rectangle.isFlipVertical(), true); + rectangle.setFlipVertical(false); + assertEquals(rectangle.isFlipVertical(), false); + + wb = HSSFTestDataSamples.writeOutAndReadBack(wb); + sheet = wb.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + + rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0); + + assertEquals(rectangle.isFlipVertical(), false); + assertEquals(rectangle.isFlipHorizontal(), false); + } + + public void testRotation() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor(0,0,100,100, (short) 0,0,(short)5,5)); + rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); + + assertEquals(rectangle.getRotationDegree(), 0); + rectangle.setRotationDegree((short) 45); + assertEquals(rectangle.getRotationDegree(), 45); + rectangle.setFlipHorizontal(true); + + wb = HSSFTestDataSamples.writeOutAndReadBack(wb); + sheet = wb.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0); + assertEquals(rectangle.getRotationDegree(), 45); + rectangle.setRotationDegree((short) 30); + assertEquals(rectangle.getRotationDegree(), 30); + + patriarch.setCoordinates(0, 0, 10, 10); + rectangle.setString(new HSSFRichTextString("1234")); + } + + public void testShapeContainerImplementsIterable(){ + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + patriarch.createSimpleShape(new HSSFClientAnchor()); + patriarch.createSimpleShape(new HSSFClientAnchor()); + + int i=2; + + for (HSSFShape shape: patriarch){ + i--; + } + assertEquals(i, 0); + } + + public void testClearShapesForPatriarch(){ + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + patriarch.createSimpleShape(new HSSFClientAnchor()); + patriarch.createSimpleShape(new HSSFClientAnchor()); + patriarch.createCellComment(new HSSFClientAnchor()); + + EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch); + + assertEquals(agg.getShapeToObjMapping().size(), 6); + assertEquals(agg.getTailRecords().size(), 1); + assertEquals(patriarch.getChildren().size(), 3); + + patriarch.clear(); + + assertEquals(agg.getShapeToObjMapping().size(), 0); + assertEquals(agg.getTailRecords().size(), 0); + assertEquals(patriarch.getChildren().size(), 0); + + wb = HSSFTestDataSamples.writeOutAndReadBack(wb); + sheet = wb.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + + assertEquals(agg.getShapeToObjMapping().size(), 0); + assertEquals(agg.getTailRecords().size(), 0); + assertEquals(patriarch.getChildren().size(), 0); + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java index 234cddccf..dcc611c05 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java @@ -184,7 +184,7 @@ public final class TestHSSFPicture extends BaseTestPicture { assertEquals(1, drawing.getChildren().size()); HSSFPicture picture = (HSSFPicture) drawing.getChildren().get(0); - assertEquals(picture.getAdditionalData(), "test"); + assertEquals(picture.getFileName(), "test"); } public void testSetGetProperties(){ @@ -199,15 +199,15 @@ public final class TestHSSFPicture extends BaseTestPicture { int idx1 = wb.addPicture(data1, Workbook.PICTURE_TYPE_JPEG); HSSFPicture p1 = dr.createPicture(anchor, idx1); - assertEquals(p1.getAdditionalData(), ""); - p1.setAdditionalData("aaa"); - assertEquals(p1.getAdditionalData(), "aaa"); + assertEquals(p1.getFileName(), ""); + p1.setFileName("aaa"); + assertEquals(p1.getFileName(), "aaa"); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sh = wb.getSheet("Pictures"); dr = sh.getDrawingPatriarch(); p1 = (HSSFPicture) dr.getChildren().get(0); - assertEquals(p1.getAdditionalData(), "aaa"); + assertEquals(p1.getFileName(), "aaa"); } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java b/src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java index f0ce3f280..d5ea732d0 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestShapeGroup.java @@ -4,6 +4,7 @@ import junit.framework.TestCase; import org.apache.poi.ddf.EscherContainerRecord; import org.apache.poi.ddf.EscherSpgrRecord; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.hssf.record.EscherAggregate; import org.apache.poi.hssf.record.ObjRecord; import java.lang.reflect.Field; @@ -226,4 +227,36 @@ public class TestShapeGroup extends TestCase{ } return null; } + + public void testClearShapes(){ + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); + + group.createShape(new HSSFChildAnchor()); + group.createShape(new HSSFChildAnchor()); + + EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch); + + assertEquals(agg.getShapeToObjMapping().size(), 5); + assertEquals(agg.getTailRecords().size(), 0); + assertEquals(group.getChildren().size(), 2); + + group.clear(); + + assertEquals(agg.getShapeToObjMapping().size(), 1); + assertEquals(agg.getTailRecords().size(), 0); + assertEquals(group.getChildren().size(), 0); + + wb = HSSFTestDataSamples.writeOutAndReadBack(wb); + sheet = wb.getSheetAt(0); + patriarch = sheet.getDrawingPatriarch(); + + group = (HSSFShapeGroup) patriarch.getChildren().get(0); + + assertEquals(agg.getShapeToObjMapping().size(), 1); + assertEquals(agg.getTailRecords().size(), 0); + assertEquals(group.getChildren().size(), 0); + } } diff --git a/test-data/spreadsheet/drawings.xls b/test-data/spreadsheet/drawings.xls index 45b9a958870d9362f8ade14e7b93e6b5d7159207..69fb3af3d19fc51bb60124e10e377bb3de9676f7 100644 GIT binary patch delta 2601 zcmZ`*3sjRw7M?!|NeqJ^AfSK*?TqbR8ZEa zqlgk-E^ma0Da2io2rUhfhxp{F<$x#(l`2vn?ED0+yXS0jzRdi0?w#+O`_J5rP-rg{ z+FyyJ(HnFQ=vRk};%G+*ebE#xVlF3Q6U{VSNvK*$oLZX$bLHz96IHaHzFHz&%O?mR z8FS3z2>8Ab06+&}anXq}@rQvHSdvCsm>_0PNJ3&958EIhj*&JRNZ=(WkUsNIt9(MD zqoR2H1ni>;+!1MGfSBmW{bA93(j+UGHL0c|BjYctB6(54ywFgdNR#hRA*@Av`3t$a zIGln}fI-EWi$TLs$56u{$LJVyFc=ujA8Yw(wR{4DfIGLqlRY)J0ihxK*PN%bO+uZ~-OqbJ8&}TuWiK-kh-Jo^45;W3k`s zeoya43hZqD4`G%Rqid$;$dMY4BM#O}x2+EQI5hM?+i zS{ihD!!rA@!{_7qR()=EX;IN#=hT01^-11V;`?HIowk31wwNAJ4|e1rp5DpEtt^?XK>9+&bP+KZu`32`a_z8epbx- zkQYA*T2s50-M1+^z2o0C`KQK$UT_Cr#c}Ue2v;jgEPemcnXcWk*Lv$j;zaC3(bU}@ zzLP_LcK6WmQ2c~!s_#A9RO3(1E#Zo!2RG(r%6GSvb|ron@Fu-6BU?sq zf-Z^s84Le|OBQNq28T-%z0w}X8Z;GWSj2pDs=!%r$n`|9SKI?F*BII2@%otOu7>YU zmA!B<+v4TWRkvy+%4{dSx(zIMb?=Wc7JiShX zuM=k#dhwOuT$<^}MZwKE8_u0iIjmN0`Rg^q!;!M)BvznX$o(vdcDN zHM8v+tGxxuxl7L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