You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSLFAutoShape.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import org.apache.poi.sl.usermodel.AutoShape;
  21. import org.apache.poi.util.Beta;
  22. import org.apache.poi.xddf.usermodel.text.XDDFTextBody;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
  28. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  29. import org.openxmlformats.schemas.presentationml.x2006.main.CTShapeNonVisual;
  30. /**
  31. * Represents a shape with a preset geometry.
  32. *
  33. * @author Yegor Kozlov
  34. */
  35. @Beta
  36. public class XSLFAutoShape extends XSLFTextShape implements AutoShape<XSLFShape, XSLFTextParagraph> {
  37. /* package */ XSLFAutoShape(CTShape shape, XSLFSheet sheet) {
  38. super(shape, sheet);
  39. }
  40. /* package */
  41. static XSLFAutoShape create(CTShape shape, XSLFSheet sheet) {
  42. if (shape.getSpPr().isSetCustGeom()) {
  43. return new XSLFFreeformShape(shape, sheet);
  44. } else if (shape.getNvSpPr().getCNvSpPr().isSetTxBox()) {
  45. return new XSLFTextBox(shape, sheet);
  46. } else {
  47. return new XSLFAutoShape(shape, sheet);
  48. }
  49. }
  50. /**
  51. * @param shapeId
  52. * 1-based shapeId
  53. */
  54. static CTShape prototype(int shapeId) {
  55. CTShape ct = CTShape.Factory.newInstance();
  56. CTShapeNonVisual nvSpPr = ct.addNewNvSpPr();
  57. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  58. cnv.setName("AutoShape " + shapeId);
  59. cnv.setId(shapeId);
  60. nvSpPr.addNewCNvSpPr();
  61. nvSpPr.addNewNvPr();
  62. CTShapeProperties spPr = ct.addNewSpPr();
  63. CTPresetGeometry2D prst = spPr.addNewPrstGeom();
  64. prst.setPrst(STShapeType.RECT);
  65. prst.addNewAvLst();
  66. return ct;
  67. }
  68. @Override
  69. protected CTTextBody getTextBody(boolean create) {
  70. CTShape shape = (CTShape) getXmlObject();
  71. CTTextBody txBody = shape.getTxBody();
  72. if (txBody == null && create) {
  73. XDDFTextBody body = new XDDFTextBody(this);
  74. shape.setTxBody(body.getXmlObject());
  75. txBody = shape.getTxBody();
  76. }
  77. return txBody;
  78. }
  79. @Override
  80. public String toString() {
  81. return "[" + getClass().getSimpleName() + "] " + getShapeName();
  82. }
  83. }