1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
29 import com
.sun
.star
.accessibility
.XAccessible
;
30 import com
.sun
.star
.accessibility
.XAccessibleComponent
;
31 import com
.sun
.star
.accessibility
.XAccessibleContext
;
32 import com
.sun
.star
.awt
.XWindow
;
33 import com
.sun
.star
.frame
.XController
;
34 import com
.sun
.star
.frame
.XFrame
;
35 import com
.sun
.star
.frame
.XModel
;
36 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import com
.sun
.star
.uno
.XInterface
;
40 import java
.io
.PrintWriter
;
43 public class AccessibilityTools
{
44 public static XAccessibleContext SearchedContext
= null;
45 public static XAccessible SearchedAccessible
= null;
46 private static boolean debug
= false;
48 public AccessibilityTools() {
50 SearchedContext
= null;
53 public static XAccessible
getAccessibleObject(XInterface xObject
) {
54 return UnoRuntime
.queryInterface(XAccessible
.class, xObject
);
57 public static XWindow
getCurrentContainerWindow(XMultiServiceFactory msf
,
59 return getWindow(msf
, xModel
, true);
62 public static XWindow
getCurrentWindow(XMultiServiceFactory msf
,
64 return getWindow(msf
, xModel
, false);
67 private static XWindow
getWindow(XMultiServiceFactory msf
, XModel xModel
,
68 boolean containerWindow
) {
69 XWindow xWindow
= null;
73 System
.out
.println("invalid model (==null)");
76 XController xController
= xModel
.getCurrentController();
78 if (xController
== null) {
79 System
.out
.println("can't get controller from model");
82 XFrame xFrame
= xController
.getFrame();
85 System
.out
.println("can't get frame from controller");
89 xWindow
= xFrame
.getContainerWindow();
91 xWindow
= xFrame
.getComponentWindow();
93 if (xWindow
== null) {
94 System
.out
.println("can't get window from frame");
96 } catch (Exception e
) {
97 System
.out
.println("caught exception while getting current window" + e
);
103 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
105 SearchedContext
= null;
106 SearchedAccessible
= null;
107 getAccessibleObjectForRole_(xacc
, role
);
109 return SearchedContext
;
112 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
114 boolean ignoreShowing
) {
115 SearchedContext
= null;
116 SearchedAccessible
= null;
119 getAccessibleObjectForRoleIgnoreShowing_(xacc
, role
);
121 getAccessibleObjectForRole_(xacc
, role
);
124 return SearchedContext
;
127 public static void getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc
,
129 XAccessibleContext ac
= xacc
.getAccessibleContext();
131 if (ac
.getAccessibleRole() == role
) {
132 SearchedContext
= ac
;
133 SearchedAccessible
= xacc
;
135 int k
= ac
.getAccessibleChildCount();
137 if (ac
.getAccessibleChildCount() > 100) {
141 for (int i
= 0; i
< k
; i
++) {
143 getAccessibleObjectForRoleIgnoreShowing_(
144 ac
.getAccessibleChild(i
), role
);
146 if (SearchedContext
!= null) {
149 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
150 System
.out
.println("Couldn't get Child");
156 public static void getAccessibleObjectForRole_(XAccessible xacc
,
158 XAccessibleContext ac
= xacc
.getAccessibleContext();
159 boolean isShowing
= ac
.getAccessibleStateSet()
160 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
162 if ((ac
.getAccessibleRole() == role
) && isShowing
) {
163 SearchedContext
= ac
;
164 SearchedAccessible
= xacc
;
166 int k
= ac
.getAccessibleChildCount();
168 if (ac
.getAccessibleChildCount() > 100) {
172 for (int i
= 0; i
< k
; i
++) {
174 getAccessibleObjectForRole_(ac
.getAccessibleChild(i
), role
);
176 if (SearchedContext
!= null) {
179 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
180 System
.out
.println("Couldn't get Child");
186 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
189 return getAccessibleObjectForRole(xacc
, role
, name
, "");
192 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
195 boolean ignoreShowing
) {
197 return getAccessibleObjectForRoleIgnoreShowing(xacc
, role
, name
,
200 return getAccessibleObjectForRole(xacc
, role
, name
, "");
204 public static XAccessibleContext
getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc
,
208 XAccessibleContext ac
= xacc
.getAccessibleContext();
209 if ((ac
.getAccessibleRole() == role
) &&
210 (ac
.getAccessibleName().indexOf(name
) > -1) &&
211 (utils
.getImplName(ac
).indexOf(implName
) > -1)) {
212 SearchedAccessible
= xacc
;
214 //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
217 int k
= ac
.getAccessibleChildCount();
219 if (ac
.getAccessibleChildCount() > 100) {
223 for (int i
= 0; i
< k
; i
++) {
225 XAccessibleContext ac1
= getAccessibleObjectForRoleIgnoreShowing(
226 ac
.getAccessibleChild(i
),
227 role
, name
, implName
);
232 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
233 System
.out
.println("Couldn't get Child");
241 public static XAccessibleContext
getAccessibleObjectForRole(XAccessible xacc
,
245 XAccessibleContext ac
= xacc
.getAccessibleContext();
246 boolean isShowing
= ac
.getAccessibleStateSet()
247 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
249 // hotfix for i91828:
250 // if role to serach is 0 then ignore the role.
251 if ( (role
== 0 || ac
.getAccessibleRole() == role
) &&
252 (ac
.getAccessibleName().indexOf(name
) > -1) &&
253 (utils
.getImplName(ac
).indexOf(implName
) > -1) &&
255 SearchedAccessible
= xacc
;
256 //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
259 int k
= ac
.getAccessibleChildCount();
261 if (ac
.getAccessibleChildCount() > 100) {
265 for (int i
= 0; i
< k
; i
++) {
267 XAccessibleContext ac1
= getAccessibleObjectForRole(
268 ac
.getAccessibleChild(i
),
269 role
, name
, implName
);
274 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
275 System
.out
.println("Couldn't get Child");
284 * This methods retunrs the <CODE>XAccessibleContext</CODE> of a named Sheet-Cell like "G5".<p>
285 * @param xSheetAcc The <CODE>XAccessibleContext</CODE> of a Sheet
286 * @param cellName The name of a cell like "A5"
287 * @return the <CODE>XAccessiblecontext</CODE> of the named cell
289 public static XAccessibleContext
getSheetCell(XAccessibleContext xSheetAcc
, String cellName
){
294 for (int n
=0; n
<cellName
.length(); n
++){
295 String cha
= cellName
.substring(n
,n
+1);
296 System
.out
.println("char: " + cha
+ " ");
298 byte[] bytes
= cha
.getBytes();
300 if ((bytes
[0] >= 'A') && (bytes
[0] <= 'Z')){
301 charMem
= bytes
[0]-64;
304 cellIndex
+= charMem
* 26;
306 cellIndex
= cellIndex
+ (bytes
[0]-65);
308 String sNumb
= cellName
.substring(n
, cellName
.length());
309 int iNumb
= new Integer(0).valueOf(sNumb
).intValue();
310 cellIndex
+= (iNumb
-1) * 256;
311 System
.out
.println("numb:" + (iNumb
-1) * 256);
316 //System.out.println("cellName: " + cellName + " cellIndex: " + cellIndex);
319 XAccessibleContext ac
= xSheetAcc
.getAccessibleChild(cellIndex
).getAccessibleContext();
320 System
.out
.println(ac
.getAccessibleRole() + "," +
321 ac
.getAccessibleName() + "(" +
322 ac
.getAccessibleDescription() + "):" +
323 utils
.getImplName(ac
));
326 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException ex
) {
327 System
.out
.println("ERROR: could not get child at index " + cellIndex
+"': " + ex
.toString());
332 public static void printAccessibleTree(PrintWriter log
, XAccessible xacc
, boolean debugIsActive
) {
333 debug
= debugIsActive
;
334 if (debug
) printAccessibleTree(log
, xacc
, "");
337 public static void printAccessibleTree(PrintWriter log
, XAccessible xacc
) {
338 printAccessibleTree(log
, xacc
, "");
341 protected static void printAccessibleTree(PrintWriter log
,
342 XAccessible xacc
, String indent
) {
344 XAccessibleContext ac
= xacc
.getAccessibleContext();
346 logging(log
,indent
+ ac
.getAccessibleRole() + "," +
347 ac
.getAccessibleName() + "(" +
348 ac
.getAccessibleDescription() + "):" +
349 utils
.getImplName(ac
));
351 XAccessibleComponent aComp
= (XAccessibleComponent
) UnoRuntime
.queryInterface(
352 XAccessibleComponent
.class, xacc
);
355 String bounds
= "(" + aComp
.getBounds().X
+ "," +
356 aComp
.getBounds().Y
+ ")" + " (" +
357 aComp
.getBounds().Width
+ "," +
358 aComp
.getBounds().Height
+ ")";
359 bounds
= "The boundary Rectangle is " + bounds
;
360 logging(log
,indent
+ indent
+ bounds
);
363 boolean isShowing
= ac
.getAccessibleStateSet()
364 .contains(com
.sun
.star
.accessibility
.AccessibleStateType
.SHOWING
);
365 logging(log
,indent
+ indent
+ "StateType contains SHOWING: " +
368 int k
= ac
.getAccessibleChildCount();
370 if (ac
.getAccessibleChildCount() > 100) {
374 for (int i
= 0; 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");
383 if (ac
.getAccessibleChildCount() > 100) {
384 k
= ac
.getAccessibleChildCount();
386 int st
= ac
.getAccessibleChildCount() - 50;
387 logging(log
,indent
+ " " + " ...... [skipped] ......");
389 for (int i
= st
; i
< k
; i
++) {
391 printAccessibleTree(log
, ac
.getAccessibleChild(i
),
393 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
394 System
.out
.println("Couldn't get Child");
400 public static String
accessibleToString(Object AC
) {
401 XAccessibleContext xAC
= (XAccessibleContext
) UnoRuntime
.queryInterface(
402 XAccessibleContext
.class, AC
);
405 return "" + xAC
.getAccessibleRole() + "," +
406 xAC
.getAccessibleName() + "(" +
407 xAC
.getAccessibleDescription() + "):";
410 XAccessible xA
= (XAccessible
) UnoRuntime
.queryInterface(
411 XAccessible
.class, AC
);
414 return "(Not supported)";
417 xAC
= xA
.getAccessibleContext();
419 return "" + xAC
.getAccessibleRole() + "," + xAC
.getAccessibleName() +
420 "(" + xAC
.getAccessibleDescription() + ")";
423 public static boolean equals(XAccessible c1
, XAccessible c2
) {
424 if ((c1
== null) || (c2
== null)) {
428 return AccessibilityTools
.equals(c1
.getAccessibleContext(),
429 c2
.getAccessibleContext());
432 public static boolean equals(XAccessibleContext c1
, XAccessibleContext c2
) {
433 if ((c1
== null) || (c2
== null)) {
437 if (c1
.getAccessibleRole() != c2
.getAccessibleRole()) {
441 if (!c1
.getAccessibleName().equals(c2
.getAccessibleName())) {
445 if (!c1
.getAccessibleDescription()
446 .equals(c2
.getAccessibleDescription())) {
450 if (c1
.getAccessibleChildCount() != c2
.getAccessibleChildCount()) {
454 return AccessibilityTools
.equals(c1
.getAccessibleParent(),
455 c2
.getAccessibleParent());
458 private static void logging(PrintWriter log
, String content
){
459 if (debug
) log
.println(content
);