summaryrefslogtreecommitdiff
path: root/qadevOOo/runner/util/AccessibilityTools.java
blob: c7cb483674c91241f45ad2b90e5cc4b7de4fc40a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
package util;

import com.sun.star.accessibility.AccessibleStateType;
import com.sun.star.accessibility.XAccessible;
import com.sun.star.accessibility.XAccessibleComponent;
import com.sun.star.accessibility.XAccessibleContext;
import com.sun.star.awt.XWindow;
import com.sun.star.frame.XController;
import com.sun.star.frame.XFrame;
import com.sun.star.frame.XModel;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XInterface;

import java.io.PrintWriter;


public class AccessibilityTools {
    private XAccessible SearchedAccessible = null;
    private static boolean debug = false;

    public AccessibilityTools() {
        //done = false;
    }

    public static XAccessible getAccessibleObject(XInterface xObject) {
        return UnoRuntime.queryInterface(XAccessible.class, xObject);
    }

    public static XWindow getCurrentContainerWindow(XMultiServiceFactory msf,
        XModel xModel) {
        return getWindow(msf, xModel, true);
    }

    public static XWindow getCurrentWindow(XMultiServiceFactory msf,
        XModel xModel) {
        return getWindow(msf, xModel, false);
    }

    private static XWindow getWindow(XMultiServiceFactory msf, XModel xModel,
        boolean containerWindow) {
        XWindow xWindow = null;

        try {
            if (xModel == null) {
                System.out.println("invalid model (==null)");
            }

            XController xController = xModel.getCurrentController();

            if (xController == null) {
                System.out.println("can't get controller from model");
            }

            XFrame xFrame = xController.getFrame();

            if (xFrame == null) {
                System.out.println("can't get frame from controller");
            }

            if (containerWindow)
                xWindow = xFrame.getContainerWindow();
            else
                xWindow = xFrame.getComponentWindow();

            if (xWindow == null) {
                System.out.println("can't get window from frame");
            }
        } catch (Exception e) {
            System.out.println("caught exception while getting current window" + e);
        }

        return xWindow;
    }

    public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
        short role) {
        return getAccessibleObjectForRole_(xacc, role);
    }

    public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
        short role,
        boolean ignoreShowing) {
        if (ignoreShowing) {
            return getAccessibleObjectForRoleIgnoreShowing_(xacc, role);
        } else {
            return getAccessibleObjectForRole_(xacc, role);
        }
    }

    public XAccessible getSearchedAccessibleObjectForRole(XAccessible xacc,
        short role,
        boolean ignoreShowing) {
        SearchedAccessible = null;
        getSearchedAccessibleObjectForRole_(xacc, role, ignoreShowing);
        return SearchedAccessible;
    }

    private XAccessibleContext getSearchedAccessibleObjectForRole_(XAccessible xacc,
            short role, boolean ignoreShowing) {
        XAccessibleContext ac = xacc.getAccessibleContext();
        boolean isShowing = isShowing(ac);

        if ((ac.getAccessibleRole() == role) && (!ignoreShowing || isShowing)) {
            SearchedAccessible = xacc;
            return ac;
        } else {
            int k = ac.getAccessibleChildCount();

            if (ac.getAccessibleChildCount() > 100) {
                k = 50;
            }

            for (int i = 0; i < k; i++) {
                try {
                    XAccessibleContext ac2 = getSearchedAccessibleObjectForRole_(ac.getAccessibleChild(i), role, ignoreShowing);

                    if (ac2 != null) {
                        return ac2;
                    }
                } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
                    System.out.println("Couldn't get Child");
                }
            }
            return null;
        }
    }

    public static XAccessibleContext getAccessibleObjectForRoleIgnoreShowing_(XAccessible xacc,
        short role) {
        XAccessibleContext ac = xacc.getAccessibleContext();

        if (ac.getAccessibleRole() == role) {
            return ac;
        } else {
            int k = ac.getAccessibleChildCount();

            if (ac.getAccessibleChildCount() > 100) {
                k = 50;
            }

            for (int i = 0; i < k; i++) {
                try {
                    XAccessibleContext ac2 = getAccessibleObjectForRoleIgnoreShowing_(
                        ac.getAccessibleChild(i), role);

                    if (ac2 != null) {
                        return ac2;
                    }
                } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
                    System.out.println("Couldn't get Child");
                }
            }
            return null;
        }
    }

    public static XAccessibleContext getAccessibleObjectForRole_(XAccessible xacc,
        short role) {
        XAccessibleContext ac = xacc.getAccessibleContext();
        boolean isShowing = isShowing(ac);

        if ((ac.getAccessibleRole() == role) && isShowing) {
            return ac;
        } else {
            int k = ac.getAccessibleChildCount();

            if (ac.getAccessibleChildCount() > 100) {
                k = 50;
            }

            for (int i = 0; i < k; i++) {
                try {
                    XAccessibleContext ac2 = getAccessibleObjectForRole_(ac.getAccessibleChild(i), role);

                    if (ac2 != null) {
                        return ac2;
                    }
                } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
                    System.out.println("Couldn't get Child");
                }
            }
            return null;
        }
    }

    public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
        short role,
        String name) {
        return getAccessibleObjectForRole(xacc, role, name, "");
    }

    public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
        short role,
        String name,
        boolean ignoreShowing) {
        if (ignoreShowing) {
            return getAccessibleObjectForRoleIgnoreShowing(xacc, role, name,
                "");
        } else {
            return getAccessibleObjectForRole(xacc, role, name, "");
        }
    }

    public static XAccessibleContext getAccessibleObjectForRoleIgnoreShowing(XAccessible xacc,
        short role,
        String name,
        String implName) {
        XAccessibleContext ac = xacc.getAccessibleContext();
        if ((ac.getAccessibleRole() == role) &&
            (ac.getAccessibleName().indexOf(name) > -1) &&
            (utils.getImplName(ac).indexOf(implName) > -1)) {

            //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
            return ac;
        } else {
            int k = ac.getAccessibleChildCount();

            if (ac.getAccessibleChildCount() > 100) {
                k = 50;
            }

            for (int i = 0; i < k; i++) {
                try {
                    XAccessibleContext ac1 = getAccessibleObjectForRoleIgnoreShowing(
                        ac.getAccessibleChild(i),
                        role, name, implName);

                    if (ac1 != null) {
                        return ac1;
                    }
                } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
                    System.out.println("Couldn't get Child");
                }
            }
        }

        return null;
    }

    public static XAccessibleContext getAccessibleObjectForRole(XAccessible xacc,
        short role,
        String name,
        String implName) {
        XAccessibleContext ac = xacc.getAccessibleContext();
        boolean isShowing = isShowing(ac);

        // hotfix for i91828:
        // if role to search is 0 then ignore the role.
        if ( (role == 0 || ac.getAccessibleRole() == role) &&
            (ac.getAccessibleName().indexOf(name) > -1) &&
            (utils.getImplName(ac).indexOf(implName) > -1) &&
            isShowing) {
            //System.out.println("FOUND the desired component -- "+ ac.getAccessibleName() +isShowing);
            return ac;
        } else {
            int k = ac.getAccessibleChildCount();

            if (ac.getAccessibleChildCount() > 100) {
                k = 50;
            }

            for (int i = 0; i < k; i++) {
                try {
                    XAccessibleContext ac1 = getAccessibleObjectForRole(
                        ac.getAccessibleChild(i),
                        role, name, implName);

                    if (ac1 != null) {
                        return ac1;
                    }
                } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
                    System.out.println("Couldn't get Child");
                }
            }
        }

        return null;
    }

    public XAccessible getSearchedAccessibleObjectForRole(XAccessible xacc,
            short role,
            String name,
            String implName) {
            XAccessibleContext ac = xacc.getAccessibleContext();
            boolean isShowing = isShowing(ac);

            // hotfix for i91828:
            // if role to search is 0 then ignore the role.
            if ( (role == 0 || ac.getAccessibleRole() == role) &&
                (ac.getAccessibleName().indexOf(name) > -1) &&
                (utils.getImplName(ac).indexOf(implName) > -1) &&
                isShowing) {
                SearchedAccessible = xacc;
            } else {
                int k = ac.getAccessibleChildCount();

                if (ac.getAccessibleChildCount() > 100) {
                    k = 50;
                }

                for (int i = 0; i < k; i++) {
                    try {
                        XAccessibleContext ac1 = getAccessibleObjectForRole(
                            ac.getAccessibleChild(i),
                            role, name, implName);

                        if (ac1 != null) {
                            break;
                        }
                    } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
                        System.out.println("Couldn't get Child");
                    }
                }
            }

            return SearchedAccessible;
        }

    /**
     * This methods retunrs the <CODE>XAccessibleContext</CODE> of a named Sheet-Cell like "G5".<p>
     * @param xSheetAcc The <CODE>XAccessibleContext</CODE> of a Sheet
     * @param cellName The name of a cell like "A5"
     * @return the <CODE>XAccessiblecontext</CODE> of the named cell
     */
    public static XAccessibleContext getSheetCell(XAccessibleContext xSheetAcc, String cellName){

        int cellIndex = 0;
        int column =0;
        int charMem = 0;
        for (int n=0; n<cellName.length(); n++){
            String cha = cellName.substring(n,n+1);
            System.out.println("char: " + cha + " ");

            byte[] bytes = cha.getBytes();

            if ((bytes[0] >= 'A') && (bytes[0] <= 'Z')){
                charMem = bytes[0]-64;
                column++;
                if ( column == 2 ){
                    cellIndex += charMem * 26;
                }
                cellIndex= cellIndex+ (bytes[0]-65);
            } else {
                String sNumb = cellName.substring(n, cellName.length());
                int iNumb = Integer.valueOf(sNumb).intValue();
                cellIndex += (iNumb-1) * 256;
                System.out.println("numb:" + (iNumb-1) * 256);
            }

        }

        //System.out.println("cellName:  " + cellName + " cellIndex: " + cellIndex);

        try {
            XAccessibleContext ac = xSheetAcc.getAccessibleChild(cellIndex).getAccessibleContext();
            System.out.println(ac.getAccessibleRole() + "," +
                ac.getAccessibleName() + "(" +
                ac.getAccessibleDescription() + "):" +
                utils.getImplName(ac));

            return ac;
        } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
            System.out.println("ERROR: could not get child at index " + cellIndex +"': " + ex.toString());
            return null;
        }
    }

    public static void printAccessibleTree(PrintWriter log, XAccessible xacc, boolean debugIsActive) {
        debug = debugIsActive;
        if (debug) printAccessibleTree(log, xacc, "");
    }

    public static void printAccessibleTree(PrintWriter log, XAccessible xacc) {
        printAccessibleTree(log, xacc, "");
    }

    protected static void printAccessibleTree(PrintWriter log,
        XAccessible xacc, String indent) {

        XAccessibleContext ac = xacc.getAccessibleContext();

        logging(log,indent + ac.getAccessibleRole() + "," +
            ac.getAccessibleName() + "(" +
            ac.getAccessibleDescription() + "):" +
            utils.getImplName(ac));

        XAccessibleComponent aComp = UnoRuntime.queryInterface(
            XAccessibleComponent.class, xacc);

        if (aComp != null) {
            String bounds = "(" + aComp.getBounds().X + "," +
                aComp.getBounds().Y + ")" + " (" +
                aComp.getBounds().Width + "," +
                aComp.getBounds().Height + ")";
            bounds = "The boundary Rectangle is " + bounds;
            logging(log,indent + indent + bounds);
        }

        boolean isShowing = isShowing(ac);
        logging(log,indent + indent + "StateType contains SHOWING: " +
            isShowing);

        int k = ac.getAccessibleChildCount();

        if (ac.getAccessibleChildCount() > 100) {
            k = 50;
        }

        for (int i = 0; i < k; i++) {
            try {
                printAccessibleTree(log, ac.getAccessibleChild(i),
                    indent + "  ");
            } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
                System.out.println("Couldn't get Child");
            }
        }

        if (ac.getAccessibleChildCount() > 100) {
            k = ac.getAccessibleChildCount();

            int st = ac.getAccessibleChildCount() - 50;
            logging(log,indent + "  " + " ...... [skipped] ......");

            for (int i = st; i < k; i++) {
                try {
                    printAccessibleTree(log, ac.getAccessibleChild(i),
                        indent + "  ");
                } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
                    System.out.println("Couldn't get Child");
                }
            }
        }
    }

    public static String accessibleToString(Object AC) {
        XAccessibleContext xAC = UnoRuntime.queryInterface(
            XAccessibleContext.class, AC);

        if (xAC != null) {
            return "" + xAC.getAccessibleRole() + "," +
                xAC.getAccessibleName() + "(" +
                xAC.getAccessibleDescription() + "):";
        }

        XAccessible xA = UnoRuntime.queryInterface(
            XAccessible.class, AC);

        if (xA == null) {
            return "(Not supported)";
        }

        xAC = xA.getAccessibleContext();

        return "" + xAC.getAccessibleRole() + "," + xAC.getAccessibleName() +
            "(" + xAC.getAccessibleDescription() + ")";
    }

    public static boolean equals(XAccessible c1, XAccessible c2) {
        if ((c1 == null) || (c2 == null)) {
            return c1 == c2;
        }

        return AccessibilityTools.equals(c1.getAccessibleContext(),
            c2.getAccessibleContext());
    }

    public static boolean equals(XAccessibleContext c1, XAccessibleContext c2) {
        if ((c1 == null) || (c2 == null)) {
            return c1 == c2;
        }

        if (c1.getAccessibleRole() != c2.getAccessibleRole()) {
            return false;
        }

        if (!c1.getAccessibleName().equals(c2.getAccessibleName())) {
            return false;
        }

        if (!c1.getAccessibleDescription()
        .equals(c2.getAccessibleDescription())) {
            return false;
        }

        if (c1.getAccessibleChildCount() != c2.getAccessibleChildCount()) {
            return false;
        }

        return AccessibilityTools.equals(c1.getAccessibleParent(),
            c2.getAccessibleParent());
    }

    private static void logging(PrintWriter log, String content){
        if (debug) log.println(content);
    }

    private static boolean isShowing(XAccessibleContext ac) {
        return ac.getAccessibleStateSet().contains(AccessibleStateType.SHOWING);
    }
}