Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Spreadsheet / EuroAdaption.java
blobef64b8e6c1d7d547b573b2232c69e1d250f5692d
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
36 // comment: Step 1: get the Desktop object from the office
37 // Step 2: open an empty Calc document
38 // Step 3: enter a example text, set the numberformat to DM
39 // Step 4: change the numberformat to EUR (Euro)
40 // Step 5: use the DM/EUR factor on each cell with a content
43 import com.sun.star.beans.PropertyValue;
44 import com.sun.star.beans.XPropertySet;
46 import com.sun.star.container.XEnumeration;
47 import com.sun.star.container.XIndexAccess;
48 import com.sun.star.container.XEnumerationAccess;
50 import com.sun.star.document.XActionLockable;
52 import com.sun.star.frame.XDesktop;
53 import com.sun.star.frame.XComponentLoader;
55 import com.sun.star.lang.Locale;
56 import com.sun.star.lang.XComponent;
57 import com.sun.star.lang.XMultiComponentFactory;
59 import com.sun.star.table.XCell;
60 import com.sun.star.table.XCellRange;
62 import com.sun.star.sheet.XSpreadsheet;
63 import com.sun.star.sheet.XSpreadsheets;
64 import com.sun.star.sheet.XSheetCellRanges;
65 import com.sun.star.sheet.XCellRangesQuery;
66 import com.sun.star.sheet.XCellFormatRangesSupplier;
67 import com.sun.star.sheet.XSpreadsheetDocument;
69 import com.sun.star.uno.UnoRuntime;
70 import com.sun.star.uno.AnyConverter;
71 import com.sun.star.uno.XComponentContext;
73 import com.sun.star.util.XNumberFormats;
74 import com.sun.star.util.XNumberFormatsSupplier;
77 public class EuroAdaption {
79 public static void main(String args[]) {
80 // You need the desktop to create a document
81 // The getDesktop method does the UNO bootstrapping, gets the
82 // remote servie manager and the desktop object.
83 com.sun.star.frame.XDesktop xDesktop = null;
84 xDesktop = getDesktop();
86 // create a sheet document
87 XSpreadsheetDocument xSheetdocument = null;
88 xSheetdocument = createSheetdocument( xDesktop );
89 System.out.println( "Create a new Spreadsheet" );
91 // get the collection of all sheets from the document
92 XSpreadsheets xSheets = null;
93 xSheets = xSheetdocument.getSheets();
95 // the Action Interface provides methods to hide actions,
96 // like inserting data, on a sheet, that increase the performance
97 XActionLockable xActionInterface = null;
98 xActionInterface = UnoRuntime.queryInterface(
99 XActionLockable.class, xSheetdocument );
101 // lock all actions
102 xActionInterface.addActionLock();
104 com.sun.star.sheet.XSpreadsheet xSheet = null;
105 try {
106 // get via the index access the first sheet
107 XIndexAccess xElements = UnoRuntime.queryInterface(
108 XIndexAccess.class, xSheets );
110 // specify the first sheet from the spreadsheet
111 xSheet = UnoRuntime.queryInterface(
112 XSpreadsheet.class, xElements.getByIndex( 0 ));
114 catch( Exception e) {
115 e.printStackTrace(System.err);
118 // get the interface to apply and create new numberformats
119 XNumberFormatsSupplier xNumberFormatSupplier = null;
120 xNumberFormatSupplier = UnoRuntime.queryInterface(
121 XNumberFormatsSupplier.class, xSheetdocument );
122 XNumberFormats xNumberFormats = null;
123 xNumberFormats = xNumberFormatSupplier.getNumberFormats();
125 // insert some example data in a sheet
126 createExampleData( xSheet, xNumberFormats );
127 System.out.println( "Insert example data and use the number format with the currency 'DM'" );
129 // Change the currency from the cells from DM to Euro
130 Convert( xSheet, xNumberFormats, "DM", "EUR", 1.95583f );
131 System.out.println( "Change the number format to EUR and divide the values with the factor 1.95583" );
133 // remove all locks, the user see all changes
134 xActionInterface.removeActionLock();
136 System.out.println("done");
137 System.exit(0);
141 public static void Convert( XSpreadsheet xSheet, XNumberFormats xNumberFormats,
142 String sOldSymbol, String sNewSymbol,
143 float fFactor ) {
144 try {
145 Locale xLanguage = new Locale();
146 xLanguage.Country = "de"; // Germany -> DM
147 xLanguage.Language = "de"; // German
149 // Numberformat string with sNewSymbol
150 String sSimple = "0 [$" + sNewSymbol + "]";
151 // create a number format key with the sNewSymbol
152 int iSimpleKey = NumberFormat( xNumberFormats, sSimple, xLanguage );
154 // you have to use the FormatSupplier interface to get the
155 // CellFormat enumeration
156 XCellFormatRangesSupplier xCellFormatSupplier =
157 UnoRuntime.queryInterface(
158 XCellFormatRangesSupplier.class, xSheet );
160 // getCellFormatRanges() has the interfaces for the enumeration
161 XEnumerationAccess xEnumerationAccess =
162 UnoRuntime.queryInterface(
163 XEnumerationAccess.class,
164 xCellFormatSupplier.getCellFormatRanges() );
166 XEnumeration xRanges = xEnumerationAccess.createEnumeration();
168 while( xRanges.hasMoreElements() ) {
169 // the enumeration returns a cellrange
170 XCellRange xCellRange = UnoRuntime.queryInterface(
171 XCellRange.class, xRanges.nextElement());
173 // the PropertySet the get and set the properties from the cellrange
174 XPropertySet xCellProp = UnoRuntime.queryInterface(
175 XPropertySet.class, xCellRange );
177 // getPropertyValue returns an Object, you have to cast it to
178 // type that you need
179 Object oNumberObject = xCellProp.getPropertyValue( "NumberFormat" );
180 int iNumberFormat = AnyConverter.toInt(oNumberObject);
182 // get the properties from the cellrange numberformat
183 XPropertySet xFormat = xNumberFormats.getByKey(iNumberFormat );
185 short fType = AnyConverter.toShort(xFormat.getPropertyValue("Type"));
186 String sCurrencySymbol = AnyConverter.toString(
187 xFormat.getPropertyValue("CurrencySymbol"));
189 // change the numberformat only on cellranges with a
190 // currency numberformat
191 if( ( (fType & com.sun.star.util.NumberFormat.CURRENCY) > 0) &&
192 ( sCurrencySymbol.equals( sOldSymbol ) ) ) {
193 boolean bThousandSep = AnyConverter.toBoolean(
194 xFormat.getPropertyValue("ThousandsSeparator"));
195 boolean bNegativeRed = AnyConverter.toBoolean(
196 xFormat.getPropertyValue("NegativeRed"));
197 short fDecimals = AnyConverter.toShort(
198 xFormat.getPropertyValue("Decimals"));
199 short fLeadingZeros = AnyConverter.toShort(
200 xFormat.getPropertyValue("LeadingZeros"));
201 Locale oLocale = (Locale) AnyConverter.toObject(
202 new com.sun.star.uno.Type(Locale.class),
203 xFormat.getPropertyValue("Locale"));
205 // create a new numberformat string
206 String sNew = xNumberFormats.generateFormat( iSimpleKey,
207 oLocale, bThousandSep, bNegativeRed,
208 fDecimals, fLeadingZeros );
210 // get the NumberKey from the numberformat
211 int iNewNumberFormat = NumberFormat( xNumberFormats,
212 sNew, oLocale );
214 // set the new numberformat to the cellrange DM->EUR
215 xCellProp.setPropertyValue( "NumberFormat",
216 Integer.valueOf( iNewNumberFormat ) );
218 // interate over all cells from the cellrange with an
219 // content and use the DM/EUR factor
220 XCellRangesQuery xCellRangesQuery = UnoRuntime.queryInterface(
221 XCellRangesQuery.class, xCellRange );
223 XSheetCellRanges xSheetCellRanges =
224 xCellRangesQuery.queryContentCells(
225 (short) com.sun.star.sheet.CellFlags.VALUE );
227 if( xSheetCellRanges.getCount() > 0 ) {
228 XEnumerationAccess xCellEnumerationAccess =
229 xSheetCellRanges.getCells();
230 XEnumeration xCellEnumeration =
231 xCellEnumerationAccess.createEnumeration();
233 while( xCellEnumeration.hasMoreElements() ) {
234 XCell xCell = UnoRuntime.queryInterface(
235 XCell.class, xCellEnumeration.nextElement());
236 xCell.setValue( xCell.getValue() / fFactor );
242 catch( Exception e) {
243 e.printStackTrace(System.err);
248 public static int NumberFormat( XNumberFormats xNumberFormat, String sFormat,
249 com.sun.star.lang.Locale xLanguage ) {
250 int nRetKey = 0;
252 try {
253 // exists the numberformat
254 nRetKey = xNumberFormat.queryKey( sFormat, xLanguage, true );
256 // if not, create a new one
257 if( nRetKey == -1 ) {
258 nRetKey = xNumberFormat.addNew( sFormat, xLanguage );
259 if( nRetKey == -1 )
260 nRetKey = 0;
263 catch( Exception e) {
264 e.printStackTrace(System.err);
267 return nRetKey;
271 public static void createExampleData( XSpreadsheet xSheet,
272 XNumberFormats xNumberFormat ) {
274 // enter in a cellrange numbers and change the numberformat to DM
275 XCell xCell = null;
276 XCellRange xCellRange = null;
278 try {
279 Locale xLanguage = new Locale();
280 xLanguage.Country = "de"; // Germany -> DM
281 xLanguage.Language = "de"; // German
283 // Numberformat string from DM
284 String sSimple = "0 [$DM]";
286 // get the numberformat key
287 int iNumberFormatKey = NumberFormat(xNumberFormat, sSimple, xLanguage);
289 for( int iCounter=1; iCounter < 10; iCounter++ ) {
290 // get one cell and insert a number
291 xCell = xSheet.getCellByPosition( 2, 1 + iCounter );
292 xCell.setValue( (double) iCounter * 2 );
293 xCellRange = xSheet.getCellRangeByPosition( 2, 1 + iCounter,
294 2, 1 + iCounter );
296 // get the ProperySet from the cell, to change the numberformat
297 XPropertySet xCellProp = UnoRuntime.queryInterface(
298 XPropertySet.class, xCellRange );
299 xCellProp.setPropertyValue( "NumberFormat",
300 Integer.valueOf(iNumberFormatKey) );
303 catch( Exception e) {
304 e.printStackTrace(System.err);
308 public static XDesktop getDesktop() {
309 XDesktop xDesktop = null;
310 XMultiComponentFactory xMCF = null;
312 try {
313 XComponentContext xContext = null;
315 // get the remote office component context
316 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
318 // get the remote office service manager
319 xMCF = xContext.getServiceManager();
320 if( xMCF != null ) {
321 System.out.println("Connected to a running office ...");
323 Object oDesktop = xMCF.createInstanceWithContext(
324 "com.sun.star.frame.Desktop", xContext);
325 xDesktop = UnoRuntime.queryInterface(
326 XDesktop.class, oDesktop);
328 else
329 System.out.println( "Can't create a desktop. No connection, no remote servicemanager available!" );
331 catch( Exception e) {
332 e.printStackTrace(System.err);
333 System.exit(1);
337 return xDesktop;
341 public static XSpreadsheetDocument createSheetdocument( XDesktop xDesktop ) {
342 XSpreadsheetDocument aSheetDocument = null;
344 try {
345 XComponent xComponent = null;
346 xComponent = CreateNewDocument( xDesktop, "scalc" );
348 aSheetDocument = UnoRuntime.queryInterface(
349 XSpreadsheetDocument.class, xComponent);
351 catch( Exception e) {
352 e.printStackTrace(System.err);
355 return aSheetDocument;
358 protected static XComponent CreateNewDocument( XDesktop xDesktop,
359 String sDocumentType ) {
360 String sURL = "private:factory/" + sDocumentType;
362 XComponent xComponent = null;
363 XComponentLoader xComponentLoader = null;
364 PropertyValue xEmptyArgs[] = new PropertyValue[0];
366 try {
367 xComponentLoader = UnoRuntime.queryInterface(
368 XComponentLoader.class, xDesktop );
370 xComponent = xComponentLoader.loadComponentFromURL(
371 sURL, "_blank", 0, xEmptyArgs);
373 catch( Exception e) {
374 e.printStackTrace(System.err);
377 return xComponent ;