1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 import com.sun.star.beans.PropertyValue;
24 import com.sun.star.beans.XPropertySet;
25 import com.sun.star.uno.XComponentContext;
26 import com.sun.star.comp.helper.Bootstrap;
27 import com.sun.star.container.XEnumeration;
28 import com.sun.star.container.XEnumerationAccess;
29 import com.sun.star.frame.XComponentLoader;
30 import com.sun.star.frame.XController;
31 import com.sun.star.frame.XModel;
32 import com.sun.star.lang.XComponent;
33 import com.sun.star.lang.XMultiComponentFactory;
34 import com.sun.star.sheet.XCellAddressable;
35 import com.sun.star.sheet.XCellRangesQuery;
36 import com.sun.star.sheet.XSheetCellRanges;
37 import com.sun.star.sheet.XSpreadsheet;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.sheet.XSpreadsheetView;
40 import com.sun.star.sheet.XSpreadsheets;
41 import com.sun.star.table.XCell;
42 import com.sun.star.uno.UnoRuntime;
43 
44 public class FirstLoadComponent {
45 
46     /** Creates a new instance of FirstLoadComponent */
FirstLoadComponent()47     public FirstLoadComponent() {
48     }
49 
50     /**
51      * @param args the command line arguments
52      */
main(String[] args)53     public static void main(String[] args) {
54         try {
55             // get the remote office component context
56             XComponentContext xRemoteContext = Bootstrap.bootstrap();
57             if (xRemoteContext == null) {
58                 System.err.println("ERROR: Could not bootstrap default Office.");
59             }
60 
61             XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager();
62 
63             Object desktop = xRemoteServiceManager.createInstanceWithContext(
64                 "com.sun.star.frame.Desktop", xRemoteContext);
65             XComponentLoader xComponentLoader = (XComponentLoader)
66                 UnoRuntime.queryInterface(XComponentLoader.class, desktop);
67 
68             PropertyValue[] loadProps = new PropertyValue[0];
69             XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps);
70 
71             XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)
72                 UnoRuntime.queryInterface(XSpreadsheetDocument.class,
73                                           xSpreadsheetComponent);
74 
75             XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
76             xSpreadsheets.insertNewByName("MySheet", (short)0);
77             com.sun.star.uno.Type elemType = xSpreadsheets.getElementType();
78 
79             System.out.println(elemType.getTypeName());
80             Object sheet = xSpreadsheets.getByName("MySheet");
81             XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
82                 XSpreadsheet.class, sheet);
83 
84             XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
85             xCell.setValue(21);
86             xCell = xSpreadsheet.getCellByPosition(0, 1);
87             xCell.setValue(21);
88             xCell = xSpreadsheet.getCellByPosition(0, 2);
89             xCell.setFormula("=sum(A1:A2)");
90 
91             XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
92                 XPropertySet.class, xCell);
93             xCellProps.setPropertyValue("CellStyle", "Result");
94 
95             XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface(
96                 XModel.class, xSpreadsheetComponent);
97             XController xSpreadsheetController = xSpreadsheetModel.getCurrentController();
98             XSpreadsheetView xSpreadsheetView = (XSpreadsheetView)
99                 UnoRuntime.queryInterface(XSpreadsheetView.class,
100                                           xSpreadsheetController);
101             xSpreadsheetView.setActiveSheet(xSpreadsheet);
102 
103             // *********************************************************
104             // example for use of enum types
105             xCellProps.setPropertyValue("VertJustify",
106                                         com.sun.star.table.CellVertJustify.TOP);
107 
108 
109             // *********************************************************
110             // example for a sequence of PropertyValue structs
111             // create an array with one PropertyValue struct, it contains
112             // references only
113             loadProps = new PropertyValue[1];
114 
115             // instantiate PropertyValue struct and set its member fields
116             PropertyValue asTemplate = new PropertyValue();
117             asTemplate.Name = "AsTemplate";
118             asTemplate.Value = new Boolean(true);
119 
120             // assign PropertyValue struct to array of references for PropertyValue
121             // structs
122             loadProps[0] = asTemplate;
123 
124             // load calc file as template
125             //xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
126             //    "file:///c:/temp/DataAnalysys.ods", "_blank", 0, loadProps);
127 
128             // *********************************************************
129             // example for use of XEnumerationAccess
130             XCellRangesQuery xCellQuery = (XCellRangesQuery)
131                 UnoRuntime.queryInterface(XCellRangesQuery.class, sheet);
132             XSheetCellRanges xFormulaCells = xCellQuery.queryContentCells(
133                 (short)com.sun.star.sheet.CellFlags.FORMULA);
134             XEnumerationAccess xFormulas = xFormulaCells.getCells();
135             XEnumeration xFormulaEnum = xFormulas.createEnumeration();
136 
137             while (xFormulaEnum.hasMoreElements()) {
138                 Object formulaCell = xFormulaEnum.nextElement();
139                 xCell = (XCell)UnoRuntime.queryInterface(XCell.class, formulaCell);
140                 XCellAddressable xCellAddress = (XCellAddressable)
141                     UnoRuntime.queryInterface(XCellAddressable.class, xCell);
142                 System.out.println("Formula cell in column " +
143                                    xCellAddress.getCellAddress().Column
144                                    + ", row " + xCellAddress.getCellAddress().Row
145                                    + " contains " + xCell.getFormula());
146             }
147 
148         }
149         catch (java.lang.Exception e){
150             e.printStackTrace();
151         }
152         finally {
153             System.exit( 0 );
154         }
155     }
156 
157 }
158 
159 
160 // import com.sun.star.uno.UnoRuntime;
161 // import com.sun.star.uno.XComponentContext;
162 // import com.sun.star.lang.XMultiComponentFactory;
163 // import com.sun.star.lang.XComponent;
164 // import com.sun.star.beans.XPropertySet;
165 // import com.sun.star.beans.PropertyValue;
166 // import com.sun.star.sheet.XSpreadsheetDocument;
167 // import com.sun.star.sheet.XSpreadsheets;
168 // import com.sun.star.sheet.XSpreadsheet;
169 // import com.sun.star.sheet.XSpreadsheetView;
170 // import com.sun.star.sheet.XCellRangesQuery;
171 // import com.sun.star.sheet.XSheetCellRanges;
172 // import com.sun.star.sheet.XCellAddressable;
173 // import com.sun.star.table.XCell;
174 // import com.sun.star.frame.XModel;
175 // import com.sun.star.frame.XController;
176 // import com.sun.star.frame.XComponentLoader;
177 // import com.sun.star.container.XEnumeration;
178 // import com.sun.star.container.XEnumerationAccess;
179 
180 // import com.sun.star.uno.AnyConverter;
181 
182 
183 // /**
184 //  *
185 //  * @author  dschulten
186 //  */
187 // public class FirstLoadComponent {
188 
189 //     /** Creates a new instance of FirstLoadComponent */
190 //     public FirstLoadComponent() {
191 //     }
192 
193 //     /**
194 //      * @param args the command line arguments
195 //      */
196 //     private XComponentContext xRemoteContext = null;
197 //     private XMultiComponentFactory xRemoteServiceManager = null;
198 
199 //     public static void main(String[] args) {
200 //         FirstLoadComponent firstLoadComponent1 = new FirstLoadComponent();
201 //         try {
202 //             firstLoadComponent1.useConnection();
203 //         }
204 //         catch (java.lang.Exception e){
205 //             System.out.println(e.getMessage());
206 //             e.printStackTrace();
207 //         }
208 //         finally {
209 //             System.exit(0);
210 //         }
211 //     }
212 
213 //     private void useConnection() throws java.lang.Exception {
214 //         try {
215 //             // get the remote office component context
216 //             xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
217 //             System.out.println("Connected to a running office ...");
218 
219 //             xRemoteServiceManager = xRemoteContext.getServiceManager();
220 //         }
221 //         catch( Exception e) {
222 //             e.printStackTrace();
223 //             System.exit(1);
224 //         }
225 
226 //         try {
227 //             Object desktop = xRemoteServiceManager.createInstanceWithContext(
228 //                 "com.sun.star.frame.Desktop", xRemoteContext);
229 //             XComponentLoader xComponentLoader = (XComponentLoader)
230 //                 UnoRuntime.queryInterface(XComponentLoader.class, desktop);
231 
232 //             PropertyValue[] loadProps = new PropertyValue[0];
233 //             XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps);
234 
235 //             XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)
236 //                 UnoRuntime.queryInterface(XSpreadsheetDocument.class,
237 //                                           xSpreadsheetComponent);
238 
239 //             XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
240 //             xSpreadsheets.insertNewByName("MySheet", (short)0);
241 //             com.sun.star.uno.Type elemType = xSpreadsheets.getElementType();
242 
243 //             System.out.println(elemType.getTypeName());
244 //             Object sheet = xSpreadsheets.getByName("MySheet");
245 //             XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
246 //                 XSpreadsheet.class, sheet);
247 
248 //             XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
249 //             xCell.setValue(21);
250 //             xCell = xSpreadsheet.getCellByPosition(0, 1);
251 //             xCell.setValue(21);
252 //             xCell = xSpreadsheet.getCellByPosition(0, 2);
253 //             xCell.setFormula("=sum(A1:A2)");
254 
255 //             XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
256 //                 XPropertySet.class, xCell);
257 //             xCellProps.setPropertyValue("CellStyle", "Result");
258 
259 //             XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface(
260 //                 XModel.class, xSpreadsheetComponent);
261 //             XController xSpreadsheetController = xSpreadsheetModel.getCurrentController();
262 //             XSpreadsheetView xSpreadsheetView = (XSpreadsheetView)
263 //                 UnoRuntime.queryInterface(XSpreadsheetView.class,
264 //                                           xSpreadsheetController);
265 //             xSpreadsheetView.setActiveSheet(xSpreadsheet);
266 
267 //             // *********************************************************
268 //             // example for use of enum types
269 //             xCellProps.setPropertyValue("VertJustify",
270 //                                         com.sun.star.table.CellVertJustify.TOP);
271 
272 
273 //             // *********************************************************
274 //             // example for a sequence of PropertyValue structs
275 //             // create an array with one PropertyValue struct, it contains
276 //             // references only
277 //             loadProps = new PropertyValue[1];
278 
279 //             // instantiate PropertyValue struct and set its member fields
280 //             PropertyValue asTemplate = new PropertyValue();
281 //             asTemplate.Name = "AsTemplate";
282 //             asTemplate.Value = new Boolean(true);
283 
284 //             // assign PropertyValue struct to array of references for PropertyValue
285 //             // structs
286 //             loadProps[0] = asTemplate;
287 
288 //             // load calc file as template
289 //             //xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
290 //             //    "file:///c:/temp/DataAnalysys.ods", "_blank", 0, loadProps);
291 
292 //             // *********************************************************
293 //             // example for use of XEnumerationAccess
294 //             XCellRangesQuery xCellQuery = (XCellRangesQuery)
295 //                 UnoRuntime.queryInterface(XCellRangesQuery.class, sheet);
296 //             XSheetCellRanges xFormulaCells = xCellQuery.queryContentCells(
297 //                 (short)com.sun.star.sheet.CellFlags.FORMULA);
298 //             XEnumerationAccess xFormulas = xFormulaCells.getCells();
299 //             XEnumeration xFormulaEnum = xFormulas.createEnumeration();
300 
301 //             while (xFormulaEnum.hasMoreElements()) {
302 //                 Object formulaCell = xFormulaEnum.nextElement();
303 //                 xCell = (XCell)UnoRuntime.queryInterface(XCell.class, formulaCell);
304 //                 XCellAddressable xCellAddress = (XCellAddressable)
305 //                     UnoRuntime.queryInterface(XCellAddressable.class, xCell);
306 //                 System.out.println("Formula cell in column " +
307 //                                    xCellAddress.getCellAddress().Column
308 //                                    + ", row " + xCellAddress.getCellAddress().Row
309 //                                    + " contains " + xCell.getFormula());
310 //             }
311 
312 //         }
313 //         catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
314 //             xRemoteContext = null;
315 //             throw e;
316 //         }
317 //     }
318 // }
319