1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 *************************************************************************/
35 // __________ Imports __________
37 import com
.sun
.star
.beans
.PropertyValue
;
38 import com
.sun
.star
.container
.XIndexAccess
;
39 import com
.sun
.star
.frame
.XComponentLoader
;
40 import com
.sun
.star
.lang
.XMultiComponentFactory
;
41 import com
.sun
.star
.sheet
.XSpreadsheet
;
42 import com
.sun
.star
.sheet
.XSpreadsheets
;
43 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
44 import com
.sun
.star
.table
.XCell
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
46 import com
.sun
.star
.uno
.XComponentContext
;
49 // __________ Implementation __________
51 /** Create a spreadsheet document and provide access to a sheet framework that
52 is then used to modify some number formats.
54 public class Number_Formats
56 // __________ public members and methods __________
61 public static void main( String args
[] )
65 Number_Formats aSample
= new Number_Formats( args
);
70 System
.err
.println( "Sample caught exception! " + ex
);
75 System
.out
.println( "Sample done." );
81 public void doFunction() throws RuntimeException
, Exception
84 // com.sun.star.sheet.XSpreadsheetDocument maSpreadsheetDoc;
85 // com.sun.star.sheet.XSpreadsheet maSheet;
87 // Query the number formats supplier of the spreadsheet document
88 com
.sun
.star
.util
.XNumberFormatsSupplier xNumberFormatsSupplier
=
89 UnoRuntime
.queryInterface(
90 com
.sun
.star
.util
.XNumberFormatsSupplier
.class, maSpreadsheetDoc
);
92 // Get the number formats from the supplier
93 com
.sun
.star
.util
.XNumberFormats xNumberFormats
=
94 xNumberFormatsSupplier
.getNumberFormats();
96 // Query the XNumberFormatTypes interface
97 com
.sun
.star
.util
.XNumberFormatTypes xNumberFormatTypes
=
98 UnoRuntime
.queryInterface(
99 com
.sun
.star
.util
.XNumberFormatTypes
.class, xNumberFormats
);
101 // Get the number format index key of the default currency format,
102 // note the empty locale for default locale
103 com
.sun
.star
.lang
.Locale aLocale
= new com
.sun
.star
.lang
.Locale();
104 int nCurrencyKey
= xNumberFormatTypes
.getStandardFormat(
105 com
.sun
.star
.util
.NumberFormat
.CURRENCY
, aLocale
);
107 // Get cell range B3:B11
108 com
.sun
.star
.table
.XCellRange xCellRange
=
109 maSheet
.getCellRangeByPosition( 1, 2, 1, 10 );
111 // Query the property set of the cell range
112 com
.sun
.star
.beans
.XPropertySet xCellProp
=
113 UnoRuntime
.queryInterface(
114 com
.sun
.star
.beans
.XPropertySet
.class, xCellRange
);
116 // Set number format to default currency
117 xCellProp
.setPropertyValue( "NumberFormat", Integer
.valueOf(nCurrencyKey
) );
120 com
.sun
.star
.table
.XCell xCell
= maSheet
.getCellByPosition( 1, 2 );
122 // Query the property set of the cell
123 xCellProp
= UnoRuntime
.queryInterface(
124 com
.sun
.star
.beans
.XPropertySet
.class, xCell
);
126 // Get the number format index key of the cell's properties
127 int nIndexKey
= ((Integer
) xCellProp
.getPropertyValue( "NumberFormat" )).intValue();
128 if ( nIndexKey
!= nCurrencyKey
)
129 System
.out
.println( "Number format doesn't match!" );
131 // Get the properties of the number format
132 com
.sun
.star
.beans
.XPropertySet xProp
= xNumberFormats
.getByKey( nIndexKey
);
134 // Get the format code string of the number format's properties
135 String aFormatCode
= (String
) xProp
.getPropertyValue( "FormatString" );
136 System
.out
.println( "FormatString: `" + aFormatCode
+ "'" );
138 // Create an arbitrary format code
139 aFormatCode
= "\"wonderful \"" + aFormatCode
;
141 // Test if it's already present
142 nIndexKey
= xNumberFormats
.queryKey( aFormatCode
, aLocale
, false );
144 // If not, add to number formats collection
145 if ( nIndexKey
== -1 )
149 nIndexKey
= xNumberFormats
.addNew( aFormatCode
, aLocale
);
151 catch( com
.sun
.star
.util
.MalformedNumberFormatException ex
)
153 System
.err
.println( "Bad number format code: " + ex
);
154 ex
.printStackTrace();
159 // Set the new format at the cell
160 if ( nIndexKey
!= -1 )
161 xCellProp
.setPropertyValue( "NumberFormat", Integer
.valueOf(nIndexKey
) );
164 // Set column containing the example values to optimal width to show
165 // the new format of cell B3
166 com
.sun
.star
.table
.XColumnRowRange xColRowRange
=
167 UnoRuntime
.queryInterface(com
.sun
.star
.table
.XColumnRowRange
.class,
170 com
.sun
.star
.container
.XIndexAccess xIndexAccess
=
171 UnoRuntime
.queryInterface(com
.sun
.star
.container
.XIndexAccess
.class,
172 xColRowRange
.getColumns());
174 com
.sun
.star
.beans
.XPropertySet xColPropSet
=
175 UnoRuntime
.queryInterface(com
.sun
.star
.beans
.XPropertySet
.class,
176 xIndexAccess
.getByIndex(1));
178 xColPropSet
.setPropertyValue( "OptimalWidth", Boolean
.TRUE
);
183 public Number_Formats( String
[] args
) throws java
.lang
.Exception
185 // get the remote office context. If necessary a new office
186 // process is started
187 XComponentContext aOfficeContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
188 System
.out
.println("Connected to a running office ...");
189 XMultiComponentFactory aServiceManager
= aOfficeContext
.getServiceManager();
191 // create a new spreadsheet document
192 XComponentLoader aLoader
= UnoRuntime
.queryInterface(
193 XComponentLoader
.class, aServiceManager
.createInstanceWithContext(
194 "com.sun.star.frame.Desktop", aOfficeContext
) );
196 maSpreadsheetDoc
= UnoRuntime
.queryInterface(
197 XSpreadsheetDocument
.class,
198 aLoader
.loadComponentFromURL( "private:factory/scalc",
201 new PropertyValue
[ 0 ] ) );
203 if ( !initSpreadsheet() )
208 // __________ private members and methods __________
210 private final XSpreadsheetDocument maSpreadsheetDoc
;
211 private XSpreadsheet maSheet
; // the first sheet
216 /** init the first sheet
218 private boolean initSpreadsheet()
221 XSpreadsheets aSheets
= maSpreadsheetDoc
.getSheets();
224 XIndexAccess aSheetsIA
= UnoRuntime
.queryInterface( XIndexAccess
.class, aSheets
);
225 maSheet
= UnoRuntime
.queryInterface(XSpreadsheet
.class, aSheetsIA
.getByIndex( 0 ));
227 // enter some values in B3:B11
228 for( int iCounter
=1; iCounter
< 10; iCounter
++ )
230 XCell aCell
= maSheet
.getCellByPosition( 1, 1 + iCounter
);
231 aCell
.setValue( iCounter
);
234 catch( Exception ex
)
236 System
.err
.println( "Couldn't initialize Spreadsheet Document: " + ex
);
237 ex
.printStackTrace();