--- /dev/null
+package org.apache.poi.hssf.usermodel;\r
+\r
+import org.apache.poi.ddf.*;\r
+import org.apache.poi.hssf.record.*;\r
+\r
+/**\r
+ * @author Evgeniy Berlog\r
+ * @date 12.07.12\r
+ */\r
+public class HSSFCombobox extends HSSFSimpleShape {\r
+\r
+ public HSSFCombobox(EscherContainerRecord spContainer, ObjRecord objRecord) {\r
+ super(spContainer, objRecord);\r
+ }\r
+\r
+ public HSSFCombobox(HSSFShape parent, HSSFAnchor anchor) {\r
+ super(parent, anchor);\r
+ super.setShapeType(OBJECT_TYPE_COMBO_BOX);\r
+ }\r
+\r
+ @Override\r
+ protected EscherContainerRecord createSpContainer() {\r
+ EscherContainerRecord spContainer = new EscherContainerRecord();\r
+ EscherSpRecord sp = new EscherSpRecord();\r
+ EscherOptRecord opt = new EscherOptRecord();\r
+ EscherClientDataRecord clientData = new EscherClientDataRecord();\r
+\r
+ spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);\r
+ spContainer.setOptions((short) 0x000F);\r
+ sp.setRecordId(EscherSpRecord.RECORD_ID);\r
+ sp.setOptions((short) ((EscherAggregate.ST_HOSTCONTROL << 4) | 0x2));\r
+\r
+ sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE);\r
+ opt.setRecordId(EscherOptRecord.RECORD_ID);\r
+ opt.addEscherProperty(new EscherBoolProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 17039620));\r
+ opt.addEscherProperty(new EscherBoolProperty(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 0x00080008));\r
+ opt.addEscherProperty(new EscherBoolProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080000));\r
+ opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GROUPSHAPE__PRINT, 0x00020000));\r
+\r
+ HSSFClientAnchor userAnchor = (HSSFClientAnchor) getAnchor();\r
+ userAnchor.setAnchorType(1);\r
+ EscherRecord anchor = userAnchor.getEscherAnchor();\r
+ clientData.setRecordId(EscherClientDataRecord.RECORD_ID);\r
+ clientData.setOptions((short) 0x0000);\r
+\r
+ spContainer.addChildRecord(sp);\r
+ spContainer.addChildRecord(opt);\r
+ spContainer.addChildRecord(anchor);\r
+ spContainer.addChildRecord(clientData);\r
+\r
+ return spContainer;\r
+ }\r
+\r
+ @Override\r
+ protected ObjRecord createObjRecord() {\r
+ ObjRecord obj = new ObjRecord();\r
+ CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();\r
+ c.setObjectType(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX);\r
+ c.setLocked(true);\r
+ c.setPrintable(false);\r
+ c.setAutofill(true);\r
+ c.setAutoline(false);\r
+ FtCblsSubRecord f = new FtCblsSubRecord();\r
+ LbsDataSubRecord l = LbsDataSubRecord.newAutoFilterInstance();\r
+ EndSubRecord e = new EndSubRecord();\r
+ obj.addSubRecord(c);\r
+ obj.addSubRecord(f);\r
+ obj.addSubRecord(l);\r
+ obj.addSubRecord(e);\r
+ return obj;\r
+ }\r
+\r
+ @Override\r
+ public void setShapeType(int shapeType) {\r
+ throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());\r
+ }\r
+}\r
public HSSFShapeGroup createGroup(HSSFClientAnchor anchor)
{
HSSFShapeGroup group = new HSSFShapeGroup(null, anchor);
- group.anchor = anchor;
addShape(group);
onCreate(group);
return group;
public HSSFSimpleShape createSimpleShape(HSSFClientAnchor anchor)
{
HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
- shape.anchor = anchor;
addShape(shape);
//open existing file
onCreate(shape);
{
HSSFPicture shape = new HSSFPicture(null, anchor);
shape.setPictureIndex( pictureIndex );
- shape.anchor = anchor;
addShape(shape);
//open existing file
onCreate(shape);
public HSSFPolygon createPolygon(HSSFClientAnchor anchor)
{
HSSFPolygon shape = new HSSFPolygon(null, anchor);
- shape.anchor = anchor;
addShape(shape);
onCreate(shape);
return shape;
public HSSFTextbox createTextbox(HSSFClientAnchor anchor)
{
HSSFTextbox shape = new HSSFTextbox(null, anchor);
- shape.anchor = anchor;
addShape(shape);
onCreate(shape);
return shape;
public HSSFComment createComment(HSSFAnchor anchor)
{
HSSFComment shape = new HSSFComment(null, anchor);
- shape.anchor = anchor;
addShape(shape);
onCreate(shape);
return shape;
*/
HSSFSimpleShape createComboBox(HSSFAnchor anchor)
{
- HSSFSimpleShape shape = new HSSFSimpleShape(null, anchor);
- shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX);
- shape.anchor = anchor;
+ HSSFCombobox shape = new HSSFCombobox(null, anchor);
addShape(shape);
+ onCreate(shape);
return shape;
}
public HSSFPicture( HSSFShape parent, HSSFAnchor anchor )
{
super( parent, anchor );
- setShapeType(OBJECT_TYPE_PICTURE);
+ super.setShapeType(OBJECT_TYPE_PICTURE);
}
public int getPictureIndex()
System.out.println("Unsupported encoding: UTF-16LE");
}
}
+
+ @Override
+ public void setShapeType(int shapeType) {
+ throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
+ }
}
case CommonObjectDataSubRecord.OBJECT_TYPE_LINE:\r
shape = new HSSFSimpleShape(container, objRecord);\r
break;\r
+ case CommonObjectDataSubRecord.OBJECT_TYPE_COMBO_BOX:\r
+ shape = new HSSFCombobox(container, objRecord);\r
+ break;\r
case CommonObjectDataSubRecord.OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING:\r
EscherOptRecord optRecord = container.getChildById(EscherOptRecord.RECORD_ID);\r
EscherProperty property = optRecord.lookup(EscherProperties.GEOMETRY__VERTICES);\r
if (null != shape){\r
out.addShape(shape);\r
}\r
-// if (null != objRecord){\r
-// HSSFShape shape = shapeCreator.createNewShape(spRecord.getShapeType(), container, objRecord);\r
-// out.addShape(shape);\r
-// }\r
-// if (null != txtRecord){\r
-// //TODO resolve textbox\r
-//// TextboxShape shape = new TextboxShape(container, txtRecord);\r
-//// out.a\r
-// }\r
-////\r
-//// //TODO decide what shape to create based on ObjRecord / EscherSpRecord\r
-//// HSSFShape shape = new HSSFUnknownShape(container, objRecord);\r
-//// out.addShape(shape);\r
}\r
}\r
}\r
public HSSFSimpleShape(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord _textObjectRecord) {
super(spContainer, objRecord);
- this._textObjectRecord = _textObjectRecord;
+ this._textObjectRecord = _textObjectRecord == null ? createTextObjRecord() : _textObjectRecord;
}
public HSSFSimpleShape(EscherContainerRecord spContainer, ObjRecord objRecord) {
super(spContainer, objRecord);
+ this._textObjectRecord = createTextObjRecord();
}
public HSSFSimpleShape( HSSFShape parent, HSSFAnchor anchor)
* @param string Sets the rich text string used by this object.
*/
public void setString(RichTextString string) {
+ //TODO add other shape types which can not contain text
+ if (getShapeType() == 0 || getShapeType() == OBJECT_TYPE_LINE){
+ throw new IllegalStateException("Cannot set text for shape type: "+getShapeType());
+ }
HSSFRichTextString rtr = (HSSFRichTextString) string;
// If font is not set we must set the default one
if (rtr.numFormattingRuns() == 0) rtr.applyFont((short) 0);
import org.apache.poi.util.HexDump;\r
\r
import java.io.IOException;\r
+import java.util.Arrays;\r
\r
import static junit.framework.Assert.assertEquals;\r
\r
EscherDgRecord dgRecord = (EscherDgRecord) aggregate.getEscherRecord(0).getChild(0);\r
assertEquals(dgRecord.getNumShapes(), 1);\r
}\r
+\r
+ public void testTextForSimpleShape(){\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sheet = wb.createSheet();\r
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();\r
+\r
+ HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor());\r
+ shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);\r
+\r
+ EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch);\r
+ assertNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));\r
+ assertEquals(agg.getShapeToObjMapping().size(), 1);\r
+\r
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+ sheet = wb.getSheetAt(0);\r
+ patriarch = sheet.getDrawingPatriarch();\r
+\r
+ shape = (HSSFSimpleShape) patriarch.getChildren().get(0);\r
+\r
+ agg = HSSFTestHelper.getEscherAggregate(patriarch);\r
+ assertNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));\r
+ assertEquals(agg.getShapeToObjMapping().size(), 1);\r
+\r
+ shape.setString(new HSSFRichTextString("string1"));\r
+ assertEquals(shape.getString().getString(), "string1");\r
+\r
+ assertNotNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));\r
+ assertEquals(agg.getShapeToObjMapping().size(), 2);\r
+\r
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+\r
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+ sheet = wb.getSheetAt(0);\r
+ patriarch = sheet.getDrawingPatriarch();\r
+\r
+ shape = (HSSFSimpleShape) patriarch.getChildren().get(0);\r
+\r
+ assertNotNull(shape.getTextObjectRecord());\r
+ assertEquals(shape.getString().getString(), "string1");\r
+ assertNotNull(shape.getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID));\r
+ assertEquals(agg.getShapeToObjMapping().size(), 2);\r
+ }\r
+\r
+ public void testComboboxRecords(){\r
+ HSSFWorkbook wb = new HSSFWorkbook();\r
+ HSSFSheet sheet = wb.createSheet();\r
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();\r
+\r
+ HSSFCombobox combobox = new HSSFCombobox(null, new HSSFClientAnchor());\r
+ HSSFTestHelper.setShapeId(combobox, 1024);\r
+ ComboboxShape comboboxShape = new ComboboxShape(combobox, 1024);\r
+\r
+ assertTrue(Arrays.equals(comboboxShape.getSpContainer().serialize(), combobox.getEscherContainer().serialize()));\r
+ assertTrue(Arrays.equals(comboboxShape.getObjRecord().serialize(), combobox.getObjRecord().serialize()));\r
+ }\r
}\r
HSSFPatriarch drawing = sheet.getDrawingPatriarch();\r
\r
HSSFSimpleShape rectangle = (HSSFSimpleShape) drawing.getChildren().get(0);\r
- rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);\r
\r
assertEquals(rectangle.getEscherContainer().getChild(0).getRecordId(), EscherSpRecord.RECORD_ID);\r
assertEquals(rectangle.getEscherContainer().getChild(1).getRecordId(), EscherOptRecord.RECORD_ID);\r
- assertEquals(" " + HexDump.toHex(rectangle.getEscherContainer().getChild(2).getRecordId()) + " ", rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);\r
+ assertEquals(rectangle.getEscherContainer().getChild(2).getRecordId(), EscherClientAnchorRecord.RECORD_ID);\r
assertEquals(rectangle.getEscherContainer().getChild(3).getRecordId(), EscherClientDataRecord.RECORD_ID);\r
\r
rectangle.setAnchor(new HSSFClientAnchor());\r
method.setAccessible(true);
method.invoke(new EscherAggregate(new MockDrawingManager()), shape, escherParent, shapeToObj);
} catch (NoSuchMethodException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ e.printStackTrace();
} catch (InvocationTargetException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ e.printStackTrace();
} catch (IllegalAccessException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ e.printStackTrace();
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ e.printStackTrace();
} catch (InvocationTargetException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ e.printStackTrace();
}
}
+
+ public static void setShapeId(HSSFShape shape, int id){
+ shape.setShapeId(id);
+ }
}