use of org.apache.poi.hssf.usermodel.HSSFPolygon 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