Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / Components / Addons / JobsAddon / AsyncJob.java
blobdfc285d7bba2b6edeb5d728601fd5f359b557835
1 /*************************************************************************
3 * $RCSfile: AsyncJob.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: vg $ $Date: 2003-07-11 11:23:14 $
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 //_______________________________________________
42 // imports
43 import com.sun.star.uno.XComponentContext;
44 import com.sun.star.lib.uno.helper.Factory;
45 import com.sun.star.lib.uno.helper.WeakBase;
46 import com.sun.star.lang.XServiceInfo;
47 import com.sun.star.awt.*;
48 import com.sun.star.beans.*;
49 import com.sun.star.task.*;
50 import com.sun.star.uno.*;
51 import java.lang.*;
52 import javax.swing.*;
54 //_______________________________________________
55 // implementation
57 /** it implements a simple job component.
59 * Such jobs are executable in different ways:
60 * <ul>
61 * <li>registered for a special URL schema "vnd.sun.star.jobs:*" and used from the generic dispatch framework</li>
62 * <li>the global com.sun.star.task.JobExecutor service and registered for special events.</li>
63 * </ul>
65 public class AsyncJob extends WeakBase implements XServiceInfo, XAsyncJob
67 //___________________________________________
68 // const
69 public final XComponentContext m_xCmpCtx;
71 /** the const list of supported uno service names. */
72 public static final java.lang.String[] SERVICENAMES = {"com.sun.star.task.AsyncJob"};
74 /** the const uno implementation name.
75 * It must be an unique value! The best naming schema seams to use
76 * a registered domain in reverse order ...
78 public static final java.lang.String IMPLEMENTATIONNAME = "com.sun.star.comp.framework.java.services.AsyncJob";
80 //___________________________________________
81 // interface
83 /** initialize a new instance of this class with default values. */
84 public AsyncJob( XComponentContext xCompContext )
86 m_xCmpCtx = xCompContext;
89 //___________________________________________
91 /** starts execution of this job.
93 * @param lArgs
94 * list which contains:
95 * <ul>
96 * <li>generic job configuration data</li>
97 * <li>job specific configuration data</li>
98 * <li>some environment informations</li>
99 * <li>may optional arguments of a corresponding dispatch request</li>
100 * </ul>
102 * @params xListener
103 * callback to the executor of this job, which control our life time
105 * @throws com.sun.star.lang.IllegalArgumentException
106 * if given argument list seams to be wrong
108 public synchronized void executeAsync(com.sun.star.beans.NamedValue[] lArgs ,
109 com.sun.star.task.XJobListener xListener)
110 throws com.sun.star.lang.IllegalArgumentException
112 // For asynchronous jobs a valid listener reference is guranteed normaly ...
113 if (xListener == null)
114 throw new com.sun.star.lang.IllegalArgumentException("invalid listener");
116 // extract all possible sub list of given argument list
117 com.sun.star.beans.NamedValue[] lGenericConfig = null;
118 com.sun.star.beans.NamedValue[] lJobConfig = null;
119 com.sun.star.beans.NamedValue[] lEnvironment = null;
120 com.sun.star.beans.NamedValue[] lDynamicData = null;
122 int c = lArgs.length;
123 for (int i=0; i<c; ++i)
125 if (lArgs[i].Name.equals("Config"))
126 lGenericConfig = (com.sun.star.beans.NamedValue[])com.sun.star.uno.AnyConverter.toArray(lArgs[i].Value);
127 else
128 if (lArgs[i].Name.equals("JobConfig"))
129 lJobConfig = (com.sun.star.beans.NamedValue[])com.sun.star.uno.AnyConverter.toArray(lArgs[i].Value);
130 else
131 if (lArgs[i].Name.equals("Environment"))
132 lEnvironment = (com.sun.star.beans.NamedValue[])com.sun.star.uno.AnyConverter.toArray(lArgs[i].Value);
133 else
134 if (lArgs[i].Name.equals("DynamicData"))
135 lDynamicData = (com.sun.star.beans.NamedValue[])com.sun.star.uno.AnyConverter.toArray(lArgs[i].Value);
138 // Analyze the environment info. This sub list is the only guarenteed one!
139 if (lEnvironment == null)
140 throw new com.sun.star.lang.IllegalArgumentException("no environment");
142 java.lang.String sEnvType = null;
143 java.lang.String sEventName = null;
144 com.sun.star.frame.XFrame xFrame = null;
145 c = lEnvironment.length;
146 for (int i=0; i<c; ++i)
148 if (lEnvironment[i].Name.equals("EnvType"))
149 sEnvType = com.sun.star.uno.AnyConverter.toString(lEnvironment[i].Value);
150 else
151 if (lEnvironment[i].Name.equals("EventName"))
152 sEventName = com.sun.star.uno.AnyConverter.toString(lEnvironment[i].Value);
153 else
154 if (lEnvironment[i].Name.equals("Frame"))
155 xFrame = (com.sun.star.frame.XFrame)com.sun.star.uno.AnyConverter.toObject(
156 new com.sun.star.uno.Type(com.sun.star.frame.XFrame.class),
157 lEnvironment[i].Value);
160 // Further the environment property "EnvType" is required as minimum.
161 if (
162 (sEnvType==null) ||
164 (!sEnvType.equals("EXECUTOR")) &&
165 (!sEnvType.equals("DISPATCH"))
169 java.lang.String sMessage = "\"" + sEnvType + "\" isn't a valid value for EnvType";
170 throw new com.sun.star.lang.IllegalArgumentException(sMessage);
173 // Analyze the set of shared config data.
174 java.lang.String sAlias = null;
175 if (lGenericConfig!=null)
177 c = lGenericConfig.length;
178 for (int i=0; i<c; ++i)
180 if (lGenericConfig[i].Name.equals("Alias"))
181 sAlias = com.sun.star.uno.AnyConverter.toString(lGenericConfig[i].Value);
185 // do your job ...
186 // Here we print out all found arguments.
187 java.lang.String sOut = formatOutArgs(lGenericConfig, lJobConfig, lEnvironment, lDynamicData);
188 if (xFrame != null)
189 showInfoModal(xFrame.getContainerWindow(), "Arguments of AsyncJob initialization ...", sOut);
190 else
191 showInfoNonModal("Arguments of AsyncJob initialization ...", sOut);
193 // use return value to start different actions
194 // But look for the right environment. Some options make no sense inside the wrong env.
195 com.sun.star.beans.NamedValue aDeactivation = null;
196 com.sun.star.beans.NamedValue aDispatchResult = null;
197 com.sun.star.beans.NamedValue aSaveRequest = null;
199 // SaveArguments will be made everytimes!
200 c = 1;
202 if (lJobConfig==null)
203 lJobConfig = new com.sun.star.beans.NamedValue[1];
204 lJobConfig[0] = new com.sun.star.beans.NamedValue();
205 lJobConfig[0].Name = "arg_1";
206 lJobConfig[0].Value = "val_1";
208 aSaveRequest = new com.sun.star.beans.NamedValue();
209 aSaveRequest.Name = "SaveArguments";
210 aSaveRequest.Value = lJobConfig;
212 // Deactivation is usefull inside EXECUTOR environment only
213 if (sEnvType.equals("EXECUTOR"))
215 ++c;
216 aDeactivation = new com.sun.star.beans.NamedValue();
217 aDeactivation.Name = "Deactivate";
218 aDeactivation.Value = java.lang.Boolean.TRUE;
221 // Sending of result events is usefull inside DISPATCH environment only
222 if (sEnvType.equals("DISPATCH"))
224 ++c;
225 aDispatchResult = new com.sun.star.beans.NamedValue();
226 aDispatchResult.Name = "SendDispatchResult";
227 aDispatchResult.Value = new com.sun.star.frame.DispatchResultEvent(this, com.sun.star.frame.DispatchResultState.SUCCESS, null);
230 // pack it together for return
231 int i=0;
232 com.sun.star.beans.NamedValue[] lReturn = new com.sun.star.beans.NamedValue[c];
233 lReturn[i++] = aSaveRequest;
234 if (aDeactivation!=null)
235 lReturn[i++] = aDeactivation;
236 if (aDispatchResult!=null)
237 lReturn[i++] = aDispatchResult;
239 xListener.jobFinished(this, lReturn);
242 //___________________________________________
244 /** show an info box with the UNO based toolkit.
246 * It tries to use the container window of a may well know
247 * office frame as parent. If such parent window could be located,
248 * the info box can be shown in modal mode. If a parent is missing
249 * (because this job is called inside an EXECUTOR environment, which
250 * does not set any frame context here) the info box can't be created!
251 * Because the toolkit needs parents for non top level windows ...
252 * In that case the only way is to implement this info box
253 * native or make it non modal using java dialogs inside it's own thread ...
254 * (see showInfoNonModal() too)
256 * @param xParent
257 * used as parent window of the shown info box.
259 * @param sTitle
260 * is shown as title of the info box.
262 * @param sMessage
263 * inclused the message body, which is shown as info.
266 private void showInfoModal( com.sun.star.awt.XWindow xParent ,
267 java.lang.String sTitle ,
268 java.lang.String sMessage )
272 // get access to the office toolkit environment
273 com.sun.star.awt.XToolkit xKit = (com.sun.star.awt.XToolkit)UnoRuntime.queryInterface(
274 com.sun.star.awt.XToolkit.class,
275 m_xCmpCtx.getServiceManager().createInstanceWithContext("com.sun.star.awt.Toolkit",
276 m_xCmpCtx));
278 // describe the info box ini it's parameters
279 com.sun.star.awt.WindowDescriptor aDescriptor = new com.sun.star.awt.WindowDescriptor();
280 aDescriptor.WindowServiceName = "infobox";
281 aDescriptor.Bounds = new com.sun.star.awt.Rectangle(0,0,300,200);
282 aDescriptor.WindowAttributes = com.sun.star.awt.WindowAttribute.BORDER |
283 com.sun.star.awt.WindowAttribute.MOVEABLE |
284 com.sun.star.awt.WindowAttribute.CLOSEABLE;
285 aDescriptor.Type = com.sun.star.awt.WindowClass.MODALTOP;
286 aDescriptor.ParentIndex = 1;
287 aDescriptor.Parent = (com.sun.star.awt.XWindowPeer)UnoRuntime.queryInterface(
288 com.sun.star.awt.XWindowPeer.class,
289 xParent);
291 // create the info box window
292 com.sun.star.awt.XWindowPeer xPeer = xKit.createWindow(aDescriptor);
293 com.sun.star.awt.XMessageBox xInfoBox = (com.sun.star.awt.XMessageBox)UnoRuntime.queryInterface(
294 com.sun.star.awt.XMessageBox.class,
295 xPeer);
296 if (xInfoBox == null)
297 return;
299 // fill it with all given informations and show it
300 xInfoBox.setCaptionText(sTitle);
301 xInfoBox.setMessageText(sMessage);
302 xInfoBox.execute();
304 catch(java.lang.Throwable exIgnore)
306 // ignore any problem, which can occure here.
307 // It's not realy a bug for this example job, if
308 // it's message could not be printed out!
312 //___________________________________________
314 private void showInfoNonModal( java.lang.String sTitle ,
315 java.lang.String sMessage )
317 // Couldnt be implemented realy using the toolkit ...
318 // Because we need a parent anytime.
319 // And showing e.g. a java dialog can make some trouble
320 // inside office ... but we have no chance here.
321 javax.swing.JOptionPane.showMessageDialog(null, sMessage, sTitle, javax.swing.JOptionPane.INFORMATION_MESSAGE);
324 //___________________________________________
326 /** helper to print out the given argument list.
328 * @param lGenericConfig
329 * contains all shared configuration items for a job
331 * @param lJobConfig
332 * contains all job sepcific configuration items
334 * @param lEnvironment
335 * contains some environment informations
337 * @param lDynamicData
338 * contains optional data of a might corresponding dispatch() request
341 private java.lang.String formatOutArgs(com.sun.star.beans.NamedValue[] lGenericConfig,
342 com.sun.star.beans.NamedValue[] lJobConfig ,
343 com.sun.star.beans.NamedValue[] lEnvironment ,
344 com.sun.star.beans.NamedValue[] lDynamicData )
346 java.lang.StringBuffer sOut = new java.lang.StringBuffer(1024);
348 sOut.append("list \"Config\": ");
349 if (lGenericConfig==null)
350 sOut.append("0 items\n");
351 else
353 int c = lGenericConfig.length;
354 sOut.append(c+" items\n");
355 for (int i=0; i<c; ++i)
356 sOut.append("\t["+i+"] \""+lGenericConfig[i].Name+"\" = {"+lGenericConfig[i].Value+"}\n");
358 sOut.append("list \"JobConfig\": ");
359 if (lJobConfig==null)
360 sOut.append("0 items\n");
361 else
363 int c = lJobConfig.length;
364 sOut.append(c+" items\n");
365 for (int i=0; i<c; ++i)
366 sOut.append("\t["+i+"] \""+lJobConfig[i].Name+"\" = {"+lJobConfig[i].Value+"}\n");
368 sOut.append("list \"Environment\": ");
369 if (lEnvironment==null)
370 sOut.append("0 items\n");
371 else
373 int c = lEnvironment.length;
374 sOut.append(c+" items\n");
375 for (int i=0; i<c; ++i)
376 sOut.append("\t["+i+"] \""+lEnvironment[i].Name+"\" = {"+lEnvironment[i].Value+"}\n");
378 sOut.append("list \"DynamicData\": ");
379 if (lDynamicData==null)
380 sOut.append("0 items\n");
381 else
383 int c = lDynamicData.length;
384 sOut.append(c+" items\n");
385 for (int i=0; i<c; ++i)
386 sOut.append("\t["+i+"] \""+lDynamicData[i].Name+"\" = {"+lDynamicData[i].Value+"}\n");
389 return sOut.toString();
392 public String[] getSupportedServiceNames() {
393 return SERVICENAMES;
396 public boolean supportsService( String sService ) {
397 int len = SERVICENAMES.length;
399 for( int i=0; i < len; i++) {
400 if ( sService.equals( SERVICENAMES[i] ) )
401 return true;
404 return false;
407 public String getImplementationName() {
408 return( AsyncJob.class.getName() );
412 //___________________________________________
414 public synchronized static com.sun.star.lang.XSingleComponentFactory __getComponentFactory(java.lang.String sImplName)
416 com.sun.star.lang.XSingleComponentFactory xFactory = null;
417 if (sImplName.equals(AsyncJob.IMPLEMENTATIONNAME))
418 xFactory = Factory.createComponentFactory(AsyncJob.class, SERVICENAMES);
420 return xFactory;
423 //___________________________________________
425 public synchronized static boolean __writeRegistryServiceInfo(com.sun.star.registry.XRegistryKey xRegKey)
427 return Factory.writeRegistryServiceInfo(
428 AsyncJob.IMPLEMENTATIONNAME,
429 AsyncJob.SERVICENAMES,
430 xRegKey);