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
.lang
.XMultiServiceFactory
;
28 import com
.sun
.star
.uno
.UnoRuntime
;
29 import com
.sun
.star
.uno
.XInterface
;
31 import java
.io
.PrintWriter
;
34 public class AccessibilityTools
{
35 public static XAccessible SearchedAccessible
= null;
36 private static boolean debug
= false;
38 public AccessibilityTools() {
42 public static XAccessible
getAccessibleObject(XInterface xObject
) {
43 return UnoRuntime
.queryInterface(XAccessible
.class, xObject
);
46 public static XWindow
getCurrentContainerWindow(XMultiServiceFactory msf
,
48 return getWindow(msf
, xModel
, true);
51 public static XWindow
getCurrentWindow(XMultiServiceFactory msf
,
53 return getWindow(msf
, xModel
, false);
56 private static XWindow
getWindow(XMultiServiceFactory msf
, XModel xModel
,
57 boolean containerWindow
) {
58 XWindow xWindow
= null;
62 System
.out
.println("invalid model (==null)");
65 XController xController
= xModel
.getCurrentController();
67 if (xController
== null) {
68 System
.out
.println("can't get controller from model");
71 XFrame xFrame
= xController
.getFrame();
74 System
.out
.println("can't get frame from controller");
78 xWindow
= xFrame
.getContainerWindow();
80 xWindow
= xFrame
.getComponentWindow();
82 if (xWindow
== null) {
83 System
.out
.println("can't get window from frame");
85 } catch (Exception e
) {
86 System
.out
.println("caught exception while getting current window" + e
);
92 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
94 SearchedAccessible
= null;
95 return getAccessibleObjectForRole_(xacc
, role
);
98 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
100 boolean ignoreShowing
) {
101 SearchedAccessible
= null;
104 return getAccessibleObjectForRoleIgnoreShowing_(xacc
, role
);
106 return getAccessibleObjectForRole_(xacc
, role
);
110 public static XAccessibleContext
getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc
,
112 XAccessibleContext ac
= xacc
.getAccessibleContext();
114 if (ac
.getAccessibleRole() == role
) {
115 SearchedAccessible
= xacc
;
118 int k
= ac
.getAccessibleChildCount();
120 if (ac
.getAccessibleChildCount() > 100) {
124 for (int i
= 0; i
< k
; i
++) {
126 XAccessibleContext ac2
= getAccessibleObjectForRoleIgnoreShowing_(
127 ac
.getAccessibleChild(i
), role
);
132 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
133 System
.out
.println("Couldn't get Child");
140 public static XAccessibleContext
getAccessibleObjectForRole_(XAccessible xacc
,
142 XAccessibleContext ac
= xacc
.getAccessibleContext();
143 boolean isShowing
= ac
.getAccessibleStateSet()
144 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
146 if ((ac
.getAccessibleRole() == role
) && isShowing
) {
147 SearchedAccessible
= xacc
;
150 int k
= ac
.getAccessibleChildCount();
152 if (ac
.getAccessibleChildCount() > 100) {
156 for (int i
= 0; i
< k
; i
++) {
158 XAccessibleContext ac2
= getAccessibleObjectForRole_(ac
.getAccessibleChild(i
), role
);
163 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
164 System
.out
.println("Couldn't get Child");
171 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
174 return getAccessibleObjectForRole(xacc
, role
, name
, "");
177 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
180 boolean ignoreShowing
) {
182 return getAccessibleObjectForRoleIgnoreShowing(xacc
, role
, name
,
185 return getAccessibleObjectForRole(xacc
, role
, name
, "");
189 public static XAccessibleContext
getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc
,
193 XAccessibleContext ac
= xacc
.getAccessibleContext();
194 if ((ac
.getAccessibleRole() == role
) &&
195 (ac
.getAccessibleName().indexOf(name
) > -1) &&
196 (utils
.getImplName(ac
).indexOf(implName
) > -1)) {
197 SearchedAccessible
= xacc
;
199 //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
202 int k
= ac
.getAccessibleChildCount();
204 if (ac
.getAccessibleChildCount() > 100) {
208 for (int i
= 0; i
< k
; i
++) {
210 XAccessibleContext ac1
= getAccessibleObjectForRoleIgnoreShowing(
211 ac
.getAccessibleChild(i
),
212 role
, name
, implName
);
217 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
218 System
.out
.println("Couldn't get Child");
226 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
230 XAccessibleContext ac
= xacc
.getAccessibleContext();
231 boolean isShowing
= ac
.getAccessibleStateSet()
232 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
234 // hotfix for i91828:
235 // if role to search is 0 then ignore the role.
236 if ( (role
== 0 || ac
.getAccessibleRole() == role
) &&
237 (ac
.getAccessibleName().indexOf(name
) > -1) &&
238 (utils
.getImplName(ac
).indexOf(implName
) > -1) &&
240 SearchedAccessible
= xacc
;
241 //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
244 int k
= ac
.getAccessibleChildCount();
246 if (ac
.getAccessibleChildCount() > 100) {
250 for (int i
= 0; i
< k
; i
++) {
252 XAccessibleContext ac1
= getAccessibleObjectForRole(
253 ac
.getAccessibleChild(i
),
254 role
, name
, implName
);
259 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
260 System
.out
.println("Couldn't get Child");
269 * This methods retunrs the <CODE>XAccessibleContext</CODE> of a named Sheet-Cell like "G5".<p>
270 * @param xSheetAcc The <CODE>XAccessibleContext</CODE> of a Sheet
271 * @param cellName The name of a cell like "A5"
272 * @return the <CODE>XAccessiblecontext</CODE> of the named cell
274 public static XAccessibleContext
getSheetCell(XAccessibleContext xSheetAcc
, String cellName
){
279 for (int n
=0; n
<cellName
.length(); n
++){
280 String cha
= cellName
.substring(n
,n
+1);
281 System
.out
.println("char: " + cha
+ " ");
283 byte[] bytes
= cha
.getBytes();
285 if ((bytes
[0] >= 'A') && (bytes
[0] <= 'Z')){
286 charMem
= bytes
[0]-64;
289 cellIndex
+= charMem
* 26;
291 cellIndex
= cellIndex
+ (bytes
[0]-65);
293 String sNumb
= cellName
.substring(n
, cellName
.length());
294 int iNumb
= Integer
.valueOf(sNumb
).intValue();
295 cellIndex
+= (iNumb
-1) * 256;
296 System
.out
.println("numb:" + (iNumb
-1) * 256);
301 //System.out.println("cellName: " + cellName + " cellIndex: " + cellIndex);
304 XAccessibleContext ac
= xSheetAcc
.getAccessibleChild(cellIndex
).getAccessibleContext();
305 System
.out
.println(ac
.getAccessibleRole() + "," +
306 ac
.getAccessibleName() + "(" +
307 ac
.getAccessibleDescription() + "):" +
308 utils
.getImplName(ac
));
311 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException ex
) {
312 System
.out
.println("ERROR: could not get child at index " + cellIndex
+"': " + ex
.toString());
317 public static void printAccessibleTree(PrintWriter log
, XAccessible xacc
, boolean debugIsActive
) {
318 debug
= debugIsActive
;
319 if (debug
) printAccessibleTree(log
, xacc
, "");
322 public static void printAccessibleTree(PrintWriter log
, XAccessible xacc
) {
323 printAccessibleTree(log
, xacc
, "");
326 protected static void printAccessibleTree(PrintWriter log
,
327 XAccessible xacc
, String indent
) {
329 XAccessibleContext ac
= xacc
.getAccessibleContext();
331 logging(log
,indent
+ ac
.getAccessibleRole() + "," +
332 ac
.getAccessibleName() + "(" +
333 ac
.getAccessibleDescription() + "):" +
334 utils
.getImplName(ac
));
336 XAccessibleComponent aComp
= UnoRuntime
.queryInterface(
337 XAccessibleComponent
.class, xacc
);
340 String bounds
= "(" + aComp
.getBounds().X
+ "," +
341 aComp
.getBounds().Y
+ ")" + " (" +
342 aComp
.getBounds().Width
+ "," +
343 aComp
.getBounds().Height
+ ")";
344 bounds
= "The boundary Rectangle is " + bounds
;
345 logging(log
,indent
+ indent
+ bounds
);
348 boolean isShowing
= ac
.getAccessibleStateSet()
349 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
350 logging(log
,indent
+ indent
+ "StateType contains SHOWING: " +
353 int k
= ac
.getAccessibleChildCount();
355 if (ac
.getAccessibleChildCount() > 100) {
359 for (int i
= 0; i
< k
; i
++) {
361 printAccessibleTree(log
, ac
.getAccessibleChild(i
),
363 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
364 System
.out
.println("Couldn't get Child");
368 if (ac
.getAccessibleChildCount() > 100) {
369 k
= ac
.getAccessibleChildCount();
371 int st
= ac
.getAccessibleChildCount() - 50;
372 logging(log
,indent
+ " " + " ...... [skipped] ......");
374 for (int i
= st
; i
< k
; i
++) {
376 printAccessibleTree(log
, ac
.getAccessibleChild(i
),
378 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
379 System
.out
.println("Couldn't get Child");
385 public static String
accessibleToString(Object AC
) {
386 XAccessibleContext xAC
= UnoRuntime
.queryInterface(
387 XAccessibleContext
.class, AC
);
390 return "" + xAC
.getAccessibleRole() + "," +
391 xAC
.getAccessibleName() + "(" +
392 xAC
.getAccessibleDescription() + "):";
395 XAccessible xA
= UnoRuntime
.queryInterface(
396 XAccessible
.class, AC
);
399 return "(Not supported)";
402 xAC
= xA
.getAccessibleContext();
404 return "" + xAC
.getAccessibleRole() + "," + xAC
.getAccessibleName() +
405 "(" + xAC
.getAccessibleDescription() + ")";
408 public static boolean equals(XAccessible c1
, XAccessible c2
) {
409 if ((c1
== null) || (c2
== null)) {
413 return AccessibilityTools
.equals(c1
.getAccessibleContext(),
414 c2
.getAccessibleContext());
417 public static boolean equals(XAccessibleContext c1
, XAccessibleContext c2
) {
418 if ((c1
== null) || (c2
== null)) {
422 if (c1
.getAccessibleRole() != c2
.getAccessibleRole()) {
426 if (!c1
.getAccessibleName().equals(c2
.getAccessibleName())) {
430 if (!c1
.getAccessibleDescription()
431 .equals(c2
.getAccessibleDescription())) {
435 if (c1
.getAccessibleChildCount() != c2
.getAccessibleChildCount()) {
439 return AccessibilityTools
.equals(c1
.getAccessibleParent(),
440 c2
.getAccessibleParent());
443 private static void logging(PrintWriter log
, String content
){
444 if (debug
) log
.println(content
);