update dev300-m58
[ooovba.git] / odk / examples / java / Spreadsheet / EuroAdaption.java
blobc464df8f34912a6197dbbb94b40ad5b0ff5b478a
1 /*************************************************************************
3 * $RCSfile: EuroAdaption.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:15:41 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 //***************************************************************************
42 // comment: Step 1: get the Desktop object from the office
43 // Step 2: open an empty Calc document
44 // Step 3: enter a example text, set the numberformat to DM
45 // Step 4: change the numberformat to EUR (Euro)
46 // Step 5: use the DM/EUR factor on each cell with a content
47 //***************************************************************************
49 import com.sun.star.beans.PropertyValue;
50 import com.sun.star.beans.XPropertySet;
52 import com.sun.star.container.XEnumeration;
53 import com.sun.star.container.XIndexAccess;
54 import com.sun.star.container.XEnumerationAccess;
56 import com.sun.star.document.XActionLockable;
58 import com.sun.star.frame.XDesktop;
59 import com.sun.star.frame.XComponentLoader;
61 import com.sun.star.lang.Locale;
62 import com.sun.star.lang.XComponent;
63 import com.sun.star.lang.XMultiComponentFactory;
65 import com.sun.star.table.XCell;
66 import com.sun.star.table.XCellRange;
68 import com.sun.star.sheet.XSpreadsheet;
69 import com.sun.star.sheet.XSpreadsheets;
70 import com.sun.star.sheet.XSheetCellRanges;
71 import com.sun.star.sheet.XCellRangesQuery;
72 import com.sun.star.sheet.XCellFormatRangesSupplier;
73 import com.sun.star.sheet.XCellRangesQuery;
74 import com.sun.star.sheet.XSpreadsheetDocument;
76 import com.sun.star.uno.UnoRuntime;
77 import com.sun.star.uno.AnyConverter;
78 import com.sun.star.uno.XInterface;
79 import com.sun.star.uno.XComponentContext;
81 import com.sun.star.util.NumberFormat;
82 import com.sun.star.util.XNumberFormats;
83 import com.sun.star.util.XNumberFormatsSupplier;
86 public class EuroAdaption {
88 public static void main(String args[]) {
89 // You need the desktop to create a document
90 // The getDesktop method does the UNO bootstrapping, gets the
91 // remote servie manager and the desktop object.
92 com.sun.star.frame.XDesktop xDesktop = null;
93 xDesktop = getDesktop();
95 // create a sheet document
96 XSpreadsheetDocument xSheetdocument = null;
97 xSheetdocument = ( XSpreadsheetDocument ) createSheetdocument( xDesktop );
98 System.out.println( "Create a new Spreadsheet" );
100 // get the collection of all sheets from the document
101 XSpreadsheets xSheets = null;
102 xSheets = (XSpreadsheets) xSheetdocument.getSheets();
104 // the Action Interface provides methods to hide actions,
105 // like inserting data, on a sheet, that increase the performance
106 XActionLockable xActionInterface = null;
107 xActionInterface = (XActionLockable) UnoRuntime.queryInterface(
108 XActionLockable.class, xSheetdocument );
110 // lock all actions
111 xActionInterface.addActionLock();
113 com.sun.star.sheet.XSpreadsheet xSheet = null;
114 try {
115 // get via the index access the first sheet
116 XIndexAccess xElements = (XIndexAccess) UnoRuntime.queryInterface(
117 XIndexAccess.class, xSheets );
119 // specify the first sheet from the spreadsheet
120 xSheet = (XSpreadsheet) UnoRuntime.queryInterface(
121 XSpreadsheet.class, xElements.getByIndex( 0 ));
123 catch( Exception e) {
124 e.printStackTrace(System.err);
127 // get the interface to apply and create new numberformats
128 XNumberFormatsSupplier xNumberFormatSupplier = null;
129 xNumberFormatSupplier = (XNumberFormatsSupplier) UnoRuntime.queryInterface(
130 XNumberFormatsSupplier.class, xSheetdocument );
131 XNumberFormats xNumberFormats = null;
132 xNumberFormats = xNumberFormatSupplier.getNumberFormats();
134 // insert some example data in a sheet
135 createExampleData( xSheet, xNumberFormats );
136 System.out.println( "Insert example data and use the number format with the currency 'DM'" );
138 // Change the currency from the cells from DM to Euro
139 Convert( xSheet, xNumberFormats, "DM", "EUR", 1.95583f );
140 System.out.println( "Change the number format to EUR and divide the values with the factor 1.95583" );
142 // remove all locks, the user see all changes
143 xActionInterface.removeActionLock();
145 System.out.println("done");
146 System.exit(0);
150 public static void Convert( XSpreadsheet xSheet, XNumberFormats xNumberFormats,
151 String sOldSymbol, String sNewSymbol,
152 float fFactor ) {
153 try {
154 Locale xLanguage = new Locale();
155 xLanguage.Country = "de"; // Germany -> DM
156 xLanguage.Language = "de"; // German
158 // Numberformat string with sNewSymbol
159 String sSimple = "0 [$" + sNewSymbol + "]";
160 // create a number format key with the sNewSymbol
161 int iSimpleKey = NumberFormat( xNumberFormats, sSimple, xLanguage );
163 // you have to use the FormatSupplier interface to get the
164 // CellFormat enumeration
165 XCellFormatRangesSupplier xCellFormatSupplier =
166 (XCellFormatRangesSupplier)UnoRuntime.queryInterface(
167 XCellFormatRangesSupplier.class, xSheet );
169 // getCellFormatRanges() has the interfaces for the enumeration
170 XEnumerationAccess xEnumerationAccess =
171 (XEnumerationAccess)UnoRuntime.queryInterface(
172 XEnumerationAccess.class,
173 xCellFormatSupplier.getCellFormatRanges() );
175 XEnumeration xRanges = xEnumerationAccess.createEnumeration();
177 // create an AnyConverter for later use
178 AnyConverter aAnyConv = new AnyConverter();
180 while( xRanges.hasMoreElements() ) {
181 // the enumeration returns a cellrange
182 XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(
183 XCellRange.class, xRanges.nextElement());
185 // the PropertySet the get and set the properties from the cellrange
186 XPropertySet xCellProp = (XPropertySet)UnoRuntime.queryInterface(
187 XPropertySet.class, xCellRange );
189 // getPropertyValue returns an Object, you have to cast it to
190 // type that you need
191 Object oNumberObject = xCellProp.getPropertyValue( "NumberFormat" );
192 int iNumberFormat = aAnyConv.toInt(oNumberObject);
194 // get the properties from the cellrange numberformat
195 XPropertySet xFormat = (XPropertySet)
196 xNumberFormats.getByKey(iNumberFormat );
198 short fType = aAnyConv.toShort(xFormat.getPropertyValue("Type"));
199 String sCurrencySymbol = aAnyConv.toString(
200 xFormat.getPropertyValue("CurrencySymbol"));
202 // change the numberformat only on cellranges with a
203 // currency numberformat
204 if( ( (fType & com.sun.star.util.NumberFormat.CURRENCY) > 0) &&
205 ( sCurrencySymbol.compareTo( sOldSymbol ) == 0 ) ) {
206 boolean bThousandSep = aAnyConv.toBoolean(
207 xFormat.getPropertyValue("ThousandsSeparator"));
208 boolean bNegativeRed = aAnyConv.toBoolean(
209 xFormat.getPropertyValue("NegativeRed"));
210 short fDecimals = aAnyConv.toShort(
211 xFormat.getPropertyValue("Decimals"));
212 short fLeadingZeros = aAnyConv.toShort(
213 xFormat.getPropertyValue("LeadingZeros"));
214 Locale oLocale = (Locale) aAnyConv.toObject(
215 new com.sun.star.uno.Type(Locale.class),
216 xFormat.getPropertyValue("Locale"));
218 // create a new numberformat string
219 String sNew = xNumberFormats.generateFormat( iSimpleKey,
220 oLocale, bThousandSep, bNegativeRed,
221 fDecimals, fLeadingZeros );
223 // get the NumberKey from the numberformat
224 int iNewNumberFormat = NumberFormat( xNumberFormats,
225 sNew, oLocale );
227 // set the new numberformat to the cellrange DM->EUR
228 xCellProp.setPropertyValue( "NumberFormat",
229 new Integer( iNewNumberFormat ) );
231 // interate over all cells from the cellrange with an
232 // content and use the DM/EUR factor
233 XCellRangesQuery xCellRangesQuery = (XCellRangesQuery)
234 UnoRuntime.queryInterface(
235 XCellRangesQuery.class, xCellRange );
237 XSheetCellRanges xSheetCellRanges =
238 xCellRangesQuery.queryContentCells(
239 (short) com.sun.star.sheet.CellFlags.VALUE );
241 if( xSheetCellRanges.getCount() > 0 ) {
242 XEnumerationAccess xCellEnumerationAccess =
243 xSheetCellRanges.getCells();
244 XEnumeration xCellEnumeration =
245 xCellEnumerationAccess.createEnumeration();
247 while( xCellEnumeration.hasMoreElements() ) {
248 XCell xCell = (XCell) UnoRuntime.queryInterface(
249 XCell.class, xCellEnumeration.nextElement());
250 xCell.setValue( (double) xCell.getValue() / fFactor );
256 catch( Exception e) {
257 e.printStackTrace(System.err);
262 public static int NumberFormat( XNumberFormats xNumberFormat, String sFormat,
263 com.sun.star.lang.Locale xLanguage ) {
264 int nRetKey = 0;
266 try {
267 // exists the numberformat
268 nRetKey = xNumberFormat.queryKey( sFormat, xLanguage, true );
270 // if not, create a new one
271 if( nRetKey == -1 ) {
272 nRetKey = xNumberFormat.addNew( sFormat, xLanguage );
273 if( nRetKey == -1 )
274 nRetKey = 0;
277 catch( Exception e) {
278 e.printStackTrace(System.err);
281 return( nRetKey );
285 public static void createExampleData( XSpreadsheet xSheet,
286 XNumberFormats xNumberFormat ) {
288 // enter in a cellrange numbers and change the numberformat to DM
289 XCell xCell = null;
290 XCellRange xCellRange = null;
292 try {
293 Locale xLanguage = new Locale();
294 xLanguage.Country = "de"; // Germany -> DM
295 xLanguage.Language = "de"; // German
297 // Numberformat string from DM
298 String sSimple = "0 [$DM]";
300 // get the numberformat key
301 int iNumberFormatKey = NumberFormat(xNumberFormat, sSimple, xLanguage);
303 for( int iCounter=1; iCounter < 10; iCounter++ ) {
304 // get one cell and insert a number
305 xCell = xSheet.getCellByPosition( 2, 1 + iCounter );
306 xCell.setValue( (double) iCounter * 2 );
307 xCellRange = xSheet.getCellRangeByPosition( 2, 1 + iCounter,
308 2, 1 + iCounter );
310 // get the ProperySet from the cell, to change the numberformat
311 XPropertySet xCellProp = (XPropertySet)UnoRuntime.queryInterface(
312 XPropertySet.class, xCellRange );
313 xCellProp.setPropertyValue( "NumberFormat",
314 new Integer(iNumberFormatKey) );
317 catch( Exception e) {
318 e.printStackTrace(System.err);
322 public static XDesktop getDesktop() {
323 XDesktop xDesktop = null;
324 XMultiComponentFactory xMCF = null;
326 try {
327 XComponentContext xContext = null;
329 // get the remote office component context
330 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
332 // get the remote office service manager
333 xMCF = xContext.getServiceManager();
334 if( xMCF != null ) {
335 System.out.println("Connected to a running office ...");
337 Object oDesktop = xMCF.createInstanceWithContext(
338 "com.sun.star.frame.Desktop", xContext);
339 xDesktop = (XDesktop) UnoRuntime.queryInterface(
340 XDesktop.class, oDesktop);
342 else
343 System.out.println( "Can't create a desktop. No connection, no remote servicemanager available!" );
345 catch( Exception e) {
346 e.printStackTrace(System.err);
347 System.exit(1);
351 return xDesktop;
355 public static XSpreadsheetDocument createSheetdocument( XDesktop xDesktop ) {
356 XSpreadsheetDocument aSheetDocument = null;
358 try {
359 XComponent xComponent = null;
360 xComponent = CreateNewDocument( xDesktop, "scalc" );
362 aSheetDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(
363 XSpreadsheetDocument.class, xComponent);
365 catch( Exception e) {
366 e.printStackTrace(System.err);
369 return aSheetDocument;
372 protected static XComponent CreateNewDocument( XDesktop xDesktop,
373 String sDocumentType ) {
374 String sURL = "private:factory/" + sDocumentType;
376 XComponent xComponent = null;
377 XComponentLoader xComponentLoader = null;
378 PropertyValue xValues[] = new PropertyValue[1];
379 PropertyValue xEmptyArgs[] = new PropertyValue[0];
381 try {
382 xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(
383 XComponentLoader.class, xDesktop );
385 xComponent = xComponentLoader.loadComponentFromURL(
386 sURL, "_blank", 0, xEmptyArgs);
388 catch( Exception e) {
389 e.printStackTrace(System.err);
392 return xComponent ;