1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package testlib.uno;
22 
23 import com.sun.star.beans.XPropertySet;
24 import com.sun.star.chart.XChartDocument;
25 import com.sun.star.chart.XDiagram;
26 import com.sun.star.drawing.XShape;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.uno.UnoRuntime;
29 
30 public class ChartUtil {
31 
32 	/**
33 	 * Retrieve Chart document as model of the OLE Shape(use to create chart)
34 	 *
35 	 * @param xShape
36 	 * @return
37 	 * @throws Exception
38 	 */
retrieveChartDocument(XShape xShape)39 	public static XChartDocument retrieveChartDocument(XShape xShape)
40 			throws Exception {
41 		XChartDocument aChartDoc = null;
42 		final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e";
43 		// make the OLE shape a chart
44 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
45 				XPropertySet.class, xShape);
46 		// set the class id for charts
47 		aShapeProp.setPropertyValue("CLSID", msChartClassID);
48 		// retrieve the chart document as model of the OLE shape
49 		aChartDoc = (XChartDocument) UnoRuntime.queryInterface(
50 				XChartDocument.class, aShapeProp.getPropertyValue("Model"));
51 		return aChartDoc;
52 	}
53 
54 	/**
55 	 * retrieve chart2 document as model of the OLE shape
56 	 *
57 	 * @param xShape
58 	 * @return
59 	 * @throws Exception
60 	 */
retrieveChart2Document( XShape xShape)61 	public static com.sun.star.chart2.XChartDocument retrieveChart2Document(
62 			XShape xShape) throws Exception {
63 		com.sun.star.chart2.XChartDocument aChartDoc = null;
64 		final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e";
65 		// make the OLE shape a chart
66 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
67 				XPropertySet.class, xShape);
68 		// set the class id for charts
69 		aShapeProp.setPropertyValue("CLSID", msChartClassID);
70 		// retrieve the chart document as model of the OLE shape
71 		aChartDoc = (com.sun.star.chart2.XChartDocument) UnoRuntime
72 				.queryInterface(com.sun.star.chart2.XChartDocument.class,
73 						aShapeProp.getPropertyValue("Model"));
74 		return aChartDoc;
75 	}
76 
77 	/**
78 	 * Create Chart in ChartDocument.
79 	 *
80 	 * @param aChartDoc
81 	 * @param ChartType
82 	 * @return
83 	 * @throws Exception
84 	 */
createChart(XChartDocument aChartDoc, String ChartType)85 	public static XDiagram createChart(XChartDocument aChartDoc,
86 			String ChartType) throws Exception {
87 
88 		// let aChartDoc be a valid XChartDocument
89 		// get the factory that can create diagrams
90 		XMultiServiceFactory aFact = (XMultiServiceFactory) UnoRuntime
91 				.queryInterface(XMultiServiceFactory.class, aChartDoc);
92 		XDiagram aDiagram = (XDiagram) UnoRuntime.queryInterface(
93 				XDiagram.class, aFact.createInstance(ChartType));
94 		return aDiagram;
95 	}
96 
97 	/**
98 	 * Create Chart2 in ChartDocument.
99 	 *
100 	 * @param aChartDoc
101 	 * @param ChartType
102 	 * @return
103 	 * @throws Exception
104 	 */
createChart2( com.sun.star.chart2.XChartDocument aChartDoc, String ChartType)105 	public static com.sun.star.chart2.XDiagram createChart2(
106 			com.sun.star.chart2.XChartDocument aChartDoc, String ChartType)
107 			throws Exception {
108 
109 		// let aChartDoc be a valid XChartDocument
110 		// get the factory that can create diagrams
111 		XMultiServiceFactory aFact = (XMultiServiceFactory) UnoRuntime
112 				.queryInterface(XMultiServiceFactory.class, aChartDoc);
113 		com.sun.star.chart2.XDiagram aDiagram = (com.sun.star.chart2.XDiagram) UnoRuntime
114 				.queryInterface(com.sun.star.chart2.XDiagram.class,
115 						aFact.createInstance(ChartType));
116 		return aDiagram;
117 	}
118 
119 	/**
120 	 * Get Chart Doc from a Shape
121 	 *
122 	 * @param xShape
123 	 * @return
124 	 * @throws Exception
125 	 */
getChartDocument(XShape xShape)126 	public static XChartDocument getChartDocument(XShape xShape)
127 			throws Exception {
128 		XChartDocument aChartDoc = null;
129 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
130 				XPropertySet.class, xShape);
131 		// retrieve the chart document as model of the OLE shape
132 		aChartDoc = (XChartDocument) UnoRuntime.queryInterface(
133 				XChartDocument.class, aShapeProp.getPropertyValue("Model"));
134 		return aChartDoc;
135 
136 	}
137 	/**
138 	 * Get Chart2 Doc from a Shape
139 	 *
140 	 * @param xShape
141 	 * @return
142 	 * @throws Exception
143 	 */
getChart2Document(XShape xShape)144 	public static com.sun.star.chart2.XChartDocument getChart2Document(XShape xShape)
145 			throws Exception {
146 		com.sun.star.chart2.XChartDocument aChartDoc = null;
147 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
148 				XPropertySet.class, xShape);
149 		// retrieve the chart document as model of the OLE shape
150 		aChartDoc = (com.sun.star.chart2.XChartDocument) UnoRuntime.queryInterface(
151 				com.sun.star.chart2.XChartDocument.class, aShapeProp.getPropertyValue("Model"));
152 		return aChartDoc;
153 
154 	}
155 }
156