merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / test / accessibility / SimpleOffice.java
blob4417267675af06eef80ab8bfcc9857adf70d2bee
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;
54 int mnPortNumber;
56 public SimpleOffice (int nPortNumber)
58 mnPortNumber = nPortNumber;
59 connect ();
60 getDesktop ();
63 public void connect ()
65 aConnection = new OfficeConnection (mnPortNumber);
66 mxDesktop = null;
67 getDesktop ();
70 public XModel loadDocument (String URL)
72 XModel xModel = null;
73 try
75 // Load the document from the specified URL.
76 XComponentLoader xLoader =
77 (XComponentLoader)UnoRuntime.queryInterface(
78 XComponentLoader.class, mxDesktop);
80 XComponent xComponent = xLoader.loadComponentFromURL (
81 URL,
82 "_blank",
84 new PropertyValue[0]
87 xModel = (XModel) UnoRuntime.queryInterface(
88 XModel.class, xComponent);
90 catch (java.lang.NullPointerException e)
92 MessageArea.println ("caught exception while loading "
93 + URL + " : " + e);
95 catch (Exception e)
97 MessageArea.println ("caught exception while loading "
98 + URL + " : " + e);
100 return xModel;
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());
123 catch (Exception e)
125 MessageArea.println ("caught exception while getting Model " + name
126 + ": " + e);
128 return xModel;
132 public XModel getModel (XDrawView xView)
134 XController xController = (XController) UnoRuntime.queryInterface(
135 XController.class, xView);
136 if (xController != null)
137 return xController.getModel();
138 else
140 MessageArea.println ("can't cast view to controller");
141 return null;
145 public XDesktop getDesktop ()
147 if (mxDesktop != null)
148 return mxDesktop;
151 // Get the factory of the connected office.
152 XMultiServiceFactory xMSF = aConnection.getServiceManager ();
153 if (xMSF == null)
155 MessageArea.println ("can't connect to office");
156 return null;
158 else
159 MessageArea.println ("Connected successfully.");
161 // Create a new desktop.
162 mxDesktop = (XDesktop) UnoRuntime.queryInterface(
163 XDesktop.class,
164 xMSF.createInstance ("com.sun.star.frame.Desktop")
167 catch (Exception e)
169 MessageArea.println ("caught exception while creating desktop: "
170 + e);
173 return mxDesktop;
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 ();
187 if (xMSF != null)
189 xToolkit = (XExtendedToolkit) UnoRuntime.queryInterface(
190 XExtendedToolkit.class,
191 xMSF.createInstance ("stardiv.Toolkit.VCLXToolkit")
195 catch (Exception e)
197 MessageArea.println ("caught exception while creating extended toolkit: " + e);
200 return xToolkit;
205 public XAccessible getAccessibleObject (XInterface xObject)
207 XAccessible xAccessible = null;
210 xAccessible = (XAccessible) UnoRuntime.queryInterface(
211 XAccessible.class, xObject);
213 catch (Exception e)
215 MessageArea.println (
216 "caught exception while getting accessible object" + e);
217 e.printStackTrace();
219 return xAccessible;
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();
234 if (xParent != null)
235 xAccessible = xParent;
237 while (xParent != null);
239 catch (Exception e)
241 MessageArea.println (
242 "caught exception while getting accessible root" + e);
243 e.printStackTrace();
245 return xAccessible;
251 /** @descr Return the current window associated with the given
252 model.
254 public XWindow getCurrentWindow ()
256 return getCurrentWindow ((XModel) UnoRuntime.queryInterface(
257 XModel.class, getDesktop()));
264 public XWindow getCurrentWindow (XModel xModel)
266 XWindow xWindow = null;
269 if (xModel == 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();
275 if (xFrame == null)
276 MessageArea.println ("can't get frame from controller");
277 xWindow = xFrame.getComponentWindow ();
278 if (xWindow == null)
279 MessageArea.println ("can't get window from frame");
281 catch (Exception e)
283 MessageArea.println ("caught exception while getting current window" + e);
286 return xWindow;
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;
306 if (xView == null)
307 MessageArea.println ("can't get current draw page from null view");
308 else
309 xPage = xView.getCurrentPage();
311 catch (Exception e)
313 MessageArea.println ("caught exception while getting current draw page : " + e);
316 return xPage;
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();
342 if (xFrame == null)
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);
351 if (xView == null)
352 MessageArea.println ("could not cast controller into view");
354 catch (Exception e)
356 MessageArea.println ("caught exception while getting current view : " + e);
359 return xView;
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;
377 try{
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);
384 return xAccessible;
386 else
387 return null;