merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / Number_Formats.java
blobab8a04ba0c721e20722a799f8c7c49a3ae995de9
1 /*************************************************************************
3 * $RCSfile: Number_Formats.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:35:39 $
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 // __________ Imports __________
43 import com.sun.star.beans.PropertyValue;
44 import com.sun.star.container.XIndexAccess;
45 import com.sun.star.frame.XComponentLoader;
46 import com.sun.star.lang.XMultiComponentFactory;
47 import com.sun.star.sheet.XSpreadsheet;
48 import com.sun.star.sheet.XSpreadsheets;
49 import com.sun.star.sheet.XSpreadsheetDocument;
50 import com.sun.star.table.XCell;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XComponentContext;
55 // __________ Implementation __________
57 /** Create a spreadsheet document and provide access to a sheet framework that
58 is then used to modify some number formats.
59 @author Eike Rathke
61 public class Number_Formats
63 // __________ public members and methods __________
66 // ____________________
68 public static void main( String args[] )
70 try
72 Number_Formats aSample = new Number_Formats( args );
73 aSample.doFunction();
75 catch( Exception ex )
77 System.err.println( "Sample caught exception! " + ex );
78 ex.printStackTrace();
79 System.exit(1);
82 System.out.println( "Sample done." );
83 System.exit(0);
86 // ____________________
88 public void doFunction() throws RuntimeException, Exception
90 // Assume:
91 // com.sun.star.sheet.XSpreadsheetDocument maSpreadsheetDoc;
92 // com.sun.star.sheet.XSpreadsheet maSheet;
94 // Query the number formats supplier of the spreadsheet document
95 com.sun.star.util.XNumberFormatsSupplier xNumberFormatsSupplier =
96 (com.sun.star.util.XNumberFormatsSupplier)
97 UnoRuntime.queryInterface(
98 com.sun.star.util.XNumberFormatsSupplier.class, maSpreadsheetDoc );
100 // Get the number formats from the supplier
101 com.sun.star.util.XNumberFormats xNumberFormats =
102 xNumberFormatsSupplier.getNumberFormats();
104 // Query the XNumberFormatTypes interface
105 com.sun.star.util.XNumberFormatTypes xNumberFormatTypes =
106 (com.sun.star.util.XNumberFormatTypes)
107 UnoRuntime.queryInterface(
108 com.sun.star.util.XNumberFormatTypes.class, xNumberFormats );
110 // Get the number format index key of the default currency format,
111 // note the empty locale for default locale
112 com.sun.star.lang.Locale aLocale = new com.sun.star.lang.Locale();
113 int nCurrencyKey = xNumberFormatTypes.getStandardFormat(
114 com.sun.star.util.NumberFormat.CURRENCY, aLocale );
116 // Get cell range B3:B11
117 com.sun.star.table.XCellRange xCellRange =
118 maSheet.getCellRangeByPosition( 1, 2, 1, 10 );
120 // Query the property set of the cell range
121 com.sun.star.beans.XPropertySet xCellProp =
122 (com.sun.star.beans.XPropertySet)
123 UnoRuntime.queryInterface(
124 com.sun.star.beans.XPropertySet.class, xCellRange );
126 // Set number format to default currency
127 xCellProp.setPropertyValue( "NumberFormat", new Integer(nCurrencyKey) );
129 // Get cell B3
130 com.sun.star.table.XCell xCell = maSheet.getCellByPosition( 1, 2 );
132 // Query the property set of the cell
133 xCellProp = (com.sun.star.beans.XPropertySet)
134 UnoRuntime.queryInterface(
135 com.sun.star.beans.XPropertySet.class, xCell );
137 // Get the number format index key of the cell's properties
138 int nIndexKey = ((Integer) xCellProp.getPropertyValue( "NumberFormat" )).intValue();
139 if ( nIndexKey != nCurrencyKey )
140 System.out.println( "Number format doesn't match!" );
142 // Get the properties of the number format
143 com.sun.star.beans.XPropertySet xProp = xNumberFormats.getByKey( nIndexKey );
145 // Get the format code string of the number format's properties
146 String aFormatCode = (String) xProp.getPropertyValue( "FormatString" );
147 System.out.println( "FormatString: `" + aFormatCode + "'" );
149 // Create an arbitrary format code
150 aFormatCode = "\"wonderful \"" + aFormatCode;
152 // Test if it's already present
153 nIndexKey = xNumberFormats.queryKey( aFormatCode, aLocale, false );
155 // If not, add to number formats collection
156 if ( nIndexKey == -1 )
160 nIndexKey = xNumberFormats.addNew( aFormatCode, aLocale );
162 catch( com.sun.star.util.MalformedNumberFormatException ex )
164 System.err.println( "Bad number format code: " + ex );
165 ex.printStackTrace();
166 nIndexKey = -1;
170 // Set the new format at the cell
171 if ( nIndexKey != -1 )
172 xCellProp.setPropertyValue( "NumberFormat", new Integer(nIndexKey) );
175 // Set column containing the example values to optimal width to show
176 // the new format of cell B3
177 com.sun.star.table.XColumnRowRange xColRowRange =
178 (com.sun.star.table.XColumnRowRange)
179 UnoRuntime.queryInterface(com.sun.star.table.XColumnRowRange.class,
180 maSheet);
182 com.sun.star.container.XIndexAccess xIndexAccess =
183 (com.sun.star.container.XIndexAccess)
184 UnoRuntime.queryInterface(com.sun.star.container.XIndexAccess.class,
185 xColRowRange.getColumns());
187 com.sun.star.beans.XPropertySet xColPropSet =
188 (com.sun.star.beans.XPropertySet)
189 UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class,
190 xIndexAccess.getByIndex(1));
192 xColPropSet.setPropertyValue( "OptimalWidth", new Boolean(true) );
195 // ____________________
197 public Number_Formats( String[] args ) throws java.lang.Exception
199 // get the remote office context. If necessary a new office
200 // process is started
201 maOfficeContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
202 System.out.println("Connected to a running office ...");
203 maServiceManager = maOfficeContext.getServiceManager();
205 // create a new spreadsheet document
206 XComponentLoader aLoader = (XComponentLoader) UnoRuntime.queryInterface(
207 XComponentLoader.class, maServiceManager.createInstanceWithContext(
208 "com.sun.star.frame.Desktop", maOfficeContext) );
210 maSpreadsheetDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(
211 XSpreadsheetDocument.class,
212 aLoader.loadComponentFromURL( "private:factory/scalc",
213 "_blank",
215 new PropertyValue[ 0 ] ) );
217 if ( !initSpreadsheet() )
218 System.exit( 0 );
222 // __________ private members and methods __________
223 private final String msDataSheetName = "Data";
225 private XComponentContext maOfficeContext;
226 private XMultiComponentFactory maServiceManager;
227 private XSpreadsheetDocument maSpreadsheetDoc;
228 private XSpreadsheet maSheet; // the first sheet
231 // ____________________
233 /** init the first sheet
235 private boolean initSpreadsheet()
237 boolean bOk = true;
238 XSpreadsheets aSheets = maSpreadsheetDoc.getSheets();
241 XIndexAccess aSheetsIA = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, aSheets );
242 maSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, aSheetsIA.getByIndex( 0 ));
244 // enter some values in B3:B11
245 for( int iCounter=1; iCounter < 10; iCounter++ )
247 XCell aCell = maSheet.getCellByPosition( 1, 1 + iCounter );
248 aCell.setValue( (double) iCounter );
251 catch( Exception ex )
253 System.err.println( "Couldn't initialize Spreadsheet Document: " + ex );
254 ex.printStackTrace();
255 bOk = false;
257 return bOk;