cid#1607171 Data race condition
[LibreOffice.git] / odk / examples / java / Spreadsheet / EuroAdaption.java
blobc6906f45ba134b47ef9113b343efe3cb169779a3
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
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 *************************************************************************/
37 // comment: Step 1: get the Desktop object from the office
38 // Step 2: open an empty Calc document
39 // Step 3: enter an example text, set the numberformat to DM
40 // Step 4: change the numberformat to EUR (Euro)
41 // Step 5: use the DM/EUR factor on each cell with a content
44 import com.sun.star.beans.PropertyValue;
45 import com.sun.star.beans.XPropertySet;
47 import com.sun.star.container.XEnumeration;
48 import com.sun.star.container.XIndexAccess;
49 import com.sun.star.container.XEnumerationAccess;
51 import com.sun.star.document.XActionLockable;
53 import com.sun.star.frame.XDesktop;
54 import com.sun.star.frame.XComponentLoader;
56 import com.sun.star.lang.Locale;
57 import com.sun.star.lang.XComponent;
58 import com.sun.star.lang.XMultiComponentFactory;
60 import com.sun.star.table.XCell;
61 import com.sun.star.table.XCellRange;
63 import com.sun.star.sheet.XSpreadsheet;
64 import com.sun.star.sheet.XSpreadsheets;
65 import com.sun.star.sheet.XSheetCellRanges;
66 import com.sun.star.sheet.XCellRangesQuery;
67 import com.sun.star.sheet.XCellFormatRangesSupplier;
68 import com.sun.star.sheet.XSpreadsheetDocument;
70 import com.sun.star.uno.UnoRuntime;
71 import com.sun.star.uno.AnyConverter;
72 import com.sun.star.uno.XComponentContext;
74 import com.sun.star.util.XNumberFormats;
75 import com.sun.star.util.XNumberFormatsSupplier;
78 public class EuroAdaption {
80 public static void main(String args[]) {
81 // You need the desktop to create a document
82 // The getDesktop method does the UNO bootstrapping, gets the
83 // remote service manager and the desktop object.
84 com.sun.star.frame.XDesktop xDesktop = null;
85 xDesktop = getDesktop();
87 // create a sheet document
88 XSpreadsheetDocument xSheetdocument = null;
89 xSheetdocument = createSheetdocument( xDesktop );
90 System.out.println( "Create a new Spreadsheet" );
92 // get the collection of all sheets from the document
93 XSpreadsheets xSheets = null;
94 xSheets = xSheetdocument.getSheets();
96 // the Action Interface provides methods to hide actions,
97 // like inserting data, on a sheet, that increase the performance
98 XActionLockable xActionInterface = null;
99 xActionInterface = UnoRuntime.queryInterface(
100 XActionLockable.class, xSheetdocument );
102 // lock all actions
103 xActionInterface.addActionLock();
105 com.sun.star.sheet.XSpreadsheet xSheet = null;
106 try {
107 // get via the index access the first sheet
108 XIndexAccess xElements = UnoRuntime.queryInterface(
109 XIndexAccess.class, xSheets );
111 // specify the first sheet from the spreadsheet
112 xSheet = UnoRuntime.queryInterface(
113 XSpreadsheet.class, xElements.getByIndex( 0 ));
115 catch( Exception e) {
116 e.printStackTrace(System.err);
119 // get the interface to apply and create new numberformats
120 XNumberFormatsSupplier xNumberFormatSupplier = null;
121 xNumberFormatSupplier = UnoRuntime.queryInterface(
122 XNumberFormatsSupplier.class, xSheetdocument );
123 XNumberFormats xNumberFormats = null;
124 xNumberFormats = xNumberFormatSupplier.getNumberFormats();
126 // insert some example data in a sheet
127 createExampleData( xSheet, xNumberFormats );
128 System.out.println( "Insert example data and use the number format with the currency 'DM'" );
130 // Change the currency from the cells from DM to Euro
131 Convert( xSheet, xNumberFormats, "DM", "EUR", 1.95583f );
132 System.out.println( "Change the number format to EUR and divide the values with the factor 1.95583" );
134 // remove all locks, the user see all changes
135 xActionInterface.removeActionLock();
137 System.out.println("done");
138 System.exit(0);
142 public static void Convert( XSpreadsheet xSheet, XNumberFormats xNumberFormats,
143 String sOldSymbol, String sNewSymbol,
144 float fFactor ) {
145 try {
146 Locale xLanguage = new Locale();
147 xLanguage.Country = "de"; // Germany -> DM
148 xLanguage.Language = "de"; // German
150 // Numberformat string with sNewSymbol
151 String sSimple = "0 [$" + sNewSymbol + "]";
152 // create a number format key with the sNewSymbol
153 int iSimpleKey = NumberFormat( xNumberFormats, sSimple, xLanguage );
155 // you have to use the FormatSupplier interface to get the
156 // CellFormat enumeration
157 XCellFormatRangesSupplier xCellFormatSupplier =
158 UnoRuntime.queryInterface(
159 XCellFormatRangesSupplier.class, xSheet );
161 // getCellFormatRanges() has the interfaces for the enumeration
162 XEnumerationAccess xEnumerationAccess =
163 UnoRuntime.queryInterface(
164 XEnumerationAccess.class,
165 xCellFormatSupplier.getCellFormatRanges() );
167 XEnumeration xRanges = xEnumerationAccess.createEnumeration();
169 while( xRanges.hasMoreElements() ) {
170 // the enumeration returns a cellrange
171 XCellRange xCellRange = UnoRuntime.queryInterface(
172 XCellRange.class, xRanges.nextElement());
174 // the PropertySet the get and set the properties from the cellrange
175 XPropertySet xCellProp = UnoRuntime.queryInterface(
176 XPropertySet.class, xCellRange );
178 // getPropertyValue returns an Object, you have to cast it to
179 // type that you need
180 Object oNumberObject = xCellProp.getPropertyValue( "NumberFormat" );
181 int iNumberFormat = AnyConverter.toInt(oNumberObject);
183 // get the properties from the cellrange numberformat
184 XPropertySet xFormat = xNumberFormats.getByKey(iNumberFormat );
186 short fType = AnyConverter.toShort(xFormat.getPropertyValue("Type"));
187 String sCurrencySymbol = AnyConverter.toString(
188 xFormat.getPropertyValue("CurrencySymbol"));
190 // change the numberformat only on cellranges with a
191 // currency numberformat
192 if( ( (fType & com.sun.star.util.NumberFormat.CURRENCY) > 0) &&
193 ( sCurrencySymbol.equals( sOldSymbol ) ) ) {
194 boolean bThousandSep = AnyConverter.toBoolean(
195 xFormat.getPropertyValue("ThousandsSeparator"));
196 boolean bNegativeRed = AnyConverter.toBoolean(
197 xFormat.getPropertyValue("NegativeRed"));
198 short fDecimals = AnyConverter.toShort(
199 xFormat.getPropertyValue("Decimals"));
200 short fLeadingZeros = AnyConverter.toShort(
201 xFormat.getPropertyValue("LeadingZeros"));
202 Locale oLocale = (Locale) AnyConverter.toObject(
203 new com.sun.star.uno.Type(Locale.class),
204 xFormat.getPropertyValue("Locale"));
206 // create a new numberformat string
207 String sNew = xNumberFormats.generateFormat( iSimpleKey,
208 oLocale, bThousandSep, bNegativeRed,
209 fDecimals, fLeadingZeros );
211 // get the NumberKey from the numberformat
212 int iNewNumberFormat = NumberFormat( xNumberFormats,
213 sNew, oLocale );
215 // set the new numberformat to the cellrange DM->EUR
216 xCellProp.setPropertyValue( "NumberFormat",
217 Integer.valueOf( iNewNumberFormat ) );
219 // iterate over all cells from the cellrange with an
220 // content and use the DM/EUR factor
221 XCellRangesQuery xCellRangesQuery = UnoRuntime.queryInterface(
222 XCellRangesQuery.class, xCellRange );
224 XSheetCellRanges xSheetCellRanges =
225 xCellRangesQuery.queryContentCells(
226 (short) com.sun.star.sheet.CellFlags.VALUE );
228 if( xSheetCellRanges.getCount() > 0 ) {
229 XEnumerationAccess xCellEnumerationAccess =
230 xSheetCellRanges.getCells();
231 XEnumeration xCellEnumeration =
232 xCellEnumerationAccess.createEnumeration();
234 while( xCellEnumeration.hasMoreElements() ) {
235 XCell xCell = UnoRuntime.queryInterface(
236 XCell.class, xCellEnumeration.nextElement());
237 xCell.setValue( xCell.getValue() / fFactor );
243 catch( Exception e) {
244 e.printStackTrace(System.err);
249 public static int NumberFormat( XNumberFormats xNumberFormat, String sFormat,
250 com.sun.star.lang.Locale xLanguage ) {
251 int nRetKey = 0;
253 try {
254 // exists the numberformat
255 nRetKey = xNumberFormat.queryKey( sFormat, xLanguage, true );
257 // if not, create a new one
258 if( nRetKey == -1 ) {
259 nRetKey = xNumberFormat.addNew( sFormat, xLanguage );
260 if( nRetKey == -1 )
261 nRetKey = 0;
264 catch( Exception e) {
265 e.printStackTrace(System.err);
268 return nRetKey;
272 public static void createExampleData( XSpreadsheet xSheet,
273 XNumberFormats xNumberFormat ) {
275 // enter in a cellrange numbers and change the numberformat to DM
276 XCell xCell = null;
277 XCellRange xCellRange = null;
279 try {
280 Locale xLanguage = new Locale();
281 xLanguage.Country = "de"; // Germany -> DM
282 xLanguage.Language = "de"; // German
284 // Numberformat string from DM
285 String sSimple = "0 [$DM]";
287 // get the numberformat key
288 int iNumberFormatKey = NumberFormat(xNumberFormat, sSimple, xLanguage);
290 for( int iCounter=1; iCounter < 10; iCounter++ ) {
291 // get one cell and insert a number
292 xCell = xSheet.getCellByPosition( 2, 1 + iCounter );
293 xCell.setValue( (double) iCounter * 2 );
294 xCellRange = xSheet.getCellRangeByPosition( 2, 1 + iCounter,
295 2, 1 + iCounter );
297 // get the PropertySet from the cell, to change the numberformat
298 XPropertySet xCellProp = UnoRuntime.queryInterface(
299 XPropertySet.class, xCellRange );
300 xCellProp.setPropertyValue( "NumberFormat",
301 Integer.valueOf(iNumberFormatKey) );
304 catch( Exception e) {
305 e.printStackTrace(System.err);
309 public static XDesktop getDesktop() {
310 XDesktop xDesktop = null;
311 XMultiComponentFactory xMCF = null;
313 try {
314 XComponentContext xContext = null;
316 // get the remote office component context
317 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
319 // get the remote office service manager
320 xMCF = xContext.getServiceManager();
321 if( xMCF != null ) {
322 System.out.println("Connected to a running office ...");
324 Object oDesktop = xMCF.createInstanceWithContext(
325 "com.sun.star.frame.Desktop", xContext);
326 xDesktop = UnoRuntime.queryInterface(
327 XDesktop.class, oDesktop);
329 else
330 System.out.println( "Can't create a desktop. No connection, no remote servicemanager available!" );
332 catch( Exception e) {
333 e.printStackTrace(System.err);
334 System.exit(1);
338 return xDesktop;
342 public static XSpreadsheetDocument createSheetdocument( XDesktop xDesktop ) {
343 XSpreadsheetDocument aSheetDocument = null;
345 try {
346 XComponent xComponent = null;
347 xComponent = CreateNewDocument( xDesktop, "scalc" );
349 aSheetDocument = UnoRuntime.queryInterface(
350 XSpreadsheetDocument.class, xComponent);
352 catch( Exception e) {
353 e.printStackTrace(System.err);
356 return aSheetDocument;
359 protected static XComponent CreateNewDocument( XDesktop xDesktop,
360 String sDocumentType ) {
361 String sURL = "private:factory/" + sDocumentType;
363 XComponent xComponent = null;
364 XComponentLoader xComponentLoader = null;
365 PropertyValue xEmptyArgs[] = new PropertyValue[0];
367 try {
368 xComponentLoader = UnoRuntime.queryInterface(
369 XComponentLoader.class, xDesktop );
371 xComponent = xComponentLoader.loadComponentFromURL(
372 sURL, "_blank", 0, xEmptyArgs);
374 catch( Exception e) {
375 e.printStackTrace(System.err);
378 return xComponent ;
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */