1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski // __________ Imports __________ 25*b1cdbd2cSJim Jagielski 26*b1cdbd2cSJim Jagielski import java.util.Random; 27*b1cdbd2cSJim Jagielski 28*b1cdbd2cSJim Jagielski // base classes 29*b1cdbd2cSJim Jagielski import com.sun.star.uno.XInterface; 30*b1cdbd2cSJim Jagielski import com.sun.star.uno.UnoRuntime; 31*b1cdbd2cSJim Jagielski import com.sun.star.uno.XComponentContext; 32*b1cdbd2cSJim Jagielski import com.sun.star.lang.*; 33*b1cdbd2cSJim Jagielski 34*b1cdbd2cSJim Jagielski // factory for creating components 35*b1cdbd2cSJim Jagielski import com.sun.star.comp.servicemanager.ServiceManager; 36*b1cdbd2cSJim Jagielski import com.sun.star.lang.XMultiServiceFactory; 37*b1cdbd2cSJim Jagielski import com.sun.star.bridge.XUnoUrlResolver; 38*b1cdbd2cSJim Jagielski import com.sun.star.uno.XNamingService; 39*b1cdbd2cSJim Jagielski import com.sun.star.frame.XDesktop; 40*b1cdbd2cSJim Jagielski import com.sun.star.frame.XComponentLoader; 41*b1cdbd2cSJim Jagielski 42*b1cdbd2cSJim Jagielski // property access 43*b1cdbd2cSJim Jagielski import com.sun.star.beans.*; 44*b1cdbd2cSJim Jagielski 45*b1cdbd2cSJim Jagielski // container access 46*b1cdbd2cSJim Jagielski import com.sun.star.container.*; 47*b1cdbd2cSJim Jagielski 48*b1cdbd2cSJim Jagielski // application specific classes 49*b1cdbd2cSJim Jagielski import com.sun.star.sheet.XSpreadsheetDocument; 50*b1cdbd2cSJim Jagielski import com.sun.star.text.XTextDocument; 51*b1cdbd2cSJim Jagielski 52*b1cdbd2cSJim Jagielski import com.sun.star.document.XEmbeddedObjectSupplier; 53*b1cdbd2cSJim Jagielski import com.sun.star.frame.XModel; 54*b1cdbd2cSJim Jagielski import com.sun.star.frame.XController; 55*b1cdbd2cSJim Jagielski 56*b1cdbd2cSJim Jagielski // Exceptions 57*b1cdbd2cSJim Jagielski import com.sun.star.uno.RuntimeException; 58*b1cdbd2cSJim Jagielski import com.sun.star.container.NoSuchElementException; 59*b1cdbd2cSJim Jagielski import com.sun.star.beans.UnknownPropertyException; 60*b1cdbd2cSJim Jagielski import com.sun.star.lang.IndexOutOfBoundsException; 61*b1cdbd2cSJim Jagielski 62*b1cdbd2cSJim Jagielski // __________ Implementation __________ 63*b1cdbd2cSJim Jagielski 64*b1cdbd2cSJim Jagielski /** Helper for creating a calc document adding cell values and charts 65*b1cdbd2cSJim Jagielski @author Björn Milcke 66*b1cdbd2cSJim Jagielski */ 67*b1cdbd2cSJim Jagielski public class Helper 68*b1cdbd2cSJim Jagielski { Helper( String[] args )69*b1cdbd2cSJim Jagielski public Helper( String[] args ) 70*b1cdbd2cSJim Jagielski { 71*b1cdbd2cSJim Jagielski // connect to a running office and get the ServiceManager 72*b1cdbd2cSJim Jagielski try { 73*b1cdbd2cSJim Jagielski // get the remote office component context 74*b1cdbd2cSJim Jagielski maContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 75*b1cdbd2cSJim Jagielski System.out.println("Connected to a running office ..."); 76*b1cdbd2cSJim Jagielski 77*b1cdbd2cSJim Jagielski // get the remote office service manager 78*b1cdbd2cSJim Jagielski maMCFactory = maContext.getServiceManager(); 79*b1cdbd2cSJim Jagielski } 80*b1cdbd2cSJim Jagielski catch( Exception e) { 81*b1cdbd2cSJim Jagielski System.out.println( "Couldn't get ServiceManager: " + e ); 82*b1cdbd2cSJim Jagielski e.printStackTrace(); 83*b1cdbd2cSJim Jagielski System.exit(1); 84*b1cdbd2cSJim Jagielski } 85*b1cdbd2cSJim Jagielski } 86*b1cdbd2cSJim Jagielski 87*b1cdbd2cSJim Jagielski // ____________________ 88*b1cdbd2cSJim Jagielski createSpreadsheetDocument()89*b1cdbd2cSJim Jagielski public XSpreadsheetDocument createSpreadsheetDocument() 90*b1cdbd2cSJim Jagielski { 91*b1cdbd2cSJim Jagielski return (XSpreadsheetDocument) UnoRuntime.queryInterface( 92*b1cdbd2cSJim Jagielski XSpreadsheetDocument.class, createDocument( "scalc" )); 93*b1cdbd2cSJim Jagielski } 94*b1cdbd2cSJim Jagielski 95*b1cdbd2cSJim Jagielski // ____________________ 96*b1cdbd2cSJim Jagielski createPresentationDocument()97*b1cdbd2cSJim Jagielski public XModel createPresentationDocument() 98*b1cdbd2cSJim Jagielski { 99*b1cdbd2cSJim Jagielski return createDocument( "simpress" ); 100*b1cdbd2cSJim Jagielski } 101*b1cdbd2cSJim Jagielski 102*b1cdbd2cSJim Jagielski // ____________________ 103*b1cdbd2cSJim Jagielski createDrawingDocument()104*b1cdbd2cSJim Jagielski public XModel createDrawingDocument() 105*b1cdbd2cSJim Jagielski { 106*b1cdbd2cSJim Jagielski return createDocument( "sdraw" ); 107*b1cdbd2cSJim Jagielski } 108*b1cdbd2cSJim Jagielski 109*b1cdbd2cSJim Jagielski // ____________________ 110*b1cdbd2cSJim Jagielski createTextDocument()111*b1cdbd2cSJim Jagielski public XModel createTextDocument() 112*b1cdbd2cSJim Jagielski { 113*b1cdbd2cSJim Jagielski return createDocument( "swriter" ); 114*b1cdbd2cSJim Jagielski } 115*b1cdbd2cSJim Jagielski 116*b1cdbd2cSJim Jagielski // ____________________ 117*b1cdbd2cSJim Jagielski createDocument( String sDocType )118*b1cdbd2cSJim Jagielski public XModel createDocument( String sDocType ) 119*b1cdbd2cSJim Jagielski { 120*b1cdbd2cSJim Jagielski XModel aResult = null; 121*b1cdbd2cSJim Jagielski try 122*b1cdbd2cSJim Jagielski { 123*b1cdbd2cSJim Jagielski XComponentLoader aLoader = (XComponentLoader) 124*b1cdbd2cSJim Jagielski UnoRuntime.queryInterface(XComponentLoader.class, 125*b1cdbd2cSJim Jagielski maMCFactory.createInstanceWithContext("com.sun.star.frame.Desktop", 126*b1cdbd2cSJim Jagielski maContext) ); 127*b1cdbd2cSJim Jagielski 128*b1cdbd2cSJim Jagielski aResult = (XModel) UnoRuntime.queryInterface( 129*b1cdbd2cSJim Jagielski XModel.class, 130*b1cdbd2cSJim Jagielski aLoader.loadComponentFromURL( "private:factory/" + sDocType, 131*b1cdbd2cSJim Jagielski "_blank", 132*b1cdbd2cSJim Jagielski 0, 133*b1cdbd2cSJim Jagielski new PropertyValue[ 0 ] ) ); 134*b1cdbd2cSJim Jagielski } 135*b1cdbd2cSJim Jagielski catch( Exception e ) 136*b1cdbd2cSJim Jagielski { 137*b1cdbd2cSJim Jagielski System.err.println("Couldn't create Document of type "+ sDocType +": "+e); 138*b1cdbd2cSJim Jagielski e.printStackTrace(); 139*b1cdbd2cSJim Jagielski System.exit( 0 ); 140*b1cdbd2cSJim Jagielski } 141*b1cdbd2cSJim Jagielski 142*b1cdbd2cSJim Jagielski return aResult; 143*b1cdbd2cSJim Jagielski } 144*b1cdbd2cSJim Jagielski getComponentContext()145*b1cdbd2cSJim Jagielski public XComponentContext getComponentContext(){ 146*b1cdbd2cSJim Jagielski return maContext; 147*b1cdbd2cSJim Jagielski 148*b1cdbd2cSJim Jagielski } 149*b1cdbd2cSJim Jagielski 150*b1cdbd2cSJim Jagielski // __________ private members and methods __________ 151*b1cdbd2cSJim Jagielski 152*b1cdbd2cSJim Jagielski private final String msDataSheetName = "Data"; 153*b1cdbd2cSJim Jagielski private final String msChartSheetName = "Chart"; 154*b1cdbd2cSJim Jagielski private final String msChartName = "SampleChart"; 155*b1cdbd2cSJim Jagielski 156*b1cdbd2cSJim Jagielski private XComponentContext maContext; 157*b1cdbd2cSJim Jagielski private XMultiComponentFactory maMCFactory; 158*b1cdbd2cSJim Jagielski private XSpreadsheetDocument maSpreadSheetDoc; 159*b1cdbd2cSJim Jagielski } 160