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