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.

XSSFShapeGroup.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import java.util.Iterator;
  17. import java.util.Spliterator;
  18. import org.apache.poi.openxml4j.opc.PackageRelationship;
  19. import org.apache.poi.ss.usermodel.ShapeContainer;
  20. import org.apache.poi.util.Internal;
  21. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
  22. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  28. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector;
  29. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape;
  30. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShapeNonVisual;
  31. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture;
  32. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape;
  33. /**
  34. * This object specifies a group shape that represents many shapes grouped together. This shape is to be treated
  35. * just as if it were a regular shape but instead of being described by a single geometry it is made up of all the
  36. * shape geometries encompassed within it. Within a group shape each of the shapes that make up the group are
  37. * specified just as they normally would.
  38. */
  39. public final class XSSFShapeGroup extends XSSFShape implements ShapeContainer<XSSFShape> {
  40. private static CTGroupShape prototype;
  41. private CTGroupShape ctGroup;
  42. /**
  43. * Construct a new XSSFSimpleShape object.
  44. *
  45. * @param drawing the XSSFDrawing that owns this shape
  46. * @param ctGroup the XML bean that stores this group content
  47. */
  48. protected XSSFShapeGroup(XSSFDrawing drawing, CTGroupShape ctGroup) {
  49. this.drawing = drawing;
  50. this.ctGroup = ctGroup;
  51. }
  52. /**
  53. * Initialize default structure of a new shape group
  54. */
  55. protected static CTGroupShape prototype() {
  56. if (prototype == null) {
  57. CTGroupShape shape = CTGroupShape.Factory.newInstance();
  58. CTGroupShapeNonVisual nv = shape.addNewNvGrpSpPr();
  59. CTNonVisualDrawingProps nvpr = nv.addNewCNvPr();
  60. nvpr.setId(0);
  61. nvpr.setName("Group 0");
  62. nv.addNewCNvGrpSpPr();
  63. CTGroupShapeProperties sp = shape.addNewGrpSpPr();
  64. CTGroupTransform2D t2d = sp.addNewXfrm();
  65. CTPositiveSize2D p1 = t2d.addNewExt();
  66. p1.setCx(0);
  67. p1.setCy(0);
  68. CTPoint2D p2 = t2d.addNewOff();
  69. p2.setX(0);
  70. p2.setY(0);
  71. CTPositiveSize2D p3 = t2d.addNewChExt();
  72. p3.setCx(0);
  73. p3.setCy(0);
  74. CTPoint2D p4 = t2d.addNewChOff();
  75. p4.setX(0);
  76. p4.setY(0);
  77. prototype = shape;
  78. }
  79. return prototype;
  80. }
  81. /**
  82. * Constructs a textbox.
  83. *
  84. * @param anchor the child anchor describes how this shape is attached
  85. * to the group.
  86. * @return the newly created textbox.
  87. */
  88. public XSSFTextBox createTextbox(XSSFChildAnchor anchor){
  89. CTShape ctShape = ctGroup.addNewSp();
  90. ctShape.set(XSSFSimpleShape.prototype());
  91. XSSFTextBox shape = new XSSFTextBox(getDrawing(), ctShape);
  92. shape.parent = this;
  93. shape.anchor = anchor;
  94. shape.setXfrm(anchor.getCTTransform2D());
  95. return shape;
  96. }
  97. /**
  98. * Creates a simple shape. This includes such shapes as lines, rectangles,
  99. * and ovals.
  100. *
  101. * @param anchor the child anchor describes how this shape is attached
  102. * to the group.
  103. * @return the newly created shape.
  104. */
  105. public XSSFSimpleShape createSimpleShape(XSSFChildAnchor anchor) {
  106. CTShape ctShape = ctGroup.addNewSp();
  107. ctShape.set(XSSFSimpleShape.prototype());
  108. XSSFSimpleShape shape = new XSSFSimpleShape(getDrawing(), ctShape);
  109. shape.parent = this;
  110. shape.anchor = anchor;
  111. shape.setXfrm(anchor.getCTTransform2D());
  112. return shape;
  113. }
  114. /**
  115. * Creates a simple shape. This includes such shapes as lines, rectangles,
  116. * and ovals.
  117. *
  118. * @param anchor the child anchor describes how this shape is attached
  119. * to the group.
  120. * @return the newly created shape.
  121. */
  122. public XSSFConnector createConnector(XSSFChildAnchor anchor) {
  123. CTConnector ctShape = ctGroup.addNewCxnSp();
  124. ctShape.set(XSSFConnector.prototype());
  125. XSSFConnector shape = new XSSFConnector(getDrawing(), ctShape);
  126. shape.parent = this;
  127. shape.anchor = anchor;
  128. shape.getCTConnector().getSpPr().setXfrm(anchor.getCTTransform2D());
  129. return shape;
  130. }
  131. /**
  132. * Creates a picture.
  133. *
  134. * @param anchor the client anchor describes how this picture is attached to the sheet.
  135. * @param pictureIndex the index of the picture in the workbook collection of pictures,
  136. * {@link XSSFWorkbook#getAllPictures()} .
  137. * @return the newly created picture shape.
  138. */
  139. public XSSFPicture createPicture(XSSFClientAnchor anchor, int pictureIndex) {
  140. PackageRelationship rel = getDrawing().addPictureReference(pictureIndex);
  141. CTPicture ctShape = ctGroup.addNewPic();
  142. ctShape.set(XSSFPicture.prototype());
  143. XSSFPicture shape = new XSSFPicture(getDrawing(), ctShape);
  144. shape.parent = this;
  145. shape.anchor = anchor;
  146. shape.setPictureReference(rel);
  147. return shape;
  148. }
  149. /**
  150. * Creates a group shape.
  151. *
  152. * @param anchor the client anchor describes how this group is attached to the group.
  153. * @return the newly created group shape.
  154. */
  155. public XSSFShapeGroup createGroup(XSSFChildAnchor anchor) {
  156. CTGroupShape ctShape = ctGroup.addNewGrpSp();
  157. ctShape.set(prototype());
  158. XSSFShapeGroup shape = new XSSFShapeGroup(getDrawing(), ctShape);
  159. shape.parent = this;
  160. shape.anchor = anchor;
  161. // TODO: calculate bounding rectangle on anchor and set off/ext correctly
  162. CTGroupTransform2D xfrm = shape.getCTGroupShape().getGrpSpPr().getXfrm();
  163. CTTransform2D t2 = anchor.getCTTransform2D();
  164. xfrm.setOff(t2.getOff());
  165. xfrm.setExt(t2.getExt());
  166. // child offset is left to 0,0
  167. xfrm.setChExt(t2.getExt());
  168. xfrm.setFlipH(t2.getFlipH());
  169. xfrm.setFlipV(t2.getFlipV());
  170. return shape;
  171. }
  172. @Internal
  173. public CTGroupShape getCTGroupShape() {
  174. return ctGroup;
  175. }
  176. /**
  177. * Sets the coordinate space of this group. All children are constrained
  178. * to these coordinates.
  179. */
  180. public void setCoordinates(int x1, int y1, int x2, int y2) {
  181. CTGroupTransform2D t2d = ctGroup.getGrpSpPr().getXfrm();
  182. CTPoint2D off = t2d.getOff();
  183. off.setX(x1);
  184. off.setY(y1);
  185. CTPositiveSize2D ext = t2d.getExt();
  186. ext.setCx(x2);
  187. ext.setCy(y2);
  188. CTPoint2D chOff = t2d.getChOff();
  189. chOff.setX(x1);
  190. chOff.setY(y1);
  191. CTPositiveSize2D chExt = t2d.getChExt();
  192. chExt.setCx(x2);
  193. chExt.setCy(y2);
  194. }
  195. @Override
  196. protected CTShapeProperties getShapeProperties() {
  197. throw new IllegalStateException("Not supported for shape group");
  198. }
  199. @Override
  200. public Iterator<XSSFShape> iterator() {
  201. return getDrawing().getShapes(this).iterator();
  202. }
  203. /**
  204. * @since POI 5.2.0
  205. */
  206. @Override
  207. public Spliterator<XSSFShape> spliterator() {
  208. return getDrawing().getShapes(this).spliterator();
  209. }
  210. @Override
  211. public String getShapeName() {
  212. return ctGroup.getNvGrpSpPr().getCNvPr().getName();
  213. }
  214. }