forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAccessibility.java
904 lines (770 loc) · 36.4 KB
/
CAccessibility.java
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.lwawt.macosx;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.Arrays;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleAction;
import javax.accessibility.AccessibleComponent;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;
import javax.accessibility.AccessibleSelection;
import javax.accessibility.AccessibleState;
import javax.accessibility.AccessibleStateSet;
import javax.accessibility.AccessibleTable;
import javax.accessibility.AccessibleText;
import javax.accessibility.AccessibleValue;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JList;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import sun.awt.AWTAccessor;
import sun.lwawt.LWWindowPeer;
@SuppressWarnings("removal")
class CAccessibility implements PropertyChangeListener {
private static Set<String> ignoredRoles;
static {
// Need to load the native library for this code.
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("awt");
return null;
}
});
}
static CAccessibility sAccessibility;
static synchronized CAccessibility getAccessibility(final String[] roles) {
if (sAccessibility != null) return sAccessibility;
sAccessibility = new CAccessibility();
if (roles != null) {
ignoredRoles = new HashSet<String>(roles.length);
for (final String role : roles) ignoredRoles.add(role);
} else {
ignoredRoles = new HashSet<String>();
}
return sAccessibility;
}
private CAccessibility() {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner", this);
}
public void propertyChange(final PropertyChangeEvent evt) {
Object newValue = evt.getNewValue();
if (newValue == null) return;
// Don't post focus on things that don't matter, i.e. alert, colorchooser,
// desktoppane, dialog, directorypane, filechooser, filler, fontchoose,
// frame, glasspane, layeredpane, optionpane, panel, rootpane, separator,
// tooltip, viewport, window.
// List taken from initializeRoles() in JavaComponentUtilities.m.
if (newValue instanceof Accessible) {
AccessibleContext nvAC = ((Accessible) newValue).getAccessibleContext();
AccessibleRole nvRole = nvAC.getAccessibleRole();
if (!ignoredRoles.contains(roleKey(nvRole))) {
focusChanged();
}
}
}
private native void focusChanged();
static <T> T invokeAndWait(final Callable<T> callable, final Component c) {
return invokeAndWait(callable, c, null);
}
static <T> T invokeAndWait(final Callable<T> callable, final Component c, final T defValue) {
T value = null;
if (c != null) {
try {
value = EventQueue.isDispatchThread() ? callable.call() : LWCToolkit.invokeAndWait(callable, c);
} catch (final Exception e) { e.printStackTrace(); }
}
return value != null ? value : defValue;
}
static void invokeLater(final Runnable runnable, final Component c) {
if (c != null) {
try {
LWCToolkit.invokeLater(runnable, c);
} catch (InvocationTargetException e) { e.printStackTrace(); }
}
}
public static String getAccessibleActionDescription(final AccessibleAction aa, final int index, final Component c) {
if (aa == null) return null;
return invokeAndWait(new Callable<String>() {
public String call() throws Exception {
return aa.getAccessibleActionDescription(index);
}
}, c);
}
public static void doAccessibleAction(final AccessibleAction aa, final int index, final Component c) {
// We make this an invokeLater because we don't need a reply.
if (aa == null) return;
invokeLater(new Runnable() {
public void run() {
aa.doAccessibleAction(index);
}
}, c);
}
public static Dimension getSize(final AccessibleComponent ac, final Component c) {
if (ac == null) return null;
return invokeAndWait(new Callable<Dimension>() {
public Dimension call() throws Exception {
return ac.getSize();
}
}, c);
}
public static AccessibleSelection getAccessibleSelection(final AccessibleContext ac, final Component c) {
if (ac == null) return null;
return invokeAndWait(new Callable<AccessibleSelection>() {
public AccessibleSelection call() throws Exception {
return ac.getAccessibleSelection();
}
}, c);
}
public static Accessible ax_getAccessibleSelection(final AccessibleContext ac, final int index, final Component c) {
if (ac == null) return null;
return invokeAndWait(new Callable<Accessible>() {
public Accessible call() throws Exception {
final AccessibleSelection as = ac.getAccessibleSelection();
if (as == null) return null;
return as.getAccessibleSelection(index);
}
}, c);
}
// KCH - can we make this a postEvent?
public static void addAccessibleSelection(final AccessibleContext ac, final int index, final Component c) {
if (ac == null) return;
invokeLater(new Runnable() {
public void run() {
final AccessibleSelection as = ac.getAccessibleSelection();
if (as == null) return;
as.addAccessibleSelection(index);
}
}, c);
}
public static AccessibleContext getAccessibleContext(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<AccessibleContext>() {
public AccessibleContext call() throws Exception {
return a.getAccessibleContext();
}
}, c);
}
public static boolean isAccessibleChildSelected(final Accessible a, final int index, final Component c) {
if (a == null) return false;
return invokeAndWait(new Callable<Boolean>() {
public Boolean call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return Boolean.FALSE;
final AccessibleSelection as = ac.getAccessibleSelection();
if (as == null) return Boolean.FALSE;
return as.isAccessibleChildSelected(index);
}
}, c, false);
}
public static AccessibleStateSet getAccessibleStateSet(final AccessibleContext ac, final Component c) {
if (ac == null) return null;
return invokeAndWait(new Callable<AccessibleStateSet>() {
public AccessibleStateSet call() throws Exception {
return ac.getAccessibleStateSet();
}
}, c);
}
public static boolean contains(final AccessibleContext ac, final AccessibleState as, final Component c) {
if (ac == null || as == null) return false;
return invokeAndWait(new Callable<Boolean>() {
public Boolean call() throws Exception {
final AccessibleStateSet ass = ac.getAccessibleStateSet();
if (ass == null) return null;
return ass.contains(as);
}
}, c, false);
}
static String getAccessibleRoleFor(final Accessible a) {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final AccessibleRole role = ac.getAccessibleRole();
return AWTAccessor.getAccessibleBundleAccessor().getKey(role);
}
public static String getAccessibleRole(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<String>() {
public String call() throws Exception {
final Accessible sa = CAccessible.getSwingAccessible(a);
final String role = getAccessibleRoleFor(a);
if (!"text".equals(role)) return role;
if (sa instanceof JTextArea || sa instanceof JEditorPane) {
return "textarea";
}
return role;
}
}, c);
}
public static Point getLocationOnScreen(final AccessibleComponent ac, final Component c) {
if (ac == null) return null;
return invokeAndWait(new Callable<Point>() {
public Point call() throws Exception {
return ac.getLocationOnScreen();
}
}, c);
}
public static int getCharCount(final AccessibleText at, final Component c) {
if (at == null) return 0;
return invokeAndWait(new Callable<Integer>() {
public Integer call() throws Exception {
return at.getCharCount();
}
}, c, 0);
}
// Accessibility Threadsafety for JavaComponentAccessibility.m
public static Accessible getAccessibleParent(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<Accessible>() {
public Accessible call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
return ac.getAccessibleParent();
}
}, c);
}
public static int getAccessibleIndexInParent(final Accessible a, final Component c) {
if (a == null) return -1;
return invokeAndWait(new Callable<Integer>() {
public Integer call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
return ac.getAccessibleIndexInParent();
}
}, c, -1);
}
public static AccessibleComponent getAccessibleComponent(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<AccessibleComponent>() {
public AccessibleComponent call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
return ac.getAccessibleComponent();
}
}, c);
}
public static AccessibleValue getAccessibleValue(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<AccessibleValue>() {
public AccessibleValue call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
AccessibleValue accessibleValue = ac.getAccessibleValue();
return accessibleValue;
}
}, c);
}
public static String getAccessibleName(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<String>() {
public String call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final String accessibleName = ac.getAccessibleName();
if (accessibleName == null) {
return ac.getAccessibleDescription();
}
final String acceleratorText = getAcceleratorText(ac);
if (!acceleratorText.isEmpty()) {
return accessibleName +' '+ acceleratorText;
}
return accessibleName;
}
}, c);
}
public static AccessibleText getAccessibleText(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<AccessibleText>() {
public AccessibleText call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
AccessibleText accessibleText = ac.getAccessibleText();
return accessibleText;
}
}, c);
}
/*
* Returns the JMenuItem accelerator. Implementation of this method is based
* on AccessBridge.getAccelerator(AccessibleContext) to access the KeyStroke
* and on AquaMenuPainter.paintMenuItem() to convert it to string.
*/
@SuppressWarnings("deprecation")
private static String getAcceleratorText(AccessibleContext ac) {
String accText = "";
Accessible parent = ac.getAccessibleParent();
if (parent != null) {
// workaround for getAccessibleKeyBinding not returning the
// JMenuItem accelerator
int indexInParent = ac.getAccessibleIndexInParent();
Accessible child = parent.getAccessibleContext()
.getAccessibleChild(indexInParent);
if (child instanceof JMenuItem) {
JMenuItem menuItem = (JMenuItem) child;
KeyStroke keyStroke = menuItem.getAccelerator();
if (keyStroke != null) {
int modifiers = keyStroke.getModifiers();
if (modifiers > 0) {
accText = KeyEvent.getKeyModifiersText(modifiers);
}
int keyCode = keyStroke.getKeyCode();
if (keyCode != 0) {
accText += KeyEvent.getKeyText(keyCode);
} else {
accText += keyStroke.getKeyChar();
}
}
}
}
return accText;
}
public static String getAccessibleDescription(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<String>() {
public String call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final String accessibleDescription = ac.getAccessibleDescription();
if (accessibleDescription == null) {
if (c instanceof JComponent) {
String toolTipText = ((JComponent)c).getToolTipText();
if (toolTipText != null) {
return toolTipText;
}
}
}
return accessibleDescription;
}
}, c);
}
public static boolean isFocusTraversable(final Accessible a, final Component c) {
if (a == null) return false;
return invokeAndWait(new Callable<Boolean>() {
public Boolean call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final AccessibleComponent aComp = ac.getAccessibleComponent();
if (aComp == null) return null;
return aComp.isFocusTraversable();
}
}, c, false);
}
public static Accessible accessibilityHitTest(final Container parent, final float hitPointX, final float hitPointY) {
return invokeAndWait(new Callable<Accessible>() {
public Accessible call() throws Exception {
final Point p = parent.getLocationOnScreen();
// Make it into local coords
final Point localPoint = new Point((int)(hitPointX - p.getX()), (int)(hitPointY - p.getY()));
final Component component = parent.findComponentAt(localPoint);
if (component == null) return null;
final AccessibleContext axContext = component.getAccessibleContext();
if (axContext == null) return null;
final AccessibleComponent axComponent = axContext.getAccessibleComponent();
if (axComponent == null) return null;
final int numChildren = axContext.getAccessibleChildrenCount();
if (numChildren > 0) {
// It has children, check to see which one is hit.
final Point p2 = axComponent.getLocationOnScreen();
final Point localP2 = new Point((int)(hitPointX - p2.getX()), (int)(hitPointY - p2.getY()));
return CAccessible.getCAccessible(axComponent.getAccessibleAt(localP2));
}
if (!(component instanceof Accessible)) return null;
return CAccessible.getCAccessible((Accessible)component);
}
}, parent);
}
public static AccessibleAction getAccessibleAction(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<AccessibleAction>() {
public AccessibleAction call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
return ac.getAccessibleAction();
}
}, c);
}
public static boolean isEnabled(final Accessible a, final Component c) {
if (a == null) return false;
return invokeAndWait(new Callable<Boolean>() {
public Boolean call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final AccessibleComponent aComp = ac.getAccessibleComponent();
if (aComp == null) return null;
return aComp.isEnabled();
}
}, c, false);
}
// KCH - can we make this a postEvent instead?
public static void requestFocus(final Accessible a, final Component c) {
if (a == null) return;
invokeLater(new Runnable() {
public void run() {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return;
final AccessibleComponent aComp = ac.getAccessibleComponent();
if (aComp == null) return;
aComp.requestFocus();
}
}, c);
}
public static void requestSelection(final Accessible a, final Component c) {
if (a == null) return;
invokeLater(new Runnable() {
public void run() {
AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return;
int i = ac.getAccessibleIndexInParent();
if (i == -1) return;
Accessible parent = ac.getAccessibleParent();
AccessibleContext pac = parent.getAccessibleContext();
if (pac == null) return;
AccessibleSelection as = pac.getAccessibleSelection();
if (as == null) return;
if (parent instanceof JList) {
((JList) parent).setSelectedIndex(i);
return;
}
as.addAccessibleSelection(i);
}
}, c);
}
public static Number getMaximumAccessibleValue(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<Number>() {
public Number call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final AccessibleValue av = ac.getAccessibleValue();
if (av == null) return null;
return av.getMaximumAccessibleValue();
}
}, c);
}
public static Number getMinimumAccessibleValue(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<Number>() {
public Number call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final AccessibleValue av = ac.getAccessibleValue();
if (av == null) return null;
return av.getMinimumAccessibleValue();
}
}, c);
}
public static String getAccessibleRoleDisplayString(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<String>() {
public String call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final AccessibleRole ar = ac.getAccessibleRole();
if (ar == null) return null;
return ar.toDisplayString();
}
}, c);
}
public static Number getCurrentAccessibleValue(final AccessibleValue av, final Component c) {
if (av == null) return null;
return invokeAndWait(new Callable<Number>() {
public Number call() throws Exception {
Number currentAccessibleValue = av.getCurrentAccessibleValue();
return currentAccessibleValue;
}
}, c);
}
public static Accessible getFocusOwner(final Component c) {
return invokeAndWait(new Callable<Accessible>() {
public Accessible call() throws Exception {
Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (c == null || !(c instanceof Accessible)) return null;
return CAccessible.getCAccessible((Accessible)c);
}
}, c);
}
public static boolean[] getInitialAttributeStates(final Accessible a, final Component c) {
final boolean[] ret = new boolean[7];
if (a == null) return ret;
return invokeAndWait(new Callable<boolean[]>() {
public boolean[] call() throws Exception {
final AccessibleContext aContext = a.getAccessibleContext();
if (aContext == null) return ret;
final AccessibleComponent aComponent = aContext.getAccessibleComponent();
ret[0] = (aComponent != null);
ret[1] = ((aComponent != null) && (aComponent.isFocusTraversable()));
ret[2] = (aContext.getAccessibleValue() != null);
ret[3] = (aContext.getAccessibleText() != null);
final AccessibleStateSet aStateSet = aContext.getAccessibleStateSet();
ret[4] = (aStateSet.contains(AccessibleState.HORIZONTAL) || aStateSet.contains(AccessibleState.VERTICAL));
ret[5] = (aContext.getAccessibleName() != null);
ret[6] = (aContext.getAccessibleChildrenCount() > 0);
return ret;
}
}, c);
}
// Duplicated from JavaComponentAccessibility
// Note that values >=0 are indexes into the child array
static final int JAVA_AX_ALL_CHILDREN = -1;
static final int JAVA_AX_SELECTED_CHILDREN = -2;
static final int JAVA_AX_VISIBLE_CHILDREN = -3;
// Each child takes up two entries in the array: one for itself and one for its role
public static Object[] getChildrenAndRoles(final Accessible a, final Component c, final int whichChildren, final boolean allowIgnored) {
if (a == null) return null;
return invokeAndWait(new Callable<Object[]>() {
public Object[] call() throws Exception {
ArrayList<Object> childrenAndRoles = new ArrayList<Object>();
_addChildren(a, whichChildren, allowIgnored, childrenAndRoles);
/* In the case of fetching a selection, need to check to see if
* the active descendant is at the beginning of the list. If it
* is not it needs to be moved to the beginning of the list so
* VoiceOver will annouce it correctly. The list returned
* from Java is always in order from top to bottom, but when shift
* selecting downward (extending the list) or multi-selecting using
* the VO keys control+option+command+return the active descendant
* is not at the top of the list in the shift select down case and
* may not be in the multi select case.
*/
if (whichChildren == JAVA_AX_SELECTED_CHILDREN) {
if (!childrenAndRoles.isEmpty()) {
AccessibleContext activeDescendantAC =
CAccessible.getActiveDescendant(a);
if (activeDescendantAC != null) {
String activeDescendantName =
activeDescendantAC.getAccessibleName();
AccessibleRole activeDescendantRole =
activeDescendantAC.getAccessibleRole();
// Move active descendant to front of list.
// List contains pairs of each selected item's
// Accessible and AccessibleRole.
ArrayList<Object> newArray = new ArrayList<Object>();
int count = childrenAndRoles.size();
Accessible currentAccessible = null;
AccessibleContext currentAC = null;
String currentName = null;
AccessibleRole currentRole = null;
for (int i = 0; i < count; i+=2) {
// Is this the active descendant?
currentAccessible = (Accessible)childrenAndRoles.get(i);
currentAC = currentAccessible.getAccessibleContext();
currentName = currentAC.getAccessibleName();
currentRole = (AccessibleRole)childrenAndRoles.get(i+1);
if (currentName != null && currentName.equals(activeDescendantName) &&
currentRole.equals(activeDescendantRole) ) {
newArray.add(0, currentAccessible);
newArray.add(1, currentRole);
} else {
newArray.add(currentAccessible);
newArray.add(currentRole);
}
}
childrenAndRoles = newArray;
}
}
}
if ((whichChildren < 0) || (whichChildren * 2 >= childrenAndRoles.size())) {
return childrenAndRoles.toArray();
}
return new Object[] { childrenAndRoles.get(whichChildren * 2), childrenAndRoles.get((whichChildren * 2) + 1) };
}
}, c);
}
// This method is called from the native
// Each child takes up three entries in the array: one for itself, one for its role, and one for the recursion level
private static Object[] getChildrenAndRolesRecursive(final Accessible a, final Component c, final int whichChildren, final boolean allowIgnored, final int level) {
if (a == null) return null;
return invokeAndWait(new Callable<Object[]>() {
public Object[] call() throws Exception {
ArrayList<Object> currentLevelChildren = new ArrayList<Object>();
currentLevelChildren.addAll(Arrays.asList(getChildrenAndRoles(a, c, JAVA_AX_ALL_CHILDREN, allowIgnored)));
ArrayList<Object> allChildren = new ArrayList<Object>();
for (int i = 0; i < currentLevelChildren.size(); i += 2) {
if ((((Accessible) currentLevelChildren.get(i)).getAccessibleContext().getAccessibleStateSet().contains(AccessibleState.SELECTED) && (whichChildren == JAVA_AX_SELECTED_CHILDREN)) ||
(((Accessible) currentLevelChildren.get(i)).getAccessibleContext().getAccessibleStateSet().contains(AccessibleState.VISIBLE) && (whichChildren == JAVA_AX_VISIBLE_CHILDREN)) ||
(whichChildren == JAVA_AX_ALL_CHILDREN)) {
allChildren.add(currentLevelChildren.get(i));
allChildren.add(currentLevelChildren.get(i + 1));
allChildren.add(String.valueOf(level));
}
if (getAccessibleStateSet(((Accessible) currentLevelChildren.get(i)).getAccessibleContext(), c).contains(AccessibleState.EXPANDED)) {
allChildren.addAll(Arrays.asList(getChildrenAndRolesRecursive(((Accessible) currentLevelChildren.get(i)), c, whichChildren, allowIgnored, level + 1)));
}
}
return allChildren.toArray();
}
}, c);
}
private static final int JAVA_AX_ROWS = 1;
private static final int JAVA_AX_COLS = 2;
public static int getTableInfo(final Accessible a, final Component c,
final int info) {
if (a == null) return 0;
return invokeAndWait(() -> {
AccessibleContext ac = a.getAccessibleContext();
AccessibleTable table = ac.getAccessibleTable();
if (table != null) {
if (info == JAVA_AX_COLS) {
return table.getAccessibleColumnCount();
} else if (info == JAVA_AX_ROWS) {
return table.getAccessibleRowCount();
}
}
return 0;
}, c);
}
private static AccessibleRole getAccessibleRoleForLabel(JLabel l, AccessibleRole fallback) {
String text = l.getText();
if (text != null && text.length() > 0) {
return fallback;
}
Icon icon = l.getIcon();
if (icon != null) {
return AccessibleRole.ICON;
}
return fallback;
}
private static AccessibleRole getAccessibleRole(Accessible a) {
AccessibleContext ac = a.getAccessibleContext();
AccessibleRole role = ac.getAccessibleRole();
Object component = CAccessible.getSwingAccessible(a);
if (role == null) return null;
String roleString = role.toString();
if ("label".equals(roleString) && component instanceof JLabel) {
return getAccessibleRoleForLabel((JLabel) component, role);
}
return role;
}
// Either gets the immediate children of a, or recursively gets all unignored children of a
private static void _addChildren(final Accessible a, final int whichChildren, final boolean allowIgnored, final ArrayList<Object> childrenAndRoles) {
if (a == null) return;
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return;
final int numChildren = ac.getAccessibleChildrenCount();
// each child takes up two entries in the array: itself, and its role
// so the array holds alternating Accessible and AccessibleRole objects
for (int i = 0; i < numChildren; i++) {
final Accessible child = ac.getAccessibleChild(i);
if (child == null) continue;
final AccessibleContext context = child.getAccessibleContext();
if (context == null) continue;
if (whichChildren == JAVA_AX_VISIBLE_CHILDREN) {
AccessibleComponent acomp = context.getAccessibleComponent();
if (acomp == null || !acomp.isVisible()) {
continue;
}
} else if (whichChildren == JAVA_AX_SELECTED_CHILDREN) {
AccessibleSelection sel = ac.getAccessibleSelection();
if (sel == null || !sel.isAccessibleChildSelected(i)) {
continue;
}
}
if (!allowIgnored) {
final AccessibleRole role = context.getAccessibleRole();
if (role != null && ignoredRoles != null && ignoredRoles.contains(roleKey(role))) {
// Get the child's unignored children.
_addChildren(child, whichChildren, false, childrenAndRoles);
} else {
childrenAndRoles.add(child);
childrenAndRoles.add(getAccessibleRole(child));
}
} else {
childrenAndRoles.add(child);
childrenAndRoles.add(getAccessibleRole(child));
}
// If there is an index, and we are beyond it, time to finish up
if ((whichChildren >= 0) && (childrenAndRoles.size() / 2) >= (whichChildren + 1)) {
return;
}
}
}
private static native String roleKey(AccessibleRole aRole);
public static Object[] getChildren(final Accessible a, final Component c) {
if (a == null) return null;
return invokeAndWait(new Callable<Object[]>() {
public Object[] call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final int numChildren = ac.getAccessibleChildrenCount();
final Object[] children = new Object[numChildren];
for (int i = 0; i < numChildren; i++) {
children[i] = ac.getAccessibleChild(i);
}
return children;
}
}, c);
}
/**
* @return AWTView ptr, a peer of the CPlatformView associated with the toplevel container of the Accessible, if any
*/
private static long getAWTView(Accessible a) {
Accessible ax = CAccessible.getSwingAccessible(a);
if (!(ax instanceof Component)) return 0;
return invokeAndWait(new Callable<Long>() {
public Long call() throws Exception {
Component cont = (Component) ax;
while (cont != null && !(cont instanceof Window)) {
cont = cont.getParent();
}
if (cont != null) {
LWWindowPeer peer = (LWWindowPeer) AWTAccessor.getComponentAccessor().getPeer(cont);
if (peer != null) {
return ((CPlatformWindow) peer.getPlatformWindow()).getContentView().getAWTView();
}
}
return 0L;
}
}, (Component)ax);
}
private static boolean isTreeRootVisible(Accessible a, Component c) {
if (a == null) return false;
return invokeAndWait(new Callable<Boolean>() {
public Boolean call() throws Exception {
Accessible sa = CAccessible.getSwingAccessible(a);
if (sa instanceof JTree) {
return ((JTree) sa).isRootVisible();
}
return false;
}
}, c);
}
}