Update ooo320-m1
[ooovba.git] / qadevOOo / runner / util / AccessibilityTools.java
blobd769e425ba5576d6b041134ba9aca2f3d2655e31
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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 ************************************************************************/
30 package util;
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() {
52 //done = false;
53 SearchedContext = null;
56 public static XAccessible getAccessibleObject(XInterface xObject) {
57 XAccessible xAccessible = null;
59 try {
60 xAccessible = (XAccessible) UnoRuntime.queryInterface(
61 XAccessible.class, xObject);
62 } catch (Exception e) {
63 System.out.println(
64 "caught exception while getting accessible object" + e);
65 e.printStackTrace();
68 return xAccessible;
71 public static XWindow getCurrentContainerWindow(XMultiServiceFactory msf,
72 XModel xModel) {
73 return getWindow(msf, xModel, true);
76 public static XWindow getCurrentWindow(XMultiServiceFactory msf,
77 XModel xModel) {
78 return getWindow(msf, xModel, false);
81 private static XWindow getWindow(XMultiServiceFactory msf, XModel xModel,
82 boolean containerWindow) {
83 XWindow xWindow = null;
85 try {
86 if (xModel == 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();
98 if (xFrame == null) {
99 System.out.println("can't get frame from controller");
102 if (containerWindow)
103 xWindow = xFrame.getContainerWindow();
104 else
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);
114 return xWindow;
117 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
118 short role) {
119 SearchedContext = null;
120 SearchedAccessible = null;
121 getAccessibleObjectForRole_(xacc, role);
123 return SearchedContext;
126 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
127 short role,
128 boolean ignoreShowing) {
129 SearchedContext = null;
130 SearchedAccessible = null;
132 if (ignoreShowing) {
133 getAccessibleObjectForRoleIgnoreShowing_(xacc, role);
134 } else {
135 getAccessibleObjectForRole_(xacc, role);
138 return SearchedContext;
141 public static void getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc,
142 short role) {
143 XAccessibleContext ac = xacc.getAccessibleContext();
145 if (ac.getAccessibleRole() == role) {
146 SearchedContext = ac;
147 SearchedAccessible = xacc;
148 } else {
149 int k = ac.getAccessibleChildCount();
151 if (ac.getAccessibleChildCount() > 100) {
152 k = 50;
155 for (int i = 0; i < k; i++) {
156 try {
157 getAccessibleObjectForRoleIgnoreShowing_(
158 ac.getAccessibleChild(i), role);
160 if (SearchedContext != null) {
161 return;
163 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
164 System.out.println("Couldn't get Child");
170 public static void getAccessibleObjectForRole_(XAccessible xacc,
171 short role) {
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;
179 } else {
180 int k = ac.getAccessibleChildCount();
182 if (ac.getAccessibleChildCount() > 100) {
183 k = 50;
186 for (int i = 0; i < k; i++) {
187 try {
188 getAccessibleObjectForRole_(ac.getAccessibleChild(i), role);
190 if (SearchedContext != null) {
191 return;
193 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
194 System.out.println("Couldn't get Child");
200 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
201 short role,
202 String name) {
203 return getAccessibleObjectForRole(xacc, role, name, "");
206 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
207 short role,
208 String name,
209 boolean ignoreShowing) {
210 if (ignoreShowing) {
211 return getAccessibleObjectForRoleIgnoreShowing(xacc, role, name,
212 "");
213 } else {
214 return getAccessibleObjectForRole(xacc, role, name, "");
218 public static XAccessibleContext getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc,
219 short role,
220 String name,
221 String implName) {
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);
229 return ac;
230 } else {
231 int k = ac.getAccessibleChildCount();
233 if (ac.getAccessibleChildCount() > 100) {
234 k = 50;
237 for (int i = 0; i < k; i++) {
238 try {
239 XAccessibleContext ac1 = getAccessibleObjectForRoleIgnoreShowing(
240 ac.getAccessibleChild(i),
241 role, name, implName);
243 if (ac1 != null) {
244 return ac1;
246 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
247 System.out.println("Couldn't get Child");
252 return null;
255 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
256 short role,
257 String name,
258 String implName) {
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) &&
268 isShowing) {
269 SearchedAccessible = xacc;
270 //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
271 return ac;
272 } else {
273 int k = ac.getAccessibleChildCount();
275 if (ac.getAccessibleChildCount() > 100) {
276 k = 50;
279 for (int i = 0; i < k; i++) {
280 try {
281 XAccessibleContext ac1 = getAccessibleObjectForRole(
282 ac.getAccessibleChild(i),
283 role, name, implName);
285 if (ac1 != null) {
286 return ac1;
288 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
289 System.out.println("Couldn't get Child");
294 return null;
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){
305 int cellIndex = 0;
306 int column =0;
307 int charMem = 0;
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;
316 column++;
317 if ( column == 2 ){
318 cellIndex += charMem * 26;
320 cellIndex= cellIndex+ (bytes[0]-65);
321 } else {
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);
332 try {
333 XAccessibleContext ac = xSheetAcc.getAccessibleChild(cellIndex).getAccessibleContext();
334 System.out.println(ac.getAccessibleRole() + "," +
335 ac.getAccessibleName() + "(" +
336 ac.getAccessibleDescription() + "):" +
337 utils.getImplName(ac));
339 return ac;
340 } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
341 System.out.println("ERROR: could not get child at index " + cellIndex +"': " + ex.toString());
342 return null;
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);
368 if (aComp != null) {
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: " +
380 isShowing);
382 int k = ac.getAccessibleChildCount();
384 if (ac.getAccessibleChildCount() > 100) {
385 k = 50;
388 for (int i = 0; i < k; i++) {
389 try {
390 printAccessibleTree(log, ac.getAccessibleChild(i),
391 indent + " ");
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++) {
404 try {
405 printAccessibleTree(log, ac.getAccessibleChild(i),
406 indent + " ");
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);
418 if (xAC != null) {
419 return "" + xAC.getAccessibleRole() + "," +
420 xAC.getAccessibleName() + "(" +
421 xAC.getAccessibleDescription() + "):";
424 XAccessible xA = (XAccessible) UnoRuntime.queryInterface(
425 XAccessible.class, AC);
427 if (xA == null) {
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)) {
439 return c1 == c2;
442 return AccessibilityTools.equals(c1.getAccessibleContext(),
443 c2.getAccessibleContext());
446 public static boolean equals(XAccessibleContext c1, XAccessibleContext c2) {
447 if ((c1 == null) || (c2 == null)) {
448 return c1 == c2;
451 if (c1.getAccessibleRole() != c2.getAccessibleRole()) {
452 return false;
455 if (!c1.getAccessibleName().equals(c2.getAccessibleName())) {
456 return false;
459 if (!c1.getAccessibleDescription()
460 .equals(c2.getAccessibleDescription())) {
461 return false;
464 if (c1.getAccessibleChildCount() != c2.getAccessibleChildCount()) {
465 return false;
468 return AccessibilityTools.equals(c1.getAccessibleParent(),
469 c2.getAccessibleParent());
472 private static void logging(PrintWriter log, String content){
473 if (debug) log.println(content);