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.

XSLFGroupShape.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 java.awt.Dimension;
  21. import java.awt.geom.Rectangle2D;
  22. import java.util.ArrayList;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import org.apache.poi.ooxml.POIXMLDocumentPart.RelationPart;
  26. import org.apache.poi.sl.draw.DrawPictureShape;
  27. import org.apache.poi.sl.usermodel.GroupShape;
  28. import org.apache.poi.sl.usermodel.PictureData;
  29. import org.apache.poi.util.Beta;
  30. import org.apache.poi.util.POILogFactory;
  31. import org.apache.poi.util.POILogger;
  32. import org.apache.poi.util.Units;
  33. import org.apache.xmlbeans.XmlObject;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupShapeProperties;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  38. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  39. import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
  40. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  41. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  42. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;
  43. import org.openxmlformats.schemas.presentationml.x2006.main.CTOleObject;
  44. import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
  45. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  46. /**
  47. * Represents a group shape that consists of many shapes grouped together.
  48. *
  49. * @author Yegor Kozlov
  50. */
  51. @Beta
  52. public class XSLFGroupShape extends XSLFShape
  53. implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
  54. private final static POILogger _logger = POILogFactory.getLogger(XSLFGroupShape.class);
  55. private final List<XSLFShape> _shapes;
  56. private final CTGroupShapeProperties _grpSpPr;
  57. private XSLFDrawing _drawing;
  58. protected XSLFGroupShape(CTGroupShape shape, XSLFSheet sheet){
  59. super(shape,sheet);
  60. _shapes = XSLFSheet.buildShapes(shape, this);
  61. _grpSpPr = shape.getGrpSpPr();
  62. }
  63. @Override
  64. protected CTGroupShapeProperties getGrpSpPr() {
  65. return _grpSpPr;
  66. }
  67. private CTGroupTransform2D getSafeXfrm() {
  68. CTGroupTransform2D xfrm = getXfrm();
  69. return (xfrm == null ? getGrpSpPr().addNewXfrm() : xfrm);
  70. }
  71. protected CTGroupTransform2D getXfrm() {
  72. return getGrpSpPr().getXfrm();
  73. }
  74. @Override
  75. public Rectangle2D getAnchor(){
  76. CTGroupTransform2D xfrm = getXfrm();
  77. CTPoint2D off = xfrm.getOff();
  78. double x = Units.toPoints(off.getX());
  79. double y = Units.toPoints(off.getY());
  80. CTPositiveSize2D ext = xfrm.getExt();
  81. double cx = Units.toPoints(ext.getCx());
  82. double cy = Units.toPoints(ext.getCy());
  83. return new Rectangle2D.Double(x,y,cx,cy);
  84. }
  85. @Override
  86. public void setAnchor(Rectangle2D anchor){
  87. CTGroupTransform2D xfrm = getSafeXfrm();
  88. CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff();
  89. long x = Units.toEMU(anchor.getX());
  90. long y = Units.toEMU(anchor.getY());
  91. off.setX(x);
  92. off.setY(y);
  93. CTPositiveSize2D ext = xfrm.isSetExt() ? xfrm.getExt() : xfrm.addNewExt();
  94. long cx = Units.toEMU(anchor.getWidth());
  95. long cy = Units.toEMU(anchor.getHeight());
  96. ext.setCx(cx);
  97. ext.setCy(cy);
  98. }
  99. /**
  100. *
  101. * @return the coordinates of the child extents rectangle
  102. * used for calculations of grouping, scaling, and rotation
  103. * behavior of shapes placed within a group.
  104. */
  105. @Override
  106. public Rectangle2D getInteriorAnchor(){
  107. CTGroupTransform2D xfrm = getXfrm();
  108. CTPoint2D off = xfrm.getChOff();
  109. double x = Units.toPoints(off.getX());
  110. double y = Units.toPoints(off.getY());
  111. CTPositiveSize2D ext = xfrm.getChExt();
  112. double cx = Units.toPoints(ext.getCx());
  113. double cy = Units.toPoints(ext.getCy());
  114. return new Rectangle2D.Double(x, y, cx, cy);
  115. }
  116. /**
  117. *
  118. * @param anchor the coordinates of the child extents rectangle
  119. * used for calculations of grouping, scaling, and rotation
  120. * behavior of shapes placed within a group.
  121. */
  122. @Override
  123. public void setInteriorAnchor(Rectangle2D anchor) {
  124. CTGroupTransform2D xfrm = getSafeXfrm();
  125. CTPoint2D off = xfrm.isSetChOff() ? xfrm.getChOff() : xfrm.addNewChOff();
  126. long x = Units.toEMU(anchor.getX());
  127. long y = Units.toEMU(anchor.getY());
  128. off.setX(x);
  129. off.setY(y);
  130. CTPositiveSize2D ext = xfrm.isSetChExt() ? xfrm.getChExt() : xfrm.addNewChExt();
  131. long cx = Units.toEMU(anchor.getWidth());
  132. long cy = Units.toEMU(anchor.getHeight());
  133. ext.setCx(cx);
  134. ext.setCy(cy);
  135. }
  136. /**
  137. * @return child shapes contained within this group
  138. */
  139. @Override
  140. public List<XSLFShape> getShapes(){
  141. return _shapes;
  142. }
  143. /**
  144. * Returns an iterator over the shapes in this sheet
  145. *
  146. * @return an iterator over the shapes in this sheet
  147. */
  148. @Override
  149. public Iterator<XSLFShape> iterator(){
  150. return _shapes.iterator();
  151. }
  152. /**
  153. * Remove the specified shape from this group
  154. */
  155. @Override
  156. public boolean removeShape(XSLFShape xShape) {
  157. XmlObject obj = xShape.getXmlObject();
  158. CTGroupShape grpSp = (CTGroupShape)getXmlObject();
  159. getSheet().deregisterShapeId(xShape.getShapeId());
  160. if(obj instanceof CTShape){
  161. grpSp.getSpList().remove(obj);
  162. } else if (obj instanceof CTGroupShape){
  163. XSLFGroupShape gs = (XSLFGroupShape)xShape;
  164. new ArrayList<>(gs.getShapes()).forEach(gs::removeShape);
  165. grpSp.getGrpSpList().remove(obj);
  166. } else if (obj instanceof CTConnector){
  167. grpSp.getCxnSpList().remove(obj);
  168. } else if (obj instanceof CTGraphicalObjectFrame) {
  169. grpSp.getGraphicFrameList().remove(obj);
  170. } else if (obj instanceof CTPicture) {
  171. XSLFPictureShape ps = (XSLFPictureShape)xShape;
  172. XSLFSheet sh = getSheet();
  173. if (sh != null) {
  174. sh.removePictureRelation(ps);
  175. }
  176. grpSp.getPicList().remove(obj);
  177. } else {
  178. throw new IllegalArgumentException("Unsupported shape: " + xShape);
  179. }
  180. return _shapes.remove(xShape);
  181. }
  182. /**
  183. * @param shapeId 1-based shapeId
  184. */
  185. static CTGroupShape prototype(int shapeId) {
  186. CTGroupShape ct = CTGroupShape.Factory.newInstance();
  187. CTGroupShapeNonVisual nvSpPr = ct.addNewNvGrpSpPr();
  188. CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
  189. cnv.setName("Group " + shapeId);
  190. cnv.setId(shapeId);
  191. nvSpPr.addNewCNvGrpSpPr();
  192. nvSpPr.addNewNvPr();
  193. ct.addNewGrpSpPr();
  194. return ct;
  195. }
  196. // shape factory methods
  197. private XSLFDrawing getDrawing(){
  198. if(_drawing == null) {
  199. _drawing = new XSLFDrawing(getSheet(), (CTGroupShape)getXmlObject());
  200. }
  201. return _drawing;
  202. }
  203. @Override
  204. public XSLFAutoShape createAutoShape(){
  205. XSLFAutoShape sh = getDrawing().createAutoShape();
  206. _shapes.add(sh);
  207. sh.setParent(this);
  208. return sh;
  209. }
  210. @Override
  211. public XSLFFreeformShape createFreeform(){
  212. XSLFFreeformShape sh = getDrawing().createFreeform();
  213. _shapes.add(sh);
  214. sh.setParent(this);
  215. return sh;
  216. }
  217. @Override
  218. public XSLFTextBox createTextBox(){
  219. XSLFTextBox sh = getDrawing().createTextBox();
  220. _shapes.add(sh);
  221. sh.setParent(this);
  222. return sh;
  223. }
  224. @Override
  225. public XSLFConnectorShape createConnector(){
  226. XSLFConnectorShape sh = getDrawing().createConnector();
  227. _shapes.add(sh);
  228. sh.setParent(this);
  229. return sh;
  230. }
  231. @Override
  232. public XSLFGroupShape createGroup(){
  233. XSLFGroupShape sh = getDrawing().createGroup();
  234. _shapes.add(sh);
  235. sh.setParent(this);
  236. return sh;
  237. }
  238. @Override
  239. public XSLFPictureShape createPicture(PictureData pictureData){
  240. if (!(pictureData instanceof XSLFPictureData)) {
  241. throw new IllegalArgumentException("pictureData needs to be of type XSLFPictureData");
  242. }
  243. RelationPart rp = getSheet().addRelation(null, XSLFRelation.IMAGES, (XSLFPictureData)pictureData);
  244. XSLFPictureShape sh = getDrawing().createPicture(rp.getRelationship().getId());
  245. new DrawPictureShape(sh).resize();
  246. _shapes.add(sh);
  247. sh.setParent(this);
  248. return sh;
  249. }
  250. @Override
  251. public XSLFObjectShape createOleShape(PictureData pictureData) {
  252. if (!(pictureData instanceof XSLFPictureData)) {
  253. throw new IllegalArgumentException("pictureData needs to be of type XSLFPictureData");
  254. }
  255. RelationPart rp = getSheet().addRelation(null, XSLFRelation.IMAGES, (XSLFPictureData)pictureData);
  256. XSLFObjectShape sh = getDrawing().createOleShape(rp.getRelationship().getId());
  257. CTOleObject oleObj = sh.getCTOleObject();
  258. Dimension dim = pictureData.getImageDimension();
  259. oleObj.setImgW(Units.toEMU(dim.getWidth()));
  260. oleObj.setImgH(Units.toEMU(dim.getHeight()));
  261. getShapes().add(sh);
  262. sh.setParent(this);
  263. return sh;
  264. }
  265. public XSLFTable createTable(){
  266. XSLFTable sh = getDrawing().createTable();
  267. _shapes.add(sh);
  268. sh.setParent(this);
  269. return sh;
  270. }
  271. @Override
  272. public XSLFTable createTable(int numRows, int numCols){
  273. if (numRows < 1 || numCols < 1) {
  274. throw new IllegalArgumentException("numRows and numCols must be greater than 0");
  275. }
  276. XSLFTable sh = getDrawing().createTable();
  277. _shapes.add(sh);
  278. sh.setParent(this);
  279. for (int r=0; r<numRows; r++) {
  280. XSLFTableRow row = sh.addRow();
  281. for (int c=0; c<numCols; c++) {
  282. row.addCell();
  283. }
  284. }
  285. return sh;
  286. }
  287. @Override
  288. public void setFlipHorizontal(boolean flip){
  289. getSafeXfrm().setFlipH(flip);
  290. }
  291. @Override
  292. public void setFlipVertical(boolean flip){
  293. getSafeXfrm().setFlipV(flip);
  294. }
  295. @Override
  296. public boolean getFlipHorizontal(){
  297. CTGroupTransform2D xfrm = getXfrm();
  298. return !(xfrm == null || !xfrm.isSetFlipH()) && xfrm.getFlipH();
  299. }
  300. @Override
  301. public boolean getFlipVertical(){
  302. CTGroupTransform2D xfrm = getXfrm();
  303. return !(xfrm == null || !xfrm.isSetFlipV()) && xfrm.getFlipV();
  304. }
  305. @Override
  306. public void setRotation(double theta){
  307. getSafeXfrm().setRot((int) (theta * 60000));
  308. }
  309. @Override
  310. public double getRotation(){
  311. CTGroupTransform2D xfrm = getXfrm();
  312. return (xfrm == null || !xfrm.isSetRot()) ? 0 : (xfrm.getRot() / 60000.d);
  313. }
  314. @Override
  315. void copy(XSLFShape src){
  316. XSLFGroupShape gr = (XSLFGroupShape)src;
  317. // recursively update each shape
  318. List<XSLFShape> tgtShapes = getShapes();
  319. List<XSLFShape> srcShapes = gr.getShapes();
  320. // workaround for a call by XSLFSheet.importContent:
  321. // if we have already the same amount of child shapes
  322. // then assume, that we've been called by import content and only need to update the children
  323. if (tgtShapes.size() == srcShapes.size()) {
  324. for(int i = 0; i < tgtShapes.size(); i++){
  325. XSLFShape s1 = srcShapes.get(i);
  326. XSLFShape s2 = tgtShapes.get(i);
  327. s2.copy(s1);
  328. }
  329. } else {
  330. // otherwise recreate the shapes from scratch
  331. clear();
  332. // recursively update each shape
  333. for(XSLFShape shape : srcShapes) {
  334. XSLFShape newShape;
  335. if (shape instanceof XSLFTextBox) {
  336. newShape = createTextBox();
  337. } else if (shape instanceof XSLFFreeformShape) {
  338. newShape = createFreeform();
  339. } else if (shape instanceof XSLFAutoShape) {
  340. newShape = createAutoShape();
  341. } else if (shape instanceof XSLFConnectorShape) {
  342. newShape = createConnector();
  343. } else if (shape instanceof XSLFPictureShape) {
  344. XSLFPictureShape p = (XSLFPictureShape)shape;
  345. XSLFPictureData pd = p.getPictureData();
  346. XSLFPictureData pdNew = getSheet().getSlideShow().addPicture(pd.getData(), pd.getType());
  347. newShape = createPicture(pdNew);
  348. } else if (shape instanceof XSLFGroupShape) {
  349. newShape = createGroup();
  350. } else if (shape instanceof XSLFTable) {
  351. newShape = createTable();
  352. } else {
  353. _logger.log(POILogger.WARN, "copying of class "+shape.getClass()+" not supported.");
  354. continue;
  355. }
  356. newShape.copy(shape);
  357. }
  358. }
  359. }
  360. /**
  361. * Removes all of the elements from this container (optional operation).
  362. * The container will be empty after this call returns.
  363. */
  364. @Override
  365. public void clear() {
  366. List<XSLFShape> shapes = new ArrayList<>(getShapes());
  367. for(XSLFShape shape : shapes){
  368. removeShape(shape);
  369. }
  370. }
  371. @Override
  372. public void addShape(XSLFShape shape) {
  373. throw new UnsupportedOperationException(
  374. "Adding a shape from a different container is not supported -"
  375. + " create it from scratch with XSLFGroupShape.create* methods");
  376. }
  377. }