use insert function instead of for loop
[LibreOffice.git] / qadevOOo / runner / util / AccessibilityTools.java
blob672754d80f1aec99bd769468c02ec264beefea01
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.uno.UnoRuntime;
28 import com.sun.star.uno.XInterface;
30 import java.io.PrintWriter;
33 public class AccessibilityTools {
34 public static XAccessible SearchedAccessible = null;
35 private static boolean debug = false;
37 private AccessibilityTools() {}
39 public static XAccessible getAccessibleObject(XInterface xObject) {
40 return UnoRuntime.queryInterface(XAccessible.class, xObject);
43 public static XWindow getCurrentContainerWindow(XModel xModel) {
44 return getWindow(xModel, true);
47 public static XWindow getCurrentWindow(XModel xModel) {
48 return getWindow(xModel, false);
51 private static XWindow getWindow(XModel xModel,
52 boolean containerWindow) {
53 XWindow xWindow = null;
55 try {
56 XController xController = xModel.getCurrentController();
57 XFrame xFrame = xController.getFrame();
59 if (xFrame == null) {
60 System.out.println("can't get frame from controller");
61 } else {
62 if (containerWindow)
63 xWindow = xFrame.getContainerWindow();
64 else
65 xWindow = xFrame.getComponentWindow();
68 if (xWindow == null) {
69 System.out.println("can't get window from frame");
71 } catch (Exception e) {
72 System.out.println("caught exception while getting current window" + e);
75 return xWindow;
78 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
79 short role) {
80 SearchedAccessible = null;
81 return getAccessibleObjectForRole_(xacc, role);
84 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
85 short role,
86 boolean ignoreShowing) {
87 SearchedAccessible = null;
89 if (ignoreShowing) {
90 return getAccessibleObjectForRoleIgnoreShowing_(xacc, role);
91 } else {
92 return getAccessibleObjectForRole_(xacc, role);
96 private static XAccessibleContext getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc,
97 short role) {
98 XAccessibleContext ac = xacc.getAccessibleContext();
99 if (ac == null) {
100 return null;
102 if (ac.getAccessibleRole() == role) {
103 SearchedAccessible = xacc;
104 return ac;
105 } else {
106 long k = ac.getAccessibleChildCount();
108 if (ac.getAccessibleChildCount() > 100) {
109 k = 50;
112 for (long i = 0; i < k; i++) {
113 try {
114 XAccessibleContext ac2 = getAccessibleObjectForRoleIgnoreShowing_(
115 ac.getAccessibleChild(i), role);
117 if (ac2 != null) {
118 return ac2;
120 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
121 System.out.println("Couldn't get Child");
124 return null;
128 private static XAccessibleContext getAccessibleObjectForRole_(XAccessible xacc,
129 short role) {
130 XAccessibleContext ac = xacc.getAccessibleContext();
131 boolean isShowing = (ac.getAccessibleStateSet()
132 & com.sun.star.accessibility.AccessibleStateType.SHOWING) != 0;
134 if ((ac.getAccessibleRole() == role) && isShowing) {
135 SearchedAccessible = xacc;
136 return ac;
137 } else {
138 long k = ac.getAccessibleChildCount();
140 if (ac.getAccessibleChildCount() > 100) {
141 k = 50;
144 for (long i = 0; i < k; i++) {
145 try {
146 XAccessibleContext ac2 = getAccessibleObjectForRole_(ac.getAccessibleChild(i), role);
148 if (ac2 != null) {
149 return ac2;
151 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
152 System.out.println("Couldn't get Child");
155 return null;
159 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
160 short role,
161 String name) {
162 return getAccessibleObjectForRole(xacc, role, name, "");
165 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
166 short role,
167 String name,
168 boolean ignoreShowing) {
169 if (ignoreShowing) {
170 return getAccessibleObjectForRoleIgnoreShowing(xacc, role, name,
171 "");
172 } else {
173 return getAccessibleObjectForRole(xacc, role, name, "");
177 public static XAccessibleContext getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc,
178 short role,
179 String name,
180 String implName) {
181 XAccessibleContext ac = xacc.getAccessibleContext();
182 if ((ac.getAccessibleRole() == role) &&
183 (ac.getAccessibleName().indexOf(name) > -1) &&
184 (utils.getImplName(ac).indexOf(implName) > -1)) {
185 SearchedAccessible = xacc;
187 return ac;
188 } else {
189 long k = ac.getAccessibleChildCount();
191 if (ac.getAccessibleChildCount() > 100) {
192 k = 50;
195 for (long i = 0; i < k; i++) {
196 try {
197 XAccessibleContext ac1 = getAccessibleObjectForRoleIgnoreShowing(
198 ac.getAccessibleChild(i),
199 role, name, implName);
201 if (ac1 != null) {
202 return ac1;
204 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
205 System.out.println("Couldn't get Child");
210 return null;
213 public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
214 short role,
215 String name,
216 String implName) {
217 XAccessibleContext ac = xacc.getAccessibleContext();
218 boolean isShowing = (ac.getAccessibleStateSet() &
219 com.sun.star.accessibility.AccessibleStateType.SHOWING) != 0;
221 // hotfix for i91828:
222 // if role to search is 0 then ignore the role.
223 if ( (role == 0 || ac.getAccessibleRole() == role) &&
224 (ac.getAccessibleName().indexOf(name) > -1) &&
225 (utils.getImplName(ac).indexOf(implName) > -1) &&
226 isShowing) {
227 SearchedAccessible = xacc;
228 return ac;
229 } else {
230 long k = ac.getAccessibleChildCount();
232 if (ac.getAccessibleChildCount() > 100) {
233 k = 50;
236 for (long i = 0; i < k; i++) {
237 try {
238 XAccessibleContext ac1 = getAccessibleObjectForRole(
239 ac.getAccessibleChild(i),
240 role, name, implName);
242 if (ac1 != null) {
243 return ac1;
245 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
246 System.out.println("Couldn't get Child");
251 return null;
254 public static void printAccessibleTree(PrintWriter log, XAccessible xacc, boolean debugIsActive) {
255 debug = debugIsActive;
256 if (debug) printAccessibleTree(log, xacc, "");
259 public static void printAccessibleTree(PrintWriter log, XAccessible xacc) {
260 printAccessibleTree(log, xacc, "");
263 private static void printAccessibleTree(PrintWriter log,
264 XAccessible xacc, String indent) {
266 XAccessibleContext ac = xacc.getAccessibleContext();
268 logging(log,indent + ac.getAccessibleRole() + "," +
269 ac.getAccessibleName() + "(" +
270 ac.getAccessibleDescription() + "):" +
271 utils.getImplName(ac));
273 XAccessibleComponent aComp = UnoRuntime.queryInterface(
274 XAccessibleComponent.class, xacc);
276 if (aComp != null) {
277 String bounds = "(" + aComp.getBounds().X + "," +
278 aComp.getBounds().Y + ")" + " (" +
279 aComp.getBounds().Width + "," +
280 aComp.getBounds().Height + ")";
281 bounds = "The boundary Rectangle is " + bounds;
282 logging(log,indent + indent + bounds);
285 boolean isShowing = (ac.getAccessibleStateSet()
286 & com.sun.star.accessibility.AccessibleStateType.SHOWING) != 0;
287 logging(log,indent + indent + "StateType contains SHOWING: " +
288 isShowing);
290 long k = ac.getAccessibleChildCount();
292 if (ac.getAccessibleChildCount() > 100) {
293 k = 50;
296 for (long i = 0; i < k; i++) {
297 try {
298 printAccessibleTree(log, ac.getAccessibleChild(i),
299 indent + " ");
300 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
301 System.out.println("Couldn't get Child");
305 if (ac.getAccessibleChildCount() > 100) {
306 k = ac.getAccessibleChildCount();
308 long st = ac.getAccessibleChildCount() - 50;
309 logging(log,indent + " " + " ...... [skipped] ......");
311 for (long i = st; i < k; i++) {
312 try {
313 printAccessibleTree(log, ac.getAccessibleChild(i),
314 indent + " ");
315 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
316 System.out.println("Couldn't get Child");
322 public static String accessibleToString(Object AC) {
323 XAccessibleContext xAC = UnoRuntime.queryInterface(
324 XAccessibleContext.class, AC);
326 if (xAC != null) {
327 return xAC.getAccessibleRole() + "," +
328 xAC.getAccessibleName() + "(" +
329 xAC.getAccessibleDescription() + "):";
332 XAccessible xA = UnoRuntime.queryInterface(
333 XAccessible.class, AC);
335 if (xA == null) {
336 return "(Not supported)";
339 xAC = xA.getAccessibleContext();
341 return xAC.getAccessibleRole() + "," + xAC.getAccessibleName() +
342 "(" + xAC.getAccessibleDescription() + ")";
345 public static boolean equals(XAccessible c1, XAccessible c2) {
346 if ((c1 == null) || (c2 == null)) {
347 return c1 == c2;
350 return AccessibilityTools.equals(c1.getAccessibleContext(),
351 c2.getAccessibleContext());
354 public static boolean equals(XAccessibleContext c1, XAccessibleContext c2) {
355 if ((c1 == null) || (c2 == null)) {
356 return c1 == c2;
359 if (c1.getAccessibleRole() != c2.getAccessibleRole()) {
360 return false;
363 if (!c1.getAccessibleName().equals(c2.getAccessibleName())) {
364 return false;
367 if (!c1.getAccessibleDescription()
368 .equals(c2.getAccessibleDescription())) {
369 return false;
372 if (c1.getAccessibleChildCount() != c2.getAccessibleChildCount()) {
373 return false;
376 return AccessibilityTools.equals(c1.getAccessibleParent(),
377 c2.getAccessibleParent());
380 private static void logging(PrintWriter log, String content){
381 if (debug) log.println(content);