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 	 * @param xShape
35 	 * @return
36 	 * @throws Exception
37 	 */
38 	public static XChartDocument retrieveChartDocument(XShape xShape)throws Exception{
39 		XChartDocument aChartDoc = null;
40 		final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e";
41 		// make the OLE shape a chart
42 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
43 				XPropertySet.class, xShape);
44 		// set the class id for charts
45 		aShapeProp.setPropertyValue("CLSID", msChartClassID);
46 		// retrieve the chart document as model of the OLE shape
47 		aChartDoc = (XChartDocument) UnoRuntime.queryInterface(
48 				XChartDocument.class, aShapeProp.getPropertyValue("Model"));
49 		return aChartDoc;
50 	}
51 
52 	/**
53 	 * Create Chart in ChartDocument.
54 	 * @param aChartDoc
55 	 * @param ChartType
56 	 * @return
57 	 * @throws Exception
58 	 */
59 	public static XDiagram createChart(XChartDocument aChartDoc, String ChartType)
60 			throws Exception {
61 
62 		// let aChartDoc be a valid XChartDocument
63 		// get the factory that can create diagrams
64 		XMultiServiceFactory aFact = (XMultiServiceFactory) UnoRuntime
65 				.queryInterface(XMultiServiceFactory.class, aChartDoc);
66 		XDiagram aDiagram = (XDiagram) UnoRuntime.queryInterface(
67 				XDiagram.class, aFact.createInstance(ChartType));
68 		return aDiagram;
69 	}
70 
71 
72 	/**
73 	 * Get Chart Doc from a Shape
74 	 * @param xShape
75 	 * @return
76 	 * @throws Exception
77 	 */
78 	public static XChartDocument getChartDocument(XShape xShape)throws Exception{
79 		XChartDocument aChartDoc = null;
80 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
81 				XPropertySet.class, xShape);
82 		// retrieve the chart document as model of the OLE shape
83 		aChartDoc = (XChartDocument) UnoRuntime.queryInterface(
84 				XChartDocument.class, aShapeProp.getPropertyValue("Model"));
85 		return aChartDoc;
86 
87 	}
88 }
89