1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ThreadRunner.java,v $
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
;
48 import com
.sun
.star
.lang
.XComponent
;
49 import lib
.StatusException
;
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
) {
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
]);
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
{
99 String actionType
= null;
100 String actionParm
= null;
101 XMultiServiceFactory oMSF
= null;
103 public ThreadRunnerImpl(XMultiServiceFactory 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': " +
125 actionParm
= (String
)params
[1];
126 oDoc
= (Object
)params
[2];
127 ToolkitDialogThread aThread
=
128 new ToolkitDialogThread(oMSF
, oDoc
, actionParm
);
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': " +
136 actionParm
= (String
)params
[1];
137 DialogFromFileThread bThread
=
138 new DialogFromFileThread(oMSF
, actionParm
);
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': " +
146 ExecuteDialogThread cThread
=
147 new ExecuteDialogThread(oMSF
, (String
)params
[1]);
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
]);
169 class ToolkitDialogThread
extends Thread
{
172 XMultiServiceFactory msf
= null;
174 public ToolkitDialogThread(XMultiServiceFactory xMSF
, Object doc
, String sUrl
) {
181 XModel aModel
= (XModel
) UnoRuntime
.queryInterface(XModel
.class, oDoc
);
182 XController xController
= aModel
.getCurrentController();
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
{
207 SOfficeFactory SOF
= null;
208 XMultiServiceFactory myMSF
= null;
210 public DialogFromFileThread(XMultiServiceFactory xMSF
, String sUrl
) {
212 SOF
= SOfficeFactory
.getFactory(xMSF
);
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
) {
241 this.serviceName
= serviceName
;
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
);