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