Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / FirstSteps / FirstLoadComponent.java
blobb7d7e97c9ac30711ded6002bf1e2a85966facca7
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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 *************************************************************************/
34 import com.sun.star.beans.PropertyValue;
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.uno.XComponentContext;
37 import com.sun.star.comp.helper.Bootstrap;
38 import com.sun.star.container.XEnumeration;
39 import com.sun.star.container.XEnumerationAccess;
40 import com.sun.star.frame.XComponentLoader;
41 import com.sun.star.frame.XController;
42 import com.sun.star.frame.XModel;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.lang.XMultiComponentFactory;
45 import com.sun.star.sheet.XCellAddressable;
46 import com.sun.star.sheet.XCellRangesQuery;
47 import com.sun.star.sheet.XSheetCellRanges;
48 import com.sun.star.sheet.XSpreadsheet;
49 import com.sun.star.sheet.XSpreadsheetDocument;
50 import com.sun.star.sheet.XSpreadsheetView;
51 import com.sun.star.sheet.XSpreadsheets;
52 import com.sun.star.table.XCell;
53 import com.sun.star.uno.UnoRuntime;
55 public class FirstLoadComponent {
57 /**
58 * @param args the command line arguments
60 public static void main(String[] args) {
61 try {
62 // get the remote office component context
63 XComponentContext xRemoteContext = Bootstrap.bootstrap();
64 if (xRemoteContext == null) {
65 System.err.println("ERROR: Could not bootstrap default Office.");
68 XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager();
70 Object desktop = xRemoteServiceManager.createInstanceWithContext(
71 "com.sun.star.frame.Desktop", xRemoteContext);
72 XComponentLoader xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, desktop);
74 PropertyValue[] loadProps = new PropertyValue[0];
75 XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps);
77 XSpreadsheetDocument xSpreadsheetDocument = UnoRuntime.queryInterface(XSpreadsheetDocument.class,
78 xSpreadsheetComponent);
80 XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
81 xSpreadsheets.insertNewByName("MySheet", (short)0);
82 com.sun.star.uno.Type elemType = xSpreadsheets.getElementType();
84 System.out.println(elemType.getTypeName());
85 Object sheet = xSpreadsheets.getByName("MySheet");
86 XSpreadsheet xSpreadsheet = UnoRuntime.queryInterface(
87 XSpreadsheet.class, sheet);
89 XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
90 xCell.setValue(21);
91 xCell = xSpreadsheet.getCellByPosition(0, 1);
92 xCell.setValue(21);
93 xCell = xSpreadsheet.getCellByPosition(0, 2);
94 xCell.setFormula("=sum(A1:A2)");
96 XPropertySet xCellProps = UnoRuntime.queryInterface(
97 XPropertySet.class, xCell);
98 xCellProps.setPropertyValue("CellStyle", "Result");
100 XModel xSpreadsheetModel = UnoRuntime.queryInterface(
101 XModel.class, xSpreadsheetComponent);
102 XController xSpreadsheetController = xSpreadsheetModel.getCurrentController();
103 XSpreadsheetView xSpreadsheetView = UnoRuntime.queryInterface(XSpreadsheetView.class,
104 xSpreadsheetController);
105 xSpreadsheetView.setActiveSheet(xSpreadsheet);
107 // *********************************************************
108 // example for use of enum types
109 xCellProps.setPropertyValue("VertJustify",
110 com.sun.star.table.CellVertJustify.TOP);
113 // *********************************************************
114 // example for a sequence of PropertyValue structs
115 // create an array with one PropertyValue struct, it contains
116 // references only
117 loadProps = new PropertyValue[1];
119 // instantiate PropertyValue struct and set its member fields
120 PropertyValue asTemplate = new PropertyValue();
121 asTemplate.Name = "AsTemplate";
122 asTemplate.Value = Boolean.TRUE;
124 // assign PropertyValue struct to array of references for PropertyValue
125 // structs
126 loadProps[0] = asTemplate;
128 // load calc file as template
129 //xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
130 // "file:///c:/temp/DataAnalysys.ods", "_blank", 0, loadProps);
132 // *********************************************************
133 // example for use of XEnumerationAccess
134 XCellRangesQuery xCellQuery = UnoRuntime.queryInterface(XCellRangesQuery.class, sheet);
135 XSheetCellRanges xFormulaCells = xCellQuery.queryContentCells(
136 (short)com.sun.star.sheet.CellFlags.FORMULA);
137 XEnumerationAccess xFormulas = xFormulaCells.getCells();
138 XEnumeration xFormulaEnum = xFormulas.createEnumeration();
140 while (xFormulaEnum.hasMoreElements()) {
141 Object formulaCell = xFormulaEnum.nextElement();
142 xCell = UnoRuntime.queryInterface(XCell.class, formulaCell);
143 XCellAddressable xCellAddress = UnoRuntime.queryInterface(XCellAddressable.class, xCell);
144 System.out.println("Formula cell in column " +
145 xCellAddress.getCellAddress().Column
146 + ", row " + xCellAddress.getCellAddress().Row
147 + " contains " + xCell.getFormula());
151 catch (java.lang.Exception e){
152 e.printStackTrace();
154 finally {
155 System.exit( 0 );