104813259SLiu Zhe /**************************************************************
204813259SLiu Zhe  *
304813259SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
404813259SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
504813259SLiu Zhe  * distributed with this work for additional information
604813259SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
704813259SLiu Zhe  * to you under the Apache License, Version 2.0 (the
804813259SLiu Zhe  * "License"); you may not use this file except in compliance
904813259SLiu Zhe  * with the License.  You may obtain a copy of the License at
1004813259SLiu Zhe  *
1104813259SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
1204813259SLiu Zhe  *
1304813259SLiu Zhe  * Unless required by applicable law or agreed to in writing,
1404813259SLiu Zhe  * software distributed under the License is distributed on an
1504813259SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1604813259SLiu Zhe  * KIND, either express or implied.  See the License for the
1704813259SLiu Zhe  * specific language governing permissions and limitations
1804813259SLiu Zhe  * under the License.
1904813259SLiu Zhe  *
2004813259SLiu Zhe  *************************************************************/
2104813259SLiu Zhe package testlib.uno;
2204813259SLiu Zhe 
2304813259SLiu Zhe import com.sun.star.beans.XPropertySet;
2404813259SLiu Zhe import com.sun.star.chart.XChartDocument;
2504813259SLiu Zhe import com.sun.star.chart.XDiagram;
2604813259SLiu Zhe import com.sun.star.drawing.XShape;
2704813259SLiu Zhe import com.sun.star.lang.XMultiServiceFactory;
2804813259SLiu Zhe import com.sun.star.uno.UnoRuntime;
2904813259SLiu Zhe 
3004813259SLiu Zhe public class ChartUtil {
3104813259SLiu Zhe 
3204813259SLiu Zhe 	/**
3304813259SLiu Zhe 	 * Retrieve Chart document as model of the OLE Shape(use to create chart)
34*70af8983SLiu Zhe 	 *
3504813259SLiu Zhe 	 * @param xShape
3604813259SLiu Zhe 	 * @return
3704813259SLiu Zhe 	 * @throws Exception
3804813259SLiu Zhe 	 */
retrieveChartDocument(XShape xShape)39*70af8983SLiu Zhe 	public static XChartDocument retrieveChartDocument(XShape xShape)
40*70af8983SLiu Zhe 			throws Exception {
4104813259SLiu Zhe 		XChartDocument aChartDoc = null;
4204813259SLiu Zhe 		final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e";
4304813259SLiu Zhe 		// make the OLE shape a chart
4404813259SLiu Zhe 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
4504813259SLiu Zhe 				XPropertySet.class, xShape);
4604813259SLiu Zhe 		// set the class id for charts
4704813259SLiu Zhe 		aShapeProp.setPropertyValue("CLSID", msChartClassID);
4804813259SLiu Zhe 		// retrieve the chart document as model of the OLE shape
4904813259SLiu Zhe 		aChartDoc = (XChartDocument) UnoRuntime.queryInterface(
5004813259SLiu Zhe 				XChartDocument.class, aShapeProp.getPropertyValue("Model"));
5104813259SLiu Zhe 		return aChartDoc;
5204813259SLiu Zhe 	}
53*70af8983SLiu Zhe 
54*70af8983SLiu Zhe 	/**
55*70af8983SLiu Zhe 	 * retrieve chart2 document as model of the OLE shape
56*70af8983SLiu Zhe 	 *
57*70af8983SLiu Zhe 	 * @param xShape
58*70af8983SLiu Zhe 	 * @return
59*70af8983SLiu Zhe 	 * @throws Exception
60*70af8983SLiu Zhe 	 */
retrieveChart2Document( XShape xShape)61*70af8983SLiu Zhe 	public static com.sun.star.chart2.XChartDocument retrieveChart2Document(
62*70af8983SLiu Zhe 			XShape xShape) throws Exception {
63*70af8983SLiu Zhe 		com.sun.star.chart2.XChartDocument aChartDoc = null;
64*70af8983SLiu Zhe 		final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e";
65*70af8983SLiu Zhe 		// make the OLE shape a chart
66*70af8983SLiu Zhe 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
67*70af8983SLiu Zhe 				XPropertySet.class, xShape);
68*70af8983SLiu Zhe 		// set the class id for charts
69*70af8983SLiu Zhe 		aShapeProp.setPropertyValue("CLSID", msChartClassID);
70*70af8983SLiu Zhe 		// retrieve the chart document as model of the OLE shape
71*70af8983SLiu Zhe 		aChartDoc = (com.sun.star.chart2.XChartDocument) UnoRuntime
72*70af8983SLiu Zhe 				.queryInterface(com.sun.star.chart2.XChartDocument.class,
73*70af8983SLiu Zhe 						aShapeProp.getPropertyValue("Model"));
74*70af8983SLiu Zhe 		return aChartDoc;
75*70af8983SLiu Zhe 	}
76*70af8983SLiu Zhe 
7704813259SLiu Zhe 	/**
7804813259SLiu Zhe 	 * Create Chart in ChartDocument.
79*70af8983SLiu Zhe 	 *
8004813259SLiu Zhe 	 * @param aChartDoc
8104813259SLiu Zhe 	 * @param ChartType
8204813259SLiu Zhe 	 * @return
8304813259SLiu Zhe 	 * @throws Exception
8404813259SLiu Zhe 	 */
createChart(XChartDocument aChartDoc, String ChartType)85*70af8983SLiu Zhe 	public static XDiagram createChart(XChartDocument aChartDoc,
86*70af8983SLiu Zhe 			String ChartType) throws Exception {
87*70af8983SLiu Zhe 
8804813259SLiu Zhe 		// let aChartDoc be a valid XChartDocument
8904813259SLiu Zhe 		// get the factory that can create diagrams
9004813259SLiu Zhe 		XMultiServiceFactory aFact = (XMultiServiceFactory) UnoRuntime
9104813259SLiu Zhe 				.queryInterface(XMultiServiceFactory.class, aChartDoc);
9204813259SLiu Zhe 		XDiagram aDiagram = (XDiagram) UnoRuntime.queryInterface(
9304813259SLiu Zhe 				XDiagram.class, aFact.createInstance(ChartType));
9404813259SLiu Zhe 		return aDiagram;
9504813259SLiu Zhe 	}
96*70af8983SLiu Zhe 
97*70af8983SLiu Zhe 	/**
98*70af8983SLiu Zhe 	 * Create Chart2 in ChartDocument.
99*70af8983SLiu Zhe 	 *
100*70af8983SLiu Zhe 	 * @param aChartDoc
101*70af8983SLiu Zhe 	 * @param ChartType
102*70af8983SLiu Zhe 	 * @return
103*70af8983SLiu Zhe 	 * @throws Exception
104*70af8983SLiu Zhe 	 */
createChart2( com.sun.star.chart2.XChartDocument aChartDoc, String ChartType)105*70af8983SLiu Zhe 	public static com.sun.star.chart2.XDiagram createChart2(
106*70af8983SLiu Zhe 			com.sun.star.chart2.XChartDocument aChartDoc, String ChartType)
107*70af8983SLiu Zhe 			throws Exception {
108*70af8983SLiu Zhe 
109*70af8983SLiu Zhe 		// let aChartDoc be a valid XChartDocument
110*70af8983SLiu Zhe 		// get the factory that can create diagrams
111*70af8983SLiu Zhe 		XMultiServiceFactory aFact = (XMultiServiceFactory) UnoRuntime
112*70af8983SLiu Zhe 				.queryInterface(XMultiServiceFactory.class, aChartDoc);
113*70af8983SLiu Zhe 		com.sun.star.chart2.XDiagram aDiagram = (com.sun.star.chart2.XDiagram) UnoRuntime
114*70af8983SLiu Zhe 				.queryInterface(com.sun.star.chart2.XDiagram.class,
115*70af8983SLiu Zhe 						aFact.createInstance(ChartType));
116*70af8983SLiu Zhe 		return aDiagram;
117*70af8983SLiu Zhe 	}
118*70af8983SLiu Zhe 
11904813259SLiu Zhe 	/**
12004813259SLiu Zhe 	 * Get Chart Doc from a Shape
121*70af8983SLiu Zhe 	 *
12204813259SLiu Zhe 	 * @param xShape
12304813259SLiu Zhe 	 * @return
12404813259SLiu Zhe 	 * @throws Exception
12504813259SLiu Zhe 	 */
getChartDocument(XShape xShape)126*70af8983SLiu Zhe 	public static XChartDocument getChartDocument(XShape xShape)
127*70af8983SLiu Zhe 			throws Exception {
12804813259SLiu Zhe 		XChartDocument aChartDoc = null;
12904813259SLiu Zhe 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
13004813259SLiu Zhe 				XPropertySet.class, xShape);
13104813259SLiu Zhe 		// retrieve the chart document as model of the OLE shape
13204813259SLiu Zhe 		aChartDoc = (XChartDocument) UnoRuntime.queryInterface(
13304813259SLiu Zhe 				XChartDocument.class, aShapeProp.getPropertyValue("Model"));
13404813259SLiu Zhe 		return aChartDoc;
135*70af8983SLiu Zhe 
136*70af8983SLiu Zhe 	}
137*70af8983SLiu Zhe 	/**
138*70af8983SLiu Zhe 	 * Get Chart2 Doc from a Shape
139*70af8983SLiu Zhe 	 *
140*70af8983SLiu Zhe 	 * @param xShape
141*70af8983SLiu Zhe 	 * @return
142*70af8983SLiu Zhe 	 * @throws Exception
143*70af8983SLiu Zhe 	 */
getChart2Document(XShape xShape)144*70af8983SLiu Zhe 	public static com.sun.star.chart2.XChartDocument getChart2Document(XShape xShape)
145*70af8983SLiu Zhe 			throws Exception {
146*70af8983SLiu Zhe 		com.sun.star.chart2.XChartDocument aChartDoc = null;
147*70af8983SLiu Zhe 		XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
148*70af8983SLiu Zhe 				XPropertySet.class, xShape);
149*70af8983SLiu Zhe 		// retrieve the chart document as model of the OLE shape
150*70af8983SLiu Zhe 		aChartDoc = (com.sun.star.chart2.XChartDocument) UnoRuntime.queryInterface(
151*70af8983SLiu Zhe 				com.sun.star.chart2.XChartDocument.class, aShapeProp.getPropertyValue("Model"));
152*70af8983SLiu Zhe 		return aChartDoc;
153*70af8983SLiu Zhe 
15404813259SLiu Zhe 	}
15504813259SLiu Zhe }
156