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_TEXT_PROPERTY;
  43 import sun.awt.AWTAccessor;
  44 
  45 
  46 class CAccessible extends CFRetainedResource implements Accessible {
  47 
  48     public static CAccessible getCAccessible(final Accessible a) {
  49         if (a == null) return null;
  50         AccessibleContext context = a.getAccessibleContext();
  51         AWTAccessor.AccessibleContextAccessor accessor
  52                 = AWTAccessor.getAccessibleContextAccessor();
  53         final CAccessible cachedCAX = (CAccessible) accessor.getNativeAXResource(context);
  54         if (cachedCAX != null) {
  55             return cachedCAX;
  56         }
  57         final CAccessible newCAX = new CAccessible(a);
  58         accessor.setNativeAXResource(context, newCAX);
  59         return newCAX;
  60     }
  61 
  62     private static native void unregisterFromCocoaAXSystem(long ptr);
  63     private static native void valueChanged(long ptr);
  64     private static native void selectedTextChanged(long ptr);
  65     private static native void selectionChanged(long ptr);
  66 
  67     private Accessible accessible;
  68 
  69     private AccessibleContext activeDescendant;
  70 
  71     private CAccessible(final Accessible accessible) {
  72         super(0L, true); // real pointer will be poked in by native
  73 
  74         if (accessible == null) throw new NullPointerException();
  75         this.accessible = accessible;
  76 
  77         if (accessible instanceof Component) {
  78             addNotificationListeners((Component)accessible);
  79         }
  80     }
  81 
  82     @Override
  83     protected synchronized void dispose() {
  84         if (ptr != 0) unregisterFromCocoaAXSystem(ptr);
  85         super.dispose();
  86     }
  87 
  88     @Override
  89     public AccessibleContext getAccessibleContext() {
  90         return accessible.getAccessibleContext();
  91     }
  92 
  93     public void addNotificationListeners(Component c) {
  94         if (c instanceof Accessible) {
  95             AccessibleContext ac = ((Accessible)c).getAccessibleContext();
  96             ac.addPropertyChangeListener(new AXChangeNotifier());
  97         }
  98         if (c instanceof JProgressBar) {
  99             JProgressBar pb = (JProgressBar) c;
 100             pb.addChangeListener(new AXProgressChangeNotifier());
 101         } else if (c instanceof JSlider) {
 102             JSlider slider = (JSlider) c;
 103             slider.addChangeListener(new AXProgressChangeNotifier());
 104         }
 105     }
 106 
 107 
 108     private class AXChangeNotifier implements PropertyChangeListener {
 109 
 110         @Override
 111         public void propertyChange(PropertyChangeEvent e) {
 112             String name = e.getPropertyName();
 113             if ( ptr != 0 ) {
 114                 if (name.compareTo(ACCESSIBLE_CARET_PROPERTY) == 0) {
 115                     selectedTextChanged(ptr);
 116                 } else if (name.compareTo(ACCESSIBLE_TEXT_PROPERTY) == 0 ) {
 117                     valueChanged(ptr);
 118                 } else if (name.compareTo(ACCESSIBLE_SELECTION_PROPERTY) == 0 ) {
 119                     selectionChanged(ptr);
 120                 }  else if (name.compareTo(ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY) == 0 ) {
 121                     Object nv = e.getNewValue();
 122                     if (nv instanceof AccessibleContext) {
 123                         activeDescendant = (AccessibleContext)nv;
 124                     }
 125                 }
 126             }
 127         }
 128     }
 129 
 130     private class AXProgressChangeNotifier implements ChangeListener {
 131         @Override
 132         public void stateChanged(ChangeEvent e) {
 133             if (ptr != 0) valueChanged(ptr);
 134         }
 135     }
 136 
 137     static Accessible getSwingAccessible(final Accessible a) {
 138         return (a instanceof CAccessible) ? ((CAccessible)a).accessible : a;
 139     }
 140 
 141     static AccessibleContext getActiveDescendant(final Accessible a) {
 142         return (a instanceof CAccessible) ? ((CAccessible)a).activeDescendant : null;
 143     }
 144 
 145 }