xref: /AOO41X/main/odk/examples/DevelopersGuide/Spreadsheet/SpreadsheetDocHelper.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 import com.sun.star.comp.servicemanager.ServiceManager;
36*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
37*cdf0e10cSrcweir import com.sun.star.uno.XNamingService;
38*cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
39*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
40*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
41*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44*cdf0e10cSrcweir import com.sun.star.uno.RuntimeException;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir // __________  implementation  ____________________________________
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir /** This is a helper class for the spreadsheet and table samples.
49*cdf0e10cSrcweir     It connects to a running office and creates a spreadsheet document.
50*cdf0e10cSrcweir     Additionally it contains various helper functions.
51*cdf0e10cSrcweir  */
52*cdf0e10cSrcweir public class SpreadsheetDocHelper
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir // __  private members  ___________________________________________
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     private final String  msDataSheetName  = "Data";
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     private com.sun.star.uno.XComponentContext  mxRemoteContext;
60*cdf0e10cSrcweir     private com.sun.star.lang.XMultiComponentFactory  mxRemoteServiceManager;
61*cdf0e10cSrcweir //    private com.sun.star.lang.XMultiServiceFactory  mxMSFactory;
62*cdf0e10cSrcweir     private com.sun.star.sheet.XSpreadsheetDocument mxDocument;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir // ________________________________________________________________
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     public SpreadsheetDocHelper( String[] args )
67*cdf0e10cSrcweir     {
68*cdf0e10cSrcweir         // Connect to a running office and get the service manager
69*cdf0e10cSrcweir         connect();
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         // Create a new spreadsheet document
72*cdf0e10cSrcweir         try
73*cdf0e10cSrcweir         {
74*cdf0e10cSrcweir             mxDocument = initDocument();
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir         catch (Exception ex)
77*cdf0e10cSrcweir         {
78*cdf0e10cSrcweir             System.err.println( "Couldn't create document: " + ex );
79*cdf0e10cSrcweir             System.err.println( "Error: Couldn't create Document\nException Message = "
80*cdf0e10cSrcweir                                 + ex.getMessage());
81*cdf0e10cSrcweir             ex.printStackTrace();
82*cdf0e10cSrcweir             System.exit( 1 );
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir // __  helper methods  ____________________________________________
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     /** Returns the service manager of the connected office.
89*cdf0e10cSrcweir         @return  XMultiComponentFactory interface of the service manager. */
90*cdf0e10cSrcweir     public com.sun.star.lang.XMultiComponentFactory getServiceManager()
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         return mxRemoteServiceManager;
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     /** Returns the component context of the connected office
96*cdf0e10cSrcweir         @return  XComponentContext interface of the context. */
97*cdf0e10cSrcweir     public com.sun.star.uno.XComponentContext getContext()
98*cdf0e10cSrcweir     {
99*cdf0e10cSrcweir         return mxRemoteContext;
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     /** Returns the whole spreadsheet document.
103*cdf0e10cSrcweir         @return  XSpreadsheetDocument interface of the document. */
104*cdf0e10cSrcweir     public com.sun.star.sheet.XSpreadsheetDocument getDocument()
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         return mxDocument;
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     /** Returns the spreadsheet with the specified index (0-based).
110*cdf0e10cSrcweir         @param nIndex  The index of the sheet.
111*cdf0e10cSrcweir         @return  XSpreadsheet interface of the sheet. */
112*cdf0e10cSrcweir     public com.sun.star.sheet.XSpreadsheet getSpreadsheet( int nIndex )
113*cdf0e10cSrcweir     {
114*cdf0e10cSrcweir         // Collection of sheets
115*cdf0e10cSrcweir         com.sun.star.sheet.XSpreadsheets xSheets = mxDocument.getSheets();
116*cdf0e10cSrcweir         com.sun.star.sheet.XSpreadsheet xSheet = null;
117*cdf0e10cSrcweir         try
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             com.sun.star.container.XIndexAccess xSheetsIA =
120*cdf0e10cSrcweir                 (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
121*cdf0e10cSrcweir                     com.sun.star.container.XIndexAccess.class, xSheets );
122*cdf0e10cSrcweir             xSheet = (com.sun.star.sheet.XSpreadsheet) UnoRuntime.queryInterface(
123*cdf0e10cSrcweir                com.sun.star.sheet.XSpreadsheet.class, xSheetsIA.getByIndex(nIndex));
124*cdf0e10cSrcweir         }
125*cdf0e10cSrcweir         catch (Exception ex)
126*cdf0e10cSrcweir         {
127*cdf0e10cSrcweir             System.err.println( "Error: caught exception in getSpreadsheet()!\nException Message = "
128*cdf0e10cSrcweir                                 + ex.getMessage());
129*cdf0e10cSrcweir             ex.printStackTrace();
130*cdf0e10cSrcweir         }
131*cdf0e10cSrcweir         return xSheet;
132*cdf0e10cSrcweir     }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     /** Inserts a new empty spreadsheet with the specified name.
135*cdf0e10cSrcweir         @param aName  The name of the new sheet.
136*cdf0e10cSrcweir         @param nIndex  The insertion index.
137*cdf0e10cSrcweir         @return  The XSpreadsheet interface of the new sheet. */
138*cdf0e10cSrcweir     public com.sun.star.sheet.XSpreadsheet insertSpreadsheet(
139*cdf0e10cSrcweir         String aName, short nIndex )
140*cdf0e10cSrcweir     {
141*cdf0e10cSrcweir         // Collection of sheets
142*cdf0e10cSrcweir         com.sun.star.sheet.XSpreadsheets xSheets = mxDocument.getSheets();
143*cdf0e10cSrcweir         com.sun.star.sheet.XSpreadsheet xSheet = null;
144*cdf0e10cSrcweir         try
145*cdf0e10cSrcweir         {
146*cdf0e10cSrcweir             xSheets.insertNewByName( aName, nIndex );
147*cdf0e10cSrcweir             xSheet = (com.sun.star.sheet.XSpreadsheet)
148*cdf0e10cSrcweir                 UnoRuntime.queryInterface(com.sun.star.sheet.XSpreadsheet.class,
149*cdf0e10cSrcweir                                           xSheets.getByName( aName ));
150*cdf0e10cSrcweir         }
151*cdf0e10cSrcweir         catch (Exception ex)
152*cdf0e10cSrcweir         {
153*cdf0e10cSrcweir             System.err.println( "Error: caught exception in insertSpreadsheet()!\nException Message = "
154*cdf0e10cSrcweir                                 + ex.getMessage());
155*cdf0e10cSrcweir             ex.printStackTrace();
156*cdf0e10cSrcweir         }
157*cdf0e10cSrcweir         return xSheet;
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir // ________________________________________________________________
161*cdf0e10cSrcweir // Methods to fill values into cells.
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     /** Writes a double value into a spreadsheet.
164*cdf0e10cSrcweir         @param xSheet  The XSpreadsheet interface of the spreadsheet.
165*cdf0e10cSrcweir         @param aCellName  The address of the cell (or a named range).
166*cdf0e10cSrcweir         @param fValue  The value to write into the cell. */
167*cdf0e10cSrcweir     public void setValue(
168*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheet xSheet,
169*cdf0e10cSrcweir             String aCellName,
170*cdf0e10cSrcweir             double fValue ) throws RuntimeException, Exception
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 ).setValue( fValue );
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     /** Writes a formula into a spreadsheet.
176*cdf0e10cSrcweir         @param xSheet  The XSpreadsheet interface of the spreadsheet.
177*cdf0e10cSrcweir         @param aCellName  The address of the cell (or a named range).
178*cdf0e10cSrcweir         @param aFormula  The formula to write into the cell. */
179*cdf0e10cSrcweir     public void setFormula(
180*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheet xSheet,
181*cdf0e10cSrcweir             String aCellName,
182*cdf0e10cSrcweir             String aFormula ) throws RuntimeException, Exception
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 ).setFormula( aFormula );
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     /** Writes a date with standard date format into a spreadsheet.
188*cdf0e10cSrcweir         @param xSheet  The XSpreadsheet interface of the spreadsheet.
189*cdf0e10cSrcweir         @param aCellName  The address of the cell (or a named range).
190*cdf0e10cSrcweir         @param nDay  The day of the date.
191*cdf0e10cSrcweir         @param nMonth  The month of the date.
192*cdf0e10cSrcweir         @param nYear  The year of the date. */
193*cdf0e10cSrcweir     public void setDate(
194*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheet xSheet,
195*cdf0e10cSrcweir             String aCellName,
196*cdf0e10cSrcweir             int nDay, int nMonth, int nYear ) throws RuntimeException, Exception
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         // Set the date value.
199*cdf0e10cSrcweir         com.sun.star.table.XCell xCell = xSheet.getCellRangeByName( aCellName ).getCellByPosition( 0, 0 );
200*cdf0e10cSrcweir         String aDateStr = nMonth + "/" + nDay + "/" + nYear;
201*cdf0e10cSrcweir         xCell.setFormula( aDateStr );
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir         // Set standard date format.
204*cdf0e10cSrcweir         com.sun.star.util.XNumberFormatsSupplier xFormatsSupplier =
205*cdf0e10cSrcweir             (com.sun.star.util.XNumberFormatsSupplier) UnoRuntime.queryInterface(
206*cdf0e10cSrcweir                 com.sun.star.util.XNumberFormatsSupplier.class, getDocument() );
207*cdf0e10cSrcweir         com.sun.star.util.XNumberFormatTypes xFormatTypes =
208*cdf0e10cSrcweir             (com.sun.star.util.XNumberFormatTypes) UnoRuntime.queryInterface(
209*cdf0e10cSrcweir                 com.sun.star.util.XNumberFormatTypes.class, xFormatsSupplier.getNumberFormats() );
210*cdf0e10cSrcweir         int nFormat = xFormatTypes.getStandardFormat(
211*cdf0e10cSrcweir             com.sun.star.util.NumberFormat.DATE, new com.sun.star.lang.Locale() );
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xPropSet = (com.sun.star.beans.XPropertySet)
214*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCell );
215*cdf0e10cSrcweir         xPropSet.setPropertyValue( "NumberFormat", new Integer( nFormat ) );
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     /** Draws a colored border around the range and writes the headline in the
219*cdf0e10cSrcweir         first cell.
220*cdf0e10cSrcweir         @param xSheet  The XSpreadsheet interface of the spreadsheet.
221*cdf0e10cSrcweir         @param aRange  The address of the cell range (or a named range).
222*cdf0e10cSrcweir         @param aHeadline  The headline text. */
223*cdf0e10cSrcweir     public void prepareRange(
224*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheet xSheet,
225*cdf0e10cSrcweir             String aRange, String aHeadline ) throws RuntimeException, Exception
226*cdf0e10cSrcweir     {
227*cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xPropSet = null;
228*cdf0e10cSrcweir         com.sun.star.table.XCellRange xCellRange = null;
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir         // draw border
231*cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByName( aRange );
232*cdf0e10cSrcweir         xPropSet = (com.sun.star.beans.XPropertySet)
233*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCellRange );
234*cdf0e10cSrcweir         com.sun.star.table.BorderLine aLine = new com.sun.star.table.BorderLine();
235*cdf0e10cSrcweir         aLine.Color = 0x99CCFF;
236*cdf0e10cSrcweir         aLine.InnerLineWidth = aLine.LineDistance = 0;
237*cdf0e10cSrcweir         aLine.OuterLineWidth = 100;
238*cdf0e10cSrcweir         com.sun.star.table.TableBorder aBorder = new com.sun.star.table.TableBorder();
239*cdf0e10cSrcweir         aBorder.TopLine = aBorder.BottomLine = aBorder.LeftLine = aBorder.RightLine = aLine;
240*cdf0e10cSrcweir         aBorder.IsTopLineValid = aBorder.IsBottomLineValid = true;
241*cdf0e10cSrcweir         aBorder.IsLeftLineValid = aBorder.IsRightLineValid = true;
242*cdf0e10cSrcweir         xPropSet.setPropertyValue( "TableBorder", aBorder );
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir         // draw headline
245*cdf0e10cSrcweir         com.sun.star.sheet.XCellRangeAddressable xAddr = (com.sun.star.sheet.XCellRangeAddressable)
246*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeAddressable.class, xCellRange );
247*cdf0e10cSrcweir         com.sun.star.table.CellRangeAddress aAddr = xAddr.getRangeAddress();
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir         xCellRange = xSheet.getCellRangeByPosition(
250*cdf0e10cSrcweir             aAddr.StartColumn, aAddr.StartRow, aAddr.EndColumn, aAddr.StartRow );
251*cdf0e10cSrcweir         xPropSet = (com.sun.star.beans.XPropertySet)
252*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCellRange );
253*cdf0e10cSrcweir         xPropSet.setPropertyValue( "CellBackColor", new Integer( 0x99CCFF ) );
254*cdf0e10cSrcweir         // write headline
255*cdf0e10cSrcweir         com.sun.star.table.XCell xCell = xCellRange.getCellByPosition( 0, 0 );
256*cdf0e10cSrcweir         xCell.setFormula( aHeadline );
257*cdf0e10cSrcweir         xPropSet = (com.sun.star.beans.XPropertySet)
258*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCell );
259*cdf0e10cSrcweir         xPropSet.setPropertyValue( "CharColor", new Integer( 0x003399 ) );
260*cdf0e10cSrcweir         xPropSet.setPropertyValue( "CharWeight", new Float( com.sun.star.awt.FontWeight.BOLD ) );
261*cdf0e10cSrcweir     }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir // ________________________________________________________________
264*cdf0e10cSrcweir // Methods to create cell addresses and range addresses.
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir     /** Creates a com.sun.star.table.CellAddress and initializes it
267*cdf0e10cSrcweir         with the given range.
268*cdf0e10cSrcweir         @param xSheet  The XSpreadsheet interface of the spreadsheet.
269*cdf0e10cSrcweir         @param aCell  The address of the cell (or a named cell). */
270*cdf0e10cSrcweir     public com.sun.star.table.CellAddress createCellAddress(
271*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheet xSheet,
272*cdf0e10cSrcweir             String aCell ) throws RuntimeException, Exception
273*cdf0e10cSrcweir     {
274*cdf0e10cSrcweir         com.sun.star.sheet.XCellAddressable xAddr = (com.sun.star.sheet.XCellAddressable)
275*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XCellAddressable.class,
276*cdf0e10cSrcweir                 xSheet.getCellRangeByName( aCell ).getCellByPosition( 0, 0 ) );
277*cdf0e10cSrcweir         return xAddr.getCellAddress();
278*cdf0e10cSrcweir     }
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     /** Creates a com.sun.star.table.CellRangeAddress and initializes
281*cdf0e10cSrcweir         it with the given range.
282*cdf0e10cSrcweir         @param xSheet  The XSpreadsheet interface of the spreadsheet.
283*cdf0e10cSrcweir         @param aRange  The address of the cell range (or a named range). */
284*cdf0e10cSrcweir     public com.sun.star.table.CellRangeAddress createCellRangeAddress(
285*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheet xSheet, String aRange )
286*cdf0e10cSrcweir     {
287*cdf0e10cSrcweir         com.sun.star.sheet.XCellRangeAddressable xAddr = (com.sun.star.sheet.XCellRangeAddressable)
288*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeAddressable.class,
289*cdf0e10cSrcweir                 xSheet.getCellRangeByName( aRange ) );
290*cdf0e10cSrcweir         return xAddr.getRangeAddress();
291*cdf0e10cSrcweir     }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir // ________________________________________________________________
294*cdf0e10cSrcweir // Methods to convert cell addresses and range addresses to strings.
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir     /** Returns the text address of the cell.
297*cdf0e10cSrcweir         @param nColumn  The column index.
298*cdf0e10cSrcweir         @param nRow  The row index.
299*cdf0e10cSrcweir         @return  A string containing the cell address. */
300*cdf0e10cSrcweir     public String getCellAddressString( int nColumn, int nRow )
301*cdf0e10cSrcweir     {
302*cdf0e10cSrcweir         String aStr = "";
303*cdf0e10cSrcweir         if (nColumn > 25)
304*cdf0e10cSrcweir             aStr += (char) ('A' + nColumn / 26 - 1);
305*cdf0e10cSrcweir         aStr += (char) ('A' + nColumn % 26);
306*cdf0e10cSrcweir         aStr += (nRow + 1);
307*cdf0e10cSrcweir         return aStr;
308*cdf0e10cSrcweir     }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir     /** Returns the text address of the cell range.
311*cdf0e10cSrcweir         @param aCellRange  The cell range address.
312*cdf0e10cSrcweir         @return  A string containing the cell range address. */
313*cdf0e10cSrcweir     public String getCellRangeAddressString(
314*cdf0e10cSrcweir             com.sun.star.table.CellRangeAddress aCellRange )
315*cdf0e10cSrcweir     {
316*cdf0e10cSrcweir         return
317*cdf0e10cSrcweir             getCellAddressString( aCellRange.StartColumn, aCellRange.StartRow )
318*cdf0e10cSrcweir             + ":"
319*cdf0e10cSrcweir             + getCellAddressString( aCellRange.EndColumn, aCellRange.EndRow );
320*cdf0e10cSrcweir     }
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir     /** Returns the text address of the cell range.
323*cdf0e10cSrcweir         @param xCellRange  The XSheetCellRange interface of the cell range.
324*cdf0e10cSrcweir         @param bWithSheet  true = Include sheet name.
325*cdf0e10cSrcweir         @return  A string containing the cell range address. */
326*cdf0e10cSrcweir     public String getCellRangeAddressString(
327*cdf0e10cSrcweir             com.sun.star.sheet.XSheetCellRange xCellRange,
328*cdf0e10cSrcweir             boolean bWithSheet )
329*cdf0e10cSrcweir     {
330*cdf0e10cSrcweir         String aStr = "";
331*cdf0e10cSrcweir         if (bWithSheet)
332*cdf0e10cSrcweir         {
333*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheet xSheet = xCellRange.getSpreadsheet();
334*cdf0e10cSrcweir             com.sun.star.container.XNamed xNamed = (com.sun.star.container.XNamed)
335*cdf0e10cSrcweir                 UnoRuntime.queryInterface( com.sun.star.container.XNamed.class, xSheet );
336*cdf0e10cSrcweir             aStr += xNamed.getName() + ".";
337*cdf0e10cSrcweir         }
338*cdf0e10cSrcweir         com.sun.star.sheet.XCellRangeAddressable xAddr = (com.sun.star.sheet.XCellRangeAddressable)
339*cdf0e10cSrcweir             UnoRuntime.queryInterface( com.sun.star.sheet.XCellRangeAddressable.class, xCellRange );
340*cdf0e10cSrcweir         aStr += getCellRangeAddressString( xAddr.getRangeAddress() );
341*cdf0e10cSrcweir         return aStr;
342*cdf0e10cSrcweir     }
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir     /** Returns a list of addresses of all cell ranges contained in the collection.
345*cdf0e10cSrcweir         @param xRangesIA  The XIndexAccess interface of the collection.
346*cdf0e10cSrcweir         @return  A string containing the cell range address list. */
347*cdf0e10cSrcweir     public String getCellRangeListString(
348*cdf0e10cSrcweir             com.sun.star.container.XIndexAccess xRangesIA ) throws RuntimeException, Exception
349*cdf0e10cSrcweir     {
350*cdf0e10cSrcweir         String aStr = "";
351*cdf0e10cSrcweir         int nCount = xRangesIA.getCount();
352*cdf0e10cSrcweir         for (int nIndex = 0; nIndex < nCount; ++nIndex)
353*cdf0e10cSrcweir         {
354*cdf0e10cSrcweir             if (nIndex > 0)
355*cdf0e10cSrcweir                 aStr += " ";
356*cdf0e10cSrcweir             Object aRangeObj = xRangesIA.getByIndex( nIndex );
357*cdf0e10cSrcweir             com.sun.star.sheet.XSheetCellRange xCellRange = (com.sun.star.sheet.XSheetCellRange)
358*cdf0e10cSrcweir                 UnoRuntime.queryInterface( com.sun.star.sheet.XSheetCellRange.class, aRangeObj );
359*cdf0e10cSrcweir             aStr += getCellRangeAddressString( xCellRange, false );
360*cdf0e10cSrcweir         }
361*cdf0e10cSrcweir         return aStr;
362*cdf0e10cSrcweir     }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir // ________________________________________________________________
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir     // Connect to a running office that is accepting connections.
367*cdf0e10cSrcweir     private void connect()
368*cdf0e10cSrcweir     {
369*cdf0e10cSrcweir         if (mxRemoteContext == null && mxRemoteServiceManager == null) {
370*cdf0e10cSrcweir             try {
371*cdf0e10cSrcweir                 // First step: get the remote office component context
372*cdf0e10cSrcweir                 mxRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
373*cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir                 mxRemoteServiceManager = mxRemoteContext.getServiceManager();
376*cdf0e10cSrcweir             }
377*cdf0e10cSrcweir             catch( Exception e) {
378*cdf0e10cSrcweir                 System.err.println("ERROR: can't get a component context from a running office ...");
379*cdf0e10cSrcweir                 e.printStackTrace();
380*cdf0e10cSrcweir                 System.exit(1);
381*cdf0e10cSrcweir             }
382*cdf0e10cSrcweir         }
383*cdf0e10cSrcweir     }
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir     /** Creates an empty spreadsheet document.
386*cdf0e10cSrcweir         @return  The XSpreadsheetDocument interface of the document. */
387*cdf0e10cSrcweir     private com.sun.star.sheet.XSpreadsheetDocument initDocument()
388*cdf0e10cSrcweir             throws RuntimeException, Exception
389*cdf0e10cSrcweir     {
390*cdf0e10cSrcweir         XComponentLoader aLoader = (XComponentLoader)
391*cdf0e10cSrcweir             UnoRuntime.queryInterface(
392*cdf0e10cSrcweir                 XComponentLoader.class,
393*cdf0e10cSrcweir                 mxRemoteServiceManager.createInstanceWithContext(
394*cdf0e10cSrcweir                     "com.sun.star.frame.Desktop", mxRemoteContext));
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir         XComponent xComponent = aLoader.loadComponentFromURL(
397*cdf0e10cSrcweir             "private:factory/scalc", "_blank", 0,
398*cdf0e10cSrcweir             new com.sun.star.beans.PropertyValue[0] );
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir         return (com.sun.star.sheet.XSpreadsheetDocument)UnoRuntime.queryInterface(
401*cdf0e10cSrcweir             com.sun.star.sheet.XSpreadsheetDocument.class, xComponent );
402*cdf0e10cSrcweir     }
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir // ________________________________________________________________
405*cdf0e10cSrcweir }
406