xref: /AOO41X/main/odk/examples/DevelopersGuide/OfficeDev/Number_Formats.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 // __________ Imports __________
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
38*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
39*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
40*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
41*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
42*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
43*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
44*cdf0e10cSrcweir import com.sun.star.table.XCell;
45*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
46*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir // __________ Implementation __________
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir /** Create a spreadsheet document and provide access to a sheet framework that
52*cdf0e10cSrcweir     is then used to modify some number formats.
53*cdf0e10cSrcweir     @author Eike Rathke
54*cdf0e10cSrcweir  */
55*cdf0e10cSrcweir public class Number_Formats
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir     // __________ public members and methods __________
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     // ____________________
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     public static void main( String args[] )
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir         try
65*cdf0e10cSrcweir         {
66*cdf0e10cSrcweir             Number_Formats aSample = new Number_Formats( args );
67*cdf0e10cSrcweir             aSample.doFunction();
68*cdf0e10cSrcweir         }
69*cdf0e10cSrcweir         catch( Exception ex )
70*cdf0e10cSrcweir         {
71*cdf0e10cSrcweir             System.err.println( "Sample caught exception! " + ex );
72*cdf0e10cSrcweir             ex.printStackTrace();
73*cdf0e10cSrcweir             System.exit(1);
74*cdf0e10cSrcweir         }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir         System.out.println( "Sample done." );
77*cdf0e10cSrcweir         System.exit(0);
78*cdf0e10cSrcweir     }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     // ____________________
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     public void doFunction() throws RuntimeException, Exception
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir         // Assume:
85*cdf0e10cSrcweir         // com.sun.star.sheet.XSpreadsheetDocument maSpreadsheetDoc;
86*cdf0e10cSrcweir         // com.sun.star.sheet.XSpreadsheet maSheet;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         // Query the number formats supplier of the spreadsheet document
89*cdf0e10cSrcweir         com.sun.star.util.XNumberFormatsSupplier xNumberFormatsSupplier =
90*cdf0e10cSrcweir             (com.sun.star.util.XNumberFormatsSupplier)
91*cdf0e10cSrcweir             UnoRuntime.queryInterface(
92*cdf0e10cSrcweir             com.sun.star.util.XNumberFormatsSupplier.class, maSpreadsheetDoc );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         // Get the number formats from the supplier
95*cdf0e10cSrcweir         com.sun.star.util.XNumberFormats xNumberFormats =
96*cdf0e10cSrcweir             xNumberFormatsSupplier.getNumberFormats();
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir         // Query the XNumberFormatTypes interface
99*cdf0e10cSrcweir         com.sun.star.util.XNumberFormatTypes xNumberFormatTypes =
100*cdf0e10cSrcweir             (com.sun.star.util.XNumberFormatTypes)
101*cdf0e10cSrcweir             UnoRuntime.queryInterface(
102*cdf0e10cSrcweir             com.sun.star.util.XNumberFormatTypes.class, xNumberFormats );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         // Get the number format index key of the default currency format,
105*cdf0e10cSrcweir         // note the empty locale for default locale
106*cdf0e10cSrcweir         com.sun.star.lang.Locale aLocale = new com.sun.star.lang.Locale();
107*cdf0e10cSrcweir         int nCurrencyKey = xNumberFormatTypes.getStandardFormat(
108*cdf0e10cSrcweir             com.sun.star.util.NumberFormat.CURRENCY, aLocale );
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir         // Get cell range B3:B11
111*cdf0e10cSrcweir         com.sun.star.table.XCellRange xCellRange =
112*cdf0e10cSrcweir             maSheet.getCellRangeByPosition( 1, 2, 1, 10 );
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         // Query the property set of the cell range
115*cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xCellProp =
116*cdf0e10cSrcweir             (com.sun.star.beans.XPropertySet)
117*cdf0e10cSrcweir             UnoRuntime.queryInterface(
118*cdf0e10cSrcweir             com.sun.star.beans.XPropertySet.class, xCellRange );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         // Set number format to default currency
121*cdf0e10cSrcweir         xCellProp.setPropertyValue( "NumberFormat", new Integer(nCurrencyKey) );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         // Get cell B3
124*cdf0e10cSrcweir         com.sun.star.table.XCell xCell = maSheet.getCellByPosition( 1, 2 );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir         // Query the property set of the cell
127*cdf0e10cSrcweir         xCellProp = (com.sun.star.beans.XPropertySet)
128*cdf0e10cSrcweir             UnoRuntime.queryInterface(
129*cdf0e10cSrcweir             com.sun.star.beans.XPropertySet.class, xCell );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         // Get the number format index key of the cell's properties
132*cdf0e10cSrcweir         int nIndexKey = ((Integer) xCellProp.getPropertyValue( "NumberFormat" )).intValue();
133*cdf0e10cSrcweir         if ( nIndexKey != nCurrencyKey )
134*cdf0e10cSrcweir             System.out.println( "Number format doesn't match!" );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         // Get the properties of the number format
137*cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xProp = xNumberFormats.getByKey( nIndexKey );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         // Get the format code string of the number format's properties
140*cdf0e10cSrcweir         String aFormatCode = (String) xProp.getPropertyValue( "FormatString" );
141*cdf0e10cSrcweir         System.out.println( "FormatString: `" + aFormatCode + "'" );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         // Create an arbitrary format code
144*cdf0e10cSrcweir         aFormatCode = "\"wonderful \"" + aFormatCode;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         // Test if it's already present
147*cdf0e10cSrcweir         nIndexKey = xNumberFormats.queryKey( aFormatCode, aLocale, false );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         // If not, add to number formats collection
150*cdf0e10cSrcweir         if ( nIndexKey == -1 )
151*cdf0e10cSrcweir         {
152*cdf0e10cSrcweir             try
153*cdf0e10cSrcweir             {
154*cdf0e10cSrcweir                 nIndexKey = xNumberFormats.addNew( aFormatCode, aLocale );
155*cdf0e10cSrcweir             }
156*cdf0e10cSrcweir             catch( com.sun.star.util.MalformedNumberFormatException ex )
157*cdf0e10cSrcweir             {
158*cdf0e10cSrcweir                 System.err.println( "Bad number format code: " + ex );
159*cdf0e10cSrcweir                 ex.printStackTrace();
160*cdf0e10cSrcweir                 nIndexKey = -1;
161*cdf0e10cSrcweir             }
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir         // Set the new format at the cell
165*cdf0e10cSrcweir         if ( nIndexKey != -1 )
166*cdf0e10cSrcweir             xCellProp.setPropertyValue( "NumberFormat", new Integer(nIndexKey) );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         // Set column containing the example values to optimal width to show
170*cdf0e10cSrcweir         // the new format of cell B3
171*cdf0e10cSrcweir         com.sun.star.table.XColumnRowRange xColRowRange =
172*cdf0e10cSrcweir             (com.sun.star.table.XColumnRowRange)
173*cdf0e10cSrcweir             UnoRuntime.queryInterface(com.sun.star.table.XColumnRowRange.class,
174*cdf0e10cSrcweir                                       maSheet);
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         com.sun.star.container.XIndexAccess xIndexAccess =
177*cdf0e10cSrcweir             (com.sun.star.container.XIndexAccess)
178*cdf0e10cSrcweir             UnoRuntime.queryInterface(com.sun.star.container.XIndexAccess.class,
179*cdf0e10cSrcweir                                       xColRowRange.getColumns());
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         com.sun.star.beans.XPropertySet xColPropSet =
182*cdf0e10cSrcweir             (com.sun.star.beans.XPropertySet)
183*cdf0e10cSrcweir             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class,
184*cdf0e10cSrcweir                                       xIndexAccess.getByIndex(1));
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         xColPropSet.setPropertyValue( "OptimalWidth", new Boolean(true) );
187*cdf0e10cSrcweir     }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     // ____________________
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     public Number_Formats( String[] args ) throws java.lang.Exception
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir         // get the remote office context. If necessary a new office
194*cdf0e10cSrcweir         // process is started
195*cdf0e10cSrcweir         maOfficeContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
196*cdf0e10cSrcweir         System.out.println("Connected to a running office ...");
197*cdf0e10cSrcweir         maServiceManager = maOfficeContext.getServiceManager();
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         // create a new spreadsheet document
200*cdf0e10cSrcweir         XComponentLoader aLoader = (XComponentLoader) UnoRuntime.queryInterface(
201*cdf0e10cSrcweir             XComponentLoader.class, maServiceManager.createInstanceWithContext(
202*cdf0e10cSrcweir                 "com.sun.star.frame.Desktop", maOfficeContext) );
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir         maSpreadsheetDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(
205*cdf0e10cSrcweir             XSpreadsheetDocument.class,
206*cdf0e10cSrcweir             aLoader.loadComponentFromURL( "private:factory/scalc",
207*cdf0e10cSrcweir                                           "_blank",
208*cdf0e10cSrcweir                                           0,
209*cdf0e10cSrcweir                                           new PropertyValue[ 0 ] ) );
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir         if ( !initSpreadsheet() )
212*cdf0e10cSrcweir             System.exit( 0 );
213*cdf0e10cSrcweir     }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     // __________ private members and methods __________
217*cdf0e10cSrcweir     private final String  msDataSheetName  = "Data";
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir     private XComponentContext      maOfficeContext;
220*cdf0e10cSrcweir     private XMultiComponentFactory maServiceManager;
221*cdf0e10cSrcweir     private XSpreadsheetDocument   maSpreadsheetDoc;
222*cdf0e10cSrcweir     private XSpreadsheet           maSheet;  // the first sheet
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     // ____________________
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir     /** init the first sheet
228*cdf0e10cSrcweir      */
229*cdf0e10cSrcweir     private boolean initSpreadsheet()
230*cdf0e10cSrcweir     {
231*cdf0e10cSrcweir         boolean bOk = true;
232*cdf0e10cSrcweir         XSpreadsheets aSheets = maSpreadsheetDoc.getSheets();
233*cdf0e10cSrcweir         try
234*cdf0e10cSrcweir         {
235*cdf0e10cSrcweir             XIndexAccess aSheetsIA = (XIndexAccess) UnoRuntime.queryInterface( XIndexAccess.class, aSheets );
236*cdf0e10cSrcweir             maSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, aSheetsIA.getByIndex( 0 ));
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir             // enter some values in B3:B11
239*cdf0e10cSrcweir             for( int iCounter=1; iCounter < 10; iCounter++ )
240*cdf0e10cSrcweir             {
241*cdf0e10cSrcweir                 XCell aCell = maSheet.getCellByPosition( 1, 1 + iCounter );
242*cdf0e10cSrcweir                 aCell.setValue( (double) iCounter );
243*cdf0e10cSrcweir             }
244*cdf0e10cSrcweir         }
245*cdf0e10cSrcweir         catch( Exception ex )
246*cdf0e10cSrcweir         {
247*cdf0e10cSrcweir             System.err.println( "Couldn't initialize Spreadsheet Document: " + ex );
248*cdf0e10cSrcweir             ex.printStackTrace();
249*cdf0e10cSrcweir             bOk = false;
250*cdf0e10cSrcweir         }
251*cdf0e10cSrcweir         return bOk;
252*cdf0e10cSrcweir     }
253*cdf0e10cSrcweir }
254