Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / Components / dialogcomponent / DialogComponent.java
blobbea5e95a0e1393dcb68bd15d284eb5b0b55facd4
1 /*************************************************************************
3 * $RCSfile: DialogComponent.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2008-07-11 14:20:17 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.uno.Type;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XComponentContext;
44 import com.sun.star.registry.XRegistryKey;
45 import com.sun.star.lang.XTypeProvider;
46 import com.sun.star.lang.XServiceInfo;
47 import com.sun.star.lang.XSingleServiceFactory;
48 import com.sun.star.lang.XMultiServiceFactory;
49 import com.sun.star.lang.XSingleComponentFactory;
50 import com.sun.star.lang.XMultiComponentFactory;
51 import com.sun.star.lib.uno.helper.Factory;
52 import com.sun.star.comp.loader.FactoryHelper;
53 import com.sun.star.awt.XDialog;
54 import com.sun.star.awt.XDialogProvider2;
55 import com.sun.star.awt.XDialogEventHandler;
56 import com.sun.star.awt.XControl;
57 import com.sun.star.awt.XControlModel;
58 import com.sun.star.awt.XControlContainer;
59 import com.sun.star.beans.XPropertySet;
60 import com.sun.star.frame.XModel;
61 import com.sun.star.frame.XFrame;
63 import com.sun.star.awt.XToolkit;
64 import com.sun.star.awt.XWindowPeer;
65 import com.sun.star.awt.XMessageBox;
66 import com.sun.star.awt.WindowAttribute;
67 import com.sun.star.awt.WindowClass;
68 import com.sun.star.awt.WindowDescriptor;
69 import com.sun.star.awt.Rectangle;
71 import com.sun.star.test.XTestDialogHandler;
73 // DialogComponent implements all necessary interfaces self, this is only
74 // for demonstration. More convenient is to use the impelmentation WeakBase or
75 // ComponentBase, see implementation of TestComponentA.
76 public class DialogComponent {
78 // public static class _DialogComponent extends WeakBase
79 public static class _DialogComponent
80 implements XTypeProvider, XServiceInfo, XTestDialogHandler, XDialogEventHandler {
82 static final String __serviceName= "com.sun.star.test.TestDialogHandler";
84 static byte[] _implementationId;
85 private XComponentContext m_xCmpCtx;
87 private XFrame m_xFrame;
88 private XToolkit m_xToolkit;
90 public _DialogComponent(XComponentContext context) {
91 m_xCmpCtx= context;
93 try {
94 // Create the toolkit to have access to it later
95 m_xToolkit = (XToolkit) UnoRuntime.queryInterface(
96 XToolkit.class,
97 m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
98 m_xCmpCtx));
100 catch (Exception e) {
101 e.printStackTrace();
105 // XTestDialogHandler
106 public String createDialog( String DialogURL, XModel xModel, XFrame xFrame ) {
107 m_xFrame = xFrame;
109 try {
110 XMultiComponentFactory xMCF = m_xCmpCtx.getServiceManager();
111 Object obj;
113 // If valid we must pass the XModel when creating a DialogProvider object
114 if( xModel != null ) {
115 Object[] args = new Object[1];
116 args[0] = xModel;
118 obj = xMCF.createInstanceWithArgumentsAndContext(
119 "com.sun.star.awt.DialogProvider2", args, m_xCmpCtx );
121 else {
122 obj = xMCF.createInstanceWithContext(
123 "com.sun.star.awt.DialogProvider2", m_xCmpCtx );
126 XDialogProvider2 xDialogProvider = (XDialogProvider2)
127 UnoRuntime.queryInterface( XDialogProvider2.class, obj );
129 XDialog xDialog = xDialogProvider.createDialogWithHandler( DialogURL, this );
130 if( xDialog != null )
131 xDialog.execute();
133 catch (Exception e) {
134 e.printStackTrace();
136 return "Created dialog \"" + DialogURL + "\"";
139 public void copyText( XDialog xDialog, Object aEventObject ) {
140 XControlContainer xControlContainer = (XControlContainer)UnoRuntime.queryInterface(
141 XControlContainer.class, xDialog );
142 String aTextPropertyStr = "Text";
143 String aText = "";
144 XControl xTextField1Control = xControlContainer.getControl( "TextField1" );
145 XControlModel xControlModel1 = xTextField1Control.getModel();
146 XPropertySet xPropertySet1 = (XPropertySet)UnoRuntime.queryInterface(
147 XPropertySet.class, xControlModel1 );
150 aText = (String)xPropertySet1.getPropertyValue( aTextPropertyStr );
152 catch (Exception e) {
153 e.printStackTrace();
156 XControl xTextField2Control = xControlContainer.getControl( "TextField2" );
157 XControlModel xControlModel2 = xTextField2Control.getModel();
158 XPropertySet xPropertySet2 = (XPropertySet)UnoRuntime.queryInterface(
159 XPropertySet.class, xControlModel2 );
162 xPropertySet2.setPropertyValue( aTextPropertyStr, aText );
164 catch (Exception e) {
165 e.printStackTrace();
168 showMessageBox( "DialogComponent", "copyText() called" );
171 public void handleEvent() {
172 showMessageBox( "DialogComponent", "handleEvent() called" );
175 public void handleEventWithArguments( XDialog xDialog, Object aEventObject ) {
176 showMessageBox( "DialogComponent", "handleEventWithArguments() called\n\n" +
177 "Event Object = " + aEventObject );
180 private final String aHandlerMethod1 = "doit1";
181 private final String aHandlerMethod2 = "doit2";
182 private final String aHandlerMethod3 = "doit3";
184 //XDialogEventHandler
185 public boolean callHandlerMethod( /*IN*/XDialog xDialog, /*IN*/Object EventObject, /*IN*/String MethodName ) {
186 if ( MethodName.equals( aHandlerMethod1 ) )
188 showMessageBox( "DialogComponent", "callHandlerMethod() handled \"" + aHandlerMethod1 + "\"" );
189 return true;
191 else if ( MethodName.equals( aHandlerMethod2 ) )
193 showMessageBox( "DialogComponent", "callHandlerMethod() handled \"" + aHandlerMethod2 + "\"" );
194 return true;
196 else if ( MethodName.equals( aHandlerMethod3 ) )
198 showMessageBox( "DialogComponent", "callHandlerMethod() handled \"" + aHandlerMethod3 + "\"" );
199 return true;
201 return false;
204 public String[] getSupportedMethodNames() {
205 String[] retValue= new String[3];
206 retValue[0]= aHandlerMethod1;
207 retValue[1]= aHandlerMethod2;
208 retValue[2]= aHandlerMethod3;
209 return retValue;
213 //XTypeProvider
214 public com.sun.star.uno.Type[] getTypes( ) {
215 Type[] retValue= new Type[4];
216 retValue[0]= new Type( XServiceInfo.class);
217 retValue[1]= new Type( XTypeProvider.class);
218 retValue[2]= new Type( XTestDialogHandler.class);
219 retValue[3]= new Type( XDialogEventHandler.class);
220 return retValue;
222 //XTypeProvider
223 synchronized public byte[] getImplementationId( ) {
224 if (_implementationId == null) {
225 _implementationId= new byte[16];
226 int hash = hashCode();
227 _implementationId[0] = (byte)(hash & 0xff);
228 _implementationId[1] = (byte)((hash >>> 8) & 0xff);
229 _implementationId[2] = (byte)((hash >>> 16) & 0xff);
230 _implementationId[3] = (byte)((hash >>>24) & 0xff);
232 return _implementationId;
237 /** This method is a simple helper function to used in the
238 * static component initialisation functions as well as in
239 * getSupportedServiceNames.
241 public static String[] getServiceNames() {
242 String[] sSupportedServiceNames = { __serviceName };
243 return sSupportedServiceNames;
246 //XServiceInfo
247 public String[] getSupportedServiceNames() {
248 return getServiceNames();
251 //XServiceInfo
252 public boolean supportsService( String sServiceName ) {
253 return sServiceName.equals( __serviceName );
256 //XServiceInfo
257 public String getImplementationName() {
258 // return DialogComponent.class.getName();
259 return _DialogComponent.class.getName();
262 public void showMessageBox(String sTitle, String sMessage) {
263 try {
264 if ( null != m_xFrame && null != m_xToolkit ) {
266 // describe window properties.
267 WindowDescriptor aDescriptor = new WindowDescriptor();
268 aDescriptor.Type = WindowClass.MODALTOP;
269 aDescriptor.WindowServiceName = new String( "infobox" );
270 aDescriptor.ParentIndex = -1;
271 aDescriptor.Parent = (XWindowPeer)UnoRuntime.queryInterface(
272 XWindowPeer.class, m_xFrame.getContainerWindow());
273 aDescriptor.Bounds = new Rectangle(0,0,300,200);
274 aDescriptor.WindowAttributes = WindowAttribute.BORDER |
275 WindowAttribute.MOVEABLE |
276 WindowAttribute.CLOSEABLE;
278 XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor );
279 if ( null != xPeer ) {
280 XMessageBox xMsgBox = (XMessageBox)UnoRuntime.queryInterface(
281 XMessageBox.class, xPeer);
282 if ( null != xMsgBox )
284 xMsgBox.setCaptionText( sTitle );
285 xMsgBox.setMessageText( sMessage );
286 xMsgBox.execute();
290 } catch ( com.sun.star.uno.Exception e) {
291 // do your error handling
297 * Gives a factory for creating the service.
298 * This method is called by the <code>JavaLoader</code>
299 * <p>
300 * @return returns a <code>XSingleComponentFactory</code> for creating
301 * the component
302 * @param sImplName the name of the implementation for which a
303 * service is desired
304 * @see com.sun.star.comp.loader.JavaLoader
306 public static XSingleComponentFactory __getComponentFactory(String sImplName)
308 XSingleComponentFactory xFactory = null;
310 if ( sImplName.equals( _DialogComponent.class.getName() ) )
311 xFactory = Factory.createComponentFactory(_DialogComponent.class,
312 _DialogComponent.getServiceNames());
314 return xFactory;
318 * Writes the service information into the given registry key.
319 * This method is called by the <code>JavaLoader</code>
320 * <p>
321 * @return returns true if the operation succeeded
322 * @param regKey the registryKey
323 * @see com.sun.star.comp.loader.JavaLoader
325 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
326 return Factory.writeRegistryServiceInfo(_DialogComponent.class.getName(),
327 _DialogComponent.getServiceNames(),
328 regKey);