xref: /AOO41X/main/odk/examples/DevelopersGuide/Components/dialogcomponent/DialogComponent.java (revision 34dd1e2512dbacb6a9a7e4c7f17b9296daa8eff3)
1*34dd1e25SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.Type;
25cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
26cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
27cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
28cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
29cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
30cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
32cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
34cdf0e10cSrcweir import com.sun.star.lib.uno.helper.Factory;
35cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper;
36cdf0e10cSrcweir import com.sun.star.awt.XDialog;
37cdf0e10cSrcweir import com.sun.star.awt.XDialogProvider2;
38cdf0e10cSrcweir import com.sun.star.awt.XDialogEventHandler;
39cdf0e10cSrcweir import com.sun.star.awt.XControl;
40cdf0e10cSrcweir import com.sun.star.awt.XControlModel;
41cdf0e10cSrcweir import com.sun.star.awt.XControlContainer;
42cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
43cdf0e10cSrcweir import com.sun.star.frame.XModel;
44cdf0e10cSrcweir import com.sun.star.frame.XFrame;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir import com.sun.star.awt.XToolkit;
47cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer;
48cdf0e10cSrcweir import com.sun.star.awt.XMessageBox;
49cdf0e10cSrcweir import com.sun.star.awt.WindowAttribute;
50cdf0e10cSrcweir import com.sun.star.awt.WindowClass;
51cdf0e10cSrcweir import com.sun.star.awt.WindowDescriptor;
52cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir import com.sun.star.test.XTestDialogHandler;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir // DialogComponent implements all necessary interfaces self, this is only
57cdf0e10cSrcweir // for demonstration. More convenient is to use the impelmentation WeakBase or
58cdf0e10cSrcweir // ComponentBase, see implementation of TestComponentA.
59cdf0e10cSrcweir public class DialogComponent {
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     // public static class _DialogComponent extends WeakBase
62cdf0e10cSrcweir 	public static class _DialogComponent
63cdf0e10cSrcweir 		implements XTypeProvider, XServiceInfo, XTestDialogHandler, XDialogEventHandler {
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 		static final String __serviceName= "com.sun.star.test.TestDialogHandler";
66cdf0e10cSrcweir 
67cdf0e10cSrcweir    		static byte[] _implementationId;
68cdf0e10cSrcweir 		private XComponentContext m_xCmpCtx;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 		private XFrame m_xFrame;
71cdf0e10cSrcweir 		private XToolkit m_xToolkit;
72cdf0e10cSrcweir 
_DialogComponent(XComponentContext context)73cdf0e10cSrcweir 		public _DialogComponent(XComponentContext context) {
74cdf0e10cSrcweir 			m_xCmpCtx= context;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 			try {
77cdf0e10cSrcweir 				// Create the toolkit to have access to it later
78cdf0e10cSrcweir 				m_xToolkit = (XToolkit) UnoRuntime.queryInterface(
79cdf0e10cSrcweir 					XToolkit.class,
80cdf0e10cSrcweir 					m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
81cdf0e10cSrcweir 																			m_xCmpCtx));
82cdf0e10cSrcweir 			}
83cdf0e10cSrcweir 			catch (Exception e) {
84cdf0e10cSrcweir 				e.printStackTrace();
85cdf0e10cSrcweir 			}
86cdf0e10cSrcweir 		}
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 		// XTestDialogHandler
createDialog( String DialogURL, XModel xModel, XFrame xFrame )89cdf0e10cSrcweir 		public String createDialog( String DialogURL, XModel xModel, XFrame xFrame ) {
90cdf0e10cSrcweir 			m_xFrame = xFrame;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 			try {
93cdf0e10cSrcweir 				XMultiComponentFactory xMCF = m_xCmpCtx.getServiceManager();
94cdf0e10cSrcweir 				Object obj;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 				// If valid we must pass the XModel when creating a DialogProvider object
97cdf0e10cSrcweir 				if( xModel != null ) {
98cdf0e10cSrcweir 					Object[] args = new Object[1];
99cdf0e10cSrcweir 					args[0] = xModel;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 					obj = xMCF.createInstanceWithArgumentsAndContext(
102cdf0e10cSrcweir 						"com.sun.star.awt.DialogProvider2", args, m_xCmpCtx );
103cdf0e10cSrcweir 				}
104cdf0e10cSrcweir 				else {
105cdf0e10cSrcweir 					obj = xMCF.createInstanceWithContext(
106cdf0e10cSrcweir 						"com.sun.star.awt.DialogProvider2", m_xCmpCtx );
107cdf0e10cSrcweir 				}
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 				XDialogProvider2 xDialogProvider = (XDialogProvider2)
110cdf0e10cSrcweir 					UnoRuntime.queryInterface( XDialogProvider2.class, obj );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 				XDialog xDialog = xDialogProvider.createDialogWithHandler( DialogURL, this );
113cdf0e10cSrcweir 				if( xDialog != null )
114cdf0e10cSrcweir 					xDialog.execute();
115cdf0e10cSrcweir 			}
116cdf0e10cSrcweir 			catch (Exception e) {
117cdf0e10cSrcweir 				e.printStackTrace();
118cdf0e10cSrcweir 			}
119cdf0e10cSrcweir 			return "Created dialog \"" + DialogURL + "\"";
120cdf0e10cSrcweir 		}
121cdf0e10cSrcweir 
copyText( XDialog xDialog, Object aEventObject )122cdf0e10cSrcweir 		public void copyText( XDialog xDialog, Object aEventObject ) {
123cdf0e10cSrcweir 			XControlContainer xControlContainer = (XControlContainer)UnoRuntime.queryInterface(
124cdf0e10cSrcweir 				XControlContainer.class, xDialog );
125cdf0e10cSrcweir 			String aTextPropertyStr = "Text";
126cdf0e10cSrcweir 			String aText = "";
127cdf0e10cSrcweir 			XControl xTextField1Control = xControlContainer.getControl( "TextField1" );
128cdf0e10cSrcweir 			XControlModel xControlModel1 = xTextField1Control.getModel();
129cdf0e10cSrcweir 			XPropertySet xPropertySet1 = (XPropertySet)UnoRuntime.queryInterface(
130cdf0e10cSrcweir 				XPropertySet.class, xControlModel1 );
131cdf0e10cSrcweir 			try
132cdf0e10cSrcweir 			{
133cdf0e10cSrcweir 				aText = (String)xPropertySet1.getPropertyValue( aTextPropertyStr );
134cdf0e10cSrcweir 			}
135cdf0e10cSrcweir 			catch (Exception e) {
136cdf0e10cSrcweir 				e.printStackTrace();
137cdf0e10cSrcweir 			}
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 			XControl xTextField2Control = xControlContainer.getControl( "TextField2" );
140cdf0e10cSrcweir 			XControlModel xControlModel2 = xTextField2Control.getModel();
141cdf0e10cSrcweir 			XPropertySet xPropertySet2 = (XPropertySet)UnoRuntime.queryInterface(
142cdf0e10cSrcweir 				XPropertySet.class, xControlModel2 );
143cdf0e10cSrcweir 			try
144cdf0e10cSrcweir 			{
145cdf0e10cSrcweir 				xPropertySet2.setPropertyValue( aTextPropertyStr, aText );
146cdf0e10cSrcweir 			}
147cdf0e10cSrcweir 			catch (Exception e) {
148cdf0e10cSrcweir 				e.printStackTrace();
149cdf0e10cSrcweir 			}
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 			showMessageBox( "DialogComponent", "copyText() called" );
152cdf0e10cSrcweir 		}
153cdf0e10cSrcweir 
handleEvent()154cdf0e10cSrcweir 		public void handleEvent() {
155cdf0e10cSrcweir 			showMessageBox( "DialogComponent", "handleEvent() called" );
156cdf0e10cSrcweir 		}
157cdf0e10cSrcweir 
handleEventWithArguments( XDialog xDialog, Object aEventObject )158cdf0e10cSrcweir 		public void handleEventWithArguments( XDialog xDialog, Object aEventObject ) {
159cdf0e10cSrcweir 			showMessageBox( "DialogComponent", "handleEventWithArguments() called\n\n" +
160cdf0e10cSrcweir 				"Event Object = " + aEventObject );
161cdf0e10cSrcweir 		}
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 		private final String aHandlerMethod1 = "doit1";
164cdf0e10cSrcweir 		private final String aHandlerMethod2 = "doit2";
165cdf0e10cSrcweir 		private final String aHandlerMethod3 = "doit3";
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 		//XDialogEventHandler
callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName )168cdf0e10cSrcweir 		public boolean callHandlerMethod( /*IN*/XDialog xDialog, /*IN*/Object EventObject, /*IN*/String MethodName ) {
169cdf0e10cSrcweir 			if ( MethodName.equals( aHandlerMethod1 ) )
170cdf0e10cSrcweir 			{
171cdf0e10cSrcweir 				showMessageBox( "DialogComponent", "callHandlerMethod() handled \"" + aHandlerMethod1 + "\"" );
172cdf0e10cSrcweir 				return true;
173cdf0e10cSrcweir 			}
174cdf0e10cSrcweir 			else if ( MethodName.equals( aHandlerMethod2 ) )
175cdf0e10cSrcweir 			{
176cdf0e10cSrcweir 				showMessageBox( "DialogComponent", "callHandlerMethod() handled \"" + aHandlerMethod2 + "\"" );
177cdf0e10cSrcweir 				return true;
178cdf0e10cSrcweir 			}
179cdf0e10cSrcweir 			else if ( MethodName.equals( aHandlerMethod3 ) )
180cdf0e10cSrcweir 			{
181cdf0e10cSrcweir 				showMessageBox( "DialogComponent", "callHandlerMethod() handled \"" + aHandlerMethod3 + "\"" );
182cdf0e10cSrcweir 				return true;
183cdf0e10cSrcweir 			}
184cdf0e10cSrcweir 			return false;
185cdf0e10cSrcweir 		}
186cdf0e10cSrcweir 
getSupportedMethodNames()187cdf0e10cSrcweir 		public String[] getSupportedMethodNames() {
188cdf0e10cSrcweir 			String[] retValue= new String[3];
189cdf0e10cSrcweir 			retValue[0]= aHandlerMethod1;
190cdf0e10cSrcweir 			retValue[1]= aHandlerMethod2;
191cdf0e10cSrcweir 			retValue[2]= aHandlerMethod3;
192cdf0e10cSrcweir 			return retValue;
193cdf0e10cSrcweir 		}
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 		//XTypeProvider
getTypes( )197cdf0e10cSrcweir 		public com.sun.star.uno.Type[] getTypes(  ) {
198cdf0e10cSrcweir 			Type[] retValue= new Type[4];
199cdf0e10cSrcweir 			retValue[0]= new Type( XServiceInfo.class);
200cdf0e10cSrcweir 			retValue[1]= new Type( XTypeProvider.class);
201cdf0e10cSrcweir 			retValue[2]= new Type( XTestDialogHandler.class);
202cdf0e10cSrcweir 			retValue[3]= new Type( XDialogEventHandler.class);
203cdf0e10cSrcweir 			return retValue;
204cdf0e10cSrcweir 		}
205cdf0e10cSrcweir 		//XTypeProvider
getImplementationId( )206cdf0e10cSrcweir 		synchronized public byte[] getImplementationId(  ) {
207cdf0e10cSrcweir 			if (_implementationId == null) {
208cdf0e10cSrcweir 				_implementationId= new byte[16];
209cdf0e10cSrcweir 				int hash = hashCode();
210cdf0e10cSrcweir 				_implementationId[0] = (byte)(hash & 0xff);
211cdf0e10cSrcweir 				_implementationId[1] = (byte)((hash >>> 8) & 0xff);
212cdf0e10cSrcweir 				_implementationId[2] = (byte)((hash >>> 16) & 0xff);
213cdf0e10cSrcweir 				_implementationId[3] = (byte)((hash >>>24) & 0xff);
214cdf0e10cSrcweir 			}
215cdf0e10cSrcweir 			return _implementationId;
216cdf0e10cSrcweir 		}
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         /** This method is a simple helper function to used in the
221cdf0e10cSrcweir          * static component initialisation functions as well as in
222cdf0e10cSrcweir          * getSupportedServiceNames.
223cdf0e10cSrcweir          */
getServiceNames()224cdf0e10cSrcweir         public static String[] getServiceNames() {
225cdf0e10cSrcweir             String[] sSupportedServiceNames = { __serviceName };
226cdf0e10cSrcweir             return sSupportedServiceNames;
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 		//XServiceInfo
getSupportedServiceNames()230cdf0e10cSrcweir         public String[] getSupportedServiceNames() {
231cdf0e10cSrcweir             return getServiceNames();
232cdf0e10cSrcweir         }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 		//XServiceInfo
supportsService( String sServiceName )235cdf0e10cSrcweir         public boolean supportsService( String sServiceName ) {
236cdf0e10cSrcweir             return sServiceName.equals( __serviceName );
237cdf0e10cSrcweir         }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 		//XServiceInfo
getImplementationName()240cdf0e10cSrcweir         public String getImplementationName() {
241cdf0e10cSrcweir             // return  DialogComponent.class.getName();
242cdf0e10cSrcweir             return  _DialogComponent.class.getName();
243cdf0e10cSrcweir         }
244cdf0e10cSrcweir 
showMessageBox(String sTitle, String sMessage)245cdf0e10cSrcweir         public void showMessageBox(String sTitle, String sMessage) {
246cdf0e10cSrcweir 			try {
247cdf0e10cSrcweir 				if ( null != m_xFrame && null != m_xToolkit ) {
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 					// describe window properties.
250cdf0e10cSrcweir 					WindowDescriptor aDescriptor = new WindowDescriptor();
251cdf0e10cSrcweir 					aDescriptor.Type              = WindowClass.MODALTOP;
252cdf0e10cSrcweir 					aDescriptor.WindowServiceName = new String( "infobox" );
253cdf0e10cSrcweir 					aDescriptor.ParentIndex       = -1;
254cdf0e10cSrcweir 					aDescriptor.Parent            = (XWindowPeer)UnoRuntime.queryInterface(
255cdf0e10cSrcweir 						XWindowPeer.class, m_xFrame.getContainerWindow());
256cdf0e10cSrcweir 					aDescriptor.Bounds            = new Rectangle(0,0,300,200);
257cdf0e10cSrcweir 					aDescriptor.WindowAttributes  = WindowAttribute.BORDER |
258cdf0e10cSrcweir 						WindowAttribute.MOVEABLE |
259cdf0e10cSrcweir 						WindowAttribute.CLOSEABLE;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 					XWindowPeer xPeer = m_xToolkit.createWindow( aDescriptor );
262cdf0e10cSrcweir 					if ( null != xPeer ) {
263cdf0e10cSrcweir 						XMessageBox xMsgBox = (XMessageBox)UnoRuntime.queryInterface(
264cdf0e10cSrcweir 							XMessageBox.class, xPeer);
265cdf0e10cSrcweir 						if ( null != xMsgBox )
266cdf0e10cSrcweir 						{
267cdf0e10cSrcweir 							xMsgBox.setCaptionText( sTitle );
268cdf0e10cSrcweir 							xMsgBox.setMessageText( sMessage );
269cdf0e10cSrcweir 							xMsgBox.execute();
270cdf0e10cSrcweir 						}
271cdf0e10cSrcweir 					}
272cdf0e10cSrcweir 				}
273cdf0e10cSrcweir 			} catch ( com.sun.star.uno.Exception e) {
274cdf0e10cSrcweir 				// do your error handling
275cdf0e10cSrcweir 			}
276cdf0e10cSrcweir 		}
277cdf0e10cSrcweir 	}
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     /**
280cdf0e10cSrcweir      * Gives a factory for creating the service.
281cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
282cdf0e10cSrcweir      * <p>
283cdf0e10cSrcweir      * @return  returns a <code>XSingleComponentFactory</code> for creating
284cdf0e10cSrcweir      *          the component
285cdf0e10cSrcweir      * @param   sImplName the name of the implementation for which a
286cdf0e10cSrcweir      *          service is desired
287cdf0e10cSrcweir      * @see     com.sun.star.comp.loader.JavaLoader
288cdf0e10cSrcweir      */
__getComponentFactory(String sImplName)289cdf0e10cSrcweir     public static XSingleComponentFactory __getComponentFactory(String sImplName)
290cdf0e10cSrcweir     {
291cdf0e10cSrcweir         XSingleComponentFactory xFactory = null;
292cdf0e10cSrcweir 
293cdf0e10cSrcweir         if ( sImplName.equals( _DialogComponent.class.getName() ) )
294cdf0e10cSrcweir             xFactory = Factory.createComponentFactory(_DialogComponent.class,
295cdf0e10cSrcweir                                              _DialogComponent.getServiceNames());
296cdf0e10cSrcweir 
297cdf0e10cSrcweir         return xFactory;
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir     /**
301cdf0e10cSrcweir      * Writes the service information into the given registry key.
302cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
303cdf0e10cSrcweir      * <p>
304cdf0e10cSrcweir      * @return  returns true if the operation succeeded
305cdf0e10cSrcweir      * @param   regKey the registryKey
306cdf0e10cSrcweir      * @see     com.sun.star.comp.loader.JavaLoader
307cdf0e10cSrcweir      */
308cdf0e10cSrcweir     // This method not longer necessary since OOo 3.4 where the component registration
309cdf0e10cSrcweir     // was changed to passive component registration. For more details see
310cdf0e10cSrcweir     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
311cdf0e10cSrcweir 
312cdf0e10cSrcweir //    public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
313cdf0e10cSrcweir //         return Factory.writeRegistryServiceInfo(_DialogComponent.class.getName(),
314cdf0e10cSrcweir //                                                 _DialogComponent.getServiceNames(),
315cdf0e10cSrcweir //                                                 regKey);
316cdf0e10cSrcweir //    }
317cdf0e10cSrcweir }
318