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 import java.lang.Thread;
23 
24 import com.sun.star.awt.Rectangle;
25 import com.sun.star.awt.XWindow;
26 
27 import com.sun.star.beans.PropertyValue;
28 import com.sun.star.beans.XPropertySet;
29 
30 import com.sun.star.container.XIndexAccess;
31 import com.sun.star.container.XChild;
32 import com.sun.star.container.XEnumerationAccess;
33 import com.sun.star.container.XEnumeration;
34 
35 import com.sun.star.frame.XComponentLoader;
36 import com.sun.star.frame.XController;
37 import com.sun.star.frame.XDesktop;
38 import com.sun.star.frame.XFrame;
39 import com.sun.star.frame.XModel;
40 import com.sun.star.frame.XTasksSupplier;
41 import com.sun.star.frame.XTask;
42 
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.lang.XServiceInfo;
46 import com.sun.star.lang.XServiceName;
47 import com.sun.star.lang.XTypeProvider;
48 
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
51 import com.sun.star.uno.Type;
52 
53 import com.sun.star.drawing.XDrawView;
54 import com.sun.star.drawing.XDrawPage;
55 import com.sun.star.drawing.XShapes;
56 import com.sun.star.drawing.XShape;
57 import com.sun.star.drawing.XShapeDescriptor;
58 
59 import com.sun.star.accessibility.XAccessible;
60 import com.sun.star.accessibility.XAccessibleContext;
61 import com.sun.star.accessibility.XAccessibleComponent;
62 import com.sun.star.accessibility.XAccessibleRelationSet;
63 import com.sun.star.accessibility.XAccessibleStateSet;
64 
65 import com.sun.star.awt.XExtendedToolkit;
66 
67 
68 /** This class tries to simplify some tasks like loading a document or
69     getting various objects.
70 */
71 public class SimpleOffice
72 {
73     XDesktop mxDesktop = null;
74     OfficeConnection aConnection;
75     int mnPortNumber;
76 
77     public SimpleOffice (int nPortNumber)
78     {
79         mnPortNumber = nPortNumber;
80         connect ();
81         getDesktop ();
82     }
83 
84     public void connect ()
85     {
86         aConnection = new OfficeConnection (mnPortNumber);
87         mxDesktop = null;
88         getDesktop ();
89     }
90 
91     public XModel loadDocument (String URL)
92     {
93         XModel xModel = null;
94         try
95         {
96             //  Load the document from the specified URL.
97             XComponentLoader xLoader =
98                 (XComponentLoader)UnoRuntime.queryInterface(
99                     XComponentLoader.class, mxDesktop);
100 
101             XComponent xComponent = xLoader.loadComponentFromURL (
102                 URL,
103                 "_blank",
104                 0,
105                 new PropertyValue[0]
106                 );
107 
108             xModel = (XModel) UnoRuntime.queryInterface(
109                 XModel.class, xComponent);
110         }
111         catch (java.lang.NullPointerException e)
112         {
113             MessageArea.println ("caught exception while loading "
114                 + URL + " : " + e);
115         }
116         catch (Exception e)
117         {
118             MessageArea.println ("caught exception while loading "
119                 + URL + " : " + e);
120         }
121         return xModel;
122     }
123 
124 
125 
126 
127     public XModel getModel (String name)
128     {
129         XModel xModel = null;
130         try
131         {
132             XTasksSupplier xTasksSupplier =
133                 (XTasksSupplier) UnoRuntime.queryInterface(
134                     XTasksSupplier.class, mxDesktop);
135             XEnumerationAccess xEA = xTasksSupplier.getTasks();
136             XEnumeration xE = xEA.createEnumeration();
137             while (xE.hasMoreElements())
138             {
139                 XTask xTask = (XTask) UnoRuntime.queryInterface(
140                     XTask.class, xE.nextElement());
141                 MessageArea.print (xTask.getName());
142             }
143         }
144         catch (Exception e)
145         {
146             MessageArea.println ("caught exception while getting Model " + name
147                 + ": " + e);
148         }
149         return xModel;
150     }
151 
152 
153     public XModel getModel (XDrawView xView)
154     {
155         XController xController = (XController) UnoRuntime.queryInterface(
156             XController.class, xView);
157         if (xController != null)
158             return xController.getModel();
159         else
160         {
161             MessageArea.println ("can't cast view to controller");
162             return null;
163         }
164     }
165 
166     public  XDesktop getDesktop ()
167     {
168         if (mxDesktop != null)
169             return mxDesktop;
170         try
171         {
172             //  Get the factory of the connected office.
173             XMultiServiceFactory xMSF = aConnection.getServiceManager ();
174             if (xMSF == null)
175             {
176                 MessageArea.println ("can't connect to office");
177                 return null;
178             }
179             else
180                 MessageArea.println ("Connected successfully.");
181 
182             //  Create a new desktop.
183             mxDesktop = (XDesktop) UnoRuntime.queryInterface(
184                 XDesktop.class,
185                 xMSF.createInstance ("com.sun.star.frame.Desktop")
186                 );
187         }
188         catch (Exception e)
189         {
190             MessageArea.println ("caught exception while creating desktop: "
191                 + e);
192         }
193 
194         return mxDesktop;
195     }
196 
197 
198     /** Return a reference to the extended toolkit which is a broadcaster of
199         top window, key, and focus events.
200     */
201     public XExtendedToolkit getExtendedToolkit ()
202     {
203         XExtendedToolkit xToolkit = null;
204         try
205         {
206             //  Get the factory of the connected office.
207             XMultiServiceFactory xMSF = aConnection.getServiceManager ();
208             if (xMSF != null)
209             {
210                 xToolkit = (XExtendedToolkit) UnoRuntime.queryInterface(
211                     XExtendedToolkit.class,
212                     xMSF.createInstance ("stardiv.Toolkit.VCLXToolkit")
213                     );
214             }
215         }
216         catch (Exception e)
217         {
218             MessageArea.println ("caught exception while creating extended toolkit: " + e);
219         }
220 
221         return xToolkit;
222     }
223 
224 
225 
226     public XAccessible getAccessibleObject (XInterface xObject)
227     {
228         XAccessible xAccessible = null;
229         try
230         {
231             xAccessible = (XAccessible) UnoRuntime.queryInterface(
232                 XAccessible.class, xObject);
233         }
234         catch (Exception e)
235         {
236             MessageArea.println (
237                 "caught exception while getting accessible object" + e);
238             e.printStackTrace();
239         }
240         return xAccessible;
241     }
242 
243     /** Return the root object of the accessibility hierarchy.
244     */
245     public XAccessible getAccessibleRoot (XAccessible xAccessible)
246     {
247         try
248         {
249             XAccessible xParent = null;
250             do
251             {
252                 XAccessibleContext xContext = xAccessible.getAccessibleContext();
253                 if (xContext != null)
254                     xParent = xContext.getAccessibleParent();
255                 if (xParent != null)
256                     xAccessible = xParent;
257             }
258             while (xParent != null);
259         }
260         catch (Exception e)
261         {
262             MessageArea.println (
263                 "caught exception while getting accessible root" + e);
264             e.printStackTrace();
265         }
266         return xAccessible;
267     }
268 
269 
270 
271 
272     /** @descr Return the current window associated with the given
273                 model.
274     */
275     public XWindow getCurrentWindow ()
276     {
277         return getCurrentWindow ((XModel) UnoRuntime.queryInterface(
278                 XModel.class, getDesktop()));
279     }
280 
281 
282 
283 
284 
285     public XWindow getCurrentWindow (XModel xModel)
286     {
287         XWindow xWindow = null;
288         try
289         {
290             if (xModel == null)
291                 MessageArea.println ("invalid model (==null)");
292             XController xController = xModel.getCurrentController();
293             if (xController == null)
294                 MessageArea.println ("can't get controller from model");
295             XFrame xFrame = xController.getFrame();
296             if (xFrame == null)
297                 MessageArea.println ("can't get frame from controller");
298             xWindow = xFrame.getComponentWindow ();
299             if (xWindow == null)
300                 MessageArea.println ("can't get window from frame");
301         }
302         catch (Exception e)
303         {
304             MessageArea.println ("caught exception while getting current window" + e);
305         }
306 
307         return xWindow;
308     }
309 
310 
311     /** @descr Return the current draw page of the given desktop.
312     */
313     public XDrawPage getCurrentDrawPage ()
314     {
315         return getCurrentDrawPage ((XDrawView) UnoRuntime.queryInterface(
316                 XDrawView.class, getCurrentView()));
317     }
318 
319 
320 
321 
322     public XDrawPage getCurrentDrawPage (XDrawView xView)
323     {
324         XDrawPage xPage = null;
325         try
326         {
327             if (xView == null)
328                 MessageArea.println ("can't get current draw page from null view");
329             else
330                 xPage = xView.getCurrentPage();
331         }
332         catch (Exception e)
333         {
334             MessageArea.println ("caught exception while getting current draw page : " + e);
335         }
336 
337         return xPage;
338     }
339 
340 
341 
342 
343     /** @descr Return the current view of the given desktop.
344     */
345     public XDrawView getCurrentView ()
346     {
347         return getCurrentView (getDesktop());
348     }
349 
350     public XDrawView getCurrentView (XDesktop xDesktop)
351     {
352         if (xDesktop == null)
353             MessageArea.println ("can't get desktop to retrieve current view");
354 
355         XDrawView xView = null;
356         try
357         {
358             XComponent xComponent = xDesktop.getCurrentComponent();
359             if (xComponent == null)
360                 MessageArea.println ("can't get component to retrieve current view");
361 
362             XFrame xFrame = xDesktop.getCurrentFrame();
363             if (xFrame == null)
364                 MessageArea.println ("can't get frame to retrieve current view");
365 
366             XController xController = xFrame.getController();
367             if (xController == null)
368                 MessageArea.println ("can't get controller to retrieve current view");
369 
370             xView = (XDrawView) UnoRuntime.queryInterface(
371                 XDrawView.class, xController);
372             if (xView == null)
373                 MessageArea.println ("could not cast controller into view");
374         }
375         catch (Exception e)
376         {
377             MessageArea.println ("caught exception while getting current view : " + e);
378         }
379 
380         return xView;
381     }
382 
383 
384 
385 
386     //  Return the accessible object of the document window.
387     public static XAccessible getAccessibleDocumentWindow (XDrawPage xPage)
388     {
389         XIndexAccess xShapeList = (XIndexAccess) UnoRuntime.queryInterface(
390             XIndexAccess.class, xPage);
391         if (xShapeList.getCount() > 0)
392         {
393             // All shapes return as accessible object the document window's
394             // accessible object.  This is, of course, a hack and will be
395             // removed as soon as the missing infrastructure for obtaining
396             // the object directly is implemented.
397             XShape xShape = null;
398             try{
399                 xShape = (XShape) UnoRuntime.queryInterface(
400                     XShape.class, xShapeList.getByIndex (0));
401             } catch (Exception e)
402             {}
403             XAccessible xAccessible = (XAccessible) UnoRuntime.queryInterface (
404                 XAccessible.class, xShape);
405             return xAccessible;
406         }
407         else
408             return null;
409     }
410 }
411