1*cdf0e10cSrcweir // base classes 2*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 3*cdf0e10cSrcweir 4*cdf0e10cSrcweir // factory for creating components 5*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 6*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 7*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 8*cdf0e10cSrcweir import com.sun.star.frame.XDesktop; 9*cdf0e10cSrcweir import com.sun.star.frame.XModel; 10*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 11*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 12*cdf0e10cSrcweir 13*cdf0e10cSrcweir // Exceptions 14*cdf0e10cSrcweir import com.sun.star.uno.RuntimeException; 15*cdf0e10cSrcweir 16*cdf0e10cSrcweir 17*cdf0e10cSrcweir /** @descr This class establishes a connection to a StarOffice application. 18*cdf0e10cSrcweir */ 19*cdf0e10cSrcweir public class OfficeConnection 20*cdf0e10cSrcweir { 21*cdf0e10cSrcweir public OfficeConnection (int nPortNumber) 22*cdf0e10cSrcweir { 23*cdf0e10cSrcweir mnDefaultPort = nPortNumber; 24*cdf0e10cSrcweir connect (); 25*cdf0e10cSrcweir } 26*cdf0e10cSrcweir 27*cdf0e10cSrcweir /** @descr Return the service manager that represents the connected 28*cdf0e10cSrcweir StarOffice application 29*cdf0e10cSrcweir */ 30*cdf0e10cSrcweir public XMultiServiceFactory getServiceManager () 31*cdf0e10cSrcweir { 32*cdf0e10cSrcweir if ( ! mbInitialized) 33*cdf0e10cSrcweir connect (); 34*cdf0e10cSrcweir return maServiceManager; 35*cdf0e10cSrcweir } 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir /** @descr Return a flag that indicates if the constructor has been able to 38*cdf0e10cSrcweir establish a valid connection. 39*cdf0e10cSrcweir */ 40*cdf0e10cSrcweir public boolean connectionIsValid () 41*cdf0e10cSrcweir { 42*cdf0e10cSrcweir return getServiceManager() != null; 43*cdf0e10cSrcweir } 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir /** @descr Connect to a already running StarOffice application. 46*cdf0e10cSrcweir */ 47*cdf0e10cSrcweir private void connect () 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir connect (msDefaultHost, mnDefaultPort); 50*cdf0e10cSrcweir } 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir private void connect (String hostname) 53*cdf0e10cSrcweir { 54*cdf0e10cSrcweir connect (hostname, mnDefaultPort); 55*cdf0e10cSrcweir } 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir /** @descr Connect to a already running StarOffice application that has 58*cdf0e10cSrcweir been started with a command line argument like 59*cdf0e10cSrcweir "-accept=socket,host=localhost,port=5678;urp;" 60*cdf0e10cSrcweir */ 61*cdf0e10cSrcweir private void connect (String hostname, int portnumber) 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir mbInitialized = true; 64*cdf0e10cSrcweir // Set up connection string. 65*cdf0e10cSrcweir String sConnectString = "uno:socket,host=" + hostname + ",port=" + portnumber 66*cdf0e10cSrcweir + ";urp;StarOffice.ServiceManager"; 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir // connect to a running office and get the ServiceManager 70*cdf0e10cSrcweir try 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir // Create a URL Resolver. 73*cdf0e10cSrcweir XMultiServiceFactory aLocalServiceManager = 74*cdf0e10cSrcweir com.sun.star.comp.helper.Bootstrap.createSimpleServiceManager(); 75*cdf0e10cSrcweir XUnoUrlResolver aURLResolver = (XUnoUrlResolver) UnoRuntime.queryInterface ( 76*cdf0e10cSrcweir XUnoUrlResolver.class, 77*cdf0e10cSrcweir aLocalServiceManager.createInstance ("com.sun.star.bridge.UnoUrlResolver") 78*cdf0e10cSrcweir ); 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir maServiceManager = (XMultiServiceFactory) UnoRuntime.queryInterface ( 81*cdf0e10cSrcweir XMultiServiceFactory.class, 82*cdf0e10cSrcweir aURLResolver.resolve (sConnectString) 83*cdf0e10cSrcweir ); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir catch (Exception e) 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir MessageArea.println ("Could not connect with " + sConnectString + " : " + e); 89*cdf0e10cSrcweir MessageArea.println ("Please start OpenOffice/StarOffice with " 90*cdf0e10cSrcweir + "\"-accept=socket,host=localhost,port=5678;urp;\""); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir private int mnDefaultPort = 5678; 95*cdf0e10cSrcweir private final String msDefaultHost = "localhost"; 96*cdf0e10cSrcweir private XMultiServiceFactory maServiceManager = null; 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir /** A value of true just indicates that it has been tried to establish a connection, 99*cdf0e10cSrcweir not that that has been successfull. 100*cdf0e10cSrcweir */ 101*cdf0e10cSrcweir private boolean mbInitialized = false; 102*cdf0e10cSrcweir } 103