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