Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

XSLFGraphicFrame.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 javax.xml.namespace.QName;
  22. import org.apache.poi.POIXMLException;
  23. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  24. import org.apache.poi.openxml4j.opc.PackagePart;
  25. import org.apache.poi.openxml4j.opc.PackageRelationship;
  26. import org.apache.poi.sl.draw.DrawNotImplemented;
  27. import org.apache.poi.sl.usermodel.PlaceableShape;
  28. import org.apache.poi.sl.usermodel.ShapeType;
  29. import org.apache.poi.util.Beta;
  30. import org.apache.poi.util.Units;
  31. import org.apache.xmlbeans.XmlCursor;
  32. import org.apache.xmlbeans.XmlObject;
  33. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
  34. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  35. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  37. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  38. /**
  39. * @author Yegor Kozlov
  40. */
  41. @Beta
  42. @DrawNotImplemented
  43. public class XSLFGraphicFrame extends XSLFShape implements PlaceableShape<XSLFShape, XSLFTextParagraph> {
  44. /*package*/ XSLFGraphicFrame(CTGraphicalObjectFrame shape, XSLFSheet sheet){
  45. super(shape,sheet);
  46. }
  47. public ShapeType getShapeType(){
  48. throw new UnsupportedOperationException();
  49. }
  50. @Override
  51. public Rectangle2D getAnchor(){
  52. CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
  53. CTPoint2D off = xfrm.getOff();
  54. double x = Units.toPoints(off.getX());
  55. double y = Units.toPoints(off.getY());
  56. CTPositiveSize2D ext = xfrm.getExt();
  57. double cx = Units.toPoints(ext.getCx());
  58. double cy = Units.toPoints(ext.getCy());
  59. return new Rectangle2D.Double(x, y, cx, cy);
  60. }
  61. @Override
  62. public void setAnchor(Rectangle2D anchor){
  63. CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
  64. CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff();
  65. long x = Units.toEMU(anchor.getX());
  66. long y = Units.toEMU(anchor.getY());
  67. off.setX(x);
  68. off.setY(y);
  69. CTPositiveSize2D ext = xfrm.isSetExt() ? xfrm.getExt() : xfrm
  70. .addNewExt();
  71. long cx = Units.toEMU(anchor.getWidth());
  72. long cy = Units.toEMU(anchor.getHeight());
  73. ext.setCx(cx);
  74. ext.setCy(cy);
  75. }
  76. static XSLFGraphicFrame create(CTGraphicalObjectFrame shape, XSLFSheet sheet){
  77. String uri = shape.getGraphic().getGraphicData().getUri();
  78. if(XSLFTable.TABLE_URI.equals(uri)){
  79. return new XSLFTable(shape, sheet);
  80. } else {
  81. return new XSLFGraphicFrame(shape, sheet);
  82. }
  83. }
  84. /**
  85. * Rotate this shape.
  86. * <p>
  87. * Positive angles are clockwise (i.e., towards the positive y axis);
  88. * negative angles are counter-clockwise (i.e., towards the negative y axis).
  89. * </p>
  90. *
  91. * @param theta the rotation angle in degrees.
  92. */
  93. public void setRotation(double theta){
  94. throw new IllegalArgumentException("Operation not supported");
  95. }
  96. /**
  97. * Rotation angle in degrees
  98. * <p>
  99. * Positive angles are clockwise (i.e., towards the positive y axis);
  100. * negative angles are counter-clockwise (i.e., towards the negative y axis).
  101. * </p>
  102. *
  103. * @return rotation angle in degrees
  104. */
  105. public double getRotation(){
  106. return 0;
  107. }
  108. public void setFlipHorizontal(boolean flip){
  109. throw new IllegalArgumentException("Operation not supported");
  110. }
  111. public void setFlipVertical(boolean flip){
  112. throw new IllegalArgumentException("Operation not supported");
  113. }
  114. /**
  115. * Whether the shape is horizontally flipped
  116. *
  117. * @return whether the shape is horizontally flipped
  118. */
  119. public boolean getFlipHorizontal(){
  120. return false;
  121. }
  122. public boolean getFlipVertical(){
  123. return false;
  124. }
  125. @Override
  126. void copy(XSLFShape sh){
  127. super.copy(sh);
  128. CTGraphicalObjectData data = ((CTGraphicalObjectFrame)getXmlObject()).getGraphic().getGraphicData();
  129. String uri = data.getUri();
  130. if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/diagram")){
  131. copyDiagram(data, (XSLFGraphicFrame)sh);
  132. } else {
  133. // TODO support other types of objects
  134. }
  135. }
  136. // TODO should be moved to a sub-class
  137. private void copyDiagram(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape){
  138. String xpath = "declare namespace dgm='http://schemas.openxmlformats.org/drawingml/2006/diagram' $this//dgm:relIds";
  139. XmlObject[] obj = objData.selectPath(xpath);
  140. if(obj != null && obj.length == 1){
  141. XmlCursor c = obj[0].newCursor();
  142. XSLFSheet sheet = srcShape.getSheet();
  143. try {
  144. String dm = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "dm"));
  145. PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm);
  146. PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel);
  147. getSheet().importPart(dmRel, dmPart);
  148. String lo = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "lo"));
  149. PackageRelationship loRel = sheet.getPackagePart().getRelationship(lo);
  150. PackagePart loPart = sheet.getPackagePart().getRelatedPart(loRel);
  151. getSheet().importPart(loRel, loPart);
  152. String qs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "qs"));
  153. PackageRelationship qsRel = sheet.getPackagePart().getRelationship(qs);
  154. PackagePart qsPart = sheet.getPackagePart().getRelatedPart(qsRel);
  155. getSheet().importPart(qsRel, qsPart);
  156. String cs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "cs"));
  157. PackageRelationship csRel = sheet.getPackagePart().getRelationship(cs);
  158. PackagePart csPart = sheet.getPackagePart().getRelatedPart(csRel);
  159. getSheet().importPart(csRel, csPart);
  160. } catch (InvalidFormatException e){
  161. throw new POIXMLException(e);
  162. }
  163. c.dispose();
  164. }
  165. }
  166. }