1*ae15d43aSAndrew Rist /**************************************************************
2*ae15d43aSAndrew Rist  *
3*ae15d43aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ae15d43aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ae15d43aSAndrew Rist  * distributed with this work for additional information
6*ae15d43aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ae15d43aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ae15d43aSAndrew Rist  * "License"); you may not use this file except in compliance
9*ae15d43aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ae15d43aSAndrew Rist  *
11*ae15d43aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ae15d43aSAndrew Rist  *
13*ae15d43aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ae15d43aSAndrew Rist  * software distributed under the License is distributed on an
15*ae15d43aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ae15d43aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ae15d43aSAndrew Rist  * specific language governing permissions and limitations
18*ae15d43aSAndrew Rist  * under the License.
19*ae15d43aSAndrew Rist  *
20*ae15d43aSAndrew Rist  *************************************************************/
21*ae15d43aSAndrew Rist 
22cdf0e10cSrcweir import java.awt.*;
23cdf0e10cSrcweir import java.awt.event.*;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.comp.servicemanager.ServiceManager;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
29cdf0e10cSrcweir import com.sun.star.connection.XConnector;
30cdf0e10cSrcweir import com.sun.star.connection.XConnection;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir import com.sun.star.uno.XInterface;
35cdf0e10cSrcweir import com.sun.star.uno.XNamingService;
36cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import com.sun.star.container.*;
39cdf0e10cSrcweir import com.sun.star.beans.*;
40cdf0e10cSrcweir import com.sun.star.lang.*;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
43cdf0e10cSrcweir public class EmbedContFrame extends Frame
44cdf0e10cSrcweir {
45cdf0e10cSrcweir 	WindowListener m_aCloser = new WindowAdapter()
46cdf0e10cSrcweir 	{
47cdf0e10cSrcweir 		public void windowClosing( WindowEvent e )
48cdf0e10cSrcweir 		{
49cdf0e10cSrcweir 			dispose();
50cdf0e10cSrcweir 			System.exit( 0 );
51cdf0e10cSrcweir 		}
52cdf0e10cSrcweir 	};
53cdf0e10cSrcweir 
EmbedContFrame( String sName )54cdf0e10cSrcweir 	public EmbedContFrame( String sName )
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 		super( sName );
57cdf0e10cSrcweir 		addWindowListener( m_aCloser );
58cdf0e10cSrcweir 	}
59cdf0e10cSrcweir 
start()60cdf0e10cSrcweir 	public static void start()
61cdf0e10cSrcweir 	{
62cdf0e10cSrcweir 		EmbedContFrame aFrame = new EmbedContFrame( "Testing container." );
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 		// connect to the office
65cdf0e10cSrcweir 		XMultiServiceFactory aServiceFactory = null;
66cdf0e10cSrcweir 		try {
67cdf0e10cSrcweir 			aServiceFactory = connectOfficeGetServiceFactory();
68cdf0e10cSrcweir 		}
69cdf0e10cSrcweir 		catch( Exception e )
70cdf0e10cSrcweir 		{}
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 		if ( aServiceFactory == null )
73cdf0e10cSrcweir 		{
74cdf0e10cSrcweir 			System.out.println( "Can't get service manager!\n" );
75cdf0e10cSrcweir 			System.exit( 1 );
76cdf0e10cSrcweir 		}
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 		EmbedContApp aApp = new EmbedContApp( aFrame, aServiceFactory );
79cdf0e10cSrcweir 		aApp.init();
80cdf0e10cSrcweir 		aApp.start();
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 		Dimension aSize = aApp.getSize();
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 		aFrame.add( "Center", aApp );
85cdf0e10cSrcweir 		aFrame.pack();
86cdf0e10cSrcweir 		aFrame.setSize( aSize );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 		aFrame.setVisible( true );
89cdf0e10cSrcweir 	}
90cdf0e10cSrcweir 
main( String args[] )91cdf0e10cSrcweir 	public static void main( String args[] )
92cdf0e10cSrcweir 	{
93cdf0e10cSrcweir 		EmbedContFrame.start();
94cdf0e10cSrcweir 	}
95cdf0e10cSrcweir 
connectOfficeGetServiceFactory()96cdf0e10cSrcweir 	public static XMultiServiceFactory connectOfficeGetServiceFactory()
97cdf0e10cSrcweir     throws com.sun.star.uno.Exception,
98cdf0e10cSrcweir     com.sun.star.uno.RuntimeException,
99cdf0e10cSrcweir 	Exception
100cdf0e10cSrcweir 	{
101cdf0e10cSrcweir         String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.NamingService";
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	    // Get component context
104cdf0e10cSrcweir         XComponentContext xComponentContext =
105cdf0e10cSrcweir         	com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         // initial serviceManager
108cdf0e10cSrcweir         XMultiComponentFactory xLocalServiceManager = xComponentContext.getServiceManager();
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         // create a connector, so that it can contact the office
111cdf0e10cSrcweir         Object  oUrlResolver  = xLocalServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver",
112cdf0e10cSrcweir 																				xComponentContext );
113cdf0e10cSrcweir         XUnoUrlResolver xUrlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface( XUnoUrlResolver.class, oUrlResolver );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         Object oInitialObject = xUrlResolver.resolve( sConnectionString );
116cdf0e10cSrcweir         XNamingService xName = (XNamingService)UnoRuntime.queryInterface( XNamingService.class, oInitialObject );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         XMultiServiceFactory xMSF = null;
119cdf0e10cSrcweir         if( xName != null ) {
120cdf0e10cSrcweir             Object oMSF = xName.getRegisteredObject( "StarOffice.ServiceManager" );
121cdf0e10cSrcweir             xMSF = (XMultiServiceFactory)UnoRuntime.queryInterface( XMultiServiceFactory.class, oMSF );
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir 		else
124cdf0e10cSrcweir 			System.out.println( "Error: Can't get XNamingService interface from url resolver!" );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         return xMSF;
127cdf0e10cSrcweir 	}
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130