1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 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. 20 * 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. 32 * 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; 54 55 public class FirstLoadComponent { 56 57 /** Creates a new instance of FirstLoadComponent */ 58 public FirstLoadComponent() { 59 } 60 61 /** 62 * @param args the command line arguments 63 */ 64 public static void main(String[] args) { 65 try { 66 // get the remote office component context 67 XComponentContext xRemoteContext = Bootstrap.bootstrap(); 68 if (xRemoteContext == null) { 69 System.err.println("ERROR: Could not bootstrap default Office."); 70 } 71 72 XMultiComponentFactory xRemoteServiceManager = xRemoteContext.getServiceManager(); 73 74 Object desktop = xRemoteServiceManager.createInstanceWithContext( 75 "com.sun.star.frame.Desktop", xRemoteContext); 76 XComponentLoader xComponentLoader = (XComponentLoader) 77 UnoRuntime.queryInterface(XComponentLoader.class, desktop); 78 79 PropertyValue[] loadProps = new PropertyValue[0]; 80 XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps); 81 82 XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument) 83 UnoRuntime.queryInterface(XSpreadsheetDocument.class, 84 xSpreadsheetComponent); 85 86 XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets(); 87 xSpreadsheets.insertNewByName("MySheet", (short)0); 88 com.sun.star.uno.Type elemType = xSpreadsheets.getElementType(); 89 90 System.out.println(elemType.getTypeName()); 91 Object sheet = xSpreadsheets.getByName("MySheet"); 92 XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface( 93 XSpreadsheet.class, sheet); 94 95 XCell xCell = xSpreadsheet.getCellByPosition(0, 0); 96 xCell.setValue(21); 97 xCell = xSpreadsheet.getCellByPosition(0, 1); 98 xCell.setValue(21); 99 xCell = xSpreadsheet.getCellByPosition(0, 2); 100 xCell.setFormula("=sum(A1:A2)"); 101 102 XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface( 103 XPropertySet.class, xCell); 104 xCellProps.setPropertyValue("CellStyle", "Result"); 105 106 XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface( 107 XModel.class, xSpreadsheetComponent); 108 XController xSpreadsheetController = xSpreadsheetModel.getCurrentController(); 109 XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) 110 UnoRuntime.queryInterface(XSpreadsheetView.class, 111 xSpreadsheetController); 112 xSpreadsheetView.setActiveSheet(xSpreadsheet); 113 114 // ********************************************************* 115 // example for use of enum types 116 xCellProps.setPropertyValue("VertJustify", 117 com.sun.star.table.CellVertJustify.TOP); 118 119 120 // ********************************************************* 121 // example for a sequence of PropertyValue structs 122 // create an array with one PropertyValue struct, it contains 123 // references only 124 loadProps = new PropertyValue[1]; 125 126 // instantiate PropertyValue struct and set its member fields 127 PropertyValue asTemplate = new PropertyValue(); 128 asTemplate.Name = "AsTemplate"; 129 asTemplate.Value = new Boolean(true); 130 131 // assign PropertyValue struct to array of references for PropertyValue 132 // structs 133 loadProps[0] = asTemplate; 134 135 // load calc file as template 136 //xSpreadsheetComponent = xComponentLoader.loadComponentFromURL( 137 // "file:///c:/temp/DataAnalysys.ods", "_blank", 0, loadProps); 138 139 // ********************************************************* 140 // example for use of XEnumerationAccess 141 XCellRangesQuery xCellQuery = (XCellRangesQuery) 142 UnoRuntime.queryInterface(XCellRangesQuery.class, sheet); 143 XSheetCellRanges xFormulaCells = xCellQuery.queryContentCells( 144 (short)com.sun.star.sheet.CellFlags.FORMULA); 145 XEnumerationAccess xFormulas = xFormulaCells.getCells(); 146 XEnumeration xFormulaEnum = xFormulas.createEnumeration(); 147 148 while (xFormulaEnum.hasMoreElements()) { 149 Object formulaCell = xFormulaEnum.nextElement(); 150 xCell = (XCell)UnoRuntime.queryInterface(XCell.class, formulaCell); 151 XCellAddressable xCellAddress = (XCellAddressable) 152 UnoRuntime.queryInterface(XCellAddressable.class, xCell); 153 System.out.println("Formula cell in column " + 154 xCellAddress.getCellAddress().Column 155 + ", row " + xCellAddress.getCellAddress().Row 156 + " contains " + xCell.getFormula()); 157 } 158 159 } 160 catch (java.lang.Exception e){ 161 e.printStackTrace(); 162 } 163 finally { 164 System.exit( 0 ); 165 } 166 } 167 168 } 169 170 171 // import com.sun.star.uno.UnoRuntime; 172 // import com.sun.star.uno.XComponentContext; 173 // import com.sun.star.lang.XMultiComponentFactory; 174 // import com.sun.star.lang.XComponent; 175 // import com.sun.star.beans.XPropertySet; 176 // import com.sun.star.beans.PropertyValue; 177 // import com.sun.star.sheet.XSpreadsheetDocument; 178 // import com.sun.star.sheet.XSpreadsheets; 179 // import com.sun.star.sheet.XSpreadsheet; 180 // import com.sun.star.sheet.XSpreadsheetView; 181 // import com.sun.star.sheet.XCellRangesQuery; 182 // import com.sun.star.sheet.XSheetCellRanges; 183 // import com.sun.star.sheet.XCellAddressable; 184 // import com.sun.star.table.XCell; 185 // import com.sun.star.frame.XModel; 186 // import com.sun.star.frame.XController; 187 // import com.sun.star.frame.XComponentLoader; 188 // import com.sun.star.container.XEnumeration; 189 // import com.sun.star.container.XEnumerationAccess; 190 191 // import com.sun.star.uno.AnyConverter; 192 193 194 // /** 195 // * 196 // * @author dschulten 197 // */ 198 // public class FirstLoadComponent { 199 200 // /** Creates a new instance of FirstLoadComponent */ 201 // public FirstLoadComponent() { 202 // } 203 204 // /** 205 // * @param args the command line arguments 206 // */ 207 // private XComponentContext xRemoteContext = null; 208 // private XMultiComponentFactory xRemoteServiceManager = null; 209 210 // public static void main(String[] args) { 211 // FirstLoadComponent firstLoadComponent1 = new FirstLoadComponent(); 212 // try { 213 // firstLoadComponent1.useConnection(); 214 // } 215 // catch (java.lang.Exception e){ 216 // System.out.println(e.getMessage()); 217 // e.printStackTrace(); 218 // } 219 // finally { 220 // System.exit(0); 221 // } 222 // } 223 224 // private void useConnection() throws java.lang.Exception { 225 // try { 226 // // get the remote office component context 227 // xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 228 // System.out.println("Connected to a running office ..."); 229 230 // xRemoteServiceManager = xRemoteContext.getServiceManager(); 231 // } 232 // catch( Exception e) { 233 // e.printStackTrace(); 234 // System.exit(1); 235 // } 236 237 // try { 238 // Object desktop = xRemoteServiceManager.createInstanceWithContext( 239 // "com.sun.star.frame.Desktop", xRemoteContext); 240 // XComponentLoader xComponentLoader = (XComponentLoader) 241 // UnoRuntime.queryInterface(XComponentLoader.class, desktop); 242 243 // PropertyValue[] loadProps = new PropertyValue[0]; 244 // XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps); 245 246 // XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument) 247 // UnoRuntime.queryInterface(XSpreadsheetDocument.class, 248 // xSpreadsheetComponent); 249 250 // XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets(); 251 // xSpreadsheets.insertNewByName("MySheet", (short)0); 252 // com.sun.star.uno.Type elemType = xSpreadsheets.getElementType(); 253 254 // System.out.println(elemType.getTypeName()); 255 // Object sheet = xSpreadsheets.getByName("MySheet"); 256 // XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface( 257 // XSpreadsheet.class, sheet); 258 259 // XCell xCell = xSpreadsheet.getCellByPosition(0, 0); 260 // xCell.setValue(21); 261 // xCell = xSpreadsheet.getCellByPosition(0, 1); 262 // xCell.setValue(21); 263 // xCell = xSpreadsheet.getCellByPosition(0, 2); 264 // xCell.setFormula("=sum(A1:A2)"); 265 266 // XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface( 267 // XPropertySet.class, xCell); 268 // xCellProps.setPropertyValue("CellStyle", "Result"); 269 270 // XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface( 271 // XModel.class, xSpreadsheetComponent); 272 // XController xSpreadsheetController = xSpreadsheetModel.getCurrentController(); 273 // XSpreadsheetView xSpreadsheetView = (XSpreadsheetView) 274 // UnoRuntime.queryInterface(XSpreadsheetView.class, 275 // xSpreadsheetController); 276 // xSpreadsheetView.setActiveSheet(xSpreadsheet); 277 278 // // ********************************************************* 279 // // example for use of enum types 280 // xCellProps.setPropertyValue("VertJustify", 281 // com.sun.star.table.CellVertJustify.TOP); 282 283 284 // // ********************************************************* 285 // // example for a sequence of PropertyValue structs 286 // // create an array with one PropertyValue struct, it contains 287 // // references only 288 // loadProps = new PropertyValue[1]; 289 290 // // instantiate PropertyValue struct and set its member fields 291 // PropertyValue asTemplate = new PropertyValue(); 292 // asTemplate.Name = "AsTemplate"; 293 // asTemplate.Value = new Boolean(true); 294 295 // // assign PropertyValue struct to array of references for PropertyValue 296 // // structs 297 // loadProps[0] = asTemplate; 298 299 // // load calc file as template 300 // //xSpreadsheetComponent = xComponentLoader.loadComponentFromURL( 301 // // "file:///c:/temp/DataAnalysys.ods", "_blank", 0, loadProps); 302 303 // // ********************************************************* 304 // // example for use of XEnumerationAccess 305 // XCellRangesQuery xCellQuery = (XCellRangesQuery) 306 // UnoRuntime.queryInterface(XCellRangesQuery.class, sheet); 307 // XSheetCellRanges xFormulaCells = xCellQuery.queryContentCells( 308 // (short)com.sun.star.sheet.CellFlags.FORMULA); 309 // XEnumerationAccess xFormulas = xFormulaCells.getCells(); 310 // XEnumeration xFormulaEnum = xFormulas.createEnumeration(); 311 312 // while (xFormulaEnum.hasMoreElements()) { 313 // Object formulaCell = xFormulaEnum.nextElement(); 314 // xCell = (XCell)UnoRuntime.queryInterface(XCell.class, formulaCell); 315 // XCellAddressable xCellAddress = (XCellAddressable) 316 // UnoRuntime.queryInterface(XCellAddressable.class, xCell); 317 // System.out.println("Formula cell in column " + 318 // xCellAddress.getCellAddress().Column 319 // + ", row " + xCellAddress.getCellAddress().Row 320 // + " contains " + xCell.getFormula()); 321 // } 322 323 // } 324 // catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1 325 // xRemoteContext = null; 326 // throw e; 327 // } 328 // } 329 // } 330