From c0f918667cb58c5fdd6bc344db7cd50dd5cf5d43 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Thu, 11 Jun 2020 15:56:31 +0100 Subject: [PATCH] JAL-3446 separated out potential exceptions for simplified key mask operation --- src/jalview/util/Platform.java | 65 +++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index 6dbe549..cfeeff0 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -69,7 +69,6 @@ public class Platform private static Boolean isHeadless = null; private static swingjs.api.JSUtilI jsutil; - static { @@ -103,28 +102,53 @@ public class Platform public static int ALT_DOWN_MASK = KeyEvent.ALT_DOWN_MASK; - public static int SHORTCUT_KEY_MASK = (Platform.isMac() ? KeyEvent.META_DOWN_MASK : KeyEvent.CTRL_DOWN_MASK); - + public static int SHORTCUT_KEY_MASK = (Platform.isMac() + ? KeyEvent.META_DOWN_MASK + : KeyEvent.CTRL_DOWN_MASK); + static { if (!GraphicsEnvironment.isHeadless()) { + // Using non-deprecated Extended key mask modifiers, but Java 8 has no + // getMenuShortcutKeyMaskEx method + Toolkit tk = Toolkit.getDefaultToolkit(); + Method method = null; try { - - Toolkit tk = Toolkit.getDefaultToolkit(); - Method method = tk.getClass().getMethod("getMenuShortcutKeyMaskEx"); - if (method == null) + method = tk.getClass().getMethod("getMenuShortcutKeyMaskEx"); + } catch (NoSuchMethodException e) + { + System.err.println( + "Could not find Toolkit method getMenuShortcutKeyMaskEx. Trying getMenuShortcutKeyMask."); + } + if (method == null) + { + try + { method = tk.getClass().getMethod("getMenuShortcutKeyMask"); - SHORTCUT_KEY_MASK = ((int) method.invoke(tk, new Object[0])); - if (SHORTCUT_KEY_MASK <= 0xF) + } catch (NoSuchMethodException e) { - // shift this into the extended region (was Java 8) - SHORTCUT_KEY_MASK = SHORTCUT_KEY_MASK << 6; + System.err.println( + "Could not find Toolkit method getMenuShortcutKeyMaskEx or getMenuShortcutKeyMask."); + e.printStackTrace(); } - } catch (Exception e) + } + if (method != null) { - e.printStackTrace(); + try + { + method.setAccessible(true); + SHORTCUT_KEY_MASK = ((int) method.invoke(tk, new Object[0])); + } catch (Exception e) + { + e.printStackTrace(); + } + } + if (SHORTCUT_KEY_MASK <= 0xF) + { + // shift this into the extended region (was Java 8) + SHORTCUT_KEY_MASK = SHORTCUT_KEY_MASK << 6; } } } @@ -230,14 +254,15 @@ public class Platform */ protected static boolean isControlDown(MouseEvent e, boolean aMac) { - - System.out.println(e.isPopupTrigger() - + " " + ((SHORTCUT_KEY_MASK & e.getModifiersEx()) != 0) - + " " + e.isControlDown()); - return (aMac ? !e.isPopupTrigger() - && (SHORTCUT_KEY_MASK & e.getModifiersEx()) != 0 + + System.out.println(e.isPopupTrigger() + " " + + ((SHORTCUT_KEY_MASK & e.getModifiersEx()) != 0) + " " + + e.isControlDown()); + return (aMac + ? !e.isPopupTrigger() + && (SHORTCUT_KEY_MASK & e.getModifiersEx()) != 0 : e.isControlDown()); - } + } // BH: I don't know about that previous method. Here is what SwingJS uses. // Notice the distinction in mouse events. (BUTTON3_MASK == META) -- 1.7.10.2