< prev index next >

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformResponder.java

Print this page




  27 
  28 import sun.awt.SunToolkit;
  29 import sun.lwawt.LWWindowPeer;
  30 import sun.lwawt.PlatformEventNotifier;
  31 
  32 import java.awt.Toolkit;
  33 import java.awt.event.MouseEvent;
  34 import java.awt.event.InputEvent;
  35 import java.awt.event.MouseWheelEvent;
  36 import java.awt.event.KeyEvent;
  37 import java.util.Locale;
  38 
  39 /**
  40  * Translates NSEvents/NPCocoaEvents into AWT events.
  41  */
  42 final class CPlatformResponder {
  43 
  44     private final PlatformEventNotifier eventNotifier;
  45     private final boolean isNpapiCallback;
  46     private int lastKeyPressCode = KeyEvent.VK_UNDEFINED;


  47 
  48     CPlatformResponder(final PlatformEventNotifier eventNotifier,
  49                        final boolean isNpapiCallback) {
  50         this.eventNotifier = eventNotifier;
  51         this.isNpapiCallback = isNpapiCallback;
  52     }
  53 
  54     /**
  55      * Handles mouse events.
  56      */
  57     void handleMouseEvent(int eventType, int modifierFlags, int buttonNumber,
  58                           int clickCount, int x, int y, int absX, int absY) {
  59         final SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
  60         if ((buttonNumber > 2 && !tk.areExtraMouseButtonsEnabled())
  61                 || buttonNumber > tk.getNumberOfButtons() - 1) {
  62             return;
  63         }
  64 
  65         int jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
  66                                            NSEvent.nsToJavaEventType(eventType);


  72             jeventType != MouseEvent.MOUSE_ENTERED &&
  73             jeventType != MouseEvent.MOUSE_EXITED)
  74         {
  75             jbuttonNumber = NSEvent.nsToJavaButton(buttonNumber);
  76             jclickCount = clickCount;
  77         }
  78 
  79         int jmodifiers = NSEvent.nsToJavaModifiers(modifierFlags);
  80         boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
  81 
  82         eventNotifier.notifyMouseEvent(jeventType, System.currentTimeMillis(), jbuttonNumber,
  83                 x, y, absX, absY, jmodifiers, jclickCount,
  84                 jpopupTrigger, null);
  85     }
  86 
  87     /**
  88      * Handles scroll events.
  89      */
  90     void handleScrollEvent(final int x, final int y, final int absX,
  91                            final int absY, final int modifierFlags,
  92                            final double deltaX, final double deltaY) {

  93         int jmodifiers = NSEvent.nsToJavaModifiers(modifierFlags);
  94         final boolean isShift = (jmodifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
  95 



  96         // Vertical scroll.
  97         if (!isShift && deltaY != 0.0) {
  98             dispatchScrollEvent(x, y, absX, absY, jmodifiers, deltaY);
  99         }
 100         // Horizontal scroll or shirt+vertical scroll.
 101         final double delta = isShift && deltaY != 0.0 ? deltaY : deltaX;
 102         if (delta != 0.0) {

 103             jmodifiers |= InputEvent.SHIFT_DOWN_MASK;
 104             dispatchScrollEvent(x, y, absX, absY, jmodifiers, delta);
 105         }
 106     }
 107 
 108     private void dispatchScrollEvent(final int x, final int y, final int absX,
 109                                      final int absY, final int modifiers,
 110                                      final double delta) {
 111         final long when = System.currentTimeMillis();
 112         final int scrollType = MouseWheelEvent.WHEEL_UNIT_SCROLL;
 113         final int scrollAmount = 1;
 114         int wheelRotation = (int) delta;
 115         int signum = (int) Math.signum(delta);
 116         if (signum * delta < 1) {
 117             wheelRotation = signum;
 118         }
 119         // invert the wheelRotation for the peer
 120         eventNotifier.notifyMouseWheelEvent(when, x, y, absX, absY, modifiers,
 121                                             scrollType, scrollAmount,
 122                                             -wheelRotation, -delta, null);
 123     }
 124 
 125     /**
 126      * Handles key events.
 127      */
 128     void handleKeyEvent(int eventType, int modifierFlags, String chars, String charsIgnoringModifiers,
 129                         short keyCode, boolean needsKeyTyped, boolean needsKeyReleased) {
 130         boolean isFlagsChangedEvent =
 131             isNpapiCallback ? (eventType == CocoaConstants.NPCocoaEventFlagsChanged) :
 132                               (eventType == CocoaConstants.NSFlagsChanged);
 133 
 134         int jeventType = KeyEvent.KEY_PRESSED;
 135         int jkeyCode = KeyEvent.VK_UNDEFINED;
 136         int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
 137         boolean postsTyped = false;
 138         boolean spaceKeyTyped = false;
 139 
 140         char testChar = KeyEvent.CHAR_UNDEFINED;
 141         boolean isDeadChar = (chars!= null && chars.length() == 0);
 142 


 243             int index = 0, length = text.length();
 244             char c = 0;
 245             while (index < length) {
 246                 c = text.charAt(index);
 247                 eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED,
 248                         System.currentTimeMillis(),
 249                         0, KeyEvent.VK_UNDEFINED, c,
 250                         KeyEvent.KEY_LOCATION_UNKNOWN);
 251                 index++;
 252             }
 253             eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED,
 254                     System.currentTimeMillis(),
 255                     0, lastKeyPressCode, c,
 256                     KeyEvent.KEY_LOCATION_UNKNOWN);
 257         }
 258     }
 259 
 260     void handleWindowFocusEvent(boolean gained, LWWindowPeer opposite) {
 261         eventNotifier.notifyActivation(gained, opposite);
 262     }











































 263 }


  27 
  28 import sun.awt.SunToolkit;
  29 import sun.lwawt.LWWindowPeer;
  30 import sun.lwawt.PlatformEventNotifier;
  31 
  32 import java.awt.Toolkit;
  33 import java.awt.event.MouseEvent;
  34 import java.awt.event.InputEvent;
  35 import java.awt.event.MouseWheelEvent;
  36 import java.awt.event.KeyEvent;
  37 import java.util.Locale;
  38 
  39 /**
  40  * Translates NSEvents/NPCocoaEvents into AWT events.
  41  */
  42 final class CPlatformResponder {
  43 
  44     private final PlatformEventNotifier eventNotifier;
  45     private final boolean isNpapiCallback;
  46     private int lastKeyPressCode = KeyEvent.VK_UNDEFINED;
  47     private final DeltaAccumulator deltaAccumulatorX = new DeltaAccumulator();
  48     private final DeltaAccumulator deltaAccumulatorY = new DeltaAccumulator();
  49 
  50     CPlatformResponder(final PlatformEventNotifier eventNotifier,
  51                        final boolean isNpapiCallback) {
  52         this.eventNotifier = eventNotifier;
  53         this.isNpapiCallback = isNpapiCallback;
  54     }
  55 
  56     /**
  57      * Handles mouse events.
  58      */
  59     void handleMouseEvent(int eventType, int modifierFlags, int buttonNumber,
  60                           int clickCount, int x, int y, int absX, int absY) {
  61         final SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
  62         if ((buttonNumber > 2 && !tk.areExtraMouseButtonsEnabled())
  63                 || buttonNumber > tk.getNumberOfButtons() - 1) {
  64             return;
  65         }
  66 
  67         int jeventType = isNpapiCallback ? NSEvent.npToJavaEventType(eventType) :
  68                                            NSEvent.nsToJavaEventType(eventType);


  74             jeventType != MouseEvent.MOUSE_ENTERED &&
  75             jeventType != MouseEvent.MOUSE_EXITED)
  76         {
  77             jbuttonNumber = NSEvent.nsToJavaButton(buttonNumber);
  78             jclickCount = clickCount;
  79         }
  80 
  81         int jmodifiers = NSEvent.nsToJavaModifiers(modifierFlags);
  82         boolean jpopupTrigger = NSEvent.isPopupTrigger(jmodifiers);
  83 
  84         eventNotifier.notifyMouseEvent(jeventType, System.currentTimeMillis(), jbuttonNumber,
  85                 x, y, absX, absY, jmodifiers, jclickCount,
  86                 jpopupTrigger, null);
  87     }
  88 
  89     /**
  90      * Handles scroll events.
  91      */
  92     void handleScrollEvent(final int x, final int y, final int absX,
  93                            final int absY, final int modifierFlags,
  94                            final double deltaX, final double deltaY,
  95                            final int scrollPhase) {
  96         int jmodifiers = NSEvent.nsToJavaModifiers(modifierFlags);
  97         final boolean isShift = (jmodifiers & InputEvent.SHIFT_DOWN_MASK) != 0;
  98 
  99         int roundDeltaX = deltaAccumulatorX.getRoundedDelta(deltaX, scrollPhase);
 100         int roundDeltaY = deltaAccumulatorY.getRoundedDelta(deltaY, scrollPhase);
 101 
 102         // Vertical scroll.
 103         if (!isShift && (deltaY != 0.0 || roundDeltaY != 0)) {
 104             dispatchScrollEvent(x, y, absX, absY, jmodifiers, roundDeltaY, deltaY);
 105         }
 106         // Horizontal scroll or shirt+vertical scroll.
 107         final double delta = isShift && deltaY != 0.0 ? deltaY : deltaX;
 108         final int roundDelta = isShift && roundDeltaY != 0.0 ? roundDeltaY : roundDeltaX;
 109         if (delta != 0.0 || roundDelta != 0) {
 110             jmodifiers |= InputEvent.SHIFT_DOWN_MASK;
 111             dispatchScrollEvent(x, y, absX, absY, jmodifiers, roundDelta, delta);
 112         }
 113     }
 114 
 115     private void dispatchScrollEvent(final int x, final int y, final int absX,
 116                                      final int absY, final int modifiers,
 117                                      final int roundDelta, final double delta) {
 118         final long when = System.currentTimeMillis();
 119         final int scrollType = MouseWheelEvent.WHEEL_UNIT_SCROLL;
 120         final int scrollAmount = 1;





 121         // invert the wheelRotation for the peer
 122         eventNotifier.notifyMouseWheelEvent(when, x, y, absX, absY, modifiers,
 123                                             scrollType, scrollAmount,
 124                                             -roundDelta, -delta, null);
 125     }
 126 
 127     /**
 128      * Handles key events.
 129      */
 130     void handleKeyEvent(int eventType, int modifierFlags, String chars, String charsIgnoringModifiers,
 131                         short keyCode, boolean needsKeyTyped, boolean needsKeyReleased) {
 132         boolean isFlagsChangedEvent =
 133             isNpapiCallback ? (eventType == CocoaConstants.NPCocoaEventFlagsChanged) :
 134                               (eventType == CocoaConstants.NSFlagsChanged);
 135 
 136         int jeventType = KeyEvent.KEY_PRESSED;
 137         int jkeyCode = KeyEvent.VK_UNDEFINED;
 138         int jkeyLocation = KeyEvent.KEY_LOCATION_UNKNOWN;
 139         boolean postsTyped = false;
 140         boolean spaceKeyTyped = false;
 141 
 142         char testChar = KeyEvent.CHAR_UNDEFINED;
 143         boolean isDeadChar = (chars!= null && chars.length() == 0);
 144 


 245             int index = 0, length = text.length();
 246             char c = 0;
 247             while (index < length) {
 248                 c = text.charAt(index);
 249                 eventNotifier.notifyKeyEvent(KeyEvent.KEY_TYPED,
 250                         System.currentTimeMillis(),
 251                         0, KeyEvent.VK_UNDEFINED, c,
 252                         KeyEvent.KEY_LOCATION_UNKNOWN);
 253                 index++;
 254             }
 255             eventNotifier.notifyKeyEvent(KeyEvent.KEY_RELEASED,
 256                     System.currentTimeMillis(),
 257                     0, lastKeyPressCode, c,
 258                     KeyEvent.KEY_LOCATION_UNKNOWN);
 259         }
 260     }
 261 
 262     void handleWindowFocusEvent(boolean gained, LWWindowPeer opposite) {
 263         eventNotifier.notifyActivation(gained, opposite);
 264     }
 265 
 266     static class DeltaAccumulator {
 267 
 268         static final double MIN_THRESHOLD = 0.1;
 269         static final double MAX_THRESHOLD = 0.8;
 270         double accumulatedDelta;
 271 
 272         int getRoundedDelta(double delta, int scrollPhase) {
 273 
 274             int roundDelta = (int) Math.round(delta);
 275 
 276             if (scrollPhase == NSEvent.SCROLL_PHASE_UNSUPPORTED) { // mouse wheel
 277                 if (roundDelta == 0 && delta != 0) {
 278                     roundDelta = delta > 0 ? 1 : -1;
 279                 }
 280             } else { // trackpad
 281                 boolean begin = scrollPhase == NSEvent.SCROLL_PHASE_BEGAN;
 282                 boolean end = scrollPhase == NSEvent.SCROLL_MASK_PHASE_ENDED
 283                         || scrollPhase == NSEvent.SCROLL_MASK_PHASE_CANCELLED;
 284 
 285                 if (begin) {
 286                     accumulatedDelta = 0;
 287                 }
 288 
 289                 accumulatedDelta += delta;
 290 
 291                 double absAccumulatedDelta = Math.abs(accumulatedDelta);
 292                 if (absAccumulatedDelta > MAX_THRESHOLD) {
 293                     roundDelta = (int) Math.round(accumulatedDelta);
 294                     accumulatedDelta -= roundDelta;
 295                 }
 296 
 297                 if (end) {
 298                     if (roundDelta == 0 && absAccumulatedDelta > MIN_THRESHOLD) {
 299                         roundDelta = accumulatedDelta > 0 ? 1 : -1;
 300                     }
 301                     accumulatedDelta = 0;
 302                 }
 303             }
 304 
 305             return roundDelta;
 306         }
 307     }
 308 }
< prev index next >