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 24cdf0e10cSrcweir // __________ Imports __________ 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.util.Random; 27cdf0e10cSrcweir 28cdf0e10cSrcweir // base classes 29cdf0e10cSrcweir import com.sun.star.uno.XInterface; 30cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 31cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 32cdf0e10cSrcweir import com.sun.star.lang.*; 33cdf0e10cSrcweir 34cdf0e10cSrcweir // factory for creating components 35cdf0e10cSrcweir import com.sun.star.comp.servicemanager.ServiceManager; 36cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 37cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 38cdf0e10cSrcweir import com.sun.star.uno.XNamingService; 39cdf0e10cSrcweir import com.sun.star.frame.XDesktop; 40cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 41cdf0e10cSrcweir 42cdf0e10cSrcweir // property access 43cdf0e10cSrcweir import com.sun.star.beans.*; 44cdf0e10cSrcweir 45cdf0e10cSrcweir // container access 46cdf0e10cSrcweir import com.sun.star.container.*; 47cdf0e10cSrcweir 48cdf0e10cSrcweir // application specific classes 49cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 50cdf0e10cSrcweir import com.sun.star.text.XTextDocument; 51cdf0e10cSrcweir 52cdf0e10cSrcweir import com.sun.star.document.XEmbeddedObjectSupplier; 53cdf0e10cSrcweir import com.sun.star.frame.XModel; 54cdf0e10cSrcweir import com.sun.star.frame.XController; 55cdf0e10cSrcweir 56cdf0e10cSrcweir // Exceptions 57cdf0e10cSrcweir import com.sun.star.uno.RuntimeException; 58cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException; 59cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException; 60cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException; 61cdf0e10cSrcweir 62cdf0e10cSrcweir // __________ Implementation __________ 63cdf0e10cSrcweir 64cdf0e10cSrcweir /** Helper for creating a calc document adding cell values and charts 65cdf0e10cSrcweir @author Björn Milcke 66cdf0e10cSrcweir */ 67cdf0e10cSrcweir public class Helper 68cdf0e10cSrcweir { Helper( String[] args )69cdf0e10cSrcweir public Helper( String[] args ) 70cdf0e10cSrcweir { 71cdf0e10cSrcweir // connect to a running office and get the ServiceManager 72cdf0e10cSrcweir try { 73cdf0e10cSrcweir // get the remote office component context 74cdf0e10cSrcweir maContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 75cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 76cdf0e10cSrcweir 77cdf0e10cSrcweir // get the remote office service manager 78cdf0e10cSrcweir maMCFactory = maContext.getServiceManager(); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir catch( Exception e) { 81cdf0e10cSrcweir System.out.println( "Couldn't get ServiceManager: " + e ); 82cdf0e10cSrcweir e.printStackTrace(); 83cdf0e10cSrcweir System.exit(1); 84cdf0e10cSrcweir } 85cdf0e10cSrcweir } 86cdf0e10cSrcweir 87cdf0e10cSrcweir // ____________________ 88cdf0e10cSrcweir createSpreadsheetDocument()89cdf0e10cSrcweir public XSpreadsheetDocument createSpreadsheetDocument() 90cdf0e10cSrcweir { 91cdf0e10cSrcweir return (XSpreadsheetDocument) UnoRuntime.queryInterface( 92cdf0e10cSrcweir XSpreadsheetDocument.class, createDocument( "scalc" )); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir // ____________________ 96cdf0e10cSrcweir createPresentationDocument()97cdf0e10cSrcweir public XModel createPresentationDocument() 98cdf0e10cSrcweir { 99cdf0e10cSrcweir return createDocument( "simpress" ); 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir // ____________________ 103cdf0e10cSrcweir createDrawingDocument()104cdf0e10cSrcweir public XModel createDrawingDocument() 105cdf0e10cSrcweir { 106cdf0e10cSrcweir return createDocument( "sdraw" ); 107cdf0e10cSrcweir } 108cdf0e10cSrcweir 109cdf0e10cSrcweir // ____________________ 110cdf0e10cSrcweir createTextDocument()111cdf0e10cSrcweir public XModel createTextDocument() 112cdf0e10cSrcweir { 113cdf0e10cSrcweir return createDocument( "swriter" ); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir // ____________________ 117cdf0e10cSrcweir createDocument( String sDocType )118cdf0e10cSrcweir public XModel createDocument( String sDocType ) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir XModel aResult = null; 121cdf0e10cSrcweir try 122cdf0e10cSrcweir { 123cdf0e10cSrcweir XComponentLoader aLoader = (XComponentLoader) 124cdf0e10cSrcweir UnoRuntime.queryInterface(XComponentLoader.class, 125cdf0e10cSrcweir maMCFactory.createInstanceWithContext("com.sun.star.frame.Desktop", 126cdf0e10cSrcweir maContext) ); 127cdf0e10cSrcweir 128cdf0e10cSrcweir aResult = (XModel) UnoRuntime.queryInterface( 129cdf0e10cSrcweir XModel.class, 130cdf0e10cSrcweir aLoader.loadComponentFromURL( "private:factory/" + sDocType, 131cdf0e10cSrcweir "_blank", 132cdf0e10cSrcweir 0, 133cdf0e10cSrcweir new PropertyValue[ 0 ] ) ); 134cdf0e10cSrcweir } 135cdf0e10cSrcweir catch( Exception e ) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir System.err.println("Couldn't create Document of type "+ sDocType +": "+e); 138cdf0e10cSrcweir e.printStackTrace(); 139cdf0e10cSrcweir System.exit( 0 ); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir 142cdf0e10cSrcweir return aResult; 143cdf0e10cSrcweir } 144cdf0e10cSrcweir getComponentContext()145cdf0e10cSrcweir public XComponentContext getComponentContext(){ 146cdf0e10cSrcweir return maContext; 147cdf0e10cSrcweir 148cdf0e10cSrcweir } 149cdf0e10cSrcweir 150cdf0e10cSrcweir // __________ private members and methods __________ 151cdf0e10cSrcweir 152cdf0e10cSrcweir private final String msDataSheetName = "Data"; 153cdf0e10cSrcweir private final String msChartSheetName = "Chart"; 154cdf0e10cSrcweir private final String msChartName = "SampleChart"; 155cdf0e10cSrcweir 156cdf0e10cSrcweir private XComponentContext maContext; 157cdf0e10cSrcweir private XMultiComponentFactory maMCFactory; 158cdf0e10cSrcweir private XSpreadsheetDocument maSpreadSheetDoc; 159cdf0e10cSrcweir } 160