JAL-3446 test for simpler key mask operation
[jalview.git] / src / jalview / util / Platform.java
index c06c755..036ae17 100644 (file)
  */
 package jalview.util;
 
-import jalview.javascript.json.JSON;
-
 import java.awt.Component;
 import java.awt.Dimension;
+import java.awt.GraphicsEnvironment;
 import java.awt.Toolkit;
+import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
 import java.io.BufferedReader;
 import java.io.File;
@@ -34,6 +34,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.Properties;
 import java.util.logging.ConsoleHandler;
@@ -47,6 +48,7 @@ import org.json.simple.parser.ParseException;
 
 import com.stevesoft.pat.Regex;
 
+import jalview.javascript.json.JSON;
 import swingjs.api.JSUtilI;
 
 /**
@@ -54,6 +56,7 @@ import swingjs.api.JSUtilI;
  * 
  * @author Jim Procter
  */
+
 public class Platform
 {
 
@@ -64,11 +67,14 @@ public class Platform
           isWin = null;
 
   private static Boolean isHeadless = null;
-  
+
   private static swingjs.api.JSUtilI jsutil;
+  
 
-  static {
-          if (isJS) {
+  static
+  {
+    if (isJS)
+    {
       try
       {
         jsutil = ((JSUtilI) Class.forName("swingjs.JSUtil").newInstance());
@@ -77,7 +83,7 @@ public class Platform
       {
         e.printStackTrace();
       }
-          }
+    }
   }
   // private static Boolean isHeadless = null;
 
@@ -93,6 +99,35 @@ 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())
+    {
+      try
+      {
+
+        Class<? extends Toolkit> tk = Toolkit.getDefaultToolkit().getClass();
+        Method method = tk.getMethod("getMenuShortcutKeyMaskEx");
+        if (method == null)
+          method = tk.getMethod("getMenuShortcutKeyMask");
+        SHORTCUT_KEY_MASK = ((int) method.invoke(tk, new Object[0]));
+        if (SHORTCUT_KEY_MASK <= 0xF)
+        {
+          // shift this into the extended region (was Java 8)
+          SHORTCUT_KEY_MASK = SHORTCUT_KEY_MASK << 6;
+        }
+      } catch (Exception e)
+      {
+      }
+    }
+  }
+
   /**
    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
    * 
@@ -136,18 +171,18 @@ public class Platform
     return (isNoJSWin == null ? (isNoJSWin = !isJS && isWin()) : isNoJSWin);
   }
 
- /**
- *
- * @return true if we are running in non-interactive no UI mode
- */
- public static boolean isHeadless()
- {
- if (isHeadless == null)
- {
- isHeadless = "true".equals(System.getProperty("java.awt.headless"));
- }
- return isHeadless;
- }
+  /**
+   *
+   * @return true if we are running in non-interactive no UI mode
+   */
+  public static boolean isHeadless()
+  {
+    if (isHeadless == null)
+    {
+      isHeadless = "true".equals(System.getProperty("java.awt.headless"));
+    }
+    return isHeadless;
+  }
 
   /**
    * 
@@ -170,7 +205,7 @@ public class Platform
   {
     return s == null ? null : s.replace("\\", "\\\\");
   }
-  
+
   /**
    * Answers true if the mouse event has Meta-down (Command key on Mac) or
    * Ctrl-down (on other o/s). Note this answers _false_ if the Ctrl key is
@@ -194,31 +229,14 @@ public class Platform
    */
   protected static boolean isControlDown(MouseEvent e, boolean aMac)
   {
-    if (!aMac)
-    {
-      return e.isControlDown();
-
-      // Jalview 2.11 code below: above is as amended for JalviewJS
-      // /*
-      // * answer false for right mouse button
-      // */
-      // if (e.isPopupTrigger())
-      // {
-      // return false;
-      // }
-      // return
-      // (jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx() //
-      // .getMenuShortcutKeyMaskEx()
-      // & jalview.util.ShortcutKeyMaskExWrapper
-      // .getModifiersEx(e)) != 0; // getModifiers()) != 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());
     }
-    // answer false for right mouse button
-    // shortcut key will be META for a Mac
-    return !e.isPopupTrigger()
-            && (Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
-                    & e.getModifiers()) != 0;
-    // could we use e.isMetaDown() here?
-  }
 
   // BH: I don't know about that previous method. Here is what SwingJS uses.
   // Notice the distinction in mouse events. (BUTTON3_MASK == META)
@@ -360,7 +378,6 @@ public class Platform
     }
   }
 
-
   public static void cacheFileData(File file)
   {
     if (isJS)
@@ -390,7 +407,7 @@ public class Platform
     }
     return null;
   }
-  
+
   public static String getFileAsString(String url)
   {
     if (isJS && url != null)
@@ -414,7 +431,6 @@ public class Platform
     return false;
   }
 
-
   public static void addJ2SBinaryType(String ext)
   {
     if (isJS)
@@ -422,7 +438,7 @@ public class Platform
       jsutil.addBinaryFileType(ext);
     }
   }
-  
+
   /**
    * Encode the URI using JavaScript encodeURIComponent
    * 
@@ -463,7 +479,6 @@ public class Platform
     return (isJS ? (String) jsutil.getAppletAttribute("_uniqueId") : null);
   }
 
-
   /**
    * Read the Info block for this applet.
    * 
@@ -538,8 +553,7 @@ public class Platform
 
   public static Object parseJSON(String json) throws ParseException
   {
-    return (isJS() ? JSON.parse(json)
-            : new JSONParser().parse(json));
+    return (isJS() ? JSON.parse(json) : new JSONParser().parse(json));
   }
 
   public static Object parseJSON(Reader r)
@@ -570,8 +584,8 @@ public class Platform
    * @param is
    * @param outFile
    * @throws IOException
-   *                       if the file cannot be created or there is a problem
-   *                       reading the input stream.
+   *           if the file cannot be created or there is a problem reading the
+   *           input stream.
    */
   public static void streamToFile(InputStream is, File outFile)
           throws IOException
@@ -617,8 +631,8 @@ public class Platform
       jsutil.addDirectDatabaseCall(domain);
 
       System.out.println(
-            "Platform adding known access-control-allow-origin * for domain "
-                    + domain);
+              "Platform adding known access-control-allow-origin * for domain "
+                      + domain);
     }
 
   }
@@ -640,8 +654,8 @@ public class Platform
   }
 
   /**
-   * A (case sensitive) file path comparator that ignores the difference between /
-   * and \
+   * A (case sensitive) file path comparator that ignores the difference between
+   * / and \
    * 
    * @param path1
    * @param path2
@@ -662,11 +676,8 @@ public class Platform
     return p1.equals(p2);
   }
 
+  ///////////// JAL-3253 Applet additions //////////////
 
-
-///////////// JAL-3253 Applet additions //////////////
-  
-  
   /**
    * Retrieve the object's embedded size from a div's style on a page if
    * embedded in SwingJS.
@@ -753,7 +764,6 @@ public class Platform
     }
   }
 
-
   /**
    * load a resource -- probably a core file -- if and only if a particular
    * class has not been instantialized. We use a String here because if we used
@@ -833,5 +843,4 @@ public class Platform
     return (isJS ? jsutil.getCodeBase() : null);
   }
 
-
 }