1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package util;
24 
25 import com.sun.star.accessibility.XAccessible;
26 import com.sun.star.accessibility.XAccessibleComponent;
27 import com.sun.star.accessibility.XAccessibleContext;
28 import com.sun.star.awt.XWindow;
29 import com.sun.star.frame.XController;
30 import com.sun.star.frame.XFrame;
31 import com.sun.star.frame.XModel;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.XInterface;
35 
36 import java.io.PrintWriter;
37 
38 
39 public class AccessibilityTools {
40     public static XAccessibleContext SearchedContext = null;
41     public static XAccessible SearchedAccessible = null;
42     private static boolean debug = false;
43 
AccessibilityTools()44     public AccessibilityTools() {
45         //done = false;
46         SearchedContext = null;
47     }
48 
getAccessibleObject(XInterface xObject)49     public static XAccessible getAccessibleObject(XInterface xObject) {
50         return UnoRuntime.queryInterface(XAccessible.class, xObject);
51     }
52 
getCurrentContainerWindow(XMultiServiceFactory msf, XModel xModel)53     public static XWindow getCurrentContainerWindow(XMultiServiceFactory msf,
54         XModel xModel) {
55         return getWindow(msf, xModel, true);
56     }
57 
getCurrentWindow(XMultiServiceFactory msf, XModel xModel)58     public static XWindow getCurrentWindow(XMultiServiceFactory msf,
59         XModel xModel) {
60         return getWindow(msf, xModel, false);
61     }
62 
getWindow(XMultiServiceFactory msf, XModel xModel, boolean containerWindow)63     private static XWindow getWindow(XMultiServiceFactory msf, XModel xModel,
64         boolean containerWindow) {
65         XWindow xWindow = null;
66 
67         try {
68             if (xModel == null) {
69                 System.out.println("invalid model (==null)");
70             }
71 
72             XController xController = xModel.getCurrentController();
73 
74             if (xController == null) {
75                 System.out.println("can't get controller from model");
76             }
77 
78             XFrame xFrame = xController.getFrame();
79 
80             if (xFrame == null) {
81                 System.out.println("can't get frame from controller");
82             }
83 
84             if (containerWindow)
85                 xWindow = xFrame.getContainerWindow();
86             else
87                 xWindow = xFrame.getComponentWindow();
88 
89             if (xWindow == null) {
90                 System.out.println("can't get window from frame");
91             }
92         } catch (Exception e) {
93             System.out.println("caught exception while getting current window" + e);
94         }
95 
96         return xWindow;
97     }
98 
getAccessibleObjectForRole(XAccessible xacc, short role)99     public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
100         short role) {
101         SearchedContext = null;
102         SearchedAccessible = null;
103         getAccessibleObjectForRole_(xacc, role);
104 
105         return SearchedContext;
106     }
107 
getAccessibleObjectForRole(XAccessible xacc, short role, boolean ignoreShowing)108     public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
109         short role,
110         boolean ignoreShowing) {
111         SearchedContext = null;
112         SearchedAccessible = null;
113 
114         if (ignoreShowing) {
115             getAccessibleObjectForRoleIgnoreShowing_(xacc, role);
116         } else {
117             getAccessibleObjectForRole_(xacc, role);
118         }
119 
120         return SearchedContext;
121     }
122 
getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc, short role)123     public static void getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc,
124         short role) {
125         XAccessibleContext ac = xacc.getAccessibleContext();
126 
127         if (ac.getAccessibleRole() == role) {
128             SearchedContext = ac;
129             SearchedAccessible = xacc;
130         } else {
131             int k = ac.getAccessibleChildCount();
132 
133             if (ac.getAccessibleChildCount() > 100) {
134                 k = 50;
135             }
136 
137             for (int i = 0; i < k; i++) {
138                 try {
139                     getAccessibleObjectForRoleIgnoreShowing_(
140                         ac.getAccessibleChild(i), role);
141 
142                     if (SearchedContext != null) {
143                         return;
144                     }
145                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
146                     System.out.println("Couldn't get Child");
147                 }
148             }
149         }
150     }
151 
getAccessibleObjectForRole_(XAccessible xacc, short role)152     public static void getAccessibleObjectForRole_(XAccessible xacc,
153         short role) {
154         XAccessibleContext ac = xacc.getAccessibleContext();
155         boolean isShowing = ac.getAccessibleStateSet()
156         .contains(com.sun.star.accessibility.AccessibleStateType.SHOWING);
157 
158         if ((ac.getAccessibleRole() == role) && isShowing) {
159             SearchedContext = ac;
160             SearchedAccessible = xacc;
161         } else {
162             int k = ac.getAccessibleChildCount();
163 
164             if (ac.getAccessibleChildCount() > 100) {
165                 k = 50;
166             }
167 
168             for (int i = 0; i < k; i++) {
169                 try {
170                     getAccessibleObjectForRole_(ac.getAccessibleChild(i), role);
171 
172                     if (SearchedContext != null) {
173                         return;
174                     }
175                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
176                     System.out.println("Couldn't get Child");
177                 }
178             }
179         }
180     }
181 
getAccessibleObjectForRole(XAccessible xacc, short role, String name)182     public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
183         short role,
184         String name) {
185         return getAccessibleObjectForRole(xacc, role, name, "");
186     }
187 
getAccessibleObjectForRole(XAccessible xacc, short role, String name, boolean ignoreShowing)188     public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
189         short role,
190         String name,
191         boolean ignoreShowing) {
192         if (ignoreShowing) {
193             return getAccessibleObjectForRoleIgnoreShowing(xacc, role, name,
194                 "");
195         } else {
196             return getAccessibleObjectForRole(xacc, role, name, "");
197         }
198     }
199 
getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc, short role, String name, String implName)200     public static XAccessibleContext getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc,
201         short role,
202         String name,
203         String implName) {
204         XAccessibleContext ac = xacc.getAccessibleContext();
205         if ((ac.getAccessibleRole() == role) &&
206             (ac.getAccessibleName().indexOf(name) > -1) &&
207             (utils.getImplName(ac).indexOf(implName) > -1)) {
208             SearchedAccessible = xacc;
209 
210             //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
211             return ac;
212         } else {
213             int k = ac.getAccessibleChildCount();
214 
215             if (ac.getAccessibleChildCount() > 100) {
216                 k = 50;
217             }
218 
219             for (int i = 0; i < k; i++) {
220                 try {
221                     XAccessibleContext ac1 = getAccessibleObjectForRoleIgnoreShowing(
222                         ac.getAccessibleChild(i),
223                         role, name, implName);
224 
225                     if (ac1 != null) {
226                         return ac1;
227                     }
228                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
229                     System.out.println("Couldn't get Child");
230                 }
231             }
232         }
233 
234         return null;
235     }
236 
getAccessibleObjectForRole(XAccessible xacc, short role, String name, String implName)237     public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
238         short role,
239         String name,
240         String implName) {
241         XAccessibleContext ac = xacc.getAccessibleContext();
242         boolean isShowing = ac.getAccessibleStateSet()
243         .contains(com.sun.star.accessibility.AccessibleStateType.SHOWING);
244 
245         // hotfix for i91828:
246         // if role to serach is 0 then ignore the role.
247         if ( (role == 0 || ac.getAccessibleRole() == role) &&
248             (ac.getAccessibleName().indexOf(name) > -1) &&
249             (utils.getImplName(ac).indexOf(implName) > -1) &&
250             isShowing) {
251             SearchedAccessible = xacc;
252             //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
253             return ac;
254         } else {
255             int k = ac.getAccessibleChildCount();
256 
257             if (ac.getAccessibleChildCount() > 100) {
258                 k = 50;
259             }
260 
261             for (int i = 0; i < k; i++) {
262                 try {
263                     XAccessibleContext ac1 = getAccessibleObjectForRole(
264                         ac.getAccessibleChild(i),
265                         role, name, implName);
266 
267                     if (ac1 != null) {
268                         return ac1;
269                     }
270                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
271                     System.out.println("Couldn't get Child");
272                 }
273             }
274         }
275 
276         return null;
277     }
278 
279     /**
280      * This methods retunrs the <CODE>XAccessibleContext</CODE> of a named Sheet-Cell like "G5".<p>
281      * @param xSheetAcc The <CODE>XAccessibleContext</CODE> of a Sheet
282      * @param cellName The name of a cell like "A5"
283      * @return the <CODE>XAccessiblecontext</CODE> of the named cell
284      */
getSheetCell(XAccessibleContext xSheetAcc, String cellName)285     public static XAccessibleContext getSheetCell(XAccessibleContext xSheetAcc, String cellName){
286 
287         int cellIndex = 0;
288         int column =0;
289         int charMem = 0;
290         for (int n=0; n<cellName.length(); n++){
291             String cha = cellName.substring(n,n+1);
292             System.out.println("char: " + cha + " ");
293 
294             byte[] bytes = cha.getBytes();
295 
296             if ((bytes[0] >= 'A') && (bytes[0] <= 'Z')){
297                 charMem = bytes[0]-64;
298                 column++;
299                 if ( column == 2 ){
300                     cellIndex += charMem * 26;
301                 }
302                 cellIndex= cellIndex+ (bytes[0]-65);
303             } else {
304                 String sNumb = cellName.substring(n, cellName.length());
305                 int iNumb = new Integer(0).valueOf(sNumb).intValue();
306                 cellIndex += (iNumb-1) * 256;
307                 System.out.println("numb:" + (iNumb-1) * 256);
308             }
309 
310         }
311 
312         //System.out.println("cellName:  " + cellName + " cellIndex: " + cellIndex);
313 
314         try {
315             XAccessibleContext ac = xSheetAcc.getAccessibleChild(cellIndex).getAccessibleContext();
316             System.out.println(ac.getAccessibleRole() + "," +
317                 ac.getAccessibleName() + "(" +
318                 ac.getAccessibleDescription() + "):" +
319                 utils.getImplName(ac));
320 
321             return ac;
322         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
323             System.out.println("ERROR: could not get child at index " + cellIndex +"': " + ex.toString());
324             return null;
325         }
326     }
327 
printAccessibleTree(PrintWriter log, XAccessible xacc, boolean debugIsActive)328     public static void printAccessibleTree(PrintWriter log, XAccessible xacc, boolean debugIsActive) {
329         debug = debugIsActive;
330         if (debug) printAccessibleTree(log, xacc, "");
331     }
332 
printAccessibleTree(PrintWriter log, XAccessible xacc)333     public static void printAccessibleTree(PrintWriter log, XAccessible xacc) {
334         printAccessibleTree(log, xacc, "");
335     }
336 
printAccessibleTree(PrintWriter log, XAccessible xacc, String indent)337     protected static void printAccessibleTree(PrintWriter log,
338         XAccessible xacc, String indent) {
339 
340         XAccessibleContext ac = xacc.getAccessibleContext();
341 
342         logging(log,indent + ac.getAccessibleRole() + "," +
343             ac.getAccessibleName() + "(" +
344             ac.getAccessibleDescription() + "):" +
345             utils.getImplName(ac));
346 
347         XAccessibleComponent aComp = (XAccessibleComponent) UnoRuntime.queryInterface(
348             XAccessibleComponent.class, xacc);
349 
350         if (aComp != null) {
351             String bounds = "(" + aComp.getBounds().X + "," +
352                 aComp.getBounds().Y + ")" + " (" +
353                 aComp.getBounds().Width + "," +
354                 aComp.getBounds().Height + ")";
355             bounds = "The boundary Rectangle is " + bounds;
356             logging(log,indent + indent + bounds);
357         }
358 
359         boolean isShowing = ac.getAccessibleStateSet()
360         .contains(com.sun.star.accessibility.AccessibleStateType.SHOWING);
361         logging(log,indent + indent + "StateType contains SHOWING: " +
362             isShowing);
363 
364         int k = ac.getAccessibleChildCount();
365 
366         if (ac.getAccessibleChildCount() > 100) {
367             k = 50;
368         }
369 
370         for (int i = 0; i < k; i++) {
371             try {
372                 printAccessibleTree(log, ac.getAccessibleChild(i),
373                     indent + "  ");
374             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
375                 System.out.println("Couldn't get Child");
376             }
377         }
378 
379         if (ac.getAccessibleChildCount() > 100) {
380             k = ac.getAccessibleChildCount();
381 
382             int st = ac.getAccessibleChildCount() - 50;
383             logging(log,indent + "  " + " ...... [skipped] ......");
384 
385             for (int i = st; i < k; i++) {
386                 try {
387                     printAccessibleTree(log, ac.getAccessibleChild(i),
388                         indent + "  ");
389                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
390                     System.out.println("Couldn't get Child");
391                 }
392             }
393         }
394     }
395 
accessibleToString(Object AC)396     public static String accessibleToString(Object AC) {
397         XAccessibleContext xAC = (XAccessibleContext) UnoRuntime.queryInterface(
398             XAccessibleContext.class, AC);
399 
400         if (xAC != null) {
401             return "" + xAC.getAccessibleRole() + "," +
402                 xAC.getAccessibleName() + "(" +
403                 xAC.getAccessibleDescription() + "):";
404         }
405 
406         XAccessible xA = (XAccessible) UnoRuntime.queryInterface(
407             XAccessible.class, AC);
408 
409         if (xA == null) {
410             return "(Not supported)";
411         }
412 
413         xAC = xA.getAccessibleContext();
414 
415         return "" + xAC.getAccessibleRole() + "," + xAC.getAccessibleName() +
416             "(" + xAC.getAccessibleDescription() + ")";
417     }
418 
equals(XAccessible c1, XAccessible c2)419     public static boolean equals(XAccessible c1, XAccessible c2) {
420         if ((c1 == null) || (c2 == null)) {
421             return c1 == c2;
422         }
423 
424         return AccessibilityTools.equals(c1.getAccessibleContext(),
425             c2.getAccessibleContext());
426     }
427 
equals(XAccessibleContext c1, XAccessibleContext c2)428     public static boolean equals(XAccessibleContext c1, XAccessibleContext c2) {
429         if ((c1 == null) || (c2 == null)) {
430             return c1 == c2;
431         }
432 
433         if (c1.getAccessibleRole() != c2.getAccessibleRole()) {
434             return false;
435         }
436 
437         if (!c1.getAccessibleName().equals(c2.getAccessibleName())) {
438             return false;
439         }
440 
441         if (!c1.getAccessibleDescription()
442         .equals(c2.getAccessibleDescription())) {
443             return false;
444         }
445 
446         if (c1.getAccessibleChildCount() != c2.getAccessibleChildCount()) {
447             return false;
448         }
449 
450         return AccessibilityTools.equals(c1.getAccessibleParent(),
451             c2.getAccessibleParent());
452     }
453 
logging(PrintWriter log, String content)454     private static void logging(PrintWriter log, String content){
455         if (debug) log.println(content);
456     }
457 }