Search in sources :

Example 1 with HSSFShape

use of org.apache.poi.hssf.usermodel.HSSFShape 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();
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) HSSFSimpleShape(org.apache.poi.hssf.usermodel.HSSFSimpleShape) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFAnchor(org.apache.poi.hssf.usermodel.HSSFAnchor) HSSFShape(org.apache.poi.hssf.usermodel.HSSFShape) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 2 with HSSFShape

use of org.apache.poi.hssf.usermodel.HSSFShape in project poi by apache.

the class TestDrawingShapes method testShapeContainerImplementsIterable.

@SuppressWarnings("unused")
@Test
public void testShapeContainerImplementsIterable() throws IOException {
    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);
    wb.close();
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFShape(org.apache.poi.hssf.usermodel.HSSFShape) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 3 with HSSFShape

use of org.apache.poi.hssf.usermodel.HSSFShape 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();
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) HSSFShapeGroup(org.apache.poi.hssf.usermodel.HSSFShapeGroup) HSSFComment(org.apache.poi.hssf.usermodel.HSSFComment) HSSFSimpleShape(org.apache.poi.hssf.usermodel.HSSFSimpleShape) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFPicture(org.apache.poi.hssf.usermodel.HSSFPicture) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFPolygon(org.apache.poi.hssf.usermodel.HSSFPolygon) HSSFChildAnchor(org.apache.poi.hssf.usermodel.HSSFChildAnchor) HSSFShape(org.apache.poi.hssf.usermodel.HSSFShape) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFTextbox(org.apache.poi.hssf.usermodel.HSSFTextbox) Test(org.junit.Test)

Aggregations

HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)3 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)3 HSSFShape (org.apache.poi.hssf.usermodel.HSSFShape)3 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)3 HSSFSimpleShape (org.apache.poi.hssf.usermodel.HSSFSimpleShape)2 Test (org.junit.Test)2 HSSFAnchor (org.apache.poi.hssf.usermodel.HSSFAnchor)1 HSSFChildAnchor (org.apache.poi.hssf.usermodel.HSSFChildAnchor)1 HSSFComment (org.apache.poi.hssf.usermodel.HSSFComment)1 HSSFPicture (org.apache.poi.hssf.usermodel.HSSFPicture)1 HSSFPolygon (org.apache.poi.hssf.usermodel.HSSFPolygon)1 HSSFShapeGroup (org.apache.poi.hssf.usermodel.HSSFShapeGroup)1 HSSFTextbox (org.apache.poi.hssf.usermodel.HSSFTextbox)1