bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / util / AccessibilityTools.java
blob85cd442a3b8452ffcf7e7ebc8bb2777e66374784
1 /*
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 .
18 package util;
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() {
39 //done = false;
42 public static XAccessible getAccessibleObject(XInterface xObject) {
43 return UnoRuntime.queryInterface(XAccessible.class, xObject);
46 public static XWindow getCurrentContainerWindow(XMultiServiceFactory msf,
47 XModel xModel) {
48 return getWindow(msf, xModel, true);
51 public static XWindow getCurrentWindow(XMultiServiceFactory msf,
52 XModel xModel) {
53 return getWindow(msf, xModel, false);
56 private static XWindow getWindow(XMultiServiceFactory msf, XModel xModel,
57 boolean containerWindow) {
58 XWindow xWindow = null;
60 try {
61 if (xModel == 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();
73 if (xFrame == null) {
74 System.out.println("can't get frame from controller");
77 if (containerWindow)
78 xWindow = xFrame.getContainerWindow();
79 else
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);
89 return xWindow;
92 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
93 short role) {
94 SearchedAccessible = null;
95 return getAccessibleObjectForRole_(xacc, role);
98 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
99 short role,
100 boolean ignoreShowing) {
101 SearchedAccessible = null;
103 if (ignoreShowing) {
104 return getAccessibleObjectForRoleIgnoreShowing_(xacc, role);
105 } else {
106 return getAccessibleObjectForRole_(xacc, role);
110 public static XAccessibleContext getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc,
111 short role) {
112 XAccessibleContext ac = xacc.getAccessibleContext();
114 if (ac.getAccessibleRole() == role) {
115 SearchedAccessible = xacc;
116 return ac;
117 } else {
118 int k = ac.getAccessibleChildCount();
120 if (ac.getAccessibleChildCount() > 100) {
121 k = 50;
124 for (int i = 0; i < k; i++) {
125 try {
126 XAccessibleContext ac2 = getAccessibleObjectForRoleIgnoreShowing_(
127 ac.getAccessibleChild(i), role);
129 if (ac2 != null) {
130 return ac2;
132 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
133 System.out.println("Couldn't get Child");
136 return null;
140 public static XAccessibleContext getAccessibleObjectForRole_(XAccessible xacc,
141 short role) {
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;
148 return ac;
149 } else {
150 int k = ac.getAccessibleChildCount();
152 if (ac.getAccessibleChildCount() > 100) {
153 k = 50;
156 for (int i = 0; i < k; i++) {
157 try {
158 XAccessibleContext ac2 = getAccessibleObjectForRole_(ac.getAccessibleChild(i), role);
160 if (ac2 != null) {
161 return ac2;
163 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
164 System.out.println("Couldn't get Child");
167 return null;
171 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
172 short role,
173 String name) {
174 return getAccessibleObjectForRole(xacc, role, name, "");
177 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
178 short role,
179 String name,
180 boolean ignoreShowing) {
181 if (ignoreShowing) {
182 return getAccessibleObjectForRoleIgnoreShowing(xacc, role, name,
183 "");
184 } else {
185 return getAccessibleObjectForRole(xacc, role, name, "");
189 public static XAccessibleContext getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc,
190 short role,
191 String name,
192 String implName) {
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);
200 return ac;
201 } else {
202 int k = ac.getAccessibleChildCount();
204 if (ac.getAccessibleChildCount() > 100) {
205 k = 50;
208 for (int i = 0; i < k; i++) {
209 try {
210 XAccessibleContext ac1 = getAccessibleObjectForRoleIgnoreShowing(
211 ac.getAccessibleChild(i),
212 role, name, implName);
214 if (ac1 != null) {
215 return ac1;
217 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
218 System.out.println("Couldn't get Child");
223 return null;
226 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
227 short role,
228 String name,
229 String implName) {
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) &&
239 isShowing) {
240 SearchedAccessible = xacc;
241 //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
242 return ac;
243 } else {
244 int k = ac.getAccessibleChildCount();
246 if (ac.getAccessibleChildCount() > 100) {
247 k = 50;
250 for (int i = 0; i < k; i++) {
251 try {
252 XAccessibleContext ac1 = getAccessibleObjectForRole(
253 ac.getAccessibleChild(i),
254 role, name, implName);
256 if (ac1 != null) {
257 return ac1;
259 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
260 System.out.println("Couldn't get Child");
265 return null;
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){
276 int cellIndex = 0;
277 int column =0;
278 int charMem = 0;
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;
287 column++;
288 if ( column == 2 ){
289 cellIndex += charMem * 26;
291 cellIndex= cellIndex+ (bytes[0]-65);
292 } else {
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);
303 try {
304 XAccessibleContext ac = xSheetAcc.getAccessibleChild(cellIndex).getAccessibleContext();
305 System.out.println(ac.getAccessibleRole() + "," +
306 ac.getAccessibleName() + "(" +
307 ac.getAccessibleDescription() + "):" +
308 utils.getImplName(ac));
310 return ac;
311 } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
312 System.out.println("ERROR: could not get child at index " + cellIndex +"': " + ex.toString());
313 return null;
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);
339 if (aComp != null) {
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: " +
351 isShowing);
353 int k = ac.getAccessibleChildCount();
355 if (ac.getAccessibleChildCount() > 100) {
356 k = 50;
359 for (int i = 0; i < k; i++) {
360 try {
361 printAccessibleTree(log, ac.getAccessibleChild(i),
362 indent + " ");
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++) {
375 try {
376 printAccessibleTree(log, ac.getAccessibleChild(i),
377 indent + " ");
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);
389 if (xAC != null) {
390 return "" + xAC.getAccessibleRole() + "," +
391 xAC.getAccessibleName() + "(" +
392 xAC.getAccessibleDescription() + "):";
395 XAccessible xA = UnoRuntime.queryInterface(
396 XAccessible.class, AC);
398 if (xA == null) {
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)) {
410 return c1 == c2;
413 return AccessibilityTools.equals(c1.getAccessibleContext(),
414 c2.getAccessibleContext());
417 public static boolean equals(XAccessibleContext c1, XAccessibleContext c2) {
418 if ((c1 == null) || (c2 == null)) {
419 return c1 == c2;
422 if (c1.getAccessibleRole() != c2.getAccessibleRole()) {
423 return false;
426 if (!c1.getAccessibleName().equals(c2.getAccessibleName())) {
427 return false;
430 if (!c1.getAccessibleDescription()
431 .equals(c2.getAccessibleDescription())) {
432 return false;
435 if (c1.getAccessibleChildCount() != c2.getAccessibleChildCount()) {
436 return false;
439 return AccessibilityTools.equals(c1.getAccessibleParent(),
440 c2.getAccessibleParent());
443 private static void logging(PrintWriter log, String content){
444 if (debug) log.println(content);