1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 *************************************************************************/
35 // __________ Imports __________
37 import java
.util
.Random
;
39 import com
.sun
.star
.uno
.UnoRuntime
;
40 import com
.sun
.star
.lang
.XMultiServiceFactory
;
42 import com
.sun
.star
.container
.*;
44 // application specific classes
45 import com
.sun
.star
.sheet
.*;
46 import com
.sun
.star
.table
.*;
47 import com
.sun
.star
.chart
.*;
48 import com
.sun
.star
.text
.XText
;
50 import com
.sun
.star
.document
.XEmbeddedObjectSupplier
;
51 import com
.sun
.star
.frame
.XModel
;
52 // base graphics things
53 import com
.sun
.star
.awt
.Point
;
54 import com
.sun
.star
.awt
.Size
;
55 import com
.sun
.star
.awt
.Rectangle
;
58 import com
.sun
.star
.uno
.RuntimeException
;
59 import com
.sun
.star
.container
.NoSuchElementException
;
61 // __________ Implementation __________
63 // Helper for accessing a calc document
65 public class CalcHelper
67 public CalcHelper( XSpreadsheetDocument aDoc
)
69 maSpreadSheetDoc
= aDoc
;
75 public XSpreadsheet
getChartSheet() throws RuntimeException
77 XNameAccess aSheetsNA
= UnoRuntime
.queryInterface(
78 XNameAccess
.class, maSpreadSheetDoc
.getSheets() );
80 XSpreadsheet aSheet
= null;
83 aSheet
= UnoRuntime
.queryInterface(
84 XSpreadsheet
.class, aSheetsNA
.getByName( msChartSheetName
) );
86 catch( NoSuchElementException ex
)
88 System
.out
.println( "Couldn't find sheet with name " + msChartSheetName
+ ": " + ex
);
98 public XSpreadsheet
getDataSheet() throws RuntimeException
100 XNameAccess aSheetsNA
= UnoRuntime
.queryInterface(
101 XNameAccess
.class, maSpreadSheetDoc
.getSheets() );
103 XSpreadsheet aSheet
= null;
104 if( aSheetsNA
!= null )
108 aSheet
= UnoRuntime
.queryInterface(
109 XSpreadsheet
.class, aSheetsNA
.getByName( msDataSheetName
) );
111 catch( NoSuchElementException ex
)
113 System
.out
.println( "Couldn't find sheet with name " + msDataSheetName
+ ": " + ex
);
115 catch( Exception ex
)
124 /** Insert a chart using the given name as name of the OLE object and the range as correspoding
125 range of data to be used for rendering. The chart is placed in the sheet for charts at
126 position aUpperLeft extending as large as given in aExtent.
128 The service name must be the name of a diagram service that can be instantiated via the
129 factory of the chart document
131 public XChartDocument
insertChart(
133 CellRangeAddress aRange
,
136 String sChartServiceName
)
138 XChartDocument aResult
= null;
139 XTableChartsSupplier aSheet
;
141 // get the sheet to insert the chart
144 aSheet
= UnoRuntime
.queryInterface(
145 XTableChartsSupplier
.class, getChartSheet() );
147 catch( Exception ex
)
149 System
.out
.println( "Sheet not found" + ex
);
153 XTableCharts aChartCollection
= aSheet
.getCharts();
154 XNameAccess aChartCollectionNA
= UnoRuntime
.queryInterface(
155 XNameAccess
.class, aChartCollection
);
157 if( aChartCollectionNA
!= null &&
158 ! aChartCollectionNA
.hasByName( sChartName
) )
160 Rectangle aRect
= new Rectangle( aUpperLeft
.X
, aUpperLeft
.Y
, aExtent
.Width
, aExtent
.Height
);
162 CellRangeAddress
[] aAddresses
= new CellRangeAddress
[ 1 ];
163 aAddresses
[ 0 ] = aRange
;
165 // first bool: ColumnHeaders
166 // second bool: RowHeaders
167 aChartCollection
.addNewByName( sChartName
, aRect
, aAddresses
, true, false );
171 XTableChart aTableChart
= UnoRuntime
.queryInterface(
172 XTableChart
.class, aChartCollectionNA
.getByName( sChartName
));
174 // the table chart is an embedded object which contains the chart document
175 aResult
= UnoRuntime
.queryInterface(
176 XChartDocument
.class,
177 UnoRuntime
.queryInterface(
178 XEmbeddedObjectSupplier
.class,
179 aTableChart
).getEmbeddedObject());
181 // create a diagram via the factory and set this as new diagram
183 UnoRuntime
.queryInterface(
185 UnoRuntime
.queryInterface(
186 XMultiServiceFactory
.class,
187 aResult
).createInstance( sChartServiceName
)));
189 catch( NoSuchElementException ex
)
191 System
.out
.println( "Couldn't find chart with name " + sChartName
+ ": " + ex
);
193 catch( Exception ex
)
202 /** Fill a rectangular range with random numbers.
203 The first column has increasing values
205 public XCellRange
insertRandomRange( int nColumnCount
, int nRowCount
)
207 XCellRange aRange
= null;
209 // get the sheet to insert the chart
212 XSpreadsheet aSheet
= getDataSheet();
213 XCellRange aSheetRange
= UnoRuntime
.queryInterface( XCellRange
.class, aSheet
);
215 aRange
= aSheetRange
.getCellRangeByPosition(
217 nColumnCount
- 1, nRowCount
- 1 );
221 double fRange
= 10.0;
223 Random aGenerator
= new Random();
226 for( nCol
= 0; nCol
< nColumnCount
; nCol
++ )
230 (aSheet
.getCellByPosition( nCol
, 0 )).setFormula( "X" );
234 (aSheet
.getCellByPosition( nCol
, 0 )).setFormula( "Random " + nCol
);
237 for( nRow
= 1; nRow
< nRowCount
; nRow
++ )
241 // x values: ascending numbers
242 fValue
= nRow
+ aGenerator
.nextDouble();
246 fValue
= fBase
+ ( aGenerator
.nextGaussian() * fRange
);
249 // put value into cell
251 // note: getCellByPosition is a method at ...table.XCellRange which
252 // the XSpreadsheet inherits via ...sheet.XSheetCellRange
253 (aSheet
.getCellByPosition( nCol
, nRow
)).setValue( fValue
);
258 catch( Exception ex
)
260 System
.out
.println( "Sheet not found" + ex
);
268 public XCellRange
insertFormulaRange( int nColumnCount
, int nRowCount
)
270 XCellRange aRange
= null;
272 // get the sheet to insert the chart
275 XSpreadsheet aSheet
= getDataSheet();
276 XCellRange aSheetRange
= UnoRuntime
.queryInterface( XCellRange
.class, aSheet
);
278 aRange
= aSheetRange
.getCellRangeByPosition(
280 nColumnCount
- 1, nRowCount
- 1 );
284 double fFactor
= 2.0 * Math
.PI
/ (nRowCount
- 1);
287 // set variable factor for cos formula
288 int nFactorCol
= nColumnCount
+ 2;
289 (aSheet
.getCellByPosition( nFactorCol
- 1, 0 )).setValue( 0.2 );
291 XText xCellText
= UnoRuntime
.queryInterface( XText
.class, aSheet
.getCellByPosition( nFactorCol
- 1, 1 ) );
292 xCellText
.setString( "Change the factor above and\nwatch the changes in the chart" );
294 for( nCol
= 0; nCol
< nColumnCount
; nCol
++ )
296 for( nRow
= 0; nRow
< nRowCount
; nRow
++ )
300 // x values: ascending numbers
301 fValue
= nRow
* fFactor
;
302 (aSheet
.getCellByPosition( nCol
, nRow
)).setValue( fValue
);
311 aFormula
+= "(INDIRECT(ADDRESS(" + (nRow
+ 1) + ";1)))+RAND()*INDIRECT(ADDRESS(1;" + nFactorCol
+ "))";
312 (aSheet
.getCellByPosition( nCol
, nRow
)).setFormula( aFormula
);
318 catch( Exception ex
)
320 System
.out
.println( "Sheet not found" + ex
);
328 /** Bring the sheet containing charts visually to the foreground
330 public void raiseChartSheet()
332 UnoRuntime
.queryInterface(
333 XSpreadsheetView
.class,
334 UnoRuntime
.queryInterface(
336 maSpreadSheetDoc
).getCurrentController()).setActiveSheet( getChartSheet() );
340 // __________ private members and methods __________
342 private static final String msDataSheetName
= "Data";
343 private static final String msChartSheetName
= "Chart";
345 private final XSpreadsheetDocument maSpreadSheetDoc
;
350 /** create two sheets, one for data and one for charts in the document
352 private void initSpreadSheet()
354 if( maSpreadSheetDoc
!= null )
356 XSpreadsheets aSheets
= maSpreadSheetDoc
.getSheets();
357 XNameContainer aSheetsNC
= UnoRuntime
.queryInterface(
358 XNameContainer
.class, aSheets
);
359 XIndexAccess aSheetsIA
= UnoRuntime
.queryInterface(
360 XIndexAccess
.class, aSheets
);
362 if( aSheets
!= null &&
368 // remove all sheets except one
369 for( int i
= aSheetsIA
.getCount() - 1; i
> 0; i
-- )
371 aSheetsNC
.removeByName(
372 UnoRuntime
.queryInterface(
373 XNamed
.class, aSheetsIA
.getByIndex( i
) ).getName() );
376 XNamed aFirstSheet
= UnoRuntime
.queryInterface(
378 aSheetsIA
.getByIndex( 0 ));
380 // first sheet becomes data sheet
381 aFirstSheet
.setName( msDataSheetName
);
383 // second sheet becomes chart sheet
384 aSheets
.insertNewByName( msChartSheetName
, (short)1 );
386 catch( Exception ex
)
388 System
.out
.println( "Couldn't initialize Spreadsheet Document: " + ex
);