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