1 /* 2 * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved. 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; 29 import java.beans.PropertyChangeEvent; 30 import java.beans.PropertyChangeListener; 31 32 import javax.accessibility.Accessible; 33 import javax.accessibility.AccessibleContext; 34 import javax.swing.JProgressBar; 35 import javax.swing.JSlider; 36 import javax.swing.event.ChangeEvent; 37 import javax.swing.event.ChangeListener; 38 39 import static javax.accessibility.AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY; 40 import static javax.accessibility.AccessibleContext.ACCESSIBLE_CARET_PROPERTY; 41 import static javax.accessibility.AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY; 42 import static javax.accessibility.AccessibleContext.ACCESSIBLE_STATE_PROPERTY; 43 import static javax.accessibility.AccessibleContext.ACCESSIBLE_TEXT_PROPERTY; 44 import javax.accessibility.AccessibleRole; 45 import javax.accessibility.AccessibleState; 46 import sun.awt.AWTAccessor; 47 48 49 class CAccessible extends CFRetainedResource implements Accessible { 50 51 public static CAccessible getCAccessible(final Accessible a) { 52 if (a == null) return null; 53 AccessibleContext context = a.getAccessibleContext(); 54 AWTAccessor.AccessibleContextAccessor accessor 55 = AWTAccessor.getAccessibleContextAccessor(); 56 final CAccessible cachedCAX = (CAccessible) accessor.getNativeAXResource(context); 57 if (cachedCAX != null) { 58 return cachedCAX; 59 } 60 final CAccessible newCAX = new CAccessible(a); 61 accessor.setNativeAXResource(context, newCAX); 62 return newCAX; 63 } 64 65 private static native void unregisterFromCocoaAXSystem(long ptr); 66 private static native void valueChanged(long ptr); 67 private static native void selectedTextChanged(long ptr); 68 private static native void selectionChanged(long ptr); 69 private static native void menuOpened(long ptr); 70 private static native void menuClosed(long ptr); 71 private static native void menuItemSelected(long ptr); 72 73 private Accessible accessible; 74 75 private AccessibleContext activeDescendant; 76 77 private CAccessible(final Accessible accessible) { 78 super(0L, true); // real pointer will be poked in by native 79 80 if (accessible == null) throw new NullPointerException(); 81 this.accessible = accessible; 82 83 if (accessible instanceof Component) { 84 addNotificationListeners((Component)accessible); 85 } 86 } 87 88 @Override 89 protected synchronized void dispose() { 90 if (ptr != 0) unregisterFromCocoaAXSystem(ptr); 91 super.dispose(); 92 } 93 94 @Override 95 public AccessibleContext getAccessibleContext() { 96 return accessible.getAccessibleContext(); 97 } 98 99 public void addNotificationListeners(Component c) { 100 if (c instanceof Accessible) { 101 AccessibleContext ac = ((Accessible)c).getAccessibleContext(); 102 ac.addPropertyChangeListener(new AXChangeNotifier()); 103 } 104 if (c instanceof JProgressBar) { 105 JProgressBar pb = (JProgressBar) c; 106 pb.addChangeListener(new AXProgressChangeNotifier()); 107 } else if (c instanceof JSlider) { 108 JSlider slider = (JSlider) c; 109 slider.addChangeListener(new AXProgressChangeNotifier()); 110 } 111 } 112 113 114 private class AXChangeNotifier implements PropertyChangeListener { 115 116 @Override 117 public void propertyChange(PropertyChangeEvent e) { 118 String name = e.getPropertyName(); 119 if ( ptr != 0 ) { 120 Object newValue = e.getNewValue(); 121 Object oldValue = e.getOldValue(); 122 if (name.compareTo(ACCESSIBLE_CARET_PROPERTY) == 0) { 123 selectedTextChanged(ptr); 124 } else if (name.compareTo(ACCESSIBLE_TEXT_PROPERTY) == 0 ) { 125 valueChanged(ptr); 126 } else if (name.compareTo(ACCESSIBLE_SELECTION_PROPERTY) == 0 ) { 127 selectionChanged(ptr); 128 } else if (name.compareTo(ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY) == 0 ) { 129 if (newValue instanceof AccessibleContext) { 130 activeDescendant = (AccessibleContext)newValue; 131 } 132 } else if (name.compareTo(ACCESSIBLE_STATE_PROPERTY) == 0) { 133 AccessibleContext thisAC = accessible.getAccessibleContext(); 134 AccessibleRole thisRole = thisAC.getAccessibleRole(); 135 Accessible parentAccessible = thisAC.getAccessibleParent(); 136 AccessibleRole parentRole = null; 137 if (parentAccessible != null) { 138 parentRole = parentAccessible.getAccessibleContext().getAccessibleRole(); 139 } 140 // At least for now don't handle combo box menu state changes. 141 // This may change when later fixing issues which currently 142 // exist for combo boxes, but for now the following is only 143 // for JPopupMenus, not for combobox menus. 144 if (parentRole != AccessibleRole.COMBO_BOX) { 145 if (thisRole == AccessibleRole.POPUP_MENU) { 146 if ( newValue != null && 147 ((AccessibleState)newValue) == AccessibleState.VISIBLE ) { 148 menuOpened(ptr); 149 } else if ( oldValue != null && 150 ((AccessibleState)oldValue) == AccessibleState.VISIBLE ) { 151 menuClosed(ptr); 152 } 153 } else if (thisRole == AccessibleRole.MENU_ITEM) { 154 if ( newValue != null && 155 ((AccessibleState)newValue) == AccessibleState.FOCUSED ) { 156 menuItemSelected(ptr); 157 } 158 } 159 } 160 } 161 } 162 } 163 } 164 165 private class AXProgressChangeNotifier implements ChangeListener { 166 @Override 167 public void stateChanged(ChangeEvent e) { 168 if (ptr != 0) valueChanged(ptr); 169 } 170 } 171 172 static Accessible getSwingAccessible(final Accessible a) { 173 return (a instanceof CAccessible) ? ((CAccessible)a).accessible : a; 174 } 175 176 static AccessibleContext getActiveDescendant(final Accessible a) { 177 return (a instanceof CAccessible) ? ((CAccessible)a).activeDescendant : null; 178 } 179 180 }