Update ooo320-m1
[ooovba.git] / qadevOOo / runner / basicrunner / basichelper / ThreadRunner.java
blob9dc6353d8b7b854b61d835a45c7fd47735beb633
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ThreadRunner.java,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package basicrunner.basichelper;
33 import com.sun.star.beans.PropertyValue;
34 import com.sun.star.lang.XInitialization;
35 import com.sun.star.lang.XSingleServiceFactory;
36 import com.sun.star.lang.XServiceInfo;
37 import com.sun.star.lang.XTypeProvider;
38 import com.sun.star.uno.Type;
39 import com.sun.star.frame.XDispatch;
40 import com.sun.star.frame.XDispatchProvider;
41 import com.sun.star.frame.XModel;
42 import com.sun.star.util.XURLTransformer;
43 import com.sun.star.frame.XController;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.util.URL;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import util.utils;
48 import com.sun.star.lang.XComponent;
49 import lib.StatusException;
50 import lib.Status;
51 import util.SOfficeFactory;
52 import com.sun.star.ui.dialogs.XExecutableDialog;
55 public class ThreadRunner implements XServiceInfo, XSingleServiceFactory {
56 static final String __serviceName = "basichelper.ThreadRunner";
57 static ThreadRunnerImpl oThreadRunner = null;
59 public ThreadRunner(XMultiServiceFactory xMSF) {
60 oThreadRunner = new ThreadRunnerImpl(xMSF);
63 public Object createInstanceWithArguments(Object[] args) {
64 return oThreadRunner;
67 public Object createInstance() {
68 return createInstanceWithArguments(null);
71 public byte[] getImplementationId() {
72 return toString().getBytes();
75 public Type[] getTypes() {
76 Class interfaces[] = getClass().getInterfaces();
77 Type types[] = new Type[interfaces.length];
78 for(int i = 0; i < interfaces.length; ++ i)
79 types[i] = new Type(interfaces[i]);
80 return types;
83 public boolean supportsService(String name) {
84 return __serviceName.equals(name);
87 public String[] getSupportedServiceNames() {
88 return new String[] {__serviceName};
91 public String getImplementationName() {
92 return getClass().getName();
97 class ThreadRunnerImpl implements XInitialization, XTypeProvider {
98 Object oDoc = null;
99 String actionType = null;
100 String actionParm = null;
101 XMultiServiceFactory oMSF = null;
103 public ThreadRunnerImpl(XMultiServiceFactory MSF) {
104 oMSF = MSF;
107 public void initialize(Object[] params) throws com.sun.star.uno.Exception {
108 boolean parmsOK = false;
110 if (!(params[0] instanceof String)) {
111 throw new StatusException(Status.failed(
112 "Wrong first parameter for ThreadRunner, allowed values:" +
113 "'OpenToolkitDialog', 'OpenDialogFromFile', 'ExecuteDialog'"));
116 actionType = (String) params[0];
118 if (actionType.equals("OpenToolkitDialog")) {
119 if (params.length != 3 || !(params[2] instanceof Object) ||
120 !(params[1] instanceof String) ) {
121 throw new StatusException(Status.failed("Expected the " +
122 "following type of parameters for 'OpenToolkitDialog': " +
123 "String, Object"));
125 actionParm = (String)params[1];
126 oDoc = (Object)params[2];
127 ToolkitDialogThread aThread =
128 new ToolkitDialogThread(oMSF, oDoc, actionParm);
129 aThread.start();
130 } else if (actionType.equals("OpenDialogFromFile")) {
131 if (params.length != 2 || !(params[1] instanceof String) ) {
132 throw new StatusException(Status.failed("Expected the " +
133 "following type of parameters for 'OpenDialogFromFile': " +
134 "String"));
136 actionParm = (String)params[1];
137 DialogFromFileThread bThread =
138 new DialogFromFileThread(oMSF, actionParm);
139 bThread.start();
140 } else if ( actionType.equals("ExecuteDialog")) {
141 if (params.length != 2 || !(params[1] instanceof String)) {
142 throw new StatusException(Status.failed("Expected the " +
143 "following type of parameters for 'ExecuteDialog': " +
144 "String"));
146 ExecuteDialogThread cThread =
147 new ExecuteDialogThread(oMSF, (String)params[1]);
148 cThread.start();
149 } else {
150 System.out.println("Error! ThreadRunnerImpl.initialize(): " +
151 "Incorrect parameters!");
155 public byte[] getImplementationId() {
156 return toString().getBytes();
159 public Type[] getTypes() {
160 Class interfaces[] = getClass().getInterfaces();
161 Type types[] = new Type[interfaces.length];
162 for(int i = 0; i < interfaces.length; ++ i)
163 types[i] = new Type(interfaces[i]);
164 return types;
169 class ToolkitDialogThread extends Thread {
170 Object oDoc = null;
171 String url = null;
172 XMultiServiceFactory msf = null;
174 public ToolkitDialogThread(XMultiServiceFactory xMSF, Object doc, String sUrl) {
175 oDoc = doc;
176 url = sUrl;
177 msf = xMSF;
180 public void run() {
181 XModel aModel = (XModel) UnoRuntime.queryInterface(XModel.class, oDoc);
182 XController xController = aModel.getCurrentController();
183 try {
184 XDispatchProvider xDispProv = (XDispatchProvider)
185 UnoRuntime.queryInterface( XDispatchProvider.class, xController );
186 XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
187 UnoRuntime.queryInterface(XURLTransformer.class,
188 msf.createInstance("com.sun.star.util.URLTransformer"));
189 URL[] aParseURL = new URL[1];
190 aParseURL[0] = new URL();
191 aParseURL[0].Complete = url;
192 xParser.parseStrict(aParseURL);
193 URL aURL = aParseURL[0];
194 XDispatch xDispatcher = xDispProv.queryDispatch( aURL,"",0);
195 if( xDispatcher != null )
196 xDispatcher.dispatch( aURL, null );
197 } catch (com.sun.star.uno.Exception e) {
198 System.out.println("Couldn't open dialog!!!");
199 throw new StatusException( "Couldn't open dialog!!!", e );
205 class DialogFromFileThread extends Thread {
206 String url = null;
207 SOfficeFactory SOF = null;
208 XMultiServiceFactory myMSF = null;
210 public DialogFromFileThread(XMultiServiceFactory xMSF, String sUrl) {
211 url = sUrl;
212 SOF = SOfficeFactory.getFactory(xMSF);
213 myMSF = xMSF;
216 public void run() {
217 try {
218 PropertyValue[] args = new PropertyValue[1];
219 args[0] = new PropertyValue();
220 args[0].Name = "InteractionHandler";
221 args[0].Value = myMSF.createInstance(
222 "com.sun.star.comp.uui.UUIInteractionHandler");
224 String testUrl= utils.getFullTestURL(url);
225 System.out.println("loading "+testUrl);
226 XComponent xDoc = SOF.loadDocument(testUrl, args);
227 } catch (com.sun.star.uno.Exception e) {
228 System.out.println("Couldn't create document!!!");
229 throw new StatusException( "Couldn't create document!!!", e );
235 class ExecuteDialogThread extends Thread {
236 XMultiServiceFactory xMSF = null;
237 String serviceName = null;
239 public ExecuteDialogThread(XMultiServiceFactory xMSF, String serviceName) {
240 this.xMSF = xMSF;
241 this.serviceName = serviceName;
244 public void run() {
245 Object dlg = null;
246 try {
247 dlg = xMSF.createInstance(serviceName);
248 } catch(com.sun.star.uno.Exception e) {
249 throw new StatusException(Status.failed("Couldn't create service"));
251 XExecutableDialog execDlg = (XExecutableDialog)UnoRuntime.queryInterface
252 (XExecutableDialog.class, dlg);
253 execDlg.execute();