1 import java.awt.*;
2 import java.awt.event.*;
3 
4 import com.sun.star.comp.servicemanager.ServiceManager;
5 
6 import com.sun.star.lang.XMultiServiceFactory;
7 import com.sun.star.lang.XMultiComponentFactory;
8 import com.sun.star.connection.XConnector;
9 import com.sun.star.connection.XConnection;
10 
11 import com.sun.star.bridge.XUnoUrlResolver;
12 import com.sun.star.uno.UnoRuntime;
13 import com.sun.star.uno.XInterface;
14 import com.sun.star.uno.XNamingService;
15 import com.sun.star.uno.XComponentContext;
16 
17 import com.sun.star.container.*;
18 import com.sun.star.beans.*;
19 import com.sun.star.lang.*;
20 
21 
22 public class EmbedContFrame extends Frame
23 {
24 	WindowListener m_aCloser = new WindowAdapter()
25 	{
26 		public void windowClosing( WindowEvent e )
27 		{
28 			dispose();
29 			System.exit( 0 );
30 		}
31 	};
32 
33 	public EmbedContFrame( String sName )
34 	{
35 		super( sName );
36 		addWindowListener( m_aCloser );
37 	}
38 
39 	public static void start()
40 	{
41 		EmbedContFrame aFrame = new EmbedContFrame( "Testing container." );
42 
43 		// connect to the office
44 		XMultiServiceFactory aServiceFactory = null;
45 		try {
46 			aServiceFactory = connectOfficeGetServiceFactory();
47 		}
48 		catch( Exception e )
49 		{}
50 
51 		if ( aServiceFactory == null )
52 		{
53 			System.out.println( "Can't get service manager!\n" );
54 			System.exit( 1 );
55 		}
56 
57 		EmbedContApp aApp = new EmbedContApp( aFrame, aServiceFactory );
58 		aApp.init();
59 		aApp.start();
60 
61 		Dimension aSize = aApp.getSize();
62 
63 		aFrame.add( "Center", aApp );
64 		aFrame.pack();
65 		aFrame.setSize( aSize );
66 
67 		aFrame.setVisible( true );
68 	}
69 
70 	public static void main( String args[] )
71 	{
72 		EmbedContFrame.start();
73 	}
74 
75 	public static XMultiServiceFactory connectOfficeGetServiceFactory()
76     throws com.sun.star.uno.Exception,
77     com.sun.star.uno.RuntimeException,
78 	Exception
79 	{
80         String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.NamingService";
81 
82 	    // Get component context
83         XComponentContext xComponentContext =
84         	com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
85 
86         // initial serviceManager
87         XMultiComponentFactory xLocalServiceManager = xComponentContext.getServiceManager();
88 
89         // create a connector, so that it can contact the office
90         Object  oUrlResolver  = xLocalServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver",
91 																				xComponentContext );
92         XUnoUrlResolver xUrlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface( XUnoUrlResolver.class, oUrlResolver );
93 
94         Object oInitialObject = xUrlResolver.resolve( sConnectionString );
95         XNamingService xName = (XNamingService)UnoRuntime.queryInterface( XNamingService.class, oInitialObject );
96 
97         XMultiServiceFactory xMSF = null;
98         if( xName != null ) {
99             Object oMSF = xName.getRegisteredObject( "StarOffice.ServiceManager" );
100             xMSF = (XMultiServiceFactory)UnoRuntime.queryInterface( XMultiServiceFactory.class, oMSF );
101         }
102 		else
103 			System.out.println( "Error: Can't get XNamingService interface from url resolver!" );
104 
105         return xMSF;
106 	}
107 }
108 
109