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
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 *************************************************************************/
36 // __________ Imports __________
38 import java
.util
.Random
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.lang
.XMultiServiceFactory
;
43 import com
.sun
.star
.container
.*;
45 // application specific classes
46 import com
.sun
.star
.sheet
.*;
47 import com
.sun
.star
.table
.*;
48 import com
.sun
.star
.chart
.*;
49 import com
.sun
.star
.text
.XText
;
51 import com
.sun
.star
.document
.XEmbeddedObjectSupplier
;
52 import com
.sun
.star
.frame
.XModel
;
53 // base graphics things
54 import com
.sun
.star
.awt
.Point
;
55 import com
.sun
.star
.awt
.Size
;
56 import com
.sun
.star
.awt
.Rectangle
;
59 import com
.sun
.star
.uno
.RuntimeException
;
60 import com
.sun
.star
.container
.NoSuchElementException
;
62 // __________ Implementation __________
64 // Helper for accessing a calc document
66 public class CalcHelper
68 public CalcHelper( XSpreadsheetDocument aDoc
)
70 maSpreadSheetDoc
= aDoc
;
76 public XSpreadsheet
getChartSheet() throws RuntimeException
78 XNameAccess aSheetsNA
= UnoRuntime
.queryInterface(
79 XNameAccess
.class, maSpreadSheetDoc
.getSheets() );
81 XSpreadsheet aSheet
= null;
84 aSheet
= UnoRuntime
.queryInterface(
85 XSpreadsheet
.class, aSheetsNA
.getByName( msChartSheetName
) );
87 catch( NoSuchElementException ex
)
89 System
.out
.println( "Couldn't find sheet with name " + msChartSheetName
+ ": " + ex
);
99 public XSpreadsheet
getDataSheet() throws RuntimeException
101 XNameAccess aSheetsNA
= UnoRuntime
.queryInterface(
102 XNameAccess
.class, maSpreadSheetDoc
.getSheets() );
104 XSpreadsheet aSheet
= null;
105 if( aSheetsNA
!= null )
109 aSheet
= UnoRuntime
.queryInterface(
110 XSpreadsheet
.class, aSheetsNA
.getByName( msDataSheetName
) );
112 catch( NoSuchElementException ex
)
114 System
.out
.println( "Couldn't find sheet with name " + msDataSheetName
+ ": " + ex
);
116 catch( Exception ex
)
125 /** Insert a chart using the given name as name of the OLE object and the range as corresponding
126 range of data to be used for rendering. The chart is placed in the sheet for charts at
127 position aUpperLeft extending as large as given in aExtent.
129 The service name must be the name of a diagram service that can be instantiated via the
130 factory of the chart document
132 public XChartDocument
insertChart(
134 CellRangeAddress aRange
,
137 String sChartServiceName
)
139 XChartDocument aResult
= null;
140 XTableChartsSupplier aSheet
;
142 // get the sheet to insert the chart
145 aSheet
= UnoRuntime
.queryInterface(
146 XTableChartsSupplier
.class, getChartSheet() );
148 catch( Exception ex
)
150 System
.out
.println( "Sheet not found" + ex
);
154 XTableCharts aChartCollection
= aSheet
.getCharts();
155 XNameAccess aChartCollectionNA
= UnoRuntime
.queryInterface(
156 XNameAccess
.class, aChartCollection
);
158 if( aChartCollectionNA
!= null &&
159 ! aChartCollectionNA
.hasByName( sChartName
) )
161 Rectangle aRect
= new Rectangle( aUpperLeft
.X
, aUpperLeft
.Y
, aExtent
.Width
, aExtent
.Height
);
163 CellRangeAddress
[] aAddresses
= new CellRangeAddress
[ 1 ];
164 aAddresses
[ 0 ] = aRange
;
166 // first bool: ColumnHeaders
167 // second bool: RowHeaders
168 aChartCollection
.addNewByName( sChartName
, aRect
, aAddresses
, true, false );
172 XTableChart aTableChart
= UnoRuntime
.queryInterface(
173 XTableChart
.class, aChartCollectionNA
.getByName( sChartName
));
175 // the table chart is an embedded object which contains the chart document
176 aResult
= UnoRuntime
.queryInterface(
177 XChartDocument
.class,
178 UnoRuntime
.queryInterface(
179 XEmbeddedObjectSupplier
.class,
180 aTableChart
).getEmbeddedObject());
182 // create a diagram via the factory and set this as new diagram
184 UnoRuntime
.queryInterface(
186 UnoRuntime
.queryInterface(
187 XMultiServiceFactory
.class,
188 aResult
).createInstance( sChartServiceName
)));
190 catch( NoSuchElementException ex
)
192 System
.out
.println( "Couldn't find chart with name " + sChartName
+ ": " + ex
);
194 catch( Exception ex
)
203 /** Fill a rectangular range with random numbers.
204 The first column has increasing values
206 public XCellRange
insertRandomRange( int nColumnCount
, int nRowCount
)
208 XCellRange aRange
= null;
210 // get the sheet to insert the chart
213 XSpreadsheet aSheet
= getDataSheet();
214 XCellRange aSheetRange
= UnoRuntime
.queryInterface( XCellRange
.class, aSheet
);
216 aRange
= aSheetRange
.getCellRangeByPosition(
218 nColumnCount
- 1, nRowCount
- 1 );
222 double fRange
= 10.0;
224 Random aGenerator
= new Random();
227 for( nCol
= 0; nCol
< nColumnCount
; nCol
++ )
231 (aSheet
.getCellByPosition( nCol
, 0 )).setFormula( "X" );
235 (aSheet
.getCellByPosition( nCol
, 0 )).setFormula( "Random " + nCol
);
238 for( nRow
= 1; nRow
< nRowCount
; nRow
++ )
242 // x values: ascending numbers
243 fValue
= nRow
+ aGenerator
.nextDouble();
247 fValue
= fBase
+ ( aGenerator
.nextGaussian() * fRange
);
250 // put value into cell
252 // note: getCellByPosition is a method at ...table.XCellRange which
253 // the XSpreadsheet inherits via ...sheet.XSheetCellRange
254 (aSheet
.getCellByPosition( nCol
, nRow
)).setValue( fValue
);
259 catch( Exception ex
)
261 System
.out
.println( "Sheet not found" + ex
);
269 public XCellRange
insertFormulaRange( int nColumnCount
, int nRowCount
)
271 XCellRange aRange
= null;
273 // get the sheet to insert the chart
276 XSpreadsheet aSheet
= getDataSheet();
277 XCellRange aSheetRange
= UnoRuntime
.queryInterface( XCellRange
.class, aSheet
);
279 aRange
= aSheetRange
.getCellRangeByPosition(
281 nColumnCount
- 1, nRowCount
- 1 );
285 double fFactor
= 2.0 * Math
.PI
/ (nRowCount
- 1);
288 // set variable factor for cos formula
289 int nFactorCol
= nColumnCount
+ 2;
290 (aSheet
.getCellByPosition( nFactorCol
- 1, 0 )).setValue( 0.2 );
292 XText xCellText
= UnoRuntime
.queryInterface( XText
.class, aSheet
.getCellByPosition( nFactorCol
- 1, 1 ) );
293 xCellText
.setString( "Change the factor above and\nwatch the changes in the chart" );
295 for( nCol
= 0; nCol
< nColumnCount
; nCol
++ )
297 for( nRow
= 0; nRow
< nRowCount
; nRow
++ )
301 // x values: ascending numbers
302 fValue
= nRow
* fFactor
;
303 (aSheet
.getCellByPosition( nCol
, nRow
)).setValue( fValue
);
312 aFormula
+= "(INDIRECT(ADDRESS(" + (nRow
+ 1) + ";1)))+RAND()*INDIRECT(ADDRESS(1;" + nFactorCol
+ "))";
313 (aSheet
.getCellByPosition( nCol
, nRow
)).setFormula( aFormula
);
319 catch( Exception ex
)
321 System
.out
.println( "Sheet not found" + ex
);
329 /** Bring the sheet containing charts visually to the foreground
331 public void raiseChartSheet()
333 UnoRuntime
.queryInterface(
334 XSpreadsheetView
.class,
335 UnoRuntime
.queryInterface(
337 maSpreadSheetDoc
).getCurrentController()).setActiveSheet( getChartSheet() );
341 // __________ private members and methods __________
343 private static final String msDataSheetName
= "Data";
344 private static final String msChartSheetName
= "Chart";
346 private final XSpreadsheetDocument maSpreadSheetDoc
;
351 /** create two sheets, one for data and one for charts in the document
353 private void initSpreadSheet()
355 if( maSpreadSheetDoc
!= null )
357 XSpreadsheets aSheets
= maSpreadSheetDoc
.getSheets();
358 XNameContainer aSheetsNC
= UnoRuntime
.queryInterface(
359 XNameContainer
.class, aSheets
);
360 XIndexAccess aSheetsIA
= UnoRuntime
.queryInterface(
361 XIndexAccess
.class, aSheets
);
363 if( aSheets
!= null &&
369 // remove all sheets except one
370 for( int i
= aSheetsIA
.getCount() - 1; i
> 0; i
-- )
372 aSheetsNC
.removeByName(
373 UnoRuntime
.queryInterface(
374 XNamed
.class, aSheetsIA
.getByIndex( i
) ).getName() );
377 XNamed aFirstSheet
= UnoRuntime
.queryInterface(
379 aSheetsIA
.getByIndex( 0 ));
381 // first sheet becomes data sheet
382 aFirstSheet
.setName( msDataSheetName
);
384 // second sheet becomes chart sheet
385 aSheets
.insertNewByName( msChartSheetName
, (short)1 );
387 catch( Exception ex
)
389 System
.out
.println( "Couldn't initialize Spreadsheet Document: " + ex
);
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */