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