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