update dev300-m58
[ooovba.git] / odk / examples / DevelopersGuide / FirstSteps / FirstLoadComponent.java
blob7bfa76f97182905153aa73357faf3ab671a2896d
1 /*************************************************************************
3 * $RCSfile: FirstLoadComponent.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:26:24 $
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 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XComponentContext;
43 import com.sun.star.lang.XMultiComponentFactory;
44 import com.sun.star.lang.XComponent;
45 import com.sun.star.beans.XPropertySet;
46 import com.sun.star.beans.PropertyValue;
47 import com.sun.star.sheet.XSpreadsheetDocument;
48 import com.sun.star.sheet.XSpreadsheets;
49 import com.sun.star.sheet.XSpreadsheet;
50 import com.sun.star.sheet.XSpreadsheetView;
51 import com.sun.star.sheet.XCellRangesQuery;
52 import com.sun.star.sheet.XSheetCellRanges;
53 import com.sun.star.sheet.XCellAddressable;
54 import com.sun.star.table.XCell;
55 import com.sun.star.frame.XModel;
56 import com.sun.star.frame.XController;
57 import com.sun.star.frame.XComponentLoader;
58 import com.sun.star.container.XEnumeration;
59 import com.sun.star.container.XEnumerationAccess;
61 import com.sun.star.uno.AnyConverter;
64 /**
66 * @author dschulten
68 public class FirstLoadComponent {
70 /** Creates a new instance of FirstLoadComponent */
71 public FirstLoadComponent() {
74 /**
75 * @param args the command line arguments
77 private XComponentContext xRemoteContext = null;
78 private XMultiComponentFactory xRemoteServiceManager = null;
80 public static void main(String[] args) {
81 FirstLoadComponent firstLoadComponent1 = new FirstLoadComponent();
82 try {
83 firstLoadComponent1.useConnection();
85 catch (java.lang.Exception e){
86 System.out.println(e.getMessage());
87 e.printStackTrace();
89 finally {
90 System.exit(0);
94 private void useConnection() throws java.lang.Exception {
95 try {
96 // get the remote office component context
97 xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
98 System.out.println("Connected to a running office ...");
100 xRemoteServiceManager = xRemoteContext.getServiceManager();
102 catch( Exception e) {
103 e.printStackTrace();
104 System.exit(1);
107 try {
108 Object desktop = xRemoteServiceManager.createInstanceWithContext(
109 "com.sun.star.frame.Desktop", xRemoteContext);
110 XComponentLoader xComponentLoader = (XComponentLoader)
111 UnoRuntime.queryInterface(XComponentLoader.class, desktop);
113 PropertyValue[] loadProps = new PropertyValue[0];
114 XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps);
116 XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)
117 UnoRuntime.queryInterface(XSpreadsheetDocument.class,
118 xSpreadsheetComponent);
120 XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
121 xSpreadsheets.insertNewByName("MySheet", (short)0);
122 com.sun.star.uno.Type elemType = xSpreadsheets.getElementType();
124 System.out.println(elemType.getTypeName());
125 Object sheet = xSpreadsheets.getByName("MySheet");
126 XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
127 XSpreadsheet.class, sheet);
129 XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
130 xCell.setValue(21);
131 xCell = xSpreadsheet.getCellByPosition(0, 1);
132 xCell.setValue(21);
133 xCell = xSpreadsheet.getCellByPosition(0, 2);
134 xCell.setFormula("=sum(A1:A2)");
136 XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
137 XPropertySet.class, xCell);
138 xCellProps.setPropertyValue("CellStyle", "Result");
140 XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface(
141 XModel.class, xSpreadsheetComponent);
142 XController xSpreadsheetController = xSpreadsheetModel.getCurrentController();
143 XSpreadsheetView xSpreadsheetView = (XSpreadsheetView)
144 UnoRuntime.queryInterface(XSpreadsheetView.class,
145 xSpreadsheetController);
146 xSpreadsheetView.setActiveSheet(xSpreadsheet);
148 // *********************************************************
149 // example for use of enum types
150 xCellProps.setPropertyValue("VertJustify",
151 com.sun.star.table.CellVertJustify.TOP);
154 // *********************************************************
155 // example for a sequence of PropertyValue structs
156 // create an array with one PropertyValue struct, it contains
157 // references only
158 loadProps = new PropertyValue[1];
160 // instantiate PropertyValue struct and set its member fields
161 PropertyValue asTemplate = new PropertyValue();
162 asTemplate.Name = "AsTemplate";
163 asTemplate.Value = new Boolean(true);
165 // assign PropertyValue struct to array of references for PropertyValue
166 // structs
167 loadProps[0] = asTemplate;
169 // load calc file as template
170 //xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
171 // "file:///c:/temp/DataAnalysys.ods", "_blank", 0, loadProps);
173 // *********************************************************
174 // example for use of XEnumerationAccess
175 XCellRangesQuery xCellQuery = (XCellRangesQuery)
176 UnoRuntime.queryInterface(XCellRangesQuery.class, sheet);
177 XSheetCellRanges xFormulaCells = xCellQuery.queryContentCells(
178 (short)com.sun.star.sheet.CellFlags.FORMULA);
179 XEnumerationAccess xFormulas = xFormulaCells.getCells();
180 XEnumeration xFormulaEnum = xFormulas.createEnumeration();
182 while (xFormulaEnum.hasMoreElements()) {
183 Object formulaCell = xFormulaEnum.nextElement();
184 xCell = (XCell)UnoRuntime.queryInterface(XCell.class, formulaCell);
185 XCellAddressable xCellAddress = (XCellAddressable)
186 UnoRuntime.queryInterface(XCellAddressable.class, xCell);
187 System.out.println("Formula cell in column " +
188 xCellAddress.getCellAddress().Column
189 + ", row " + xCellAddress.getCellAddress().Row
190 + " contains " + xCell.getFormula());
194 catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
195 xRemoteContext = null;
196 throw e;