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.3KB

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