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.

XSSFGraphicFrame.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.xssf.usermodel;
  20. import javax.xml.namespace.QName;
  21. import org.apache.poi.ooxml.POIXMLDocumentPart;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.xmlbeans.XmlCursor;
  24. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  28. import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
  29. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  30. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrame;
  31. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrameNonVisual;
  32. import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId;
  33. import org.w3c.dom.Node;
  34. import org.w3c.dom.NodeList;
  35. /**
  36. * Represents DrawingML GraphicalObjectFrame.
  37. */
  38. public final class XSSFGraphicFrame extends XSSFShape {
  39. private static CTGraphicalObjectFrame prototype;
  40. private final CTGraphicalObjectFrame graphicFrame;
  41. /**
  42. * Construct a new XSSFGraphicFrame object.
  43. *
  44. * @param drawing the XSSFDrawing that owns this frame
  45. * @param ctGraphicFrame the XML bean that stores this frame content
  46. */
  47. protected XSSFGraphicFrame(XSSFDrawing drawing, CTGraphicalObjectFrame ctGraphicFrame) {
  48. this.drawing = drawing; // protected field on XSSFShape
  49. this.graphicFrame = ctGraphicFrame;
  50. // TODO: there may be a better way to delegate this
  51. CTGraphicalObjectData graphicData = graphicFrame.getGraphic() == null ?
  52. null : graphicFrame.getGraphic().getGraphicData();
  53. if (graphicData != null) {
  54. NodeList nodes = graphicData.getDomNode().getChildNodes();
  55. for (int i = 0; i < nodes.getLength(); i++) {
  56. final Node node = nodes.item(i);
  57. // if the frame references a chart, associate the chart with this instance
  58. if (node.getNodeName().equals("c:chart")) {
  59. // this better succeed or the document is invalid
  60. POIXMLDocumentPart relation = drawing.getRelationById(node.getAttributes().getNamedItem("r:id").getNodeValue());
  61. // Do XWPF charts need similar treatment?
  62. if (relation instanceof XSSFChart) {
  63. ((XSSFChart) relation).setGraphicFrame(this);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. @Internal
  70. public CTGraphicalObjectFrame getCTGraphicalObjectFrame() {
  71. return graphicFrame;
  72. }
  73. /**
  74. * Initialize default structure of a new graphic frame
  75. */
  76. protected static CTGraphicalObjectFrame prototype() {
  77. if (prototype == null) {
  78. CTGraphicalObjectFrame graphicFrame = CTGraphicalObjectFrame.Factory.newInstance();
  79. CTGraphicalObjectFrameNonVisual nvGraphic = graphicFrame.addNewNvGraphicFramePr();
  80. CTNonVisualDrawingProps props = nvGraphic.addNewCNvPr();
  81. props.setId(0);
  82. props.setName("Diagramm 1");
  83. nvGraphic.addNewCNvGraphicFramePr();
  84. CTTransform2D transform = graphicFrame.addNewXfrm();
  85. CTPositiveSize2D extPoint = transform.addNewExt();
  86. CTPoint2D offPoint = transform.addNewOff();
  87. extPoint.setCx(0);
  88. extPoint.setCy(0);
  89. offPoint.setX(0);
  90. offPoint.setY(0);
  91. /* CTGraphicalObject graphic = */ graphicFrame.addNewGraphic();
  92. prototype = graphicFrame;
  93. }
  94. return prototype;
  95. }
  96. /**
  97. * Sets the frame macro.
  98. */
  99. public void setMacro(String macro) {
  100. graphicFrame.setMacro(macro);
  101. }
  102. /**
  103. * Sets the frame name.
  104. */
  105. public void setName(String name) {
  106. getNonVisualProperties().setName(name);
  107. }
  108. /**
  109. * Returns the frame name.
  110. * @return name of the frame
  111. */
  112. public String getName() {
  113. return getNonVisualProperties().getName();
  114. }
  115. private CTNonVisualDrawingProps getNonVisualProperties() {
  116. CTGraphicalObjectFrameNonVisual nvGraphic = graphicFrame.getNvGraphicFramePr();
  117. return nvGraphic.getCNvPr();
  118. }
  119. /**
  120. * Attaches frame to an anchor.
  121. */
  122. protected void setAnchor(XSSFClientAnchor anchor) {
  123. this.anchor = anchor;
  124. }
  125. /**
  126. * Returns the frame anchor.
  127. * @return the XSSFClientAnchor anchor this frame is attached to
  128. */
  129. @Override
  130. public XSSFClientAnchor getAnchor() {
  131. return (XSSFClientAnchor) anchor;
  132. }
  133. /**
  134. * Assign a DrawingML chart to the graphic frame.
  135. */
  136. protected void setChart(XSSFChart chart, String relId) {
  137. CTGraphicalObjectData data = graphicFrame.getGraphic().addNewGraphicData();
  138. appendChartElement(data, relId);
  139. chart.setGraphicFrame(this);
  140. }
  141. /**
  142. * Gets the frame id.
  143. */
  144. public long getId() {
  145. return graphicFrame.getNvGraphicFramePr().getCNvPr().getId();
  146. }
  147. /**
  148. * Sets the frame id.
  149. */
  150. protected void setId(long id) {
  151. graphicFrame.getNvGraphicFramePr().getCNvPr().setId(id);
  152. }
  153. /**
  154. * The low level code to insert {@code <c:chart>} tag into
  155. * {@code <a:graphicData>}.
  156. *
  157. * Here is the schema (ECMA-376):
  158. * <pre>
  159. * {@code
  160. * <complexType name="CT_GraphicalObjectData">
  161. * <sequence>
  162. * <any minOccurs="0" maxOccurs="unbounded" processContents="strict"/>
  163. * </sequence>
  164. * <attribute name="uri" type="xsd:token"/>
  165. * </complexType>
  166. * }
  167. * </pre>
  168. */
  169. private void appendChartElement(CTGraphicalObjectData data, String id) {
  170. String r_namespaceUri = STRelationshipId.type.getName().getNamespaceURI();
  171. String c_namespaceUri = XSSFDrawing.NAMESPACE_C;
  172. try (XmlCursor cursor = data.newCursor()) {
  173. cursor.toNextToken();
  174. cursor.beginElement(new QName(c_namespaceUri, "chart", "c"));
  175. cursor.insertAttributeWithValue(new QName(r_namespaceUri, "id", "r"), id);
  176. }
  177. data.setUri(c_namespaceUri);
  178. }
  179. @Override
  180. protected CTShapeProperties getShapeProperties(){
  181. return null;
  182. }
  183. @Override
  184. public String getShapeName() {
  185. return graphicFrame.getNvGraphicFramePr().getCNvPr().getName();
  186. }
  187. }