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