src/macosx/classes/com/apple/eawt/event/GestureHandler.java

Print this page




   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 com.apple.eawt.event;
  27 


  28 import java.awt.*;
  29 import java.util.*;
  30 import java.util.List;
  31 
  32 import javax.swing.*;
  33 
  34 import java.lang.annotation.Native;
  35 
  36 final class GestureHandler {
  37     private static final String CLIENT_PROPERTY = "com.apple.eawt.event.internalGestureHandler";
  38 
  39     // native constants for the supported types of high-level gestures
  40     @Native static final int PHASE = 1;
  41     @Native static final int ROTATE = 2;
  42     @Native static final int MAGNIFY = 3;
  43     @Native static final int SWIPE = 4;
  44 
  45     // installs a private instance of GestureHandler, if necessary
  46     static void addGestureListenerTo(final JComponent component, final GestureListener listener) {
  47         final Object value = component.getClientProperty(CLIENT_PROPERTY);


  53         if (value != null) return; // some other garbage is in our client property
  54 
  55         final GestureHandler newHandler = new GestureHandler();
  56         newHandler.addListener(listener);
  57         component.putClientProperty(CLIENT_PROPERTY, newHandler);
  58     }
  59 
  60     // asks the installed GestureHandler to remove it's listener (does not uninstall the GestureHandler)
  61     static void removeGestureListenerFrom(final JComponent component, final GestureListener listener) {
  62         final Object value = component.getClientProperty(CLIENT_PROPERTY);
  63         if (!(value instanceof GestureHandler)) return;
  64         ((GestureHandler)value).removeListener(listener);
  65     }
  66 
  67 
  68     // called from native - finds the deepest component with an installed GestureHandler,
  69     // creates a single event, and dispatches it to a recursive notifier
  70     static void handleGestureFromNative(final Window window, final int type, final double x, final double y, final double a, final double b) {
  71         if (window == null) return; // should never happen...
  72 
  73         EventQueue.invokeLater(new Runnable() {
  74             public void run() {
  75                 final Component component = SwingUtilities.getDeepestComponentAt(window, (int)x, (int)y);
  76 
  77                 final PerComponentNotifier firstNotifier;
  78                 if (component instanceof RootPaneContainer) {
  79                     firstNotifier = getNextNotifierForComponent(((RootPaneContainer)component).getRootPane());
  80                 } else {
  81                     firstNotifier = getNextNotifierForComponent(component);
  82                 }
  83                 if (firstNotifier == null) return;
  84 
  85                 switch (type) {
  86                     case PHASE:
  87                         firstNotifier.recursivelyHandlePhaseChange(a, new GesturePhaseEvent());
  88                         return;
  89                     case ROTATE:
  90                         firstNotifier.recursivelyHandleRotate(new RotationEvent(a));
  91                         return;
  92                     case MAGNIFY:
  93                         firstNotifier.recursivelyHandleMagnify(new MagnificationEvent(a));




   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 com.apple.eawt.event;
  27 
  28 import sun.awt.SunToolkit;
  29 
  30 import java.awt.*;
  31 import java.util.*;
  32 import java.util.List;
  33 
  34 import javax.swing.*;
  35 
  36 import java.lang.annotation.Native;
  37 
  38 final class GestureHandler {
  39     private static final String CLIENT_PROPERTY = "com.apple.eawt.event.internalGestureHandler";
  40 
  41     // native constants for the supported types of high-level gestures
  42     @Native static final int PHASE = 1;
  43     @Native static final int ROTATE = 2;
  44     @Native static final int MAGNIFY = 3;
  45     @Native static final int SWIPE = 4;
  46 
  47     // installs a private instance of GestureHandler, if necessary
  48     static void addGestureListenerTo(final JComponent component, final GestureListener listener) {
  49         final Object value = component.getClientProperty(CLIENT_PROPERTY);


  55         if (value != null) return; // some other garbage is in our client property
  56 
  57         final GestureHandler newHandler = new GestureHandler();
  58         newHandler.addListener(listener);
  59         component.putClientProperty(CLIENT_PROPERTY, newHandler);
  60     }
  61 
  62     // asks the installed GestureHandler to remove it's listener (does not uninstall the GestureHandler)
  63     static void removeGestureListenerFrom(final JComponent component, final GestureListener listener) {
  64         final Object value = component.getClientProperty(CLIENT_PROPERTY);
  65         if (!(value instanceof GestureHandler)) return;
  66         ((GestureHandler)value).removeListener(listener);
  67     }
  68 
  69 
  70     // called from native - finds the deepest component with an installed GestureHandler,
  71     // creates a single event, and dispatches it to a recursive notifier
  72     static void handleGestureFromNative(final Window window, final int type, final double x, final double y, final double a, final double b) {
  73         if (window == null) return; // should never happen...
  74 
  75         SunToolkit.executeOnEventHandlerThread(window, new Runnable() {
  76             public void run() {
  77                 final Component component = SwingUtilities.getDeepestComponentAt(window, (int)x, (int)y);
  78 
  79                 final PerComponentNotifier firstNotifier;
  80                 if (component instanceof RootPaneContainer) {
  81                     firstNotifier = getNextNotifierForComponent(((RootPaneContainer)component).getRootPane());
  82                 } else {
  83                     firstNotifier = getNextNotifierForComponent(component);
  84                 }
  85                 if (firstNotifier == null) return;
  86 
  87                 switch (type) {
  88                     case PHASE:
  89                         firstNotifier.recursivelyHandlePhaseChange(a, new GesturePhaseEvent());
  90                         return;
  91                     case ROTATE:
  92                         firstNotifier.recursivelyHandleRotate(new RotationEvent(a));
  93                         return;
  94                     case MAGNIFY:
  95                         firstNotifier.recursivelyHandleMagnify(new MagnificationEvent(a));