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 .
20 import com
.sun
.star
.accessibility
.XAccessible
;
21 import com
.sun
.star
.accessibility
.XAccessibleComponent
;
22 import com
.sun
.star
.accessibility
.XAccessibleContext
;
23 import com
.sun
.star
.awt
.XWindow
;
24 import com
.sun
.star
.frame
.XController
;
25 import com
.sun
.star
.frame
.XFrame
;
26 import com
.sun
.star
.frame
.XModel
;
27 import com
.sun
.star
.uno
.UnoRuntime
;
28 import com
.sun
.star
.uno
.XInterface
;
30 import java
.io
.PrintWriter
;
33 public class AccessibilityTools
{
34 public static XAccessible SearchedAccessible
= null;
35 private static boolean debug
= false;
37 private AccessibilityTools() {}
39 public static XAccessible
getAccessibleObject(XInterface xObject
) {
40 return UnoRuntime
.queryInterface(XAccessible
.class, xObject
);
43 public static XWindow
getCurrentContainerWindow(XModel xModel
) {
44 return getWindow(xModel
, true);
47 public static XWindow
getCurrentWindow(XModel xModel
) {
48 return getWindow(xModel
, false);
51 private static XWindow
getWindow(XModel xModel
,
52 boolean containerWindow
) {
53 XWindow xWindow
= null;
56 XController xController
= xModel
.getCurrentController();
57 XFrame xFrame
= xController
.getFrame();
60 System
.out
.println("can't get frame from controller");
63 xWindow
= xFrame
.getContainerWindow();
65 xWindow
= xFrame
.getComponentWindow();
68 if (xWindow
== null) {
69 System
.out
.println("can't get window from frame");
71 } catch (Exception e
) {
72 System
.out
.println("caught exception while getting current window" + e
);
78 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
80 SearchedAccessible
= null;
81 return getAccessibleObjectForRole_(xacc
, role
);
84 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
86 boolean ignoreShowing
) {
87 SearchedAccessible
= null;
90 return getAccessibleObjectForRoleIgnoreShowing_(xacc
, role
);
92 return getAccessibleObjectForRole_(xacc
, role
);
96 private static XAccessibleContext
getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc
,
98 XAccessibleContext ac
= xacc
.getAccessibleContext();
102 if (ac
.getAccessibleRole() == role
) {
103 SearchedAccessible
= xacc
;
106 int k
= ac
.getAccessibleChildCount();
108 if (ac
.getAccessibleChildCount() > 100) {
112 for (int i
= 0; i
< k
; i
++) {
114 XAccessibleContext ac2
= getAccessibleObjectForRoleIgnoreShowing_(
115 ac
.getAccessibleChild(i
), role
);
120 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
121 System
.out
.println("Couldn't get Child");
128 private static XAccessibleContext
getAccessibleObjectForRole_(XAccessible xacc
,
130 XAccessibleContext ac
= xacc
.getAccessibleContext();
131 boolean isShowing
= ac
.getAccessibleStateSet()
132 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
134 if ((ac
.getAccessibleRole() == role
) && isShowing
) {
135 SearchedAccessible
= xacc
;
138 int k
= ac
.getAccessibleChildCount();
140 if (ac
.getAccessibleChildCount() > 100) {
144 for (int i
= 0; i
< k
; i
++) {
146 XAccessibleContext ac2
= getAccessibleObjectForRole_(ac
.getAccessibleChild(i
), role
);
151 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
152 System
.out
.println("Couldn't get Child");
159 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
162 return getAccessibleObjectForRole(xacc
, role
, name
, "");
165 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
168 boolean ignoreShowing
) {
170 return getAccessibleObjectForRoleIgnoreShowing(xacc
, role
, name
,
173 return getAccessibleObjectForRole(xacc
, role
, name
, "");
177 public static XAccessibleContext
getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc
,
181 XAccessibleContext ac
= xacc
.getAccessibleContext();
182 if ((ac
.getAccessibleRole() == role
) &&
183 (ac
.getAccessibleName().indexOf(name
) > -1) &&
184 (utils
.getImplName(ac
).indexOf(implName
) > -1)) {
185 SearchedAccessible
= xacc
;
189 int k
= ac
.getAccessibleChildCount();
191 if (ac
.getAccessibleChildCount() > 100) {
195 for (int i
= 0; i
< k
; i
++) {
197 XAccessibleContext ac1
= getAccessibleObjectForRoleIgnoreShowing(
198 ac
.getAccessibleChild(i
),
199 role
, name
, implName
);
204 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
205 System
.out
.println("Couldn't get Child");
213 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
217 XAccessibleContext ac
= xacc
.getAccessibleContext();
218 boolean isShowing
= ac
.getAccessibleStateSet()
219 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
221 // hotfix for i91828:
222 // if role to search is 0 then ignore the role.
223 if ( (role
== 0 || ac
.getAccessibleRole() == role
) &&
224 (ac
.getAccessibleName().indexOf(name
) > -1) &&
225 (utils
.getImplName(ac
).indexOf(implName
) > -1) &&
227 SearchedAccessible
= xacc
;
230 int k
= ac
.getAccessibleChildCount();
232 if (ac
.getAccessibleChildCount() > 100) {
236 for (int i
= 0; i
< k
; i
++) {
238 XAccessibleContext ac1
= getAccessibleObjectForRole(
239 ac
.getAccessibleChild(i
),
240 role
, name
, implName
);
245 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
246 System
.out
.println("Couldn't get Child");
254 public static void printAccessibleTree(PrintWriter log
, XAccessible xacc
, boolean debugIsActive
) {
255 debug
= debugIsActive
;
256 if (debug
) printAccessibleTree(log
, xacc
, "");
259 public static void printAccessibleTree(PrintWriter log
, XAccessible xacc
) {
260 printAccessibleTree(log
, xacc
, "");
263 private static void printAccessibleTree(PrintWriter log
,
264 XAccessible xacc
, String indent
) {
266 XAccessibleContext ac
= xacc
.getAccessibleContext();
268 logging(log
,indent
+ ac
.getAccessibleRole() + "," +
269 ac
.getAccessibleName() + "(" +
270 ac
.getAccessibleDescription() + "):" +
271 utils
.getImplName(ac
));
273 XAccessibleComponent aComp
= UnoRuntime
.queryInterface(
274 XAccessibleComponent
.class, xacc
);
277 String bounds
= "(" + aComp
.getBounds().X
+ "," +
278 aComp
.getBounds().Y
+ ")" + " (" +
279 aComp
.getBounds().Width
+ "," +
280 aComp
.getBounds().Height
+ ")";
281 bounds
= "The boundary Rectangle is " + bounds
;
282 logging(log
,indent
+ indent
+ bounds
);
285 boolean isShowing
= ac
.getAccessibleStateSet()
286 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
287 logging(log
,indent
+ indent
+ "StateType contains SHOWING: " +
290 int k
= ac
.getAccessibleChildCount();
292 if (ac
.getAccessibleChildCount() > 100) {
296 for (int i
= 0; i
< k
; i
++) {
298 printAccessibleTree(log
, ac
.getAccessibleChild(i
),
300 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
301 System
.out
.println("Couldn't get Child");
305 if (ac
.getAccessibleChildCount() > 100) {
306 k
= ac
.getAccessibleChildCount();
308 int st
= ac
.getAccessibleChildCount() - 50;
309 logging(log
,indent
+ " " + " ...... [skipped] ......");
311 for (int i
= st
; i
< k
; i
++) {
313 printAccessibleTree(log
, ac
.getAccessibleChild(i
),
315 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
316 System
.out
.println("Couldn't get Child");
322 public static String
accessibleToString(Object AC
) {
323 XAccessibleContext xAC
= UnoRuntime
.queryInterface(
324 XAccessibleContext
.class, AC
);
327 return xAC
.getAccessibleRole() + "," +
328 xAC
.getAccessibleName() + "(" +
329 xAC
.getAccessibleDescription() + "):";
332 XAccessible xA
= UnoRuntime
.queryInterface(
333 XAccessible
.class, AC
);
336 return "(Not supported)";
339 xAC
= xA
.getAccessibleContext();
341 return xAC
.getAccessibleRole() + "," + xAC
.getAccessibleName() +
342 "(" + xAC
.getAccessibleDescription() + ")";
345 public static boolean equals(XAccessible c1
, XAccessible c2
) {
346 if ((c1
== null) || (c2
== null)) {
350 return AccessibilityTools
.equals(c1
.getAccessibleContext(),
351 c2
.getAccessibleContext());
354 public static boolean equals(XAccessibleContext c1
, XAccessibleContext c2
) {
355 if ((c1
== null) || (c2
== null)) {
359 if (c1
.getAccessibleRole() != c2
.getAccessibleRole()) {
363 if (!c1
.getAccessibleName().equals(c2
.getAccessibleName())) {
367 if (!c1
.getAccessibleDescription()
368 .equals(c2
.getAccessibleDescription())) {
372 if (c1
.getAccessibleChildCount() != c2
.getAccessibleChildCount()) {
376 return AccessibilityTools
.equals(c1
.getAccessibleParent(),
377 c2
.getAccessibleParent());
380 private static void logging(PrintWriter log
, String content
){
381 if (debug
) log
.println(content
);