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 //***************************************************************************
36*cdf0e10cSrcweir // comment: Step 1: get the Desktop object from the office
37*cdf0e10cSrcweir //          Step 2: open an empty Calc document
38*cdf0e10cSrcweir //          Step 3: enter a example text, set the numberformat to DM
39*cdf0e10cSrcweir //          Step 4: change the numberformat to EUR (Euro)
40*cdf0e10cSrcweir //          Step 5: use the DM/EUR factor on each cell with a content
41*cdf0e10cSrcweir //***************************************************************************
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
44*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir import com.sun.star.container.XEnumeration;
47*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
48*cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir import com.sun.star.document.XActionLockable;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
53*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir import com.sun.star.lang.Locale;
56*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
57*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir import com.sun.star.table.XCell;
60*cdf0e10cSrcweir import com.sun.star.table.XCellRange;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
63*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
64*cdf0e10cSrcweir import com.sun.star.sheet.XSheetCellRanges;
65*cdf0e10cSrcweir import com.sun.star.sheet.XCellRangesQuery;
66*cdf0e10cSrcweir import com.sun.star.sheet.XCellFormatRangesSupplier;
67*cdf0e10cSrcweir import com.sun.star.sheet.XCellRangesQuery;
68*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
71*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
72*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
73*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir import com.sun.star.util.NumberFormat;
76*cdf0e10cSrcweir import com.sun.star.util.XNumberFormats;
77*cdf0e10cSrcweir import com.sun.star.util.XNumberFormatsSupplier;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir public class EuroAdaption {
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     public static void main(String args[]) {
83*cdf0e10cSrcweir         // You need the desktop to create a document
84*cdf0e10cSrcweir         // The getDesktop method does the UNO bootstrapping, gets the
85*cdf0e10cSrcweir         // remote servie manager and the desktop object.
86*cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop = null;
87*cdf0e10cSrcweir         xDesktop = getDesktop();
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir         // create a sheet document
90*cdf0e10cSrcweir         XSpreadsheetDocument xSheetdocument = null;
91*cdf0e10cSrcweir         xSheetdocument = ( XSpreadsheetDocument ) createSheetdocument( xDesktop );
92*cdf0e10cSrcweir         System.out.println( "Create a new Spreadsheet" );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         // get the collection of all sheets from the document
95*cdf0e10cSrcweir         XSpreadsheets xSheets = null;
96*cdf0e10cSrcweir         xSheets = (XSpreadsheets) xSheetdocument.getSheets();
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir         // the Action Interface provides methods to hide actions,
99*cdf0e10cSrcweir         // like inserting data, on a sheet, that increase the performance
100*cdf0e10cSrcweir         XActionLockable xActionInterface = null;
101*cdf0e10cSrcweir         xActionInterface = (XActionLockable) UnoRuntime.queryInterface(
102*cdf0e10cSrcweir             XActionLockable.class, xSheetdocument );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         // lock all actions
105*cdf0e10cSrcweir         xActionInterface.addActionLock();
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir         com.sun.star.sheet.XSpreadsheet xSheet = null;
108*cdf0e10cSrcweir         try {
109*cdf0e10cSrcweir             // get via the index access the first sheet
110*cdf0e10cSrcweir             XIndexAccess xElements = (XIndexAccess) UnoRuntime.queryInterface(
111*cdf0e10cSrcweir                 XIndexAccess.class, xSheets );
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir             // specify the first sheet from the spreadsheet
114*cdf0e10cSrcweir             xSheet = (XSpreadsheet) UnoRuntime.queryInterface(
115*cdf0e10cSrcweir                 XSpreadsheet.class, xElements.getByIndex( 0 ));
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir         catch( Exception e) {
118*cdf0e10cSrcweir             e.printStackTrace(System.err);
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         // get the interface to apply and create new numberformats
122*cdf0e10cSrcweir         XNumberFormatsSupplier xNumberFormatSupplier = null;
123*cdf0e10cSrcweir         xNumberFormatSupplier = (XNumberFormatsSupplier) UnoRuntime.queryInterface(
124*cdf0e10cSrcweir             XNumberFormatsSupplier.class, xSheetdocument );
125*cdf0e10cSrcweir         XNumberFormats xNumberFormats = null;
126*cdf0e10cSrcweir         xNumberFormats = xNumberFormatSupplier.getNumberFormats();
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         // insert some example data in a sheet
129*cdf0e10cSrcweir         createExampleData( xSheet, xNumberFormats );
130*cdf0e10cSrcweir         System.out.println( "Insert example data and use the number format with the currency 'DM'" );
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         // Change the currency from the cells from DM to Euro
133*cdf0e10cSrcweir         Convert(  xSheet, xNumberFormats, "DM", "EUR", 1.95583f );
134*cdf0e10cSrcweir         System.out.println( "Change the number format to EUR and divide the values with the factor 1.95583" );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         // remove all locks, the user see all changes
137*cdf0e10cSrcweir         xActionInterface.removeActionLock();
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         System.out.println("done");
140*cdf0e10cSrcweir         System.exit(0);
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     public static void Convert( XSpreadsheet xSheet, XNumberFormats xNumberFormats,
145*cdf0e10cSrcweir                                 String sOldSymbol, String sNewSymbol,
146*cdf0e10cSrcweir                                 float fFactor ) {
147*cdf0e10cSrcweir         try {
148*cdf0e10cSrcweir             Locale xLanguage = new Locale();
149*cdf0e10cSrcweir             xLanguage.Country = "de";    // Germany -> DM
150*cdf0e10cSrcweir             xLanguage.Language = "de";   // German
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir             // Numberformat string with sNewSymbol
153*cdf0e10cSrcweir             String sSimple = "0 [$" + sNewSymbol + "]";
154*cdf0e10cSrcweir             // create a number format key with the sNewSymbol
155*cdf0e10cSrcweir             int iSimpleKey = NumberFormat( xNumberFormats, sSimple, xLanguage );
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir             // you have to use the FormatSupplier interface to get the
158*cdf0e10cSrcweir             // CellFormat enumeration
159*cdf0e10cSrcweir             XCellFormatRangesSupplier xCellFormatSupplier =
160*cdf0e10cSrcweir                 (XCellFormatRangesSupplier)UnoRuntime.queryInterface(
161*cdf0e10cSrcweir                     XCellFormatRangesSupplier.class, xSheet );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir             // getCellFormatRanges() has the interfaces for the enumeration
164*cdf0e10cSrcweir             XEnumerationAccess xEnumerationAccess =
165*cdf0e10cSrcweir                 (XEnumerationAccess)UnoRuntime.queryInterface(
166*cdf0e10cSrcweir                     XEnumerationAccess.class,
167*cdf0e10cSrcweir                     xCellFormatSupplier.getCellFormatRanges() );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir             XEnumeration xRanges = xEnumerationAccess.createEnumeration();
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir             // create an AnyConverter for later use
172*cdf0e10cSrcweir             AnyConverter aAnyConv = new AnyConverter();
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir             while( xRanges.hasMoreElements() ) {
175*cdf0e10cSrcweir                 // the enumeration returns a cellrange
176*cdf0e10cSrcweir                 XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(
177*cdf0e10cSrcweir                     XCellRange.class, xRanges.nextElement());
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir                 // the PropertySet the get and set the properties from the cellrange
180*cdf0e10cSrcweir                 XPropertySet xCellProp = (XPropertySet)UnoRuntime.queryInterface(
181*cdf0e10cSrcweir                     XPropertySet.class, xCellRange );
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir                 // getPropertyValue returns an Object, you have to cast it to
184*cdf0e10cSrcweir                 // type that you need
185*cdf0e10cSrcweir                 Object oNumberObject = xCellProp.getPropertyValue( "NumberFormat" );
186*cdf0e10cSrcweir                 int iNumberFormat = aAnyConv.toInt(oNumberObject);
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir                 // get the properties from the cellrange numberformat
189*cdf0e10cSrcweir                 XPropertySet xFormat = (XPropertySet)
190*cdf0e10cSrcweir                     xNumberFormats.getByKey(iNumberFormat );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir                 short fType = aAnyConv.toShort(xFormat.getPropertyValue("Type"));
193*cdf0e10cSrcweir                 String sCurrencySymbol = aAnyConv.toString(
194*cdf0e10cSrcweir                     xFormat.getPropertyValue("CurrencySymbol"));
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir                 // change the numberformat only on cellranges with a
197*cdf0e10cSrcweir                 // currency numberformat
198*cdf0e10cSrcweir                 if( ( (fType & com.sun.star.util.NumberFormat.CURRENCY) > 0) &&
199*cdf0e10cSrcweir                     ( sCurrencySymbol.compareTo( sOldSymbol ) == 0 ) ) {
200*cdf0e10cSrcweir                     boolean bThousandSep = aAnyConv.toBoolean(
201*cdf0e10cSrcweir                         xFormat.getPropertyValue("ThousandsSeparator"));
202*cdf0e10cSrcweir                     boolean bNegativeRed = aAnyConv.toBoolean(
203*cdf0e10cSrcweir                         xFormat.getPropertyValue("NegativeRed"));
204*cdf0e10cSrcweir                     short fDecimals = aAnyConv.toShort(
205*cdf0e10cSrcweir                         xFormat.getPropertyValue("Decimals"));
206*cdf0e10cSrcweir                     short fLeadingZeros = aAnyConv.toShort(
207*cdf0e10cSrcweir                         xFormat.getPropertyValue("LeadingZeros"));
208*cdf0e10cSrcweir                     Locale oLocale = (Locale) aAnyConv.toObject(
209*cdf0e10cSrcweir                        new com.sun.star.uno.Type(Locale.class),
210*cdf0e10cSrcweir                        xFormat.getPropertyValue("Locale"));
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir                     // create a new numberformat string
213*cdf0e10cSrcweir                     String sNew = xNumberFormats.generateFormat( iSimpleKey,
214*cdf0e10cSrcweir                                          oLocale, bThousandSep, bNegativeRed,
215*cdf0e10cSrcweir                                          fDecimals, fLeadingZeros );
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir                     // get the NumberKey from the numberformat
218*cdf0e10cSrcweir                     int iNewNumberFormat = NumberFormat( xNumberFormats,
219*cdf0e10cSrcweir                                                          sNew, oLocale );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir                     // set the new numberformat to the cellrange DM->EUR
222*cdf0e10cSrcweir                     xCellProp.setPropertyValue( "NumberFormat",
223*cdf0e10cSrcweir                                                 new Integer( iNewNumberFormat ) );
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir                     // interate over all cells from the cellrange with an
226*cdf0e10cSrcweir                     // content and use the DM/EUR factor
227*cdf0e10cSrcweir                     XCellRangesQuery xCellRangesQuery = (XCellRangesQuery)
228*cdf0e10cSrcweir                         UnoRuntime.queryInterface(
229*cdf0e10cSrcweir                         XCellRangesQuery.class, xCellRange );
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir                     XSheetCellRanges xSheetCellRanges =
232*cdf0e10cSrcweir                         xCellRangesQuery.queryContentCells(
233*cdf0e10cSrcweir                             (short) com.sun.star.sheet.CellFlags.VALUE );
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir                     if( xSheetCellRanges.getCount() > 0 ) {
236*cdf0e10cSrcweir                         XEnumerationAccess xCellEnumerationAccess =
237*cdf0e10cSrcweir                             xSheetCellRanges.getCells();
238*cdf0e10cSrcweir                         XEnumeration xCellEnumeration =
239*cdf0e10cSrcweir                             xCellEnumerationAccess.createEnumeration();
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir                         while( xCellEnumeration.hasMoreElements() ) {
242*cdf0e10cSrcweir                             XCell xCell = (XCell) UnoRuntime.queryInterface(
243*cdf0e10cSrcweir                                 XCell.class, xCellEnumeration.nextElement());
244*cdf0e10cSrcweir                             xCell.setValue( (double) xCell.getValue() / fFactor );
245*cdf0e10cSrcweir                         }
246*cdf0e10cSrcweir                     }
247*cdf0e10cSrcweir                 }
248*cdf0e10cSrcweir             }
249*cdf0e10cSrcweir         }
250*cdf0e10cSrcweir         catch( Exception e) {
251*cdf0e10cSrcweir             e.printStackTrace(System.err);
252*cdf0e10cSrcweir         }
253*cdf0e10cSrcweir     }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir     public static int NumberFormat( XNumberFormats xNumberFormat, String sFormat,
257*cdf0e10cSrcweir                                     com.sun.star.lang.Locale xLanguage ) {
258*cdf0e10cSrcweir         int nRetKey = 0;
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         try {
261*cdf0e10cSrcweir             // exists the numberformat
262*cdf0e10cSrcweir             nRetKey = xNumberFormat.queryKey( sFormat, xLanguage, true );
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir             // if not, create a new one
265*cdf0e10cSrcweir             if( nRetKey == -1 ) {
266*cdf0e10cSrcweir                 nRetKey = xNumberFormat.addNew( sFormat, xLanguage );
267*cdf0e10cSrcweir                 if( nRetKey == -1 )
268*cdf0e10cSrcweir                     nRetKey = 0;
269*cdf0e10cSrcweir             }
270*cdf0e10cSrcweir         }
271*cdf0e10cSrcweir         catch( Exception e) {
272*cdf0e10cSrcweir             e.printStackTrace(System.err);
273*cdf0e10cSrcweir         }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir         return( nRetKey );
276*cdf0e10cSrcweir     }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir     public static void createExampleData( XSpreadsheet xSheet,
280*cdf0e10cSrcweir                                           XNumberFormats xNumberFormat ) {
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir         // enter in a cellrange numbers and change the numberformat to DM
283*cdf0e10cSrcweir         XCell xCell = null;
284*cdf0e10cSrcweir         XCellRange xCellRange = null;
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir         try {
287*cdf0e10cSrcweir             Locale xLanguage = new Locale();
288*cdf0e10cSrcweir             xLanguage.Country = "de";    // Germany -> DM
289*cdf0e10cSrcweir             xLanguage.Language = "de";   // German
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir             // Numberformat string from DM
292*cdf0e10cSrcweir             String sSimple = "0 [$DM]";
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir             // get the numberformat key
295*cdf0e10cSrcweir             int iNumberFormatKey = NumberFormat(xNumberFormat, sSimple, xLanguage);
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir             for( int iCounter=1; iCounter < 10; iCounter++ ) {
298*cdf0e10cSrcweir                 // get one cell and insert a number
299*cdf0e10cSrcweir                 xCell = xSheet.getCellByPosition( 2, 1 + iCounter );
300*cdf0e10cSrcweir                 xCell.setValue( (double) iCounter * 2 );
301*cdf0e10cSrcweir                 xCellRange = xSheet.getCellRangeByPosition( 2, 1 + iCounter,
302*cdf0e10cSrcweir                                                             2, 1 + iCounter );
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir                 // get the ProperySet from the cell, to change the numberformat
305*cdf0e10cSrcweir                 XPropertySet xCellProp = (XPropertySet)UnoRuntime.queryInterface(
306*cdf0e10cSrcweir                     XPropertySet.class, xCellRange );
307*cdf0e10cSrcweir                 xCellProp.setPropertyValue( "NumberFormat",
308*cdf0e10cSrcweir                                             new Integer(iNumberFormatKey) );
309*cdf0e10cSrcweir             }
310*cdf0e10cSrcweir         }
311*cdf0e10cSrcweir         catch( Exception e) {
312*cdf0e10cSrcweir             e.printStackTrace(System.err);
313*cdf0e10cSrcweir         }
314*cdf0e10cSrcweir     }
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir     public static XDesktop getDesktop() {
317*cdf0e10cSrcweir         XDesktop xDesktop = null;
318*cdf0e10cSrcweir         XMultiComponentFactory xMCF = null;
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir         try {
321*cdf0e10cSrcweir             XComponentContext xContext = null;
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir             // get the remote office component context
324*cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir             // get the remote office service manager
327*cdf0e10cSrcweir             xMCF = xContext.getServiceManager();
328*cdf0e10cSrcweir             if( xMCF != null ) {
329*cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir                 Object oDesktop = xMCF.createInstanceWithContext(
332*cdf0e10cSrcweir                     "com.sun.star.frame.Desktop", xContext);
333*cdf0e10cSrcweir                 xDesktop = (XDesktop) UnoRuntime.queryInterface(
334*cdf0e10cSrcweir                     XDesktop.class, oDesktop);
335*cdf0e10cSrcweir             }
336*cdf0e10cSrcweir             else
337*cdf0e10cSrcweir                 System.out.println( "Can't create a desktop. No connection, no remote servicemanager available!" );
338*cdf0e10cSrcweir         }
339*cdf0e10cSrcweir         catch( Exception e) {
340*cdf0e10cSrcweir             e.printStackTrace(System.err);
341*cdf0e10cSrcweir             System.exit(1);
342*cdf0e10cSrcweir         }
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir         return xDesktop;
346*cdf0e10cSrcweir     }
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir     public static XSpreadsheetDocument createSheetdocument( XDesktop xDesktop ) {
350*cdf0e10cSrcweir         XSpreadsheetDocument aSheetDocument = null;
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir         try {
353*cdf0e10cSrcweir             XComponent xComponent = null;
354*cdf0e10cSrcweir             xComponent = CreateNewDocument( xDesktop, "scalc" );
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir             aSheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(
357*cdf0e10cSrcweir                 XSpreadsheetDocument.class, xComponent);
358*cdf0e10cSrcweir         }
359*cdf0e10cSrcweir         catch( Exception e) {
360*cdf0e10cSrcweir             e.printStackTrace(System.err);
361*cdf0e10cSrcweir         }
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir         return aSheetDocument;
364*cdf0e10cSrcweir     }
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir     protected static XComponent CreateNewDocument( XDesktop xDesktop,
367*cdf0e10cSrcweir                                                    String sDocumentType ) {
368*cdf0e10cSrcweir         String sURL = "private:factory/" + sDocumentType;
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir         XComponent xComponent = null;
371*cdf0e10cSrcweir         XComponentLoader xComponentLoader = null;
372*cdf0e10cSrcweir         PropertyValue xValues[] = new PropertyValue[1];
373*cdf0e10cSrcweir         PropertyValue xEmptyArgs[] = new PropertyValue[0];
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir         try {
376*cdf0e10cSrcweir             xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(
377*cdf0e10cSrcweir                 XComponentLoader.class, xDesktop );
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir             xComponent  = xComponentLoader.loadComponentFromURL(
380*cdf0e10cSrcweir                 sURL, "_blank", 0, xEmptyArgs);
381*cdf0e10cSrcweir         }
382*cdf0e10cSrcweir         catch( Exception e) {
383*cdf0e10cSrcweir             e.printStackTrace(System.err);
384*cdf0e10cSrcweir         }
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir         return xComponent ;
387*cdf0e10cSrcweir     }
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir }
390