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