bump product version to 4.2.0.1
[LibreOffice.git] / toolkit / test / accessibility / SimpleOffice.java
blob634169141f714e00cca58b1d9c4b9b5992b17f70
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com.sun.star.awt.XWindow;
21 import com.sun.star.beans.PropertyValue;
22 import com.sun.star.container.XIndexAccess;
23 import com.sun.star.container.XEnumerationAccess;
24 import com.sun.star.container.XEnumeration;
26 import com.sun.star.frame.XComponentLoader;
27 import com.sun.star.frame.XController;
28 import com.sun.star.frame.XDesktop;
29 import com.sun.star.frame.XFrame;
30 import com.sun.star.frame.XModel;
31 import com.sun.star.frame.XTasksSupplier;
32 import com.sun.star.frame.XTask;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
38 import com.sun.star.drawing.XDrawView;
39 import com.sun.star.drawing.XDrawPage;
40 import com.sun.star.drawing.XShape;
41 import com.sun.star.accessibility.XAccessible;
42 import com.sun.star.accessibility.XAccessibleContext;
43 import com.sun.star.awt.XExtendedToolkit;
46 /** This class tries to simplify some tasks like loading a document or
47 getting various objects.
49 public class SimpleOffice
51 XDesktop mxDesktop = null;
52 OfficeConnection aConnection;
53 int mnPortNumber;
55 public SimpleOffice (int nPortNumber)
57 mnPortNumber = nPortNumber;
58 connect ();
59 getDesktop ();
62 public void connect ()
64 aConnection = new OfficeConnection (mnPortNumber);
65 mxDesktop = null;
66 getDesktop ();
69 public XModel loadDocument (String URL)
71 XModel xModel = null;
72 try
74 // Load the document from the specified URL.
75 XComponentLoader xLoader =
76 UnoRuntime.queryInterface(
77 XComponentLoader.class, mxDesktop);
79 XComponent xComponent = xLoader.loadComponentFromURL (
80 URL,
81 "_blank",
83 new PropertyValue[0]
86 xModel = UnoRuntime.queryInterface(
87 XModel.class, xComponent);
89 catch (java.lang.NullPointerException e)
91 MessageArea.println ("caught exception while loading "
92 + URL + " : " + e);
94 catch (Exception e)
96 MessageArea.println ("caught exception while loading "
97 + URL + " : " + e);
99 return xModel;
105 public XModel getModel (String name)
107 XModel xModel = null;
110 XTasksSupplier xTasksSupplier =
111 UnoRuntime.queryInterface(
112 XTasksSupplier.class, mxDesktop);
113 XEnumerationAccess xEA = xTasksSupplier.getTasks();
114 XEnumeration xE = xEA.createEnumeration();
115 while (xE.hasMoreElements())
117 XTask xTask = UnoRuntime.queryInterface(
118 XTask.class, xE.nextElement());
119 MessageArea.print (xTask.getName());
122 catch (Exception e)
124 MessageArea.println ("caught exception while getting Model " + name
125 + ": " + e);
127 return xModel;
131 public XModel getModel (XDrawView xView)
133 XController xController = UnoRuntime.queryInterface(
134 XController.class, xView);
135 if (xController != null)
136 return xController.getModel();
137 else
139 MessageArea.println ("can't cast view to controller");
140 return null;
144 public XDesktop getDesktop ()
146 if (mxDesktop != null)
147 return mxDesktop;
150 // Get the factory of the connected office.
151 XMultiServiceFactory xMSF = aConnection.getServiceManager ();
152 if (xMSF == null)
154 MessageArea.println ("can't connect to office");
155 return null;
157 else
158 MessageArea.println ("Connected successfully.");
160 // Create a new desktop.
161 mxDesktop = UnoRuntime.queryInterface(
162 XDesktop.class,
163 xMSF.createInstance ("com.sun.star.frame.Desktop")
166 catch (Exception e)
168 MessageArea.println ("caught exception while creating desktop: "
169 + e);
172 return mxDesktop;
176 /** Return a reference to the extended toolkit which is a broadcaster of
177 top window, key, and focus events.
179 public XExtendedToolkit getExtendedToolkit ()
181 XExtendedToolkit xToolkit = null;
184 // Get the factory of the connected office.
185 XMultiServiceFactory xMSF = aConnection.getServiceManager ();
186 if (xMSF != null)
188 xToolkit = UnoRuntime.queryInterface(
189 XExtendedToolkit.class,
190 xMSF.createInstance ("stardiv.Toolkit.VCLXToolkit")
194 catch (Exception e)
196 MessageArea.println ("caught exception while creating extended toolkit: " + e);
199 return xToolkit;
204 public XAccessible getAccessibleObject (XInterface xObject)
206 XAccessible xAccessible = null;
209 xAccessible = UnoRuntime.queryInterface(
210 XAccessible.class, xObject);
212 catch (Exception e)
214 MessageArea.println (
215 "caught exception while getting accessible object" + e);
216 e.printStackTrace();
218 return xAccessible;
221 /** Return the root object of the accessibility hierarchy.
223 public XAccessible getAccessibleRoot (XAccessible xAccessible)
227 XAccessible xParent = null;
230 XAccessibleContext xContext = xAccessible.getAccessibleContext();
231 if (xContext != null)
232 xParent = xContext.getAccessibleParent();
233 if (xParent != null)
234 xAccessible = xParent;
236 while (xParent != null);
238 catch (Exception e)
240 MessageArea.println (
241 "caught exception while getting accessible root" + e);
242 e.printStackTrace();
244 return xAccessible;
250 /** @descr Return the current window associated with the given
251 model.
253 public XWindow getCurrentWindow ()
255 return getCurrentWindow (UnoRuntime.queryInterface(
256 XModel.class, getDesktop()));
263 public XWindow getCurrentWindow (XModel xModel)
265 XWindow xWindow = null;
268 if (xModel == null)
269 MessageArea.println ("invalid model (==null)");
270 XController xController = xModel.getCurrentController();
271 if (xController == null)
272 MessageArea.println ("can't get controller from model");
273 XFrame xFrame = xController.getFrame();
274 if (xFrame == null)
275 MessageArea.println ("can't get frame from controller");
276 xWindow = xFrame.getComponentWindow ();
277 if (xWindow == null)
278 MessageArea.println ("can't get window from frame");
280 catch (Exception e)
282 MessageArea.println ("caught exception while getting current window" + e);
285 return xWindow;
289 /** @descr Return the current draw page of the given desktop.
291 public XDrawPage getCurrentDrawPage ()
293 return getCurrentDrawPage (UnoRuntime.queryInterface(
294 XDrawView.class, getCurrentView()));
300 public XDrawPage getCurrentDrawPage (XDrawView xView)
302 XDrawPage xPage = null;
305 if (xView == null)
306 MessageArea.println ("can't get current draw page from null view");
307 else
308 xPage = xView.getCurrentPage();
310 catch (Exception e)
312 MessageArea.println ("caught exception while getting current draw page : " + e);
315 return xPage;
321 /** @descr Return the current view of the given desktop.
323 public XDrawView getCurrentView ()
325 return getCurrentView (getDesktop());
328 public XDrawView getCurrentView (XDesktop xDesktop)
330 if (xDesktop == null)
331 MessageArea.println ("can't get desktop to retrieve current view");
333 XDrawView xView = null;
336 XComponent xComponent = xDesktop.getCurrentComponent();
337 if (xComponent == null)
338 MessageArea.println ("can't get component to retrieve current view");
340 XFrame xFrame = xDesktop.getCurrentFrame();
341 if (xFrame == null)
342 MessageArea.println ("can't get frame to retrieve current view");
344 XController xController = xFrame.getController();
345 if (xController == null)
346 MessageArea.println ("can't get controller to retrieve current view");
348 xView = UnoRuntime.queryInterface(
349 XDrawView.class, xController);
350 if (xView == null)
351 MessageArea.println ("could not cast controller into view");
353 catch (Exception e)
355 MessageArea.println ("caught exception while getting current view : " + e);
358 return xView;
364 // Return the accessible object of the document window.
365 public static XAccessible getAccessibleDocumentWindow (XDrawPage xPage)
367 XIndexAccess xShapeList = UnoRuntime.queryInterface(
368 XIndexAccess.class, xPage);
369 if (xShapeList.getCount() > 0)
371 // All shapes return as accessible object the document window's
372 // accessible object. This is, of course, a hack and will be
373 // removed as soon as the missing infrastructure for obtaining
374 // the object directly is implemented.
375 XShape xShape = null;
376 try{
377 xShape = UnoRuntime.queryInterface(
378 XShape.class, xShapeList.getByIndex (0));
379 } catch (Exception e)
381 XAccessible xAccessible = UnoRuntime.queryInterface (
382 XAccessible.class, xShape);
383 return xAccessible;
385 else
386 return null;