Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Spreadsheet / ChartTypeChange.java
blob44673e2b6a5039be9a80eab16ee0a2e08c96cc82
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
34 import com.sun.star.awt.Rectangle;
36 import com.sun.star.beans.PropertyValue;
37 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.chart.XDiagram;
40 import com.sun.star.chart.XChartDocument;
42 import com.sun.star.container.XNameAccess;
43 import com.sun.star.container.XIndexAccess;
45 import com.sun.star.document.XEmbeddedObjectSupplier;
47 import com.sun.star.frame.XComponentLoader;
49 import com.sun.star.lang.XComponent;
50 import com.sun.star.lang.XMultiServiceFactory;
51 import com.sun.star.lang.XMultiComponentFactory;
53 import com.sun.star.sheet.XSpreadsheets;
54 import com.sun.star.sheet.XSpreadsheet;
55 import com.sun.star.sheet.XSpreadsheetDocument;
56 import com.sun.star.sheet.XCellRangeAddressable;
58 import com.sun.star.table.XTableChart;
59 import com.sun.star.table.XTableCharts;
60 import com.sun.star.table.XCell;
61 import com.sun.star.table.XCellRange;
62 import com.sun.star.table.XTableChartsSupplier;
63 import com.sun.star.table.CellRangeAddress;
65 import com.sun.star.uno.UnoRuntime;
66 import com.sun.star.uno.XInterface;
67 import com.sun.star.uno.XComponentContext;
71 /** This class loads an OpenOffice.org Calc document and changes the type of the
72 * embedded chart.
74 public class ChartTypeChange {
76 /** Table chart, which type will be changed.
78 private XTableChart xtablechart = null;
80 /** Service factory
82 private final XMultiComponentFactory xMCF;
84 /** Component context
86 private final XComponentContext xCompContext;
88 /** Beginning of the program.
89 * @param args No arguments will be passed to the class.
91 public static void main(String args[]) {
92 try {
93 ChartTypeChange charttypechange = new ChartTypeChange();
95 // Double array holding all values the chart should be based on.
96 String[][] stringValues = {
97 { "", "Jan", "Feb", "Mar", "Apr", "Mai" },
98 { "Profit", "12.3", "43.2", "5.1", "76", "56.8" },
99 { "Rival in business", "12.2", "12.6", "17.7", "20.4", "100" },
102 // Create the chart with
103 charttypechange.getChart( stringValues );
105 String[] stringChartType = {
106 "com.sun.star.chart.LineDiagram",
107 "com.sun.star.chart.BarDiagram",
108 "com.sun.star.chart.PieDiagram",
109 "com.sun.star.chart.NetDiagram",
110 "com.sun.star.chart.XYDiagram",
111 "com.sun.star.chart.StockDiagram",
112 "com.sun.star.chart.AreaDiagram"
115 for ( int intCounter = 0; intCounter < stringChartType.length;
116 intCounter++ ) {
117 charttypechange.changeChartType( stringChartType[ intCounter ],
118 false );
119 Thread.sleep( 3000 );
122 System.exit(0);
124 catch( Exception exception ) {
125 System.err.println( exception );
129 /** The constructor connects to the OpenOffice.org.
130 * @throws Exception All exceptions are thrown from this method.
132 public ChartTypeChange()
133 throws Exception {
135 /* Bootstraps a component context. Component context to be granted
136 to a component for running. Arbitrary values can be retrieved
137 from the context. */
138 xCompContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
140 /* Gets the service manager instance to be used (or null). This method has
141 been added for convenience, because the service manager is a often used
142 object. */
143 xMCF = xCompContext.getServiceManager();
146 /** This method will change the type of a specified chart.
147 * @param stringType The chart will be converted to this type.
148 * @param booleanIs3D If the chart should be displayed in 3D this parameter should be set to true.
149 * @throws Exception All exceptions are thrown from this method.
151 public void changeChartType( String stringType, boolean booleanIs3D )
152 throws Exception {
153 XEmbeddedObjectSupplier xEmbeddedObjSupplier = UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, xtablechart);
154 XInterface xInterface = xEmbeddedObjSupplier.getEmbeddedObject();
156 XChartDocument xChartDoc = UnoRuntime.queryInterface(
157 XChartDocument.class, xInterface);
158 XDiagram xDiagram = xChartDoc.getDiagram();
159 XMultiServiceFactory xMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, xChartDoc );
160 Object object = xMSF.createInstance( stringType );
161 xDiagram = UnoRuntime.queryInterface(XDiagram.class, object);
163 XPropertySet xPropSet = UnoRuntime.queryInterface(
164 XPropertySet.class, xDiagram );
165 xPropSet.setPropertyValue( "Dim3D", Boolean.valueOf( booleanIs3D ) );
167 xChartDoc.setDiagram(xDiagram);
170 /** Loading an OpenOffice.org Calc document and getting a chart by name.
171 * @param stringFileName Name of the OpenOffice.org Calc document which should
172 * be loaded.
173 * @param stringChartName Name of the chart which should get a new chart type.
175 public void getChart( String stringFileName, String stringChartName ) {
176 try {
177 /* A desktop environment contains tasks with one or more
178 frames in which components can be loaded. Desktop is the
179 environment for components which can instanciate within
180 frames. */
181 XComponentLoader xComponentloader = UnoRuntime.queryInterface( XComponentLoader.class,
182 xMCF.createInstanceWithContext("com.sun.star.frame.Desktop",
183 xCompContext ) );
185 // Load a Writer document, which will be automatically displayed
186 XComponent xComponent = xComponentloader.loadComponentFromURL(
187 "file:///" + stringFileName, "_blank", 0,
188 new PropertyValue[0] );
190 // Query for the interface XSpreadsheetDocument
191 XSpreadsheetDocument xSpreadSheetDocument = UnoRuntime.queryInterface( XSpreadsheetDocument.class, xComponent );
193 XSpreadsheets xSpreadsheets = xSpreadSheetDocument.getSheets() ;
195 XIndexAccess xIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets );
197 XSpreadsheet xSpreadsheet = UnoRuntime.queryInterface(
198 XSpreadsheet.class, xIndexAccess.getByIndex(0));
200 XTableChartsSupplier xTableChartsSupplier = UnoRuntime.queryInterface( XTableChartsSupplier.class, xSpreadsheet );
202 xIndexAccess = UnoRuntime.queryInterface(
203 XIndexAccess.class, xTableChartsSupplier.getCharts() );
205 this.xtablechart = UnoRuntime.queryInterface(
206 XTableChart.class, xIndexAccess.getByIndex( 0 ) );
208 catch( Exception exception ) {
209 System.err.println( exception );
213 /** Creating an empty OpenOffice.org Calc document, inserting data, and getting a
214 * chart by name.
215 * @param stringValues Double array with the values for the chart.
217 public void getChart( String[][] stringValues ) {
218 try {
219 /* A desktop environment contains tasks with one or more
220 frames in which components can be loaded. Desktop is the
221 environment for components which can instanciate within
222 frames. */
223 XComponentLoader xcomponentloader = UnoRuntime.queryInterface( XComponentLoader.class,
224 xMCF.createInstanceWithContext(
225 "com.sun.star.frame.Desktop",
226 xCompContext ) );
228 // Create an empty calc document, which will be automatically displayed
229 XComponent xComponent = xcomponentloader.loadComponentFromURL(
230 "private:factory/scalc", "_blank", 0,
231 new PropertyValue[0] );
233 // Query for the interface XSpreadsheetDocument
234 XSpreadsheetDocument xspreadsheetdocument = UnoRuntime.queryInterface( XSpreadsheetDocument.class, xComponent );
236 // Get all sheets of the spreadsheet document.
237 XSpreadsheets xspreadsheets = xspreadsheetdocument.getSheets() ;
239 // Get the index of the spreadsheet document.
240 XIndexAccess xindexaccess = UnoRuntime.queryInterface(
241 XIndexAccess.class, xspreadsheets );
243 // Get the first spreadsheet.
244 XSpreadsheet xspreadsheet = UnoRuntime.queryInterface(
245 XSpreadsheet.class, xindexaccess.getByIndex(0));
247 // The double array will written to the spreadsheet
248 for ( int intY = 0; intY < stringValues.length; intY++ ) {
249 for ( int intX = 0; intX < stringValues[ intY ].length;
250 intX++ ) {
251 // Insert the value to the cell, specified by intY and intX.
252 ChartTypeChange.insertIntoCell( intY, intX,
253 stringValues[ intY ][ intX ], xspreadsheet, "" );
257 // Create a rectangle, which holds the size of the chart.
258 Rectangle rectangle = new Rectangle();
259 rectangle.X = 500;
260 rectangle.Y = 3000;
261 rectangle.Width = 25000;
262 rectangle.Height = 11000;
264 // Get the cell range of the spreadsheet.
265 XCellRange xcellrange = UnoRuntime.queryInterface(
266 XCellRange.class, xspreadsheet );
268 // Create the Unicode of the character for the column name.
269 char charRectangle = ( char ) ( 65 + stringValues.length - 1 );
271 // Get maximum length all rows in the double array.
272 int intMaximumWidthRow = 0;
273 for ( int intRow = 0; intRow < stringValues.length; intRow++ ) {
274 if ( stringValues[ intRow ].length > intMaximumWidthRow ) {
275 intMaximumWidthRow = stringValues[ intRow ].length;
279 // Get the cell range of the written values.
280 XCellRange xcellrangeChart = xcellrange.getCellRangeByName( "A1:" +
281 charRectangle + intMaximumWidthRow );
283 // Get the addressable cell range.
284 XCellRangeAddressable xcellrangeaddressable =
285 UnoRuntime.queryInterface(
286 XCellRangeAddressable.class, xcellrangeChart );
288 // Get the cell range address.
289 CellRangeAddress cellrangeaddress = xcellrangeaddressable.getRangeAddress();
291 // Create the cell range address for the chart.
292 CellRangeAddress[] cellrangeaddressChart =
293 new CellRangeAddress[ 1 ];
294 cellrangeaddressChart[ 0 ] = cellrangeaddress;
296 // Get the table charts supplier of the spreadsheet.
297 XTableChartsSupplier xtablechartssupplier = UnoRuntime.queryInterface( XTableChartsSupplier.class, xspreadsheet );
299 // Get all table charts of the spreadsheet.
300 XTableCharts xtablecharts = xtablechartssupplier.getCharts();
302 // Create a table chart with all written values.
303 xtablecharts.addNewByName( "Example", rectangle,
304 cellrangeaddressChart, true, true );
306 // Get the created table chart.
307 this.xtablechart = UnoRuntime.queryInterface(
308 XTableChart.class, UnoRuntime.queryInterface(
309 XNameAccess.class, xtablecharts ).getByName( "Example" ));
311 catch( Exception exception ) {
312 System.err.println( exception );
316 /** Inserting a given value to a cell, that is specified by the parameters intX
317 * and intY.
318 * @param intX Column on the spreadsheet.
319 * @param intY Row on the spreadsheet.
320 * @param stringValue Value to be inserted to a cell.
321 * @param xspreadsheet Spreadsheet of the cell, which will be changed.
322 * @param stringFlag If the value of stringFlag is "V", the stringValue
323 * will be converted to the
324 * float type. Otherwise the stringValue will be written as a formula.
326 public static void insertIntoCell( int intX, int intY, String stringValue,
327 XSpreadsheet xspreadsheet, String stringFlag )
329 XCell xcell = null;
331 try {
332 xcell = xspreadsheet.getCellByPosition( intX, intY );
334 catch ( com.sun.star.lang.IndexOutOfBoundsException exception ) {
335 System.out.println( "Could not get cell." );
337 if ( stringFlag.equals( "V" ) ) {
338 xcell.setValue( ( new Float( stringValue ) ).floatValue() );
340 else {
341 xcell.setFormula( stringValue );