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 package complex.sfx2.tools;
24 
25 import com.sun.star.beans.PropertyValue;
26 import com.sun.star.frame.XController;
27 import com.sun.star.frame.XDispatch;
28 import com.sun.star.frame.XDispatchProvider;
29 import com.sun.star.frame.XModel;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.util.URL;
34 import com.sun.star.util.XURLTransformer;
35 
36 /**
37  * This class opens a given dialog in a separate Thread by dispatching an url
38  *
39  */
40 public class DialogThread extends Thread {
41     public XComponent m_xDoc = null;
42     public XMultiServiceFactory m_xMSF = null;
43     public String m_url = "";
44 
DialogThread(XComponent xDoc, XMultiServiceFactory msf, String url)45     public DialogThread(XComponent xDoc, XMultiServiceFactory msf, String url) {
46         this.m_xDoc = xDoc;
47         this.m_xMSF = msf;
48         this.m_url = url;
49     }
50 
51     @Override
run()52     public void run() {
53         XModel aModel = UnoRuntime.queryInterface( XModel.class, m_xDoc );
54 
55         XController xController = aModel.getCurrentController();
56 
57         //Opening Dialog
58         try {
59             XDispatchProvider xDispProv = UnoRuntime.queryInterface( XDispatchProvider.class, xController.getFrame() );
60             XURLTransformer xParser = UnoRuntime.queryInterface( XURLTransformer.class,
61                 m_xMSF.createInstance( "com.sun.star.util.URLTransformer" ) );
62 
63             // Because it's an in/out parameter
64             // we must use an array of URL objects.
65             URL[] aParseURL = new URL[1];
66             aParseURL[0] = new URL();
67             aParseURL[0].Complete = m_url;
68             xParser.parseStrict(aParseURL);
69 
70             URL aURL = aParseURL[0];
71             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", com.sun.star.frame.FrameSearchFlag.SELF |
72                                     com.sun.star.frame.FrameSearchFlag.CHILDREN);
73             PropertyValue[] dispatchArguments = new PropertyValue[0];
74 
75             if (xDispatcher != null) {
76                 xDispatcher.dispatch(aURL, dispatchArguments);
77             } else {
78                 System.out.println("xDispatcher is null");
79             }
80         } catch (com.sun.star.uno.Exception e) {
81             System.out.println("Couldn't open dialog");
82         }
83     }
84 }