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