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 com.sun.star.uno.UnoRuntime; 27cdf0e10cSrcweir 28cdf0e10cSrcweir public class Helper 29cdf0e10cSrcweir { 30cdf0e10cSrcweir // __________ static helper methods __________ 31cdf0e10cSrcweir 32cdf0e10cSrcweir /** Connect to an office, if no office is running a new instance is started. 33cdf0e10cSrcweir * A new connection is established and the service manger from the running 34cdf0e10cSrcweir * offic eis returned. 35cdf0e10cSrcweir */ connect()36cdf0e10cSrcweir static public com.sun.star.uno.XComponentContext connect() 37cdf0e10cSrcweir throws Exception 38cdf0e10cSrcweir { 39cdf0e10cSrcweir // get the remote office component context 40cdf0e10cSrcweir com.sun.star.uno.XComponentContext xOfficeContext = 41cdf0e10cSrcweir com.sun.star.comp.helper.Bootstrap.bootstrap(); 42cdf0e10cSrcweir 43cdf0e10cSrcweir // if connection fails an exception is thrown 44cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 45cdf0e10cSrcweir 46cdf0e10cSrcweir return xOfficeContext; 47cdf0e10cSrcweir } 48cdf0e10cSrcweir 49cdf0e10cSrcweir /** creates and instantiates new document 50cdf0e10cSrcweir */ createDocument( com.sun.star.uno.XComponentContext xOfficeContext, String sURL, String sTargetFrame, int nSearchFlags, com.sun.star.beans.PropertyValue[] aArgs )51cdf0e10cSrcweir static public com.sun.star.lang.XComponent createDocument( 52cdf0e10cSrcweir com.sun.star.uno.XComponentContext xOfficeContext, 53cdf0e10cSrcweir String sURL, String sTargetFrame, int nSearchFlags, 54cdf0e10cSrcweir com.sun.star.beans.PropertyValue[] aArgs ) 55cdf0e10cSrcweir throws Exception 56cdf0e10cSrcweir { 57cdf0e10cSrcweir com.sun.star.lang.XComponent xComponent = null; 58cdf0e10cSrcweir com.sun.star.frame.XComponentLoader aLoader = 59cdf0e10cSrcweir (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface( 60cdf0e10cSrcweir com.sun.star.frame.XComponentLoader.class, 61cdf0e10cSrcweir xOfficeContext.getServiceManager().createInstanceWithContext( 62cdf0e10cSrcweir "com.sun.star.frame.Desktop", xOfficeContext)); 63cdf0e10cSrcweir 64cdf0e10cSrcweir xComponent = (com.sun.star.lang.XComponent)UnoRuntime.queryInterface( 65cdf0e10cSrcweir com.sun.star.lang.XComponent.class, aLoader.loadComponentFromURL( 66cdf0e10cSrcweir sURL, sTargetFrame, nSearchFlags, aArgs ) ); 67cdf0e10cSrcweir 68cdf0e10cSrcweir if ( xComponent == null ) 69cdf0e10cSrcweir throw new Exception( "could not create document: " + sURL ); 70cdf0e10cSrcweir return xComponent; 71cdf0e10cSrcweir } 72cdf0e10cSrcweir } 73