use of org.apache.poi.hssf.usermodel.HSSFSimpleShape in project poi by apache.
the class TestDrawingShapes method checkWorkbookBack.
private void checkWorkbookBack(HSSFWorkbook wb) throws IOException {
HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
assertNotNull(wbBack);
HSSFSheet sheetBack = wbBack.getSheetAt(0);
assertNotNull(sheetBack);
HSSFPatriarch patriarchBack = sheetBack.getDrawingPatriarch();
assertNotNull(patriarchBack);
List<HSSFShape> children = patriarchBack.getChildren();
assertEquals(4, children.size());
HSSFShape hssfShape = children.get(0);
assertTrue(hssfShape instanceof HSSFSimpleShape);
HSSFAnchor anchor = hssfShape.getAnchor();
assertTrue(anchor instanceof HSSFClientAnchor);
assertEquals(0, anchor.getDx1());
assertEquals(512, anchor.getDx2());
assertEquals(0, anchor.getDy1());
assertEquals(100, anchor.getDy2());
HSSFClientAnchor cAnchor = (HSSFClientAnchor) anchor;
assertEquals(1, cAnchor.getCol1());
assertEquals(1, cAnchor.getCol2());
assertEquals(1, cAnchor.getRow1());
assertEquals(1, cAnchor.getRow2());
hssfShape = children.get(1);
assertTrue(hssfShape instanceof HSSFSimpleShape);
anchor = hssfShape.getAnchor();
assertTrue(anchor instanceof HSSFClientAnchor);
assertEquals(512, anchor.getDx1());
assertEquals(1024, anchor.getDx2());
assertEquals(0, anchor.getDy1());
assertEquals(100, anchor.getDy2());
cAnchor = (HSSFClientAnchor) anchor;
assertEquals(1, cAnchor.getCol1());
assertEquals(1, cAnchor.getCol2());
assertEquals(1, cAnchor.getRow1());
assertEquals(1, cAnchor.getRow2());
hssfShape = children.get(2);
assertTrue(hssfShape instanceof HSSFSimpleShape);
anchor = hssfShape.getAnchor();
assertTrue(anchor instanceof HSSFClientAnchor);
assertEquals(0, anchor.getDx1());
assertEquals(512, anchor.getDx2());
assertEquals(0, anchor.getDy1());
assertEquals(100, anchor.getDy2());
cAnchor = (HSSFClientAnchor) anchor;
assertEquals(2, cAnchor.getCol1());
assertEquals(2, cAnchor.getCol2());
assertEquals(2, cAnchor.getRow1());
assertEquals(2, cAnchor.getRow2());
hssfShape = children.get(3);
assertTrue(hssfShape instanceof HSSFSimpleShape);
anchor = hssfShape.getAnchor();
assertTrue(anchor instanceof HSSFClientAnchor);
assertEquals(0, anchor.getDx1());
assertEquals(512, anchor.getDx2());
assertEquals(100, anchor.getDy1());
assertEquals(200, anchor.getDy2());
cAnchor = (HSSFClientAnchor) anchor;
assertEquals(2, cAnchor.getCol1());
assertEquals(2, cAnchor.getCol2());
assertEquals(2, cAnchor.getRow1());
assertEquals(2, cAnchor.getRow2());
wbBack.close();
}
use of org.apache.poi.hssf.usermodel.HSSFSimpleShape in project poi by apache.
the class TestDrawingShapes method testHSSFShapeCompatibility.
@Test
public void testHSSFShapeCompatibility() {
HSSFSimpleShape shape = new HSSFSimpleShape(null, new HSSFClientAnchor());
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
assertEquals(0x08000040, shape.getLineStyleColor());
assertEquals(0x08000009, shape.getFillColor());
assertEquals(HSSFShape.LINEWIDTH_DEFAULT, shape.getLineWidth());
assertEquals(HSSFShape.LINESTYLE_SOLID, shape.getLineStyle());
assertFalse(shape.isNoFill());
EscherOptRecord opt = shape.getOptRecord();
assertEquals(7, opt.getEscherProperties().size());
assertTrue(((EscherBoolProperty) opt.lookup(EscherProperties.GROUPSHAPE__PRINT)).isTrue());
assertTrue(((EscherBoolProperty) opt.lookup(EscherProperties.LINESTYLE__NOLINEDRAWDASH)).isTrue());
assertEquals(0x00000004, ((EscherSimpleProperty) opt.lookup(EscherProperties.GEOMETRY__SHAPEPATH)).getPropertyValue());
assertNull(opt.lookup(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE));
}
use of org.apache.poi.hssf.usermodel.HSSFSimpleShape in project poi by apache.
the class TestDrawingShapes method testTextForSimpleShape.
@Test
public void testTextForSimpleShape() throws IOException {
HSSFWorkbook wb1 = new HSSFWorkbook();
HSSFSheet sheet = wb1.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor());
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch);
assertEquals(agg.getShapeToObjMapping().size(), 2);
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
shape = (HSSFSimpleShape) patriarch.getChildren().get(0);
agg = HSSFTestHelper.getEscherAggregate(patriarch);
assertEquals(agg.getShapeToObjMapping().size(), 2);
shape.setString(new HSSFRichTextString("string1"));
assertEquals(shape.getString().getString(), "string1");
assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID));
assertEquals(agg.getShapeToObjMapping().size(), 2);
HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
wb2.close();
HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
wb3.close();
sheet = wb4.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
shape = (HSSFSimpleShape) patriarch.getChildren().get(0);
assertNotNull(HSSFTestHelper.getTextObjRecord(shape));
assertEquals(shape.getString().getString(), "string1");
assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID));
assertEquals(agg.getShapeToObjMapping().size(), 2);
wb4.close();
}
use of org.apache.poi.hssf.usermodel.HSSFSimpleShape in project poi by apache.
the class TestDrawingShapes method testReadWriteRectangle.
/**
* create a rectangle, save the workbook, read back and verify that all shape properties are there
*/
@Test
public void testReadWriteRectangle() throws IOException {
HSSFWorkbook wb1 = new HSSFWorkbook();
HSSFSheet sheet = wb1.createSheet();
HSSFPatriarch drawing = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 50, 50, (short) 2, 2, (short) 4, 4);
anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE);
assertEquals(AnchorType.MOVE_DONT_RESIZE, anchor.getAnchorType());
//noinspection deprecation
anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE.value);
assertEquals(AnchorType.MOVE_DONT_RESIZE, anchor.getAnchorType());
HSSFSimpleShape rectangle = drawing.createSimpleShape(anchor);
rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
rectangle.setLineWidth(10000);
rectangle.setFillColor(777);
assertEquals(rectangle.getFillColor(), 777);
assertEquals(10000, rectangle.getLineWidth());
rectangle.setLineStyle(10);
assertEquals(10, rectangle.getLineStyle());
assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_SQUARE);
rectangle.setLineStyleColor(1111);
rectangle.setNoFill(true);
rectangle.setWrapText(HSSFSimpleShape.WRAP_NONE);
rectangle.setString(new HSSFRichTextString("teeeest"));
assertEquals(rectangle.getLineStyleColor(), 1111);
EscherContainerRecord escherContainer = HSSFTestHelper.getEscherContainer(rectangle);
assertNotNull(escherContainer);
EscherRecord childById = escherContainer.getChildById(EscherOptRecord.RECORD_ID);
assertNotNull(childById);
EscherProperty lookup = ((EscherOptRecord) childById).lookup(EscherProperties.TEXT__TEXTID);
assertNotNull(lookup);
assertEquals(((EscherSimpleProperty) lookup).getPropertyValue(), "teeeest".hashCode());
assertEquals(rectangle.isNoFill(), true);
assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_NONE);
assertEquals(rectangle.getString().getString(), "teeeest");
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
drawing = sheet.getDrawingPatriarch();
assertEquals(1, drawing.getChildren().size());
HSSFSimpleShape rectangle2 = (HSSFSimpleShape) drawing.getChildren().get(0);
assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, rectangle2.getShapeType());
assertEquals(10000, rectangle2.getLineWidth());
assertEquals(10, rectangle2.getLineStyle());
assertEquals(anchor, rectangle2.getAnchor());
assertEquals(rectangle2.getLineStyleColor(), 1111);
assertEquals(rectangle2.getFillColor(), 777);
assertEquals(rectangle2.isNoFill(), true);
assertEquals(rectangle2.getString().getString(), "teeeest");
assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_NONE);
rectangle2.setFillColor(3333);
rectangle2.setLineStyle(9);
rectangle2.setLineStyleColor(4444);
rectangle2.setNoFill(false);
rectangle2.setLineWidth(77);
rectangle2.getAnchor().setDx1(2);
rectangle2.getAnchor().setDx2(3);
rectangle2.getAnchor().setDy1(4);
rectangle2.getAnchor().setDy2(5);
rectangle.setWrapText(HSSFSimpleShape.WRAP_BY_POINTS);
rectangle2.setString(new HSSFRichTextString("test22"));
HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
wb2.close();
sheet = wb3.getSheetAt(0);
drawing = sheet.getDrawingPatriarch();
assertEquals(1, drawing.getChildren().size());
rectangle2 = (HSSFSimpleShape) drawing.getChildren().get(0);
assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, rectangle2.getShapeType());
assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_BY_POINTS);
assertEquals(77, rectangle2.getLineWidth());
assertEquals(9, rectangle2.getLineStyle());
assertEquals(rectangle2.getLineStyleColor(), 4444);
assertEquals(rectangle2.getFillColor(), 3333);
assertEquals(rectangle2.getAnchor().getDx1(), 2);
assertEquals(rectangle2.getAnchor().getDx2(), 3);
assertEquals(rectangle2.getAnchor().getDy1(), 4);
assertEquals(rectangle2.getAnchor().getDy2(), 5);
assertEquals(rectangle2.isNoFill(), false);
assertEquals(rectangle2.getString().getString(), "test22");
HSSFSimpleShape rect3 = drawing.createSimpleShape(new HSSFClientAnchor());
rect3.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
wb3.close();
drawing = wb4.getSheetAt(0).getDrawingPatriarch();
assertEquals(drawing.getChildren().size(), 2);
wb4.close();
}
use of org.apache.poi.hssf.usermodel.HSSFSimpleShape in project poi by apache.
the class TestDrawingShapes method testRemoveShapes.
@Test
public void testRemoveShapes() throws IOException {
HSSFWorkbook wb1 = new HSSFWorkbook();
HSSFSheet sheet = wb1.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor());
rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
int idx = wb1.addPicture(new byte[] { 1, 2, 3 }, Workbook.PICTURE_TYPE_JPEG);
patriarch.createPicture(new HSSFClientAnchor(), idx);
patriarch.createCellComment(new HSSFClientAnchor());
HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());
polygon.setPoints(new int[] { 1, 2 }, new int[] { 2, 3 });
patriarch.createTextbox(new HSSFClientAnchor());
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
group.createTextbox(new HSSFChildAnchor());
group.createPicture(new HSSFChildAnchor(), idx);
assertEquals(patriarch.getChildren().size(), 6);
assertEquals(group.getChildren().size(), 2);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 12);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 12);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
assertEquals(patriarch.getChildren().size(), 6);
group = (HSSFShapeGroup) patriarch.getChildren().get(5);
group.removeShape(group.getChildren().get(0));
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 10);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
wb2.close();
sheet = wb3.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 10);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
group = (HSSFShapeGroup) patriarch.getChildren().get(5);
patriarch.removeShape(group);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 8);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
wb3.close();
sheet = wb4.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 8);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
assertEquals(patriarch.getChildren().size(), 5);
HSSFShape shape = patriarch.getChildren().get(0);
patriarch.removeShape(shape);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 6);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
assertEquals(patriarch.getChildren().size(), 4);
HSSFWorkbook wb5 = HSSFTestDataSamples.writeOutAndReadBack(wb4);
wb4.close();
sheet = wb5.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 6);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
assertEquals(patriarch.getChildren().size(), 4);
HSSFPicture picture = (HSSFPicture) patriarch.getChildren().get(0);
patriarch.removeShape(picture);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 5);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
assertEquals(patriarch.getChildren().size(), 3);
HSSFWorkbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
wb5.close();
sheet = wb6.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 5);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
assertEquals(patriarch.getChildren().size(), 3);
HSSFComment comment = (HSSFComment) patriarch.getChildren().get(0);
patriarch.removeShape(comment);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 3);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
assertEquals(patriarch.getChildren().size(), 2);
HSSFWorkbook wb7 = HSSFTestDataSamples.writeOutAndReadBack(wb6);
wb6.close();
sheet = wb7.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 3);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
assertEquals(patriarch.getChildren().size(), 2);
polygon = (HSSFPolygon) patriarch.getChildren().get(0);
patriarch.removeShape(polygon);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 2);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
assertEquals(patriarch.getChildren().size(), 1);
HSSFWorkbook wb8 = HSSFTestDataSamples.writeOutAndReadBack(wb7);
wb7.close();
sheet = wb8.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 2);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
assertEquals(patriarch.getChildren().size(), 1);
HSSFTextbox textbox = (HSSFTextbox) patriarch.getChildren().get(0);
patriarch.removeShape(textbox);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 0);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
assertEquals(patriarch.getChildren().size(), 0);
HSSFWorkbook wb9 = HSSFTestDataSamples.writeOutAndReadBack(wb8);
wb8.close();
sheet = wb9.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 0);
assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
assertEquals(patriarch.getChildren().size(), 0);
wb9.close();
}
Aggregations