1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package basicrunner.basichelper;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
27cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
28cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
29cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
30cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
31cdf0e10cSrcweir import com.sun.star.uno.Type;
32cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
33cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
34cdf0e10cSrcweir import com.sun.star.frame.XModel;
35cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
36cdf0e10cSrcweir import com.sun.star.frame.XController;
37cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38cdf0e10cSrcweir import com.sun.star.util.URL;
39cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
40cdf0e10cSrcweir import util.utils;
41cdf0e10cSrcweir import com.sun.star.lang.XComponent;
42cdf0e10cSrcweir import lib.StatusException;
43cdf0e10cSrcweir import lib.Status;
44cdf0e10cSrcweir import util.SOfficeFactory;
45cdf0e10cSrcweir import com.sun.star.ui.dialogs.XExecutableDialog;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir public class ThreadRunner implements XServiceInfo, XSingleServiceFactory {
49cdf0e10cSrcweir     static final String __serviceName = "basichelper.ThreadRunner";
50cdf0e10cSrcweir     static ThreadRunnerImpl oThreadRunner = null;
51cdf0e10cSrcweir 
ThreadRunner(XMultiServiceFactory xMSF)52cdf0e10cSrcweir     public ThreadRunner(XMultiServiceFactory xMSF) {
53cdf0e10cSrcweir 	oThreadRunner = new ThreadRunnerImpl(xMSF);
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
createInstanceWithArguments(Object[] args)56cdf0e10cSrcweir     public Object createInstanceWithArguments(Object[] args) {
57cdf0e10cSrcweir 	return oThreadRunner;
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir 
createInstance()60cdf0e10cSrcweir     public Object createInstance() {
61cdf0e10cSrcweir         return createInstanceWithArguments(null);
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
getImplementationId()64cdf0e10cSrcweir     public byte[] getImplementationId() {
65cdf0e10cSrcweir         return toString().getBytes();
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir 
getTypes()68cdf0e10cSrcweir     public Type[] getTypes() {
69cdf0e10cSrcweir         Class interfaces[] = getClass().getInterfaces();
70cdf0e10cSrcweir         Type types[] = new Type[interfaces.length];
71cdf0e10cSrcweir         for(int i = 0; i < interfaces.length; ++ i)
72cdf0e10cSrcweir             types[i] = new Type(interfaces[i]);
73cdf0e10cSrcweir         return types;
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir 
supportsService(String name)76cdf0e10cSrcweir     public boolean supportsService(String name) {
77cdf0e10cSrcweir         return __serviceName.equals(name);
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
getSupportedServiceNames()80cdf0e10cSrcweir     public String[] getSupportedServiceNames() {
81cdf0e10cSrcweir         return new String[] {__serviceName};
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
getImplementationName()84cdf0e10cSrcweir     public String getImplementationName() {
85cdf0e10cSrcweir         return getClass().getName();
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
90cdf0e10cSrcweir class ThreadRunnerImpl implements XInitialization, XTypeProvider {
91cdf0e10cSrcweir     Object oDoc = null;
92cdf0e10cSrcweir     String actionType = null;
93cdf0e10cSrcweir     String actionParm = null;
94cdf0e10cSrcweir     XMultiServiceFactory oMSF = null;
95cdf0e10cSrcweir 
ThreadRunnerImpl(XMultiServiceFactory MSF)96cdf0e10cSrcweir     public ThreadRunnerImpl(XMultiServiceFactory MSF) {
97cdf0e10cSrcweir 	oMSF = MSF;
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
initialize(Object[] params)100cdf0e10cSrcweir     public void initialize(Object[] params) throws com.sun.star.uno.Exception {
101cdf0e10cSrcweir 	boolean parmsOK = false;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         if (!(params[0] instanceof String)) {
104cdf0e10cSrcweir             throw new StatusException(Status.failed(
105cdf0e10cSrcweir                 "Wrong first parameter for ThreadRunner, allowed values:" +
106cdf0e10cSrcweir                 "'OpenToolkitDialog', 'OpenDialogFromFile', 'ExecuteDialog'"));
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         actionType = (String) params[0];
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         if (actionType.equals("OpenToolkitDialog")) {
112cdf0e10cSrcweir             if (params.length != 3 || !(params[2] instanceof Object) ||
113cdf0e10cSrcweir                 !(params[1] instanceof String) ) {
114cdf0e10cSrcweir                 throw new StatusException(Status.failed("Expected the " +
115cdf0e10cSrcweir                     "following type of parameters for 'OpenToolkitDialog': " +
116cdf0e10cSrcweir                     "String, Object"));
117cdf0e10cSrcweir             }
118cdf0e10cSrcweir             actionParm = (String)params[1];
119cdf0e10cSrcweir             oDoc = (Object)params[2];
120cdf0e10cSrcweir             ToolkitDialogThread aThread =
121cdf0e10cSrcweir                 new ToolkitDialogThread(oMSF, oDoc, actionParm);
122cdf0e10cSrcweir             aThread.start();
123cdf0e10cSrcweir         } else if (actionType.equals("OpenDialogFromFile")) {
124cdf0e10cSrcweir             if (params.length != 2 || !(params[1] instanceof String) ) {
125cdf0e10cSrcweir                 throw new StatusException(Status.failed("Expected the " +
126cdf0e10cSrcweir                     "following type of parameters for 'OpenDialogFromFile': " +
127cdf0e10cSrcweir                     "String"));
128cdf0e10cSrcweir             }
129cdf0e10cSrcweir             actionParm = (String)params[1];
130cdf0e10cSrcweir             DialogFromFileThread bThread =
131cdf0e10cSrcweir                 new DialogFromFileThread(oMSF, actionParm);
132cdf0e10cSrcweir             bThread.start();
133cdf0e10cSrcweir         } else if ( actionType.equals("ExecuteDialog")) {
134cdf0e10cSrcweir             if (params.length != 2 || !(params[1] instanceof String)) {
135cdf0e10cSrcweir                 throw new StatusException(Status.failed("Expected the " +
136cdf0e10cSrcweir                     "following type of parameters for 'ExecuteDialog': " +
137cdf0e10cSrcweir                     "String"));
138cdf0e10cSrcweir             }
139cdf0e10cSrcweir             ExecuteDialogThread cThread =
140cdf0e10cSrcweir                 new ExecuteDialogThread(oMSF, (String)params[1]);
141cdf0e10cSrcweir             cThread.start();
142cdf0e10cSrcweir         } else {
143cdf0e10cSrcweir             System.out.println("Error! ThreadRunnerImpl.initialize(): " +
144cdf0e10cSrcweir             "Incorrect parameters!");
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
getImplementationId()148cdf0e10cSrcweir     public byte[] getImplementationId() {
149cdf0e10cSrcweir         return toString().getBytes();
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
getTypes()152cdf0e10cSrcweir     public Type[] getTypes() {
153cdf0e10cSrcweir         Class interfaces[] = getClass().getInterfaces();
154cdf0e10cSrcweir         Type types[] = new Type[interfaces.length];
155cdf0e10cSrcweir         for(int i = 0; i < interfaces.length; ++ i)
156cdf0e10cSrcweir             types[i] = new Type(interfaces[i]);
157cdf0e10cSrcweir         return types;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 
162cdf0e10cSrcweir class ToolkitDialogThread extends Thread {
163cdf0e10cSrcweir     Object oDoc = null;
164cdf0e10cSrcweir     String url = null;
165cdf0e10cSrcweir     XMultiServiceFactory msf = null;
166cdf0e10cSrcweir 
ToolkitDialogThread(XMultiServiceFactory xMSF, Object doc, String sUrl)167cdf0e10cSrcweir     public ToolkitDialogThread(XMultiServiceFactory xMSF, Object doc, String sUrl) {
168cdf0e10cSrcweir 	oDoc = doc;
169cdf0e10cSrcweir 	url = sUrl;
170cdf0e10cSrcweir 	msf = xMSF;
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir 
run()173cdf0e10cSrcweir     public void run() {
174cdf0e10cSrcweir 	XModel aModel = (XModel) UnoRuntime.queryInterface(XModel.class, oDoc);
175cdf0e10cSrcweir 	XController xController = aModel.getCurrentController();
176cdf0e10cSrcweir 	try {
177cdf0e10cSrcweir 	    XDispatchProvider xDispProv = (XDispatchProvider)
178cdf0e10cSrcweir 		UnoRuntime.queryInterface( XDispatchProvider.class, xController );
179cdf0e10cSrcweir 	    XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
180cdf0e10cSrcweir 		UnoRuntime.queryInterface(XURLTransformer.class,
181cdf0e10cSrcweir 					  msf.createInstance("com.sun.star.util.URLTransformer"));
182cdf0e10cSrcweir 	    URL[] aParseURL = new URL[1];
183cdf0e10cSrcweir 	    aParseURL[0] = new URL();
184cdf0e10cSrcweir 	    aParseURL[0].Complete = url;
185cdf0e10cSrcweir 	    xParser.parseStrict(aParseURL);
186cdf0e10cSrcweir 	    URL aURL = aParseURL[0];
187cdf0e10cSrcweir 	    XDispatch xDispatcher = xDispProv.queryDispatch( aURL,"",0);
188cdf0e10cSrcweir 	    if( xDispatcher != null )
189cdf0e10cSrcweir 		xDispatcher.dispatch( aURL, null );
190cdf0e10cSrcweir 	} catch (com.sun.star.uno.Exception e) {
191cdf0e10cSrcweir 	    System.out.println("Couldn't open dialog!!!");
192cdf0e10cSrcweir 	    throw new StatusException( "Couldn't open dialog!!!", e );
193cdf0e10cSrcweir 	}
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 
198cdf0e10cSrcweir class DialogFromFileThread extends Thread {
199cdf0e10cSrcweir     String url = null;
200cdf0e10cSrcweir     SOfficeFactory SOF = null;
201cdf0e10cSrcweir     XMultiServiceFactory myMSF = null;
202cdf0e10cSrcweir 
DialogFromFileThread(XMultiServiceFactory xMSF, String sUrl)203cdf0e10cSrcweir     public DialogFromFileThread(XMultiServiceFactory xMSF, String sUrl) {
204cdf0e10cSrcweir 	url = sUrl;
205cdf0e10cSrcweir 	SOF = SOfficeFactory.getFactory(xMSF);
206cdf0e10cSrcweir         myMSF = xMSF;
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir 
run()209cdf0e10cSrcweir     public void run() {
210cdf0e10cSrcweir 	try {
211cdf0e10cSrcweir             PropertyValue[] args = new PropertyValue[1];
212cdf0e10cSrcweir             args[0] = new PropertyValue();
213cdf0e10cSrcweir             args[0].Name = "InteractionHandler";
214cdf0e10cSrcweir             args[0].Value = myMSF.createInstance(
215cdf0e10cSrcweir                 "com.sun.star.comp.uui.UUIInteractionHandler");
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	    String testUrl= utils.getFullTestURL(url);
218cdf0e10cSrcweir 	    System.out.println("loading "+testUrl);
219cdf0e10cSrcweir 	    XComponent xDoc = SOF.loadDocument(testUrl, args);
220cdf0e10cSrcweir 	} catch (com.sun.star.uno.Exception e) {
221cdf0e10cSrcweir 	    System.out.println("Couldn't create document!!!");
222cdf0e10cSrcweir 	    throw new StatusException( "Couldn't create document!!!", e );
223cdf0e10cSrcweir 	}
224cdf0e10cSrcweir     }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir class ExecuteDialogThread extends Thread {
229cdf0e10cSrcweir     XMultiServiceFactory xMSF = null;
230cdf0e10cSrcweir     String serviceName = null;
231cdf0e10cSrcweir 
ExecuteDialogThread(XMultiServiceFactory xMSF, String serviceName)232cdf0e10cSrcweir     public ExecuteDialogThread(XMultiServiceFactory xMSF, String serviceName) {
233cdf0e10cSrcweir         this.xMSF = xMSF;
234cdf0e10cSrcweir         this.serviceName = serviceName;
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir 
run()237cdf0e10cSrcweir     public void run() {
238cdf0e10cSrcweir         Object dlg = null;
239cdf0e10cSrcweir         try {
240cdf0e10cSrcweir             dlg = xMSF.createInstance(serviceName);
241cdf0e10cSrcweir         } catch(com.sun.star.uno.Exception e) {
242cdf0e10cSrcweir             throw new StatusException(Status.failed("Couldn't create service"));
243cdf0e10cSrcweir         }
244cdf0e10cSrcweir         XExecutableDialog execDlg = (XExecutableDialog)UnoRuntime.queryInterface
245cdf0e10cSrcweir             (XExecutableDialog.class, dlg);
246cdf0e10cSrcweir         execDlg.execute();
247cdf0e10cSrcweir     }
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 
251