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 javafx.embed.swing;
  27 
  28 import java.awt.event.InputEvent;
  29 import java.awt.event.KeyEvent;
  30 import java.awt.event.MouseEvent;
  31 import java.awt.event.MouseWheelEvent;
  32 
  33 import com.sun.javafx.embed.AbstractEvents;
  34 import javafx.event.EventType;
  35 import javafx.scene.input.ScrollEvent;
  36 
  37 /**
  38  * A utility class to translate event types and data between embedded
  39  * application and Swing.
  40  */
  41 class SwingEvents {
  42 
  43     static int mouseIDToEmbedMouseType(int id) {
  44         switch (id) {
  45             case MouseEvent.MOUSE_PRESSED:
  46                 return AbstractEvents.MOUSEEVENT_PRESSED;
  47             case MouseEvent.MOUSE_RELEASED:
  48                 return AbstractEvents.MOUSEEVENT_RELEASED;
  49             case MouseEvent.MOUSE_CLICKED:
  50                 return AbstractEvents.MOUSEEVENT_CLICKED;
  51             case MouseEvent.MOUSE_MOVED:
  52                 return AbstractEvents.MOUSEEVENT_MOVED;
  53             case MouseEvent.MOUSE_DRAGGED:
  54                 return AbstractEvents.MOUSEEVENT_DRAGGED;
  55             case MouseEvent.MOUSE_ENTERED:
  56                 return AbstractEvents.MOUSEEVENT_ENTERED;
  57             case MouseEvent.MOUSE_EXITED:
  58                 return AbstractEvents.MOUSEEVENT_EXITED;
  59         }
  60         return 0;
  61     }
  62 
  63     static int mouseButtonToEmbedMouseButton(int button, int extModifiers) {
  64         int abstractButton = AbstractEvents.MOUSEEVENT_NONE_BUTTON;
  65         switch (button) {
  66             case MouseEvent.BUTTON1:
  67                 abstractButton = AbstractEvents.MOUSEEVENT_PRIMARY_BUTTON;
  68                 break;
  69             case MouseEvent.BUTTON2:
  70                 abstractButton = AbstractEvents.MOUSEEVENT_MIDDLE_BUTTON;
  71                 break;
  72             case MouseEvent.BUTTON3:
  73                 abstractButton = AbstractEvents.MOUSEEVENT_SECONDARY_BUTTON;
  74                 break;
  75             default:
  76                 break;
  77         }
  78         // Fix for RT-15457: we should report mouse buttons for mouse drags
  79         if ((extModifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
  80             abstractButton = AbstractEvents.MOUSEEVENT_PRIMARY_BUTTON;
  81         } else if ((extModifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0) {
  82             abstractButton = AbstractEvents.MOUSEEVENT_MIDDLE_BUTTON;
  83         } else if ((extModifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) {
  84             abstractButton = AbstractEvents.MOUSEEVENT_SECONDARY_BUTTON;
  85         }
  86         return abstractButton;
  87     }
  88 
  89     static int getWheelRotation(MouseEvent e) {
  90         if (e instanceof MouseWheelEvent) {
  91             return ((MouseWheelEvent)e).getWheelRotation();
  92         }
  93         return 0;
  94     }
  95 
  96     static int keyIDToEmbedKeyType(int id) {
  97         switch (id) {
  98             case KeyEvent.KEY_PRESSED:
  99                 return AbstractEvents.KEYEVENT_PRESSED;
 100             case KeyEvent.KEY_RELEASED:
 101                 return AbstractEvents.KEYEVENT_RELEASED;
 102             case KeyEvent.KEY_TYPED:
 103                 return AbstractEvents.KEYEVENT_TYPED;
 104         }
 105         return 0;
 106     }
 107 
 108     static int keyModifiersToEmbedKeyModifiers(int extModifiers) {
 109         int embedModifiers = 0;
 110         if ((extModifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
 111             embedModifiers |= AbstractEvents.MODIFIER_SHIFT;
 112         }
 113         if ((extModifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
 114             embedModifiers |= AbstractEvents.MODIFIER_CONTROL;
 115         }
 116         if ((extModifiers & InputEvent.ALT_DOWN_MASK) != 0) {
 117             embedModifiers |= AbstractEvents.MODIFIER_ALT;
 118         }
 119         if ((extModifiers & InputEvent.META_DOWN_MASK) != 0) {
 120             embedModifiers |= AbstractEvents.MODIFIER_META;
 121         }
 122         return embedModifiers;
 123     }
 124 
 125     static char keyCharToEmbedKeyChar(char ch) {
 126         // Convert Swing LF character to Fx CR character.
 127         return ch == '\n' ? '\r' : ch;
 128     }
 129 
 130     // FX -> Swing conversion methods
 131 
 132     static int fxMouseEventTypeToMouseID(javafx.scene.input.MouseEvent event) {
 133         EventType<?> type = event.getEventType();
 134         if (type == javafx.scene.input.MouseEvent.MOUSE_MOVED) {
 135             return MouseEvent.MOUSE_MOVED;
 136         }
 137         if (type == javafx.scene.input.MouseEvent.MOUSE_PRESSED) {
 138             return MouseEvent.MOUSE_PRESSED;
 139         }
 140         if (type == javafx.scene.input.MouseEvent.MOUSE_RELEASED) {
 141             return MouseEvent.MOUSE_RELEASED;
 142         }
 143         if (type == javafx.scene.input.MouseEvent.MOUSE_CLICKED) {
 144             return MouseEvent.MOUSE_CLICKED;
 145         }
 146         if (type == javafx.scene.input.MouseEvent.MOUSE_ENTERED) {
 147             return MouseEvent.MOUSE_ENTERED;
 148         }
 149         if (type == javafx.scene.input.MouseEvent.MOUSE_EXITED) {
 150             return MouseEvent.MOUSE_EXITED;
 151         }
 152         if (type == javafx.scene.input.MouseEvent.MOUSE_DRAGGED) {
 153             return MouseEvent.MOUSE_DRAGGED;
 154         }
 155         if (type == javafx.scene.input.MouseEvent.DRAG_DETECTED) {
 156             return -1;
 157         }
 158         throw new RuntimeException("Unknown MouseEvent type: " + type);
 159     }
 160 
 161     static int fxMouseModsToMouseMods(javafx.scene.input.MouseEvent event) {
 162         int mods = 0;
 163         if (event.isAltDown()) {
 164             mods |= InputEvent.ALT_DOWN_MASK;
 165         }
 166         if (event.isControlDown()) {
 167             mods |= InputEvent.CTRL_DOWN_MASK;
 168         }
 169         if (event.isMetaDown()) {
 170             mods |= InputEvent.META_DOWN_MASK;
 171         }
 172         if (event.isShiftDown()) {
 173             mods |= InputEvent.SHIFT_DOWN_MASK;
 174         }
 175         if (event.isPrimaryButtonDown()) {
 176             mods |= MouseEvent.BUTTON1_DOWN_MASK;
 177         }
 178         if (event.isSecondaryButtonDown()) {
 179             mods |= MouseEvent.BUTTON3_DOWN_MASK;
 180         }
 181         if (event.isMiddleButtonDown()) {
 182             mods |= MouseEvent.BUTTON2_DOWN_MASK;
 183         }
 184         return mods;
 185     }
 186 
 187     static int fxMouseButtonToMouseButton(javafx.scene.input.MouseEvent event) {
 188         switch (event.getButton()) {
 189             case PRIMARY:
 190                 return MouseEvent.BUTTON1;
 191             case SECONDARY:
 192                 return MouseEvent.BUTTON3;
 193             case MIDDLE:
 194                 return MouseEvent.BUTTON2;
 195         }
 196         return 0;
 197     }
 198 
 199     static int fxKeyEventTypeToKeyID(javafx.scene.input.KeyEvent event) {
 200         EventType<?> eventType = event.getEventType();
 201         if (eventType == javafx.scene.input.KeyEvent.KEY_PRESSED) {
 202             return KeyEvent.KEY_PRESSED;
 203         }
 204         if (eventType == javafx.scene.input.KeyEvent.KEY_RELEASED) {
 205             return KeyEvent.KEY_RELEASED;
 206         }
 207         if (eventType == javafx.scene.input.KeyEvent.KEY_TYPED) {
 208             return KeyEvent.KEY_TYPED;
 209         }
 210         throw new RuntimeException("Unknown KeyEvent type: " + eventType);
 211     }
 212 
 213     static int fxKeyModsToKeyMods(javafx.scene.input.KeyEvent event) {
 214         int mods = 0;
 215         if (event.isAltDown()) {
 216             mods |= InputEvent.ALT_DOWN_MASK;
 217         }
 218         if (event.isControlDown()) {
 219             mods |= InputEvent.CTRL_DOWN_MASK;
 220         }
 221         if (event.isMetaDown()) {
 222             mods |= InputEvent.META_DOWN_MASK;
 223         }
 224         if (event.isShiftDown()) {
 225             mods |= InputEvent.SHIFT_DOWN_MASK;
 226         }
 227         return mods;
 228     }
 229 
 230     static int fxScrollModsToMouseWheelMods(ScrollEvent event) {
 231         int mods = 0;
 232         if (event.isAltDown()) {
 233             mods |= InputEvent.ALT_DOWN_MASK;
 234         }
 235         if (event.isControlDown()) {
 236             mods |= InputEvent.CTRL_DOWN_MASK;
 237         }
 238         if (event.isMetaDown()) {
 239             mods |= InputEvent.META_DOWN_MASK;
 240         }
 241         if (event.isShiftDown()) {
 242             mods |= InputEvent.SHIFT_DOWN_MASK;
 243         }
 244         return mods;
 245     }
 246 }