JAL-3446 separated out potential exceptions for simplified key mask operation
[jalview.git] / src / jalview / util / Platform.java
index 458c02d..bc21fa1 100644 (file)
@@ -111,32 +111,49 @@ public class Platform
             : isMac);
   }
 
-  public static int SHIFT_DOWN_MASK = KeyEvent.SHIFT_DOWN_MASK;
-
-  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);
  
   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;
       }
     }
   }
@@ -242,10 +259,10 @@ public class Platform
    */
   protected static boolean isControlDown(MouseEvent e, boolean aMac)
   {
-
-    System.out.println(e.isPopupTrigger() 
-            + " " + ((SHORTCUT_KEY_MASK & e.getModifiersEx()) != 0) 
-            + " " + e.isControlDown());
+//
+//    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());