1 /*************************************************************************
3 * $RCSfile: ChartTypeChange.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:15:29 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
40 import com
.sun
.star
.awt
.Rectangle
;
42 import com
.sun
.star
.beans
.PropertyValue
;
43 import com
.sun
.star
.beans
.XPropertySet
;
45 import com
.sun
.star
.chart
.XDiagram
;
46 import com
.sun
.star
.chart
.XChartDocument
;
48 import com
.sun
.star
.container
.XNameAccess
;
49 import com
.sun
.star
.container
.XIndexAccess
;
51 import com
.sun
.star
.document
.XEmbeddedObjectSupplier
;
53 import com
.sun
.star
.frame
.XComponentLoader
;
55 import com
.sun
.star
.lang
.XComponent
;
56 import com
.sun
.star
.lang
.XMultiServiceFactory
;
57 import com
.sun
.star
.lang
.XMultiComponentFactory
;
59 import com
.sun
.star
.sheet
.XSpreadsheets
;
60 import com
.sun
.star
.sheet
.XSpreadsheet
;
61 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
62 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
64 import com
.sun
.star
.table
.XTableChart
;
65 import com
.sun
.star
.table
.XTableCharts
;
66 import com
.sun
.star
.table
.XCell
;
67 import com
.sun
.star
.table
.XCellRange
;
68 import com
.sun
.star
.table
.XTableChartsSupplier
;
69 import com
.sun
.star
.table
.CellRangeAddress
;
71 import com
.sun
.star
.uno
.UnoRuntime
;
72 import com
.sun
.star
.uno
.XInterface
;
73 import com
.sun
.star
.uno
.XComponentContext
;
77 /** This class loads an OpenOffice.org Calc document and changes the type of the
79 * @author Bertram Nolte
81 public class ChartTypeChange
{
83 /** Table chart, which type will be changed.
85 private XTableChart xtablechart
= null;
89 private XMultiComponentFactory xMCF
= null;
93 private XComponentContext xCompContext
= null;
95 /** Beginning of the program.
96 * @param args No arguments will be passed to the class.
98 public static void main(String args
[]) {
100 ChartTypeChange charttypechange
= new ChartTypeChange();
102 // Double array holding all values the chart should be based on.
103 String
[][] stringValues
= {
104 { "", "Jan", "Feb", "Mar", "Apr", "Mai" },
105 { "Profit", "12.3", "43.2", "5.1", "76", "56.8" },
106 { "Rival in business", "12.2", "12.6", "17.7", "20.4", "100" },
109 // Create the chart with
110 charttypechange
.getChart( stringValues
);
112 String
[] stringChartType
= {
113 "com.sun.star.chart.LineDiagram",
114 "com.sun.star.chart.BarDiagram",
115 "com.sun.star.chart.PieDiagram",
116 "com.sun.star.chart.NetDiagram",
117 "com.sun.star.chart.XYDiagram",
118 "com.sun.star.chart.StockDiagram",
119 "com.sun.star.chart.AreaDiagram"
122 for ( int intCounter
= 0; intCounter
< stringChartType
.length
;
124 charttypechange
.changeChartType( stringChartType
[ intCounter
],
126 Thread
.sleep( 3000 );
131 catch( Exception exception
) {
132 System
.err
.println( exception
);
136 /** The constructor connects to the OpenOffice.org.
137 * @param args Parameters for this constructor (connection string).
138 * @throws Exception All exceptions are thrown from this method.
140 public ChartTypeChange()
143 /* Bootstraps a component context. Component context to be granted
144 to a component for running. Arbitrary values can be retrieved
146 xCompContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
148 /* Gets the service manager instance to be used (or null). This method has
149 been added for convenience, because the service manager is a often used
151 xMCF
= xCompContext
.getServiceManager();
154 /** This method will change the type of a specified chart.
155 * @param stringType The chart will be converted to this type.
156 * @param booleanIs3D If the chart should be displayed in 3D this parameter should be set to true.
157 * @throws Exception All exceptions are thrown from this method.
159 public void changeChartType( String stringType
, boolean booleanIs3D
)
161 XEmbeddedObjectSupplier xEmbeddedObjSupplier
= (XEmbeddedObjectSupplier
)
162 UnoRuntime
.queryInterface(XEmbeddedObjectSupplier
.class, xtablechart
);
163 XInterface xInterface
= xEmbeddedObjSupplier
.getEmbeddedObject();
165 XChartDocument xChartDoc
= (XChartDocument
)UnoRuntime
.queryInterface(
166 XChartDocument
.class, xInterface
);
167 XDiagram xDiagram
= (XDiagram
) xChartDoc
.getDiagram();
168 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)
169 UnoRuntime
.queryInterface( XMultiServiceFactory
.class, xChartDoc
);
170 Object object
= xMSF
.createInstance( stringType
);
171 xDiagram
= (XDiagram
) UnoRuntime
.queryInterface(XDiagram
.class, object
);
173 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface(
174 XPropertySet
.class, xDiagram
);
175 xPropSet
.setPropertyValue( "Dim3D", new Boolean( booleanIs3D
) );
177 xChartDoc
.setDiagram(xDiagram
);
180 /** Loading an OpenOffice.org Calc document and getting a chart by name.
181 * @param stringFileName Name of the OpenOffice.org Calc document which should
183 * @param stringChartName Name of the chart which should get a new chart type.
185 public void getChart( String stringFileName
, String stringChartName
) {
187 /* A desktop environment contains tasks with one or more
188 frames in which components can be loaded. Desktop is the
189 environment for components which can instanciate within
191 XComponentLoader xComponentloader
= (XComponentLoader
)
192 UnoRuntime
.queryInterface( XComponentLoader
.class,
193 xMCF
.createInstanceWithContext("com.sun.star.frame.Desktop",
196 // Load a Writer document, which will be automaticly displayed
197 XComponent xComponent
= xComponentloader
.loadComponentFromURL(
198 "file:///" + stringFileName
, "_blank", 0,
199 new PropertyValue
[0] );
201 // Query for the interface XSpreadsheetDocument
202 XSpreadsheetDocument xSpreadSheetDocument
= ( XSpreadsheetDocument
)
203 UnoRuntime
.queryInterface( XSpreadsheetDocument
.class, xComponent
);
205 XSpreadsheets xSpreadsheets
= xSpreadSheetDocument
.getSheets() ;
207 XIndexAccess xIndexAccess
= (XIndexAccess
)
208 UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
210 XSpreadsheet xSpreadsheet
= (XSpreadsheet
) UnoRuntime
.queryInterface(
211 XSpreadsheet
.class, xIndexAccess
.getByIndex(0));
213 XTableChartsSupplier xTableChartsSupplier
= ( XTableChartsSupplier
)
214 UnoRuntime
.queryInterface( XTableChartsSupplier
.class, xSpreadsheet
);
216 xIndexAccess
= (XIndexAccess
) UnoRuntime
.queryInterface(
217 XIndexAccess
.class, xTableChartsSupplier
.getCharts() );
219 this.xtablechart
= (XTableChart
) UnoRuntime
.queryInterface(
220 XTableChart
.class, xIndexAccess
.getByIndex( 0 ) );
222 catch( Exception exception
) {
223 System
.err
.println( exception
);
227 /** Creating an empty OpenOffice.org Calc document, inserting data, and getting a
229 * @param stringValues Double array with the values for the chart.
231 public void getChart( String
[][] stringValues
) {
233 /* A desktop environment contains tasks with one or more
234 frames in which components can be loaded. Desktop is the
235 environment for components which can instanciate within
237 XComponentLoader xcomponentloader
= ( XComponentLoader
)
238 UnoRuntime
.queryInterface( XComponentLoader
.class,
239 xMCF
.createInstanceWithContext(
240 "com.sun.star.frame.Desktop",
243 // Create an empty calc document, which will be automaticly displayed
244 XComponent xComponent
= xcomponentloader
.loadComponentFromURL(
245 "private:factory/scalc", "_blank", 0,
246 new PropertyValue
[0] );
248 // Query for the interface XSpreadsheetDocument
249 XSpreadsheetDocument xspreadsheetdocument
= ( XSpreadsheetDocument
)
250 UnoRuntime
.queryInterface( XSpreadsheetDocument
.class, xComponent
);
252 // Get all sheets of the spreadsheet document.
253 XSpreadsheets xspreadsheets
= xspreadsheetdocument
.getSheets() ;
255 // Get the index of the spreadsheet document.
256 XIndexAccess xindexaccess
= (XIndexAccess
) UnoRuntime
.queryInterface(
257 XIndexAccess
.class, xspreadsheets
);
259 // Get the first spreadsheet.
260 XSpreadsheet xspreadsheet
= (XSpreadsheet
) UnoRuntime
.queryInterface(
261 XSpreadsheet
.class, xindexaccess
.getByIndex(0));
263 // The double array will written to the spreadsheet
264 for ( int intY
= 0; intY
< stringValues
.length
; intY
++ ) {
265 for ( int intX
= 0; intX
< stringValues
[ intY
].length
;
267 // Insert the value to the cell, specified by intY and intX.
268 this.insertIntoCell( intY
, intX
,
269 stringValues
[ intY
][ intX
], xspreadsheet
, "" );
273 // Create a rectangle, which holds the size of the chart.
274 Rectangle rectangle
= new Rectangle();
277 rectangle
.Width
= 25000;
278 rectangle
.Height
= 11000;
280 // Get the cell range of the spreadsheet.
281 XCellRange xcellrange
= ( XCellRange
) UnoRuntime
.queryInterface(
282 XCellRange
.class, xspreadsheet
);
284 // Create the Unicode of the character for the column name.
285 char charRectangle
= ( char ) ( 65 + stringValues
.length
- 1 );
287 // Get maximum length all rows in the double array.
288 int intMaximumWidthRow
= 0;
289 for ( int intRow
= 0; intRow
< stringValues
.length
; intRow
++ ) {
290 if ( stringValues
[ intRow
].length
> intMaximumWidthRow
) {
291 intMaximumWidthRow
= stringValues
[ intRow
].length
;
295 // Get the cell range of the written values.
296 XCellRange xcellrangeChart
= xcellrange
.getCellRangeByName( "A1:" +
297 charRectangle
+ intMaximumWidthRow
);
299 // Get the addressable cell range.
300 XCellRangeAddressable xcellrangeaddressable
=
301 ( XCellRangeAddressable
) UnoRuntime
.queryInterface(
302 XCellRangeAddressable
.class, xcellrangeChart
);
304 // Get the cell range address.
305 CellRangeAddress cellrangeaddress
= xcellrangeaddressable
.getRangeAddress();
307 // Create the cell range address for the chart.
308 CellRangeAddress
[] cellrangeaddressChart
=
309 new CellRangeAddress
[ 1 ];
310 cellrangeaddressChart
[ 0 ] = cellrangeaddress
;
312 // Get the table charts supplier of the spreadsheet.
313 XTableChartsSupplier xtablechartssupplier
= ( XTableChartsSupplier
)
314 UnoRuntime
.queryInterface( XTableChartsSupplier
.class, xspreadsheet
);
316 // Get all table charts of the spreadsheet.
317 XTableCharts xtablecharts
= xtablechartssupplier
.getCharts();
319 // Create a table chart with all written values.
320 xtablecharts
.addNewByName( "Example", rectangle
,
321 cellrangeaddressChart
, true, true );
323 // Get the created table chart.
324 this.xtablechart
= ( XTableChart
) UnoRuntime
.queryInterface(
325 XTableChart
.class, (( XNameAccess
) UnoRuntime
.queryInterface(
326 XNameAccess
.class, xtablecharts
) ).getByName( "Example" ));
328 catch( Exception exception
) {
329 System
.err
.println( exception
);
333 /** Inserting a given value to a cell, that is specified by the parameters intX
335 * @param intX Column on the spreadsheet.
336 * @param intY Row on the spreadsheet.
337 * @param stringValue Value to be inserted to a cell.
338 * @param xspreadsheet Spreadsheet of the cell, which will be changed.
339 * @param stringFlag If the value of stringFlag is "V", the stringValue
340 * will be converted to the
341 * float type. Otherwise the stringValue will be written as a formula.
343 public static void insertIntoCell( int intX
, int intY
, String stringValue
,
344 XSpreadsheet xspreadsheet
, String stringFlag
)
349 xcell
= xspreadsheet
.getCellByPosition( intX
, intY
);
351 catch ( com
.sun
.star
.lang
.IndexOutOfBoundsException exception
) {
352 System
.out
.println( "Could not get cell." );
354 if ( stringFlag
.equals( "V" ) ) {
355 xcell
.setValue( ( new Float( stringValue
) ).floatValue() );
358 xcell
.setFormula( stringValue
);