コード例 #1
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);
  }
コード例 #2
0
  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"));
  }
コード例 #3
0
  public void testShapeIds() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet();
    HSSFPatriarch patriarch1 = sheet1.createDrawingPatriarch();
    for (int i = 0; i < 2; i++) {
      patriarch1.createSimpleShape(new HSSFClientAnchor());
    }

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet1 = wb.getSheetAt(0);
    patriarch1 = sheet1.getDrawingPatriarch();

    EscherAggregate agg1 = HSSFTestHelper.getEscherAggregate(patriarch1);
    // last shape ID cached in EscherDgRecord
    EscherDgRecord dg1 = agg1.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);
    assertEquals(1026, dg1.getLastMSOSPID());

    // iterate over shapes and check shapeId
    EscherContainerRecord spgrContainer = agg1.getEscherContainer().getChildContainers().get(0);
    // root spContainer + 2 spContainers for shapes
    assertEquals(3, spgrContainer.getChildRecords().size());

    EscherSpRecord sp0 =
        ((EscherContainerRecord) spgrContainer.getChild(0)).getChildById(EscherSpRecord.RECORD_ID);
    assertEquals(1024, sp0.getShapeId());

    EscherSpRecord sp1 =
        ((EscherContainerRecord) spgrContainer.getChild(1)).getChildById(EscherSpRecord.RECORD_ID);
    assertEquals(1025, sp1.getShapeId());

    EscherSpRecord sp2 =
        ((EscherContainerRecord) spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID);
    assertEquals(1026, sp2.getShapeId());
  }
コード例 #4
0
  public void testBug45312() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    try {
      HSSFSheet sheet = wb.createSheet();
      HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

      {
        HSSFClientAnchor a1 = new HSSFClientAnchor();
        a1.setAnchor((short) 1, 1, 0, 0, (short) 1, 1, 512, 100);
        HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
        shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
      }
      {
        HSSFClientAnchor a1 = new HSSFClientAnchor();
        a1.setAnchor((short) 1, 1, 512, 0, (short) 1, 1, 1024, 100);
        HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
        shape1.setFlipVertical(true);
        shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
      }

      {
        HSSFClientAnchor a1 = new HSSFClientAnchor();
        a1.setAnchor((short) 2, 2, 0, 0, (short) 2, 2, 512, 100);
        HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
        shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
      }
      {
        HSSFClientAnchor a1 = new HSSFClientAnchor();
        a1.setAnchor((short) 2, 2, 0, 100, (short) 2, 2, 512, 200);
        HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
        shape1.setFlipHorizontal(true);
        shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
      }

      /*OutputStream stream = new FileOutputStream("/tmp/45312.xls");
      try {
          wb.write(stream);
      } finally {
          stream.close();
      }*/

      checkWorkbookBack(wb);
    } finally {
      wb.close();
    }
  }
コード例 #5
0
  public void testShapeContainerImplementsIterable() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();

    try {
      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);
    } finally {
      wb.close();
    }
  }
コード例 #6
0
  public void testTextForSimpleShape() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);
  }
コード例 #7
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);
  }
コード例 #8
0
  public void testRemoveShapes() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

    HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor());
    rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);

    int idx = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();

    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 0);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
    assertEquals(patriarch.getChildren().size(), 0);
  }
コード例 #9
0
  /**
   * create a rectangle, save the workbook, read back and verify that all shape properties are there
   */
  public void testReadWriteRectangle() throws IOException {

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();

    HSSFPatriarch drawing = sheet.createDrawingPatriarch();
    HSSFClientAnchor anchor = new HSSFClientAnchor(10, 10, 50, 50, (short) 2, 2, (short) 4, 4);
    anchor.setAnchorType(2);
    assertEquals(anchor.getAnchorType(), 2);

    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);
    assertEquals(
        ((EscherSimpleProperty)
                ((EscherOptRecord)
                        HSSFTestHelper.getEscherContainer(rectangle)
                            .getChildById(EscherOptRecord.RECORD_ID))
                    .lookup(EscherProperties.TEXT__TEXTID))
            .getPropertyValue(),
        "teeeest".hashCode());
    assertEquals(rectangle.isNoFill(), true);
    assertEquals(rectangle.getWrapText(), HSSFSimpleShape.WRAP_NONE);
    assertEquals(rectangle.getString().getString(), "teeeest");

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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"));

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet = wb.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);
    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);

    drawing = wb.getSheetAt(0).getDrawingPatriarch();
    assertEquals(drawing.getChildren().size(), 2);
  }