1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: AccessibilityTools.java,v $
10 * $Revision: 1.12.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import com
.sun
.star
.accessibility
.XAccessible
;
33 import com
.sun
.star
.accessibility
.XAccessibleComponent
;
34 import com
.sun
.star
.accessibility
.XAccessibleContext
;
35 import com
.sun
.star
.awt
.XWindow
;
36 import com
.sun
.star
.frame
.XController
;
37 import com
.sun
.star
.frame
.XFrame
;
38 import com
.sun
.star
.frame
.XModel
;
39 import com
.sun
.star
.lang
.XMultiServiceFactory
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.uno
.XInterface
;
43 import java
.io
.PrintWriter
;
46 public class AccessibilityTools
{
47 public static XAccessibleContext SearchedContext
= null;
48 public static XAccessible SearchedAccessible
= null;
49 private static boolean debug
= false;
51 public AccessibilityTools() {
53 SearchedContext
= null;
56 public static XAccessible
getAccessibleObject(XInterface xObject
) {
57 XAccessible xAccessible
= null;
60 xAccessible
= (XAccessible
) UnoRuntime
.queryInterface(
61 XAccessible
.class, xObject
);
62 } catch (Exception e
) {
64 "caught exception while getting accessible object" + e
);
71 public static XWindow
getCurrentContainerWindow(XMultiServiceFactory msf
,
73 return getWindow(msf
, xModel
, true);
76 public static XWindow
getCurrentWindow(XMultiServiceFactory msf
,
78 return getWindow(msf
, xModel
, false);
81 private static XWindow
getWindow(XMultiServiceFactory msf
, XModel xModel
,
82 boolean containerWindow
) {
83 XWindow xWindow
= null;
87 System
.out
.println("invalid model (==null)");
90 XController xController
= xModel
.getCurrentController();
92 if (xController
== null) {
93 System
.out
.println("can't get controller from model");
96 XFrame xFrame
= xController
.getFrame();
99 System
.out
.println("can't get frame from controller");
103 xWindow
= xFrame
.getContainerWindow();
105 xWindow
= xFrame
.getComponentWindow();
107 if (xWindow
== null) {
108 System
.out
.println("can't get window from frame");
110 } catch (Exception e
) {
111 System
.out
.println("caught exception while getting current window" + e
);
117 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
119 SearchedContext
= null;
120 SearchedAccessible
= null;
121 getAccessibleObjectForRole_(xacc
, role
);
123 return SearchedContext
;
126 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
128 boolean ignoreShowing
) {
129 SearchedContext
= null;
130 SearchedAccessible
= null;
133 getAccessibleObjectForRoleIgnoreShowing_(xacc
, role
);
135 getAccessibleObjectForRole_(xacc
, role
);
138 return SearchedContext
;
141 public static void getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc
,
143 XAccessibleContext ac
= xacc
.getAccessibleContext();
145 if (ac
.getAccessibleRole() == role
) {
146 SearchedContext
= ac
;
147 SearchedAccessible
= xacc
;
149 int k
= ac
.getAccessibleChildCount();
151 if (ac
.getAccessibleChildCount() > 100) {
155 for (int i
= 0; i
< k
; i
++) {
157 getAccessibleObjectForRoleIgnoreShowing_(
158 ac
.getAccessibleChild(i
), role
);
160 if (SearchedContext
!= null) {
163 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
164 System
.out
.println("Couldn't get Child");
170 public static void getAccessibleObjectForRole_(XAccessible xacc
,
172 XAccessibleContext ac
= xacc
.getAccessibleContext();
173 boolean isShowing
= ac
.getAccessibleStateSet()
174 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
176 if ((ac
.getAccessibleRole() == role
) && isShowing
) {
177 SearchedContext
= ac
;
178 SearchedAccessible
= xacc
;
180 int k
= ac
.getAccessibleChildCount();
182 if (ac
.getAccessibleChildCount() > 100) {
186 for (int i
= 0; i
< k
; i
++) {
188 getAccessibleObjectForRole_(ac
.getAccessibleChild(i
), role
);
190 if (SearchedContext
!= null) {
193 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
194 System
.out
.println("Couldn't get Child");
200 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
203 return getAccessibleObjectForRole(xacc
, role
, name
, "");
206 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
209 boolean ignoreShowing
) {
211 return getAccessibleObjectForRoleIgnoreShowing(xacc
, role
, name
,
214 return getAccessibleObjectForRole(xacc
, role
, name
, "");
218 public static XAccessibleContext
getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc
,
222 XAccessibleContext ac
= xacc
.getAccessibleContext();
223 if ((ac
.getAccessibleRole() == role
) &&
224 (ac
.getAccessibleName().indexOf(name
) > -1) &&
225 (utils
.getImplName(ac
).indexOf(implName
) > -1)) {
226 SearchedAccessible
= xacc
;
228 //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
231 int k
= ac
.getAccessibleChildCount();
233 if (ac
.getAccessibleChildCount() > 100) {
237 for (int i
= 0; i
< k
; i
++) {
239 XAccessibleContext ac1
= getAccessibleObjectForRoleIgnoreShowing(
240 ac
.getAccessibleChild(i
),
241 role
, name
, implName
);
246 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
247 System
.out
.println("Couldn't get Child");
255 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
259 XAccessibleContext ac
= xacc
.getAccessibleContext();
260 boolean isShowing
= ac
.getAccessibleStateSet()
261 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
263 // hotfix for i91828:
264 // if role to serach is 0 then ignore the role.
265 if ( (role
== 0 || ac
.getAccessibleRole() == role
) &&
266 (ac
.getAccessibleName().indexOf(name
) > -1) &&
267 (utils
.getImplName(ac
).indexOf(implName
) > -1) &&
269 SearchedAccessible
= xacc
;
270 //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
273 int k
= ac
.getAccessibleChildCount();
275 if (ac
.getAccessibleChildCount() > 100) {
279 for (int i
= 0; i
< k
; i
++) {
281 XAccessibleContext ac1
= getAccessibleObjectForRole(
282 ac
.getAccessibleChild(i
),
283 role
, name
, implName
);
288 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
289 System
.out
.println("Couldn't get Child");
298 * This methods retunrs the <CODE>XAccessibleContext</CODE> of a named Sheet-Cell like "G5".<p>
299 * @param xSheetAcc The <CODE>XAccessibleContext</CODE> of a Sheet
300 * @param cellName The name of a cell like "A5"
301 * @return the <CODE>XAccessiblecontext</CODE> of the named cell
303 public static XAccessibleContext
getSheetCell(XAccessibleContext xSheetAcc
, String cellName
){
308 for (int n
=0; n
<cellName
.length(); n
++){
309 String cha
= cellName
.substring(n
,n
+1);
310 System
.out
.println("char: " + cha
+ " ");
312 byte[] bytes
= cha
.getBytes();
314 if ((bytes
[0] >= 'A') && (bytes
[0] <= 'Z')){
315 charMem
= bytes
[0]-64;
318 cellIndex
+= charMem
* 26;
320 cellIndex
= cellIndex
+ (bytes
[0]-65);
322 String sNumb
= cellName
.substring(n
, cellName
.length());
323 int iNumb
= new Integer(0).valueOf(sNumb
).intValue();
324 cellIndex
+= (iNumb
-1) * 256;
325 System
.out
.println("numb:" + (iNumb
-1) * 256);
330 //System.out.println("cellName: " + cellName + " cellIndex: " + cellIndex);
333 XAccessibleContext ac
= xSheetAcc
.getAccessibleChild(cellIndex
).getAccessibleContext();
334 System
.out
.println(ac
.getAccessibleRole() + "," +
335 ac
.getAccessibleName() + "(" +
336 ac
.getAccessibleDescription() + "):" +
337 utils
.getImplName(ac
));
340 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException ex
) {
341 System
.out
.println("ERROR: could not get child at index " + cellIndex
+"': " + ex
.toString());
346 public static void printAccessibleTree(PrintWriter log
, XAccessible xacc
, boolean debugIsActive
) {
347 debug
= debugIsActive
;
348 if (debug
) printAccessibleTree(log
, xacc
, "");
351 public static void printAccessibleTree(PrintWriter log
, XAccessible xacc
) {
352 printAccessibleTree(log
, xacc
, "");
355 protected static void printAccessibleTree(PrintWriter log
,
356 XAccessible xacc
, String indent
) {
358 XAccessibleContext ac
= xacc
.getAccessibleContext();
360 logging(log
,indent
+ ac
.getAccessibleRole() + "," +
361 ac
.getAccessibleName() + "(" +
362 ac
.getAccessibleDescription() + "):" +
363 utils
.getImplName(ac
));
365 XAccessibleComponent aComp
= (XAccessibleComponent
) UnoRuntime
.queryInterface(
366 XAccessibleComponent
.class, xacc
);
369 String bounds
= "(" + aComp
.getBounds().X
+ "," +
370 aComp
.getBounds().Y
+ ")" + " (" +
371 aComp
.getBounds().Width
+ "," +
372 aComp
.getBounds().Height
+ ")";
373 bounds
= "The boundary Rectangle is " + bounds
;
374 logging(log
,indent
+ indent
+ bounds
);
377 boolean isShowing
= ac
.getAccessibleStateSet()
378 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
379 logging(log
,indent
+ indent
+ "StateType contains SHOWING: " +
382 int k
= ac
.getAccessibleChildCount();
384 if (ac
.getAccessibleChildCount() > 100) {
388 for (int i
= 0; i
< k
; i
++) {
390 printAccessibleTree(log
, ac
.getAccessibleChild(i
),
392 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
393 System
.out
.println("Couldn't get Child");
397 if (ac
.getAccessibleChildCount() > 100) {
398 k
= ac
.getAccessibleChildCount();
400 int st
= ac
.getAccessibleChildCount() - 50;
401 logging(log
,indent
+ " " + " ...... [skipped] ......");
403 for (int i
= st
; i
< k
; i
++) {
405 printAccessibleTree(log
, ac
.getAccessibleChild(i
),
407 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
408 System
.out
.println("Couldn't get Child");
414 public static String
accessibleToString(Object AC
) {
415 XAccessibleContext xAC
= (XAccessibleContext
) UnoRuntime
.queryInterface(
416 XAccessibleContext
.class, AC
);
419 return "" + xAC
.getAccessibleRole() + "," +
420 xAC
.getAccessibleName() + "(" +
421 xAC
.getAccessibleDescription() + "):";
424 XAccessible xA
= (XAccessible
) UnoRuntime
.queryInterface(
425 XAccessible
.class, AC
);
428 return "(Not supported)";
431 xAC
= xA
.getAccessibleContext();
433 return "" + xAC
.getAccessibleRole() + "," + xAC
.getAccessibleName() +
434 "(" + xAC
.getAccessibleDescription() + ")";
437 public static boolean equals(XAccessible c1
, XAccessible c2
) {
438 if ((c1
== null) || (c2
== null)) {
442 return AccessibilityTools
.equals(c1
.getAccessibleContext(),
443 c2
.getAccessibleContext());
446 public static boolean equals(XAccessibleContext c1
, XAccessibleContext c2
) {
447 if ((c1
== null) || (c2
== null)) {
451 if (c1
.getAccessibleRole() != c2
.getAccessibleRole()) {
455 if (!c1
.getAccessibleName().equals(c2
.getAccessibleName())) {
459 if (!c1
.getAccessibleDescription()
460 .equals(c2
.getAccessibleDescription())) {
464 if (c1
.getAccessibleChildCount() != c2
.getAccessibleChildCount()) {
468 return AccessibilityTools
.equals(c1
.getAccessibleParent(),
469 c2
.getAccessibleParent());
472 private static void logging(PrintWriter log
, String content
){
473 if (debug
) log
.println(content
);