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 *************************************************************************/
35 import com
.sun
.star
.beans
.PropertyValue
;
36 import com
.sun
.star
.beans
.XPropertySet
;
37 import com
.sun
.star
.uno
.XComponentContext
;
38 import com
.sun
.star
.comp
.helper
.Bootstrap
;
39 import com
.sun
.star
.container
.XEnumeration
;
40 import com
.sun
.star
.container
.XEnumerationAccess
;
41 import com
.sun
.star
.frame
.XComponentLoader
;
42 import com
.sun
.star
.frame
.XController
;
43 import com
.sun
.star
.frame
.XModel
;
44 import com
.sun
.star
.lang
.XComponent
;
45 import com
.sun
.star
.lang
.XMultiComponentFactory
;
46 import com
.sun
.star
.sheet
.XCellAddressable
;
47 import com
.sun
.star
.sheet
.XCellRangesQuery
;
48 import com
.sun
.star
.sheet
.XSheetCellRanges
;
49 import com
.sun
.star
.sheet
.XSpreadsheet
;
50 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
51 import com
.sun
.star
.sheet
.XSpreadsheetView
;
52 import com
.sun
.star
.sheet
.XSpreadsheets
;
53 import com
.sun
.star
.table
.XCell
;
54 import com
.sun
.star
.uno
.UnoRuntime
;
56 public class FirstLoadComponent
{
59 * @param args the command line arguments
61 public static void main(String
[] args
) {
63 // get the remote office component context
64 XComponentContext xRemoteContext
= Bootstrap
.bootstrap();
65 if (xRemoteContext
== null) {
66 System
.err
.println("ERROR: Could not bootstrap default Office.");
69 XMultiComponentFactory xRemoteServiceManager
= xRemoteContext
.getServiceManager();
71 Object desktop
= xRemoteServiceManager
.createInstanceWithContext(
72 "com.sun.star.frame.Desktop", xRemoteContext
);
73 XComponentLoader xComponentLoader
= UnoRuntime
.queryInterface(XComponentLoader
.class, desktop
);
75 PropertyValue
[] loadProps
= new PropertyValue
[0];
76 XComponent xSpreadsheetComponent
= xComponentLoader
.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps
);
78 XSpreadsheetDocument xSpreadsheetDocument
= UnoRuntime
.queryInterface(XSpreadsheetDocument
.class,
79 xSpreadsheetComponent
);
81 XSpreadsheets xSpreadsheets
= xSpreadsheetDocument
.getSheets();
82 xSpreadsheets
.insertNewByName("MySheet", (short)0);
83 com
.sun
.star
.uno
.Type elemType
= xSpreadsheets
.getElementType();
85 System
.out
.println(elemType
.getTypeName());
86 Object sheet
= xSpreadsheets
.getByName("MySheet");
87 XSpreadsheet xSpreadsheet
= UnoRuntime
.queryInterface(
88 XSpreadsheet
.class, sheet
);
90 XCell xCell
= xSpreadsheet
.getCellByPosition(0, 0);
92 xCell
= xSpreadsheet
.getCellByPosition(0, 1);
94 xCell
= xSpreadsheet
.getCellByPosition(0, 2);
95 xCell
.setFormula("=sum(A1:A2)");
97 XPropertySet xCellProps
= UnoRuntime
.queryInterface(
98 XPropertySet
.class, xCell
);
99 xCellProps
.setPropertyValue("CellStyle", "Result");
101 XModel xSpreadsheetModel
= UnoRuntime
.queryInterface(
102 XModel
.class, xSpreadsheetComponent
);
103 XController xSpreadsheetController
= xSpreadsheetModel
.getCurrentController();
104 XSpreadsheetView xSpreadsheetView
= UnoRuntime
.queryInterface(XSpreadsheetView
.class,
105 xSpreadsheetController
);
106 xSpreadsheetView
.setActiveSheet(xSpreadsheet
);
108 // *********************************************************
109 // example for use of enum types
110 xCellProps
.setPropertyValue("VertJustify",
111 com
.sun
.star
.table
.CellVertJustify
.TOP
);
114 // *********************************************************
115 // example for a sequence of PropertyValue structs
116 // create an array with one PropertyValue struct, it contains
118 loadProps
= new PropertyValue
[1];
120 // instantiate PropertyValue struct and set its member fields
121 PropertyValue asTemplate
= new PropertyValue();
122 asTemplate
.Name
= "AsTemplate";
123 asTemplate
.Value
= Boolean
.TRUE
;
125 // assign PropertyValue struct to array of references for PropertyValue
127 loadProps
[0] = asTemplate
;
129 // load calc file as template
130 //xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
131 // "file:///c:/temp/DataAnalysys.ods", "_blank", 0, loadProps);
133 // *********************************************************
134 // example for use of XEnumerationAccess
135 XCellRangesQuery xCellQuery
= UnoRuntime
.queryInterface(XCellRangesQuery
.class, sheet
);
136 XSheetCellRanges xFormulaCells
= xCellQuery
.queryContentCells(
137 (short)com
.sun
.star
.sheet
.CellFlags
.FORMULA
);
138 XEnumerationAccess xFormulas
= xFormulaCells
.getCells();
139 XEnumeration xFormulaEnum
= xFormulas
.createEnumeration();
141 while (xFormulaEnum
.hasMoreElements()) {
142 Object formulaCell
= xFormulaEnum
.nextElement();
143 xCell
= UnoRuntime
.queryInterface(XCell
.class, formulaCell
);
144 XCellAddressable xCellAddress
= UnoRuntime
.queryInterface(XCellAddressable
.class, xCell
);
145 System
.out
.println("Formula cell in column " +
146 xCellAddress
.getCellAddress().Column
147 + ", row " + xCellAddress
.getCellAddress().Row
148 + " contains " + xCell
.getFormula());
152 catch (java
.lang
.Exception e
){
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */