cid#1607171 Data race condition
[LibreOffice.git] / odk / examples / java / Spreadsheet / ChartTypeChange.java
blob78f8b14bb4173f61ec59941419aa686c9b89c60d
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
35 import com.sun.star.awt.Rectangle;
37 import com.sun.star.beans.PropertyValue;
38 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.chart.XDiagram;
41 import com.sun.star.chart.XChartDocument;
43 import com.sun.star.container.XNameAccess;
44 import com.sun.star.container.XIndexAccess;
46 import com.sun.star.document.XEmbeddedObjectSupplier;
48 import com.sun.star.frame.XComponentLoader;
50 import com.sun.star.lang.XComponent;
51 import com.sun.star.lang.XMultiServiceFactory;
52 import com.sun.star.lang.XMultiComponentFactory;
54 import com.sun.star.sheet.XSpreadsheets;
55 import com.sun.star.sheet.XSpreadsheet;
56 import com.sun.star.sheet.XSpreadsheetDocument;
57 import com.sun.star.sheet.XCellRangeAddressable;
59 import com.sun.star.table.XTableChart;
60 import com.sun.star.table.XTableCharts;
61 import com.sun.star.table.XCell;
62 import com.sun.star.table.XCellRange;
63 import com.sun.star.table.XTableChartsSupplier;
64 import com.sun.star.table.CellRangeAddress;
66 import com.sun.star.uno.UnoRuntime;
67 import com.sun.star.uno.XInterface;
68 import com.sun.star.uno.XComponentContext;
72 /** This class loads an OpenOffice.org Calc document and changes the type of the
73 * embedded chart.
75 public class ChartTypeChange {
77 /** Table chart, which type will be changed.
79 private XTableChart xtablechart = null;
81 /** Service factory
83 private final XMultiComponentFactory xMCF;
85 /** Component context
87 private final XComponentContext xCompContext;
89 /** Beginning of the program.
90 * @param args No arguments will be passed to the class.
92 public static void main(String args[]) {
93 try {
94 ChartTypeChange charttypechange = new ChartTypeChange();
96 // Double array holding all values the chart should be based on.
97 String[][] stringValues = {
98 { "", "Jan", "Feb", "Mar", "Apr", "Mai" },
99 { "Profit", "12.3", "43.2", "5.1", "76", "56.8" },
100 { "Rival in business", "12.2", "12.6", "17.7", "20.4", "100" },
103 // Create the chart with
104 charttypechange.getChart( stringValues );
106 String[] stringChartType = {
107 "com.sun.star.chart.LineDiagram",
108 "com.sun.star.chart.BarDiagram",
109 "com.sun.star.chart.PieDiagram",
110 "com.sun.star.chart.NetDiagram",
111 "com.sun.star.chart.XYDiagram",
112 "com.sun.star.chart.StockDiagram",
113 "com.sun.star.chart.AreaDiagram"
116 for ( int intCounter = 0; intCounter < stringChartType.length;
117 intCounter++ ) {
118 charttypechange.changeChartType( stringChartType[ intCounter ],
119 false );
120 Thread.sleep( 3000 );
123 System.exit(0);
125 catch( Exception exception ) {
126 System.err.println( exception );
130 /** The constructor connects to the OpenOffice.org.
131 * @throws Exception All exceptions are thrown from this method.
133 public ChartTypeChange()
134 throws Exception {
136 /* Bootstraps a component context. Component context to be granted
137 to a component for running. Arbitrary values can be retrieved
138 from the context. */
139 xCompContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
141 /* Gets the service manager instance to be used (or null). This method has
142 been added for convenience, because the service manager is an often used
143 object. */
144 xMCF = xCompContext.getServiceManager();
147 /** This method will change the type of a specified chart.
148 * @param stringType The chart will be converted to this type.
149 * @param booleanIs3D If the chart should be displayed in 3D this parameter should be set to true.
150 * @throws Exception All exceptions are thrown from this method.
152 public void changeChartType( String stringType, boolean booleanIs3D )
153 throws Exception {
154 XEmbeddedObjectSupplier xEmbeddedObjSupplier = UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class, xtablechart);
155 XInterface xInterface = xEmbeddedObjSupplier.getEmbeddedObject();
157 XChartDocument xChartDoc = UnoRuntime.queryInterface(
158 XChartDocument.class, xInterface);
159 XDiagram xDiagram = xChartDoc.getDiagram();
160 XMultiServiceFactory xMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, xChartDoc );
161 Object object = xMSF.createInstance( stringType );
162 xDiagram = UnoRuntime.queryInterface(XDiagram.class, object);
164 XPropertySet xPropSet = UnoRuntime.queryInterface(
165 XPropertySet.class, xDiagram );
166 xPropSet.setPropertyValue( "Dim3D", Boolean.valueOf( booleanIs3D ) );
168 xChartDoc.setDiagram(xDiagram);
171 /** Loading an OpenOffice.org Calc document and getting a chart by name.
172 * @param stringFileName Name of the OpenOffice.org Calc document which should
173 * be loaded.
174 * @param stringChartName Name of the chart which should get a new chart type.
176 public void getChart( String stringFileName, String stringChartName ) {
177 try {
178 /* A desktop environment contains tasks with one or more
179 frames in which components can be loaded. Desktop is the
180 environment for components which can instantiate within
181 frames. */
182 XComponentLoader xComponentloader = UnoRuntime.queryInterface( XComponentLoader.class,
183 xMCF.createInstanceWithContext("com.sun.star.frame.Desktop",
184 xCompContext ) );
186 // Load a Writer document, which will be automatically displayed
187 XComponent xComponent = xComponentloader.loadComponentFromURL(
188 "file:///" + stringFileName, "_blank", 0,
189 new PropertyValue[0] );
191 // Query for the interface XSpreadsheetDocument
192 XSpreadsheetDocument xSpreadSheetDocument = UnoRuntime.queryInterface( XSpreadsheetDocument.class, xComponent );
194 XSpreadsheets xSpreadsheets = xSpreadSheetDocument.getSheets() ;
196 XIndexAccess xIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets );
198 XSpreadsheet xSpreadsheet = UnoRuntime.queryInterface(
199 XSpreadsheet.class, xIndexAccess.getByIndex(0));
201 XTableChartsSupplier xTableChartsSupplier = UnoRuntime.queryInterface( XTableChartsSupplier.class, xSpreadsheet );
203 xIndexAccess = UnoRuntime.queryInterface(
204 XIndexAccess.class, xTableChartsSupplier.getCharts() );
206 this.xtablechart = UnoRuntime.queryInterface(
207 XTableChart.class, xIndexAccess.getByIndex( 0 ) );
209 catch( Exception exception ) {
210 System.err.println( exception );
214 /** Creating an empty OpenOffice.org Calc document, inserting data, and getting a
215 * chart by name.
216 * @param stringValues Double array with the values for the chart.
218 public void getChart( String[][] stringValues ) {
219 try {
220 /* A desktop environment contains tasks with one or more
221 frames in which components can be loaded. Desktop is the
222 environment for components which can instantiate within
223 frames. */
224 XComponentLoader xcomponentloader = UnoRuntime.queryInterface( XComponentLoader.class,
225 xMCF.createInstanceWithContext(
226 "com.sun.star.frame.Desktop",
227 xCompContext ) );
229 // Create an empty calc document, which will be automatically displayed
230 XComponent xComponent = xcomponentloader.loadComponentFromURL(
231 "private:factory/scalc", "_blank", 0,
232 new PropertyValue[0] );
234 // Query for the interface XSpreadsheetDocument
235 XSpreadsheetDocument xspreadsheetdocument = UnoRuntime.queryInterface( XSpreadsheetDocument.class, xComponent );
237 // Get all sheets of the spreadsheet document.
238 XSpreadsheets xspreadsheets = xspreadsheetdocument.getSheets() ;
240 // Get the index of the spreadsheet document.
241 XIndexAccess xindexaccess = UnoRuntime.queryInterface(
242 XIndexAccess.class, xspreadsheets );
244 // Get the first spreadsheet.
245 XSpreadsheet xspreadsheet = UnoRuntime.queryInterface(
246 XSpreadsheet.class, xindexaccess.getByIndex(0));
248 // The double array will written to the spreadsheet
249 for ( int intY = 0; intY < stringValues.length; intY++ ) {
250 for ( int intX = 0; intX < stringValues[ intY ].length;
251 intX++ ) {
252 // Insert the value to the cell, specified by intY and intX.
253 ChartTypeChange.insertIntoCell( intY, intX,
254 stringValues[ intY ][ intX ], xspreadsheet, "" );
258 // Create a rectangle, which holds the size of the chart.
259 Rectangle rectangle = new Rectangle();
260 rectangle.X = 500;
261 rectangle.Y = 3000;
262 rectangle.Width = 25000;
263 rectangle.Height = 11000;
265 // Get the cell range of the spreadsheet.
266 XCellRange xcellrange = UnoRuntime.queryInterface(
267 XCellRange.class, xspreadsheet );
269 // Create the Unicode of the character for the column name.
270 char charRectangle = ( char ) ( 65 + stringValues.length - 1 );
272 // Get maximum length all rows in the double array.
273 int intMaximumWidthRow = 0;
274 for ( int intRow = 0; intRow < stringValues.length; intRow++ ) {
275 if ( stringValues[ intRow ].length > intMaximumWidthRow ) {
276 intMaximumWidthRow = stringValues[ intRow ].length;
280 // Get the cell range of the written values.
281 XCellRange xcellrangeChart = xcellrange.getCellRangeByName( "A1:" +
282 charRectangle + intMaximumWidthRow );
284 // Get the addressable cell range.
285 XCellRangeAddressable xcellrangeaddressable =
286 UnoRuntime.queryInterface(
287 XCellRangeAddressable.class, xcellrangeChart );
289 // Get the cell range address.
290 CellRangeAddress cellrangeaddress = xcellrangeaddressable.getRangeAddress();
292 // Create the cell range address for the chart.
293 CellRangeAddress[] cellrangeaddressChart =
294 new CellRangeAddress[ 1 ];
295 cellrangeaddressChart[ 0 ] = cellrangeaddress;
297 // Get the table charts supplier of the spreadsheet.
298 XTableChartsSupplier xtablechartssupplier = UnoRuntime.queryInterface( XTableChartsSupplier.class, xspreadsheet );
300 // Get all table charts of the spreadsheet.
301 XTableCharts xtablecharts = xtablechartssupplier.getCharts();
303 // Create a table chart with all written values.
304 xtablecharts.addNewByName( "Example", rectangle,
305 cellrangeaddressChart, true, true );
307 // Get the created table chart.
308 this.xtablechart = UnoRuntime.queryInterface(
309 XTableChart.class, UnoRuntime.queryInterface(
310 XNameAccess.class, xtablecharts ).getByName( "Example" ));
312 catch( Exception exception ) {
313 System.err.println( exception );
317 /** Inserting a given value to a cell, that is specified by the parameters intX
318 * and intY.
319 * @param intX Column on the spreadsheet.
320 * @param intY Row on the spreadsheet.
321 * @param stringValue Value to be inserted to a cell.
322 * @param xspreadsheet Spreadsheet of the cell, which will be changed.
323 * @param stringFlag If the value of stringFlag is "V", the stringValue
324 * will be converted to the
325 * float type. Otherwise the stringValue will be written as a formula.
327 public static void insertIntoCell( int intX, int intY, String stringValue,
328 XSpreadsheet xspreadsheet, String stringFlag )
330 XCell xcell = null;
332 try {
333 xcell = xspreadsheet.getCellByPosition( intX, intY );
335 catch ( com.sun.star.lang.IndexOutOfBoundsException exception ) {
336 System.out.println( "Could not get cell." );
338 if ( stringFlag.equals( "V" ) ) {
339 xcell.setValue( ( Float.valueOf( stringValue ) ).floatValue() );
341 else {
342 xcell.setFormula( stringValue );
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */