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