1 import java
.lang
.Thread
;
3 import com
.sun
.star
.awt
.Rectangle
;
4 import com
.sun
.star
.awt
.XWindow
;
6 import com
.sun
.star
.beans
.PropertyValue
;
7 import com
.sun
.star
.beans
.XPropertySet
;
9 import com
.sun
.star
.container
.XIndexAccess
;
10 import com
.sun
.star
.container
.XChild
;
11 import com
.sun
.star
.container
.XEnumerationAccess
;
12 import com
.sun
.star
.container
.XEnumeration
;
14 import com
.sun
.star
.frame
.XComponentLoader
;
15 import com
.sun
.star
.frame
.XController
;
16 import com
.sun
.star
.frame
.XDesktop
;
17 import com
.sun
.star
.frame
.XFrame
;
18 import com
.sun
.star
.frame
.XModel
;
19 import com
.sun
.star
.frame
.XTasksSupplier
;
20 import com
.sun
.star
.frame
.XTask
;
22 import com
.sun
.star
.lang
.XComponent
;
23 import com
.sun
.star
.lang
.XMultiServiceFactory
;
24 import com
.sun
.star
.lang
.XServiceInfo
;
25 import com
.sun
.star
.lang
.XServiceName
;
26 import com
.sun
.star
.lang
.XTypeProvider
;
28 import com
.sun
.star
.uno
.UnoRuntime
;
29 import com
.sun
.star
.uno
.XInterface
;
30 import com
.sun
.star
.uno
.Type
;
32 import com
.sun
.star
.drawing
.XDrawView
;
33 import com
.sun
.star
.drawing
.XDrawPage
;
34 import com
.sun
.star
.drawing
.XShapes
;
35 import com
.sun
.star
.drawing
.XShape
;
36 import com
.sun
.star
.drawing
.XShapeDescriptor
;
38 import com
.sun
.star
.accessibility
.XAccessible
;
39 import com
.sun
.star
.accessibility
.XAccessibleContext
;
40 import com
.sun
.star
.accessibility
.XAccessibleComponent
;
41 import com
.sun
.star
.accessibility
.XAccessibleRelationSet
;
42 import com
.sun
.star
.accessibility
.XAccessibleStateSet
;
44 import com
.sun
.star
.awt
.XExtendedToolkit
;
47 /** This class tries to simplify some tasks like loading a document or
48 getting various objects.
50 public class SimpleOffice
52 XDesktop mxDesktop
= null;
53 OfficeConnection aConnection
;
56 public SimpleOffice (int nPortNumber
)
58 mnPortNumber
= nPortNumber
;
63 public void connect ()
65 aConnection
= new OfficeConnection (mnPortNumber
);
70 public XModel
loadDocument (String URL
)
75 // Load the document from the specified URL.
76 XComponentLoader xLoader
=
77 (XComponentLoader
)UnoRuntime
.queryInterface(
78 XComponentLoader
.class, mxDesktop
);
80 XComponent xComponent
= xLoader
.loadComponentFromURL (
87 xModel
= (XModel
) UnoRuntime
.queryInterface(
88 XModel
.class, xComponent
);
90 catch (java
.lang
.NullPointerException e
)
92 MessageArea
.println ("caught exception while loading "
97 MessageArea
.println ("caught exception while loading "
106 public XModel
getModel (String name
)
108 XModel xModel
= null;
111 XTasksSupplier xTasksSupplier
=
112 (XTasksSupplier
) UnoRuntime
.queryInterface(
113 XTasksSupplier
.class, mxDesktop
);
114 XEnumerationAccess xEA
= xTasksSupplier
.getTasks();
115 XEnumeration xE
= xEA
.createEnumeration();
116 while (xE
.hasMoreElements())
118 XTask xTask
= (XTask
) UnoRuntime
.queryInterface(
119 XTask
.class, xE
.nextElement());
120 MessageArea
.print (xTask
.getName());
125 MessageArea
.println ("caught exception while getting Model " + name
132 public XModel
getModel (XDrawView xView
)
134 XController xController
= (XController
) UnoRuntime
.queryInterface(
135 XController
.class, xView
);
136 if (xController
!= null)
137 return xController
.getModel();
140 MessageArea
.println ("can't cast view to controller");
145 public XDesktop
getDesktop ()
147 if (mxDesktop
!= null)
151 // Get the factory of the connected office.
152 XMultiServiceFactory xMSF
= aConnection
.getServiceManager ();
155 MessageArea
.println ("can't connect to office");
159 MessageArea
.println ("Connected successfully.");
161 // Create a new desktop.
162 mxDesktop
= (XDesktop
) UnoRuntime
.queryInterface(
164 xMSF
.createInstance ("com.sun.star.frame.Desktop")
169 MessageArea
.println ("caught exception while creating desktop: "
177 /** Return a reference to the extended toolkit which is a broadcaster of
178 top window, key, and focus events.
180 public XExtendedToolkit
getExtendedToolkit ()
182 XExtendedToolkit xToolkit
= null;
185 // Get the factory of the connected office.
186 XMultiServiceFactory xMSF
= aConnection
.getServiceManager ();
189 xToolkit
= (XExtendedToolkit
) UnoRuntime
.queryInterface(
190 XExtendedToolkit
.class,
191 xMSF
.createInstance ("stardiv.Toolkit.VCLXToolkit")
197 MessageArea
.println ("caught exception while creating extended toolkit: " + e
);
205 public XAccessible
getAccessibleObject (XInterface xObject
)
207 XAccessible xAccessible
= null;
210 xAccessible
= (XAccessible
) UnoRuntime
.queryInterface(
211 XAccessible
.class, xObject
);
215 MessageArea
.println (
216 "caught exception while getting accessible object" + e
);
222 /** Return the root object of the accessibility hierarchy.
224 public XAccessible
getAccessibleRoot (XAccessible xAccessible
)
228 XAccessible xParent
= null;
231 XAccessibleContext xContext
= xAccessible
.getAccessibleContext();
232 if (xContext
!= null)
233 xParent
= xContext
.getAccessibleParent();
235 xAccessible
= xParent
;
237 while (xParent
!= null);
241 MessageArea
.println (
242 "caught exception while getting accessible root" + e
);
251 /** @descr Return the current window associated with the given
254 public XWindow
getCurrentWindow ()
256 return getCurrentWindow ((XModel
) UnoRuntime
.queryInterface(
257 XModel
.class, getDesktop()));
264 public XWindow
getCurrentWindow (XModel xModel
)
266 XWindow xWindow
= null;
270 MessageArea
.println ("invalid model (==null)");
271 XController xController
= xModel
.getCurrentController();
272 if (xController
== null)
273 MessageArea
.println ("can't get controller from model");
274 XFrame xFrame
= xController
.getFrame();
276 MessageArea
.println ("can't get frame from controller");
277 xWindow
= xFrame
.getComponentWindow ();
279 MessageArea
.println ("can't get window from frame");
283 MessageArea
.println ("caught exception while getting current window" + e
);
290 /** @descr Return the current draw page of the given desktop.
292 public XDrawPage
getCurrentDrawPage ()
294 return getCurrentDrawPage ((XDrawView
) UnoRuntime
.queryInterface(
295 XDrawView
.class, getCurrentView()));
301 public XDrawPage
getCurrentDrawPage (XDrawView xView
)
303 XDrawPage xPage
= null;
307 MessageArea
.println ("can't get current draw page from null view");
309 xPage
= xView
.getCurrentPage();
313 MessageArea
.println ("caught exception while getting current draw page : " + e
);
322 /** @descr Return the current view of the given desktop.
324 public XDrawView
getCurrentView ()
326 return getCurrentView (getDesktop());
329 public XDrawView
getCurrentView (XDesktop xDesktop
)
331 if (xDesktop
== null)
332 MessageArea
.println ("can't get desktop to retrieve current view");
334 XDrawView xView
= null;
337 XComponent xComponent
= xDesktop
.getCurrentComponent();
338 if (xComponent
== null)
339 MessageArea
.println ("can't get component to retrieve current view");
341 XFrame xFrame
= xDesktop
.getCurrentFrame();
343 MessageArea
.println ("can't get frame to retrieve current view");
345 XController xController
= xFrame
.getController();
346 if (xController
== null)
347 MessageArea
.println ("can't get controller to retrieve current view");
349 xView
= (XDrawView
) UnoRuntime
.queryInterface(
350 XDrawView
.class, xController
);
352 MessageArea
.println ("could not cast controller into view");
356 MessageArea
.println ("caught exception while getting current view : " + e
);
365 // Return the accessible object of the document window.
366 public static XAccessible
getAccessibleDocumentWindow (XDrawPage xPage
)
368 XIndexAccess xShapeList
= (XIndexAccess
) UnoRuntime
.queryInterface(
369 XIndexAccess
.class, xPage
);
370 if (xShapeList
.getCount() > 0)
372 // All shapes return as accessible object the document window's
373 // accessible object. This is, of course, a hack and will be
374 // removed as soon as the missing infrastructure for obtaining
375 // the object directly is implemented.
376 XShape xShape
= null;
378 xShape
= (XShape
) UnoRuntime
.queryInterface(
379 XShape
.class, xShapeList
.getByIndex (0));
380 } catch (Exception e
)
382 XAccessible xAccessible
= (XAccessible
) UnoRuntime
.queryInterface (
383 XAccessible
.class, xShape
);