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 javafx.event.EventType; 34 import javafx.scene.input.MouseButton; 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 EventType<javafx.scene.input.MouseEvent> mouseIDToEmbedMouseType(int id) { 44 switch (id) { 45 case MouseEvent.MOUSE_PRESSED: 46 return javafx.scene.input.MouseEvent.MOUSE_PRESSED; 47 case MouseEvent.MOUSE_RELEASED: 48 return javafx.scene.input.MouseEvent.MOUSE_RELEASED; 49 case MouseEvent.MOUSE_CLICKED: 50 return javafx.scene.input.MouseEvent.MOUSE_CLICKED; 51 case MouseEvent.MOUSE_MOVED: 52 return javafx.scene.input.MouseEvent.MOUSE_MOVED; 53 case MouseEvent.MOUSE_DRAGGED: 54 return javafx.scene.input.MouseEvent.MOUSE_DRAGGED; 55 case MouseEvent.MOUSE_ENTERED: 56 return javafx.scene.input.MouseEvent.MOUSE_ENTERED; 57 case MouseEvent.MOUSE_EXITED: 58 return javafx.scene.input.MouseEvent.MOUSE_EXITED; 59 } 60 return null; 61 } 62 63 static MouseButton mouseButtonToEmbedMouseButton(int button, int extModifiers) { 64 MouseButton embeddedButton = MouseButton.NONE; 65 switch (button) { 66 case MouseEvent.BUTTON1: 67 embeddedButton = MouseButton.PRIMARY; 68 break; 69 case MouseEvent.BUTTON2: 70 embeddedButton = MouseButton.MIDDLE; 71 break; 72 case MouseEvent.BUTTON3: 73 embeddedButton = MouseButton.SECONDARY; 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 embeddedButton = MouseButton.PRIMARY; 81 } else if ((extModifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0) { 82 embeddedButton = MouseButton.MIDDLE; 83 } else if ((extModifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) { 84 embeddedButton = MouseButton.SECONDARY; 85 } 86 return embeddedButton; 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 EventType<javafx.scene.input.KeyEvent> keyIDToEmbedKeyType(int id) { 97 switch (id) { 98 case KeyEvent.KEY_PRESSED: 99 return javafx.scene.input.KeyEvent.KEY_PRESSED; 100 case KeyEvent.KEY_RELEASED: 101 return javafx.scene.input.KeyEvent.KEY_RELEASED; 102 case KeyEvent.KEY_TYPED: 103 return javafx.scene.input.KeyEvent.KEY_TYPED; 104 } 105 return null; 106 } 107 108 static char keyCharToEmbedKeyChar(char ch) { 109 // Convert Swing LF character to Fx CR character. 110 return ch == '\n' ? '\r' : ch; 111 } 112 113 // FX -> Swing conversion methods 114 115 static int fxMouseEventTypeToMouseID(javafx.scene.input.MouseEvent event) { 116 EventType<?> type = event.getEventType(); 117 if (type == javafx.scene.input.MouseEvent.MOUSE_MOVED) { 118 return MouseEvent.MOUSE_MOVED; 119 } 120 if (type == javafx.scene.input.MouseEvent.MOUSE_PRESSED) { 121 return MouseEvent.MOUSE_PRESSED; 122 } 123 if (type == javafx.scene.input.MouseEvent.MOUSE_RELEASED) { 124 return MouseEvent.MOUSE_RELEASED; 125 } 126 if (type == javafx.scene.input.MouseEvent.MOUSE_CLICKED) { 127 return MouseEvent.MOUSE_CLICKED; 128 } 129 if (type == javafx.scene.input.MouseEvent.MOUSE_ENTERED) { 130 return MouseEvent.MOUSE_ENTERED; 131 } 132 if (type == javafx.scene.input.MouseEvent.MOUSE_EXITED) { 133 return MouseEvent.MOUSE_EXITED; 134 } 135 if (type == javafx.scene.input.MouseEvent.MOUSE_DRAGGED) { 136 return MouseEvent.MOUSE_DRAGGED; 137 } 138 if (type == javafx.scene.input.MouseEvent.DRAG_DETECTED) { 139 return -1; 140 } 141 throw new RuntimeException("Unknown MouseEvent type: " + type); 142 } 143 144 static int fxMouseModsToMouseMods(javafx.scene.input.MouseEvent event) { 145 int mods = 0; 146 if (event.isAltDown()) { 147 mods |= InputEvent.ALT_DOWN_MASK; 148 } 149 if (event.isControlDown()) { 150 mods |= InputEvent.CTRL_DOWN_MASK; 151 } 152 if (event.isMetaDown()) { 153 mods |= InputEvent.META_DOWN_MASK; 154 } 155 if (event.isShiftDown()) { 156 mods |= InputEvent.SHIFT_DOWN_MASK; 157 } 158 if (event.isPrimaryButtonDown()) { 159 mods |= MouseEvent.BUTTON1_DOWN_MASK; 160 } 161 if (event.isSecondaryButtonDown()) { 162 mods |= MouseEvent.BUTTON3_DOWN_MASK; 163 } 164 if (event.isMiddleButtonDown()) { 165 mods |= MouseEvent.BUTTON2_DOWN_MASK; 166 } 167 return mods; 168 } 169 170 static int fxMouseButtonToMouseButton(javafx.scene.input.MouseEvent event) { 171 switch (event.getButton()) { 172 case PRIMARY: 173 return MouseEvent.BUTTON1; 174 case SECONDARY: 175 return MouseEvent.BUTTON3; 176 case MIDDLE: 177 return MouseEvent.BUTTON2; 178 } 179 return 0; 180 } 181 182 static int fxKeyEventTypeToKeyID(javafx.scene.input.KeyEvent event) { 183 EventType<?> eventType = event.getEventType(); 184 if (eventType == javafx.scene.input.KeyEvent.KEY_PRESSED) { 185 return KeyEvent.KEY_PRESSED; 186 } 187 if (eventType == javafx.scene.input.KeyEvent.KEY_RELEASED) { 188 return KeyEvent.KEY_RELEASED; 189 } 190 if (eventType == javafx.scene.input.KeyEvent.KEY_TYPED) { 191 return KeyEvent.KEY_TYPED; 192 } 193 throw new RuntimeException("Unknown KeyEvent type: " + eventType); 194 } 195 196 static int fxKeyModsToKeyMods(javafx.scene.input.KeyEvent event) { 197 int mods = 0; 198 if (event.isAltDown()) { 199 mods |= InputEvent.ALT_DOWN_MASK; 200 } 201 if (event.isControlDown()) { 202 mods |= InputEvent.CTRL_DOWN_MASK; 203 } 204 if (event.isMetaDown()) { 205 mods |= InputEvent.META_DOWN_MASK; 206 } 207 if (event.isShiftDown()) { 208 mods |= InputEvent.SHIFT_DOWN_MASK; 209 } 210 return mods; 211 } 212 213 static int fxScrollModsToMouseWheelMods(ScrollEvent 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 }