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
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 *************************************************************************/
36 // __________ Imports __________
38 import com
.sun
.star
.beans
.PropertyValue
;
39 import com
.sun
.star
.container
.XIndexAccess
;
40 import com
.sun
.star
.frame
.XComponentLoader
;
41 import com
.sun
.star
.lang
.XMultiComponentFactory
;
42 import com
.sun
.star
.sheet
.XSpreadsheet
;
43 import com
.sun
.star
.sheet
.XSpreadsheets
;
44 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
45 import com
.sun
.star
.table
.XCell
;
46 import com
.sun
.star
.uno
.UnoRuntime
;
47 import com
.sun
.star
.uno
.XComponentContext
;
50 // __________ Implementation __________
52 /** Create a spreadsheet document and provide access to a sheet framework that
53 is then used to modify some number formats.
55 public class Number_Formats
57 // __________ public members and methods __________
62 public static void main( String args
[] )
66 Number_Formats aSample
= new Number_Formats( args
);
71 System
.err
.println( "Sample caught exception! " + ex
);
76 System
.out
.println( "Sample done." );
82 public void doFunction() throws RuntimeException
, Exception
85 // com.sun.star.sheet.XSpreadsheetDocument maSpreadsheetDoc;
86 // com.sun.star.sheet.XSpreadsheet maSheet;
88 // Query the number formats supplier of the spreadsheet document
89 com
.sun
.star
.util
.XNumberFormatsSupplier xNumberFormatsSupplier
=
90 UnoRuntime
.queryInterface(
91 com
.sun
.star
.util
.XNumberFormatsSupplier
.class, maSpreadsheetDoc
);
93 // Get the number formats from the supplier
94 com
.sun
.star
.util
.XNumberFormats xNumberFormats
=
95 xNumberFormatsSupplier
.getNumberFormats();
97 // Query the XNumberFormatTypes interface
98 com
.sun
.star
.util
.XNumberFormatTypes xNumberFormatTypes
=
99 UnoRuntime
.queryInterface(
100 com
.sun
.star
.util
.XNumberFormatTypes
.class, xNumberFormats
);
102 // Get the number format index key of the default currency format,
103 // note the empty locale for default locale
104 com
.sun
.star
.lang
.Locale aLocale
= new com
.sun
.star
.lang
.Locale();
105 int nCurrencyKey
= xNumberFormatTypes
.getStandardFormat(
106 com
.sun
.star
.util
.NumberFormat
.CURRENCY
, aLocale
);
108 // Get cell range B3:B11
109 com
.sun
.star
.table
.XCellRange xCellRange
=
110 maSheet
.getCellRangeByPosition( 1, 2, 1, 10 );
112 // Query the property set of the cell range
113 com
.sun
.star
.beans
.XPropertySet xCellProp
=
114 UnoRuntime
.queryInterface(
115 com
.sun
.star
.beans
.XPropertySet
.class, xCellRange
);
117 // Set number format to default currency
118 xCellProp
.setPropertyValue( "NumberFormat", Integer
.valueOf(nCurrencyKey
) );
121 com
.sun
.star
.table
.XCell xCell
= maSheet
.getCellByPosition( 1, 2 );
123 // Query the property set of the cell
124 xCellProp
= UnoRuntime
.queryInterface(
125 com
.sun
.star
.beans
.XPropertySet
.class, xCell
);
127 // Get the number format index key of the cell's properties
128 int nIndexKey
= ((Integer
) xCellProp
.getPropertyValue( "NumberFormat" )).intValue();
129 if ( nIndexKey
!= nCurrencyKey
)
130 System
.out
.println( "Number format doesn't match!" );
132 // Get the properties of the number format
133 com
.sun
.star
.beans
.XPropertySet xProp
= xNumberFormats
.getByKey( nIndexKey
);
135 // Get the format code string of the number format's properties
136 String aFormatCode
= (String
) xProp
.getPropertyValue( "FormatString" );
137 System
.out
.println( "FormatString: `" + aFormatCode
+ "'" );
139 // Create an arbitrary format code
140 aFormatCode
= "\"wonderful \"" + aFormatCode
;
142 // Test if it's already present
143 nIndexKey
= xNumberFormats
.queryKey( aFormatCode
, aLocale
, false );
145 // If not, add to number formats collection
146 if ( nIndexKey
== -1 )
150 nIndexKey
= xNumberFormats
.addNew( aFormatCode
, aLocale
);
152 catch( com
.sun
.star
.util
.MalformedNumberFormatException ex
)
154 System
.err
.println( "Bad number format code: " + ex
);
155 ex
.printStackTrace();
160 // Set the new format at the cell
161 if ( nIndexKey
!= -1 )
162 xCellProp
.setPropertyValue( "NumberFormat", Integer
.valueOf(nIndexKey
) );
165 // Set column containing the example values to optimal width to show
166 // the new format of cell B3
167 com
.sun
.star
.table
.XColumnRowRange xColRowRange
=
168 UnoRuntime
.queryInterface(com
.sun
.star
.table
.XColumnRowRange
.class,
171 com
.sun
.star
.container
.XIndexAccess xIndexAccess
=
172 UnoRuntime
.queryInterface(com
.sun
.star
.container
.XIndexAccess
.class,
173 xColRowRange
.getColumns());
175 com
.sun
.star
.beans
.XPropertySet xColPropSet
=
176 UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class,
177 xIndexAccess
.getByIndex(1));
179 xColPropSet
.setPropertyValue( "OptimalWidth", Boolean
.TRUE
);
184 public Number_Formats( String
[] args
) throws java
.lang
.Exception
186 // get the remote office context. If necessary a new office
187 // process is started
188 XComponentContext aOfficeContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
189 System
.out
.println("Connected to a running office ...");
190 XMultiComponentFactory aServiceManager
= aOfficeContext
.getServiceManager();
192 // create a new spreadsheet document
193 XComponentLoader aLoader
= UnoRuntime
.queryInterface(
194 XComponentLoader
.class, aServiceManager
.createInstanceWithContext(
195 "com.sun.star.frame.Desktop", aOfficeContext
) );
197 maSpreadsheetDoc
= UnoRuntime
.queryInterface(
198 XSpreadsheetDocument
.class,
199 aLoader
.loadComponentFromURL( "private:factory/scalc",
202 new PropertyValue
[ 0 ] ) );
204 if ( !initSpreadsheet() )
209 // __________ private members and methods __________
211 private final XSpreadsheetDocument maSpreadsheetDoc
;
212 private XSpreadsheet maSheet
; // the first sheet
217 /** init the first sheet
219 private boolean initSpreadsheet()
222 XSpreadsheets aSheets
= maSpreadsheetDoc
.getSheets();
225 XIndexAccess aSheetsIA
= UnoRuntime
.queryInterface( XIndexAccess
.class, aSheets
);
226 maSheet
= UnoRuntime
.queryInterface(XSpreadsheet
.class, aSheetsIA
.getByIndex( 0 ));
228 // enter some values in B3:B11
229 for( int iCounter
=1; iCounter
< 10; iCounter
++ )
231 XCell aCell
= maSheet
.getCellByPosition( 1, 1 + iCounter
);
232 aCell
.setValue( iCounter
);
235 catch( Exception ex
)
237 System
.err
.println( "Couldn't initialize Spreadsheet Document: " + ex
);
238 ex
.printStackTrace();
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */