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 13KB

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