author | ptbrunet |
Mon, 18 Jul 2016 15:43:30 -0500 | |
changeset 39873 | 76907ae1b680 |
parent 37707 | 6cebe9608430 |
child 40719 | 4ae72a69bd3b |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
37707
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved. |
12047 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.lwawt.macosx; |
|
27 |
||
28 |
import java.awt.Component; |
|
37707
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
29 |
import java.beans.PropertyChangeEvent; |
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
30 |
import java.beans.PropertyChangeListener; |
12047 | 31 |
import java.lang.reflect.Field; |
32 |
||
33 |
import javax.accessibility.Accessible; |
|
34 |
import javax.accessibility.AccessibleContext; |
|
35 |
import javax.swing.JProgressBar; |
|
36 |
import javax.swing.JSlider; |
|
37 |
import javax.swing.event.ChangeEvent; |
|
38 |
import javax.swing.event.ChangeListener; |
|
39 |
||
39873
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
40 |
import static javax.accessibility.AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY; |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
41 |
import static javax.accessibility.AccessibleContext.ACCESSIBLE_CARET_PROPERTY; |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
42 |
import static javax.accessibility.AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY; |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
43 |
import static javax.accessibility.AccessibleContext.ACCESSIBLE_TEXT_PROPERTY; |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
44 |
|
12047 | 45 |
|
46 |
class CAccessible extends CFRetainedResource implements Accessible { |
|
47 |
static Field getNativeAXResourceField() { |
|
48 |
try { |
|
49 |
final Field field = AccessibleContext.class.getDeclaredField("nativeAXResource"); |
|
50 |
field.setAccessible(true); |
|
51 |
return field; |
|
52 |
} catch (final Exception e) { |
|
53 |
e.printStackTrace(); |
|
54 |
return null; |
|
55 |
} |
|
56 |
} |
|
57 |
||
58 |
private static Field nativeAXResourceField = getNativeAXResourceField(); |
|
59 |
||
60 |
public static CAccessible getCAccessible(final Accessible a) { |
|
61 |
if (a == null) return null; |
|
62 |
AccessibleContext context = a.getAccessibleContext(); |
|
63 |
try { |
|
64 |
final CAccessible cachedCAX = (CAccessible) nativeAXResourceField.get(context); |
|
65 |
if (cachedCAX != null) return cachedCAX; |
|
66 |
||
67 |
final CAccessible newCAX = new CAccessible(a); |
|
68 |
nativeAXResourceField.set(context, newCAX); |
|
69 |
return newCAX; |
|
70 |
} catch (final Exception e) { |
|
71 |
e.printStackTrace(); |
|
72 |
return null; |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
private static native void unregisterFromCocoaAXSystem(long ptr); |
|
77 |
private static native void valueChanged(long ptr); |
|
39873
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
78 |
private static native void selectedTextChanged(long ptr); |
12047 | 79 |
private static native void selectionChanged(long ptr); |
80 |
||
81 |
private Accessible accessible; |
|
82 |
||
39873
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
83 |
private AccessibleContext activeDescendant; |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
84 |
|
12047 | 85 |
private CAccessible(final Accessible accessible) { |
86 |
super(0L, true); // real pointer will be poked in by native |
|
87 |
||
88 |
if (accessible == null) throw new NullPointerException(); |
|
89 |
this.accessible = accessible; |
|
90 |
||
91 |
if (accessible instanceof Component) { |
|
92 |
addNotificationListeners((Component)accessible); |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
@Override |
|
97 |
protected synchronized void dispose() { |
|
98 |
if (ptr != 0) unregisterFromCocoaAXSystem(ptr); |
|
99 |
super.dispose(); |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public AccessibleContext getAccessibleContext() { |
|
104 |
return accessible.getAccessibleContext(); |
|
105 |
} |
|
106 |
||
107 |
public void addNotificationListeners(Component c) { |
|
37707
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
108 |
if (c instanceof Accessible) { |
39873
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
109 |
AccessibleContext ac = ((Accessible)c).getAccessibleContext(); |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
110 |
ac.addPropertyChangeListener(new AXChangeNotifier()); |
12047 | 111 |
} |
112 |
if (c instanceof JProgressBar) { |
|
113 |
JProgressBar pb = (JProgressBar) c; |
|
114 |
pb.addChangeListener(new AXProgressChangeNotifier()); |
|
115 |
} else if (c instanceof JSlider) { |
|
116 |
JSlider slider = (JSlider) c; |
|
117 |
slider.addChangeListener(new AXProgressChangeNotifier()); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
||
39873
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
122 |
private class AXChangeNotifier implements PropertyChangeListener { |
12047 | 123 |
|
124 |
@Override |
|
37707
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
125 |
public void propertyChange(PropertyChangeEvent e) { |
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
126 |
String name = e.getPropertyName(); |
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
127 |
if ( ptr != 0 ) { |
39873
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
128 |
if (name.compareTo(ACCESSIBLE_CARET_PROPERTY) == 0) { |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
129 |
selectedTextChanged(ptr); |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
130 |
} else if (name.compareTo(ACCESSIBLE_TEXT_PROPERTY) == 0 ) { |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
131 |
valueChanged(ptr); |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
132 |
} else if (name.compareTo(ACCESSIBLE_SELECTION_PROPERTY) == 0 ) { |
37707
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
133 |
selectionChanged(ptr); |
39873
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
134 |
} else if (name.compareTo(ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY) == 0 ) { |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
135 |
Object nv = e.getNewValue(); |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
136 |
if (nv instanceof AccessibleContext) { |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
137 |
activeDescendant = (AccessibleContext)nv; |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
138 |
} |
37707
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
139 |
} |
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
140 |
} |
12047 | 141 |
} |
142 |
} |
|
143 |
||
144 |
private class AXProgressChangeNotifier implements ChangeListener { |
|
37707
6cebe9608430
8076554: [macosx] Custom Swing text components need to allow standard accessibility
ptbrunet
parents:
25859
diff
changeset
|
145 |
@Override |
12047 | 146 |
public void stateChanged(ChangeEvent e) { |
147 |
if (ptr != 0) valueChanged(ptr); |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
static Accessible getSwingAccessible(final Accessible a) { |
|
152 |
return (a instanceof CAccessible) ? ((CAccessible)a).accessible : a; |
|
153 |
} |
|
39873
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
154 |
|
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
155 |
static AccessibleContext getActiveDescendant(final Accessible a) { |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
156 |
return (a instanceof CAccessible) ? ((CAccessible)a).activeDescendant : null; |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
157 |
} |
76907ae1b680
8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents:
37707
diff
changeset
|
158 |
|
12047 | 159 |
} |