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