xref: /AOO41X/main/odk/examples/DevelopersGuide/Charts/CalcHelper.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // __________ Imports __________
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import java.util.Random;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // base classes
40*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
41*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
42*cdf0e10cSrcweir import com.sun.star.lang.*;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir // factory for creating components
45*cdf0e10cSrcweir import com.sun.star.comp.servicemanager.ServiceManager;
46*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
47*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
48*cdf0e10cSrcweir import com.sun.star.uno.XNamingService;
49*cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
50*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir // property access
53*cdf0e10cSrcweir import com.sun.star.beans.*;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir // container access
56*cdf0e10cSrcweir import com.sun.star.container.*;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir // application specific classes
59*cdf0e10cSrcweir import com.sun.star.sheet.*;
60*cdf0e10cSrcweir import com.sun.star.table.*;
61*cdf0e10cSrcweir import com.sun.star.chart.*;
62*cdf0e10cSrcweir import com.sun.star.text.XText;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir import com.sun.star.document.XEmbeddedObjectSupplier;
65*cdf0e10cSrcweir import com.sun.star.frame.XModel;
66*cdf0e10cSrcweir import com.sun.star.frame.XController;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir // base graphics things
69*cdf0e10cSrcweir import com.sun.star.awt.Point;
70*cdf0e10cSrcweir import com.sun.star.awt.Size;
71*cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir // Exceptions
74*cdf0e10cSrcweir import com.sun.star.uno.RuntimeException;
75*cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
76*cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException;
77*cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir // __________ Implementation __________
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir /** Helper for accessing a calc document
82*cdf0e10cSrcweir     @author Björn Milcke
83*cdf0e10cSrcweir  */
84*cdf0e10cSrcweir public class CalcHelper
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir     public CalcHelper( XSpreadsheetDocument aDoc )
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         maSpreadSheetDoc = aDoc;
89*cdf0e10cSrcweir         initSpreadSheet();
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     // ____________________
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     public XSpreadsheet getChartSheet() throws RuntimeException
95*cdf0e10cSrcweir     {
96*cdf0e10cSrcweir         XNameAccess aSheetsNA = (XNameAccess) UnoRuntime.queryInterface(
97*cdf0e10cSrcweir             XNameAccess.class, maSpreadSheetDoc.getSheets() );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir         XSpreadsheet aSheet = null;
100*cdf0e10cSrcweir         try
101*cdf0e10cSrcweir         {
102*cdf0e10cSrcweir             aSheet = (XSpreadsheet) UnoRuntime.queryInterface(
103*cdf0e10cSrcweir                 XSpreadsheet.class, aSheetsNA.getByName( msChartSheetName ) );
104*cdf0e10cSrcweir         }
105*cdf0e10cSrcweir         catch( NoSuchElementException ex )
106*cdf0e10cSrcweir         {
107*cdf0e10cSrcweir             System.out.println( "Couldn't find sheet with name " + msChartSheetName + ": " + ex );
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir         catch( Exception ex )
110*cdf0e10cSrcweir         {}
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         return aSheet;
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     // ____________________
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     public XSpreadsheet getDataSheet() throws RuntimeException
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir         XNameAccess aSheetsNA = (XNameAccess) UnoRuntime.queryInterface(
120*cdf0e10cSrcweir             XNameAccess.class, maSpreadSheetDoc.getSheets() );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         XSpreadsheet aSheet = null;
123*cdf0e10cSrcweir         if( aSheetsNA != null )
124*cdf0e10cSrcweir         {
125*cdf0e10cSrcweir             try
126*cdf0e10cSrcweir             {
127*cdf0e10cSrcweir                 aSheet = (XSpreadsheet) UnoRuntime.queryInterface(
128*cdf0e10cSrcweir                     XSpreadsheet.class, aSheetsNA.getByName( msDataSheetName ) );
129*cdf0e10cSrcweir             }
130*cdf0e10cSrcweir             catch( NoSuchElementException ex )
131*cdf0e10cSrcweir             {
132*cdf0e10cSrcweir                 System.out.println( "Couldn't find sheet with name " + msDataSheetName + ": " + ex );
133*cdf0e10cSrcweir             }
134*cdf0e10cSrcweir             catch( Exception ex )
135*cdf0e10cSrcweir             {}
136*cdf0e10cSrcweir         }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         return aSheet;
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     // ____________________
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     /** Insert a chart using the given name as name of the OLE object and the range as correspoding
144*cdf0e10cSrcweir         range of data to be used for rendering.  The chart is placed in the sheet for charts at
145*cdf0e10cSrcweir         position aUpperLeft extending as large as given in aExtent.
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         The service name must be the name of a diagram service that can be instantiated via the
148*cdf0e10cSrcweir         factory of the chart document
149*cdf0e10cSrcweir      */
150*cdf0e10cSrcweir     public XChartDocument insertChart(
151*cdf0e10cSrcweir         String               sChartName,
152*cdf0e10cSrcweir         CellRangeAddress     aRange,
153*cdf0e10cSrcweir         Point                aUpperLeft,
154*cdf0e10cSrcweir         Size                 aExtent,
155*cdf0e10cSrcweir         String               sChartServiceName )
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         XChartDocument aResult = null;
158*cdf0e10cSrcweir         XTableChartsSupplier aSheet;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         // get the sheet to insert the chart
161*cdf0e10cSrcweir         try
162*cdf0e10cSrcweir         {
163*cdf0e10cSrcweir             aSheet = (XTableChartsSupplier) UnoRuntime.queryInterface(
164*cdf0e10cSrcweir                 XTableChartsSupplier.class, getChartSheet() );
165*cdf0e10cSrcweir         }
166*cdf0e10cSrcweir         catch( Exception ex )
167*cdf0e10cSrcweir         {
168*cdf0e10cSrcweir             System.out.println( "Sheet not found" + ex );
169*cdf0e10cSrcweir             return aResult;
170*cdf0e10cSrcweir         }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         XTableCharts aChartCollection = aSheet.getCharts();
173*cdf0e10cSrcweir         XNameAccess  aChartCollectionNA = (XNameAccess) UnoRuntime.queryInterface(
174*cdf0e10cSrcweir             XNameAccess.class, aChartCollection );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         if( aChartCollectionNA != null &&
177*cdf0e10cSrcweir             ! aChartCollectionNA.hasByName( sChartName ) )
178*cdf0e10cSrcweir         {
179*cdf0e10cSrcweir             Rectangle aRect = new Rectangle( aUpperLeft.X, aUpperLeft.Y, aExtent.Width, aExtent.Height );
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir             CellRangeAddress[] aAddresses = new CellRangeAddress[ 1 ];
182*cdf0e10cSrcweir             aAddresses[ 0 ] = aRange;
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir             // first bool: ColumnHeaders
185*cdf0e10cSrcweir             // second bool: RowHeaders
186*cdf0e10cSrcweir             aChartCollection.addNewByName( sChartName, aRect, aAddresses, true, false );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir             try
189*cdf0e10cSrcweir             {
190*cdf0e10cSrcweir                 XTableChart aTableChart = (XTableChart) UnoRuntime.queryInterface(
191*cdf0e10cSrcweir                     XTableChart.class, aChartCollectionNA.getByName( sChartName ));
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir                 // the table chart is an embedded object which contains the chart document
194*cdf0e10cSrcweir                 aResult = (XChartDocument) UnoRuntime.queryInterface(
195*cdf0e10cSrcweir                     XChartDocument.class,
196*cdf0e10cSrcweir                     ((XEmbeddedObjectSupplier) UnoRuntime.queryInterface(
197*cdf0e10cSrcweir                         XEmbeddedObjectSupplier.class,
198*cdf0e10cSrcweir                         aTableChart )).getEmbeddedObject());
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir                 // create a diagram via the factory and set this as new diagram
201*cdf0e10cSrcweir                 aResult.setDiagram(
202*cdf0e10cSrcweir                     (XDiagram) UnoRuntime.queryInterface(
203*cdf0e10cSrcweir                         XDiagram.class,
204*cdf0e10cSrcweir                         ((XMultiServiceFactory) UnoRuntime.queryInterface(
205*cdf0e10cSrcweir                             XMultiServiceFactory.class,
206*cdf0e10cSrcweir                             aResult )).createInstance( sChartServiceName )));
207*cdf0e10cSrcweir             }
208*cdf0e10cSrcweir             catch( NoSuchElementException ex )
209*cdf0e10cSrcweir             {
210*cdf0e10cSrcweir                 System.out.println( "Couldn't find chart with name " + sChartName + ": " + ex );
211*cdf0e10cSrcweir             }
212*cdf0e10cSrcweir             catch( Exception ex )
213*cdf0e10cSrcweir             {}
214*cdf0e10cSrcweir         }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         return aResult;
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir     // ____________________
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     /** Fill a rectangular range with random numbers.
222*cdf0e10cSrcweir         The first column has increasing values
223*cdf0e10cSrcweir      */
224*cdf0e10cSrcweir     public XCellRange insertRandomRange( int nColumnCount, int nRowCount )
225*cdf0e10cSrcweir     {
226*cdf0e10cSrcweir         XCellRange aRange = null;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir         // get the sheet to insert the chart
229*cdf0e10cSrcweir         try
230*cdf0e10cSrcweir         {
231*cdf0e10cSrcweir             XSpreadsheet aSheet = getDataSheet();
232*cdf0e10cSrcweir             XCellRange aSheetRange = (XCellRange) UnoRuntime.queryInterface( XCellRange.class, aSheet );
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir             aRange = aSheetRange.getCellRangeByPosition(
235*cdf0e10cSrcweir                 0, 0,
236*cdf0e10cSrcweir                 nColumnCount - 1, nRowCount - 1 );
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir             int nCol, nRow;
239*cdf0e10cSrcweir             double fBase  = 0.0;
240*cdf0e10cSrcweir             double fRange = 10.0;
241*cdf0e10cSrcweir             double fValue;
242*cdf0e10cSrcweir             Random aGenerator = new Random();
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir             for( nCol = 0; nCol < nColumnCount; nCol++ )
246*cdf0e10cSrcweir             {
247*cdf0e10cSrcweir                 if( 0 == nCol )
248*cdf0e10cSrcweir                 {
249*cdf0e10cSrcweir                     (aSheet.getCellByPosition( nCol, 0 )).setFormula( "X" );
250*cdf0e10cSrcweir                 }
251*cdf0e10cSrcweir                 else
252*cdf0e10cSrcweir                 {
253*cdf0e10cSrcweir                     (aSheet.getCellByPosition( nCol, 0 )).setFormula( "Random " + nCol );
254*cdf0e10cSrcweir                 }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir                 for( nRow = 1; nRow < nRowCount; nRow++ )
257*cdf0e10cSrcweir                 {
258*cdf0e10cSrcweir                     if( 0 == nCol )
259*cdf0e10cSrcweir                     {
260*cdf0e10cSrcweir                         // x values: ascending numbers
261*cdf0e10cSrcweir                         fValue = (double)nRow + aGenerator.nextDouble();
262*cdf0e10cSrcweir                     }
263*cdf0e10cSrcweir                     else
264*cdf0e10cSrcweir                     {
265*cdf0e10cSrcweir                         fValue = fBase + ( aGenerator.nextGaussian() * fRange );
266*cdf0e10cSrcweir                     }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir                     // put value into cell
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir                     // note: getCellByPosition is a method at ...table.XCellRange which
271*cdf0e10cSrcweir                     //       the XSpreadsheet inherits via ...sheet.XSheetCellRange
272*cdf0e10cSrcweir                     (aSheet.getCellByPosition( nCol, nRow )).setValue( fValue );
273*cdf0e10cSrcweir                 }
274*cdf0e10cSrcweir             }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir         }
277*cdf0e10cSrcweir         catch( Exception ex )
278*cdf0e10cSrcweir         {
279*cdf0e10cSrcweir             System.out.println( "Sheet not found" + ex );
280*cdf0e10cSrcweir         }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir         return aRange;
283*cdf0e10cSrcweir     }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir     // ____________________
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir     public XCellRange insertFormulaRange( int nColumnCount, int nRowCount )
288*cdf0e10cSrcweir     {
289*cdf0e10cSrcweir         XCellRange aRange = null;
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir         // get the sheet to insert the chart
292*cdf0e10cSrcweir         try
293*cdf0e10cSrcweir         {
294*cdf0e10cSrcweir             XSpreadsheet aSheet = getDataSheet();
295*cdf0e10cSrcweir             XCellRange aSheetRange = (XCellRange) UnoRuntime.queryInterface( XCellRange.class, aSheet );
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir             aRange = aSheetRange.getCellRangeByPosition(
298*cdf0e10cSrcweir                 0, 0,
299*cdf0e10cSrcweir                 nColumnCount - 1, nRowCount - 1 );
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir             int nCol, nRow;
302*cdf0e10cSrcweir             double fValue;
303*cdf0e10cSrcweir             double fFactor = 2.0 * java.lang.Math.PI / (double)(nRowCount - 1);
304*cdf0e10cSrcweir             String aFormula;
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir             // set variable factor for cos formula
307*cdf0e10cSrcweir             int nFactorCol = nColumnCount + 2;
308*cdf0e10cSrcweir             (aSheet.getCellByPosition( nFactorCol - 1, 0 )).setValue( 0.2 );
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir             XText xCellText = (XText) UnoRuntime.queryInterface( XText.class, aSheet.getCellByPosition( nFactorCol - 1, 1 ) );
311*cdf0e10cSrcweir             xCellText.setString( "Change the factor above and\nwatch the changes in the chart" );
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir             for( nCol = 0; nCol < nColumnCount; nCol++ )
314*cdf0e10cSrcweir             {
315*cdf0e10cSrcweir                 for( nRow = 0; nRow < nRowCount; nRow++ )
316*cdf0e10cSrcweir                 {
317*cdf0e10cSrcweir                     if( 0 == nCol )
318*cdf0e10cSrcweir                     {
319*cdf0e10cSrcweir                         // x values: ascending numbers
320*cdf0e10cSrcweir                         fValue = (double)nRow * fFactor;
321*cdf0e10cSrcweir                         (aSheet.getCellByPosition( nCol, nRow )).setValue( fValue );
322*cdf0e10cSrcweir                     }
323*cdf0e10cSrcweir                     else
324*cdf0e10cSrcweir                     {
325*cdf0e10cSrcweir                         aFormula = new String( "=" );
326*cdf0e10cSrcweir                         if( nCol % 2 == 0 )
327*cdf0e10cSrcweir                             aFormula += "SIN";
328*cdf0e10cSrcweir                         else
329*cdf0e10cSrcweir                             aFormula += "COS";
330*cdf0e10cSrcweir                         aFormula += "(INDIRECT(ADDRESS(" + (nRow + 1) + ";1)))+RAND()*INDIRECT(ADDRESS(1;" + nFactorCol + "))";
331*cdf0e10cSrcweir                         (aSheet.getCellByPosition( nCol, nRow )).setFormula( aFormula );
332*cdf0e10cSrcweir                     }
333*cdf0e10cSrcweir                 }
334*cdf0e10cSrcweir             }
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir         }
337*cdf0e10cSrcweir         catch( Exception ex )
338*cdf0e10cSrcweir         {
339*cdf0e10cSrcweir             System.out.println( "Sheet not found" + ex );
340*cdf0e10cSrcweir         }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir         return aRange;
343*cdf0e10cSrcweir     }
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir     // ____________________
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir     /** Bring the sheet containing charts visually to the foreground
348*cdf0e10cSrcweir      */
349*cdf0e10cSrcweir     public void raiseChartSheet()
350*cdf0e10cSrcweir     {
351*cdf0e10cSrcweir         ((XSpreadsheetView) UnoRuntime.queryInterface(
352*cdf0e10cSrcweir             XSpreadsheetView.class,
353*cdf0e10cSrcweir             ((XModel) UnoRuntime.queryInterface(
354*cdf0e10cSrcweir                 XModel.class,
355*cdf0e10cSrcweir                 maSpreadSheetDoc )).getCurrentController()) ).setActiveSheet( getChartSheet() );
356*cdf0e10cSrcweir     }
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir     // __________ private members and methods __________
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir     private final String  msDataSheetName  = "Data";
362*cdf0e10cSrcweir     private final String  msChartSheetName = "Chart";
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir     private XSpreadsheetDocument   maSpreadSheetDoc;
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir     // ____________________
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir     /** create two sheets, one for data and one for charts in the document
370*cdf0e10cSrcweir      */
371*cdf0e10cSrcweir     private void initSpreadSheet()
372*cdf0e10cSrcweir     {
373*cdf0e10cSrcweir         if( maSpreadSheetDoc != null )
374*cdf0e10cSrcweir         {
375*cdf0e10cSrcweir             XSpreadsheets  aSheets    = maSpreadSheetDoc.getSheets();
376*cdf0e10cSrcweir             XNameContainer aSheetsNC  = (XNameContainer)  UnoRuntime.queryInterface(
377*cdf0e10cSrcweir                 XNameContainer.class, aSheets );
378*cdf0e10cSrcweir             XIndexAccess   aSheetsIA  = (XIndexAccess)    UnoRuntime.queryInterface(
379*cdf0e10cSrcweir                 XIndexAccess.class, aSheets );
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir             if( aSheets   != null &&
382*cdf0e10cSrcweir                 aSheetsNC != null &&
383*cdf0e10cSrcweir                 aSheetsIA != null )
384*cdf0e10cSrcweir             {
385*cdf0e10cSrcweir                 try
386*cdf0e10cSrcweir                 {
387*cdf0e10cSrcweir                     // remove all sheets except one
388*cdf0e10cSrcweir                     for( int i = aSheetsIA.getCount() - 1; i > 0; i-- )
389*cdf0e10cSrcweir                     {
390*cdf0e10cSrcweir                         aSheetsNC.removeByName(
391*cdf0e10cSrcweir                             ( (XNamed) UnoRuntime.queryInterface(
392*cdf0e10cSrcweir                                 XNamed.class, aSheetsIA.getByIndex( i ) )).getName() );
393*cdf0e10cSrcweir                     }
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir                     XNamed aFirstSheet = (XNamed) UnoRuntime.queryInterface(
396*cdf0e10cSrcweir                         XNamed.class,
397*cdf0e10cSrcweir                         aSheetsIA.getByIndex( 0 ));
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir                     // first sheet becomes data sheet
400*cdf0e10cSrcweir                     aFirstSheet.setName( msDataSheetName );
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir                     // second sheet becomes chart sheet
403*cdf0e10cSrcweir                     aSheets.insertNewByName( msChartSheetName, (short)1 );
404*cdf0e10cSrcweir                 }
405*cdf0e10cSrcweir                 catch( Exception ex )
406*cdf0e10cSrcweir                 {
407*cdf0e10cSrcweir                     System.out.println( "Couldn't initialize Spreadsheet Document: " + ex );
408*cdf0e10cSrcweir                 }
409*cdf0e10cSrcweir             }
410*cdf0e10cSrcweir         }
411*cdf0e10cSrcweir     }
412*cdf0e10cSrcweir }
413