Merge commit 'alpha/update_2_12_for_2_11_2_series_merge^2' into HEAD
[jalview.git] / src / jalview / util / Platform.java
index ac84846..7b93fe5 100644 (file)
@@ -22,6 +22,8 @@ package jalview.util;
 
 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;
@@ -60,7 +62,6 @@ import com.stevesoft.pat.Regex;
 import jalview.bin.Jalview;
 import jalview.javascript.json.JSON;
 import swingjs.api.JSUtilI;
-
 /**
  * System platform information used by Applet and Application
  * 
@@ -73,7 +74,8 @@ public class Platform
           false;
 
   private static Boolean isNoJSMac = null, isNoJSWin = null, isMac = null,
-          isWin = null;
+          isWin = null, isLinux = null;
+
 
   private static Boolean isHeadless = null;
 
@@ -96,7 +98,6 @@ public class Platform
       }
     }
   }
-  // private static Boolean isHeadless = null;
 
   /**
    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
@@ -116,48 +117,50 @@ public class Platform
 
   static
   {
-    // Using non-deprecated Extended key mask modifiers, but Java 8 has no
-    // getMenuShortcutKeyMaskEx method
-    Toolkit tk = Toolkit.getDefaultToolkit();
-    Method method = null;
-    try
-    {
-      method = tk.getClass().getMethod("getMenuShortcutKeyMaskEx");
-    } catch (NoSuchMethodException e)
-    {
-      System.err.println(
-              "Could not find Toolkit method getMenuShortcutKeyMaskEx. Trying getMenuShortcutKeyMask.");
-    }
-    if (method == null)
+    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
       {
-        method = tk.getClass().getMethod("getMenuShortcutKeyMask");
-      } catch (NoSuchMethodException e)
+        method = tk.getClass().getMethod("getMenuShortcutKeyMaskEx");
+      } catch (Exception e)
       {
         System.err.println(
-                "Could not find Toolkit method getMenuShortcutKeyMaskEx or getMenuShortcutKeyMask.");
-        e.printStackTrace();
+                "Could not find Toolkit method getMenuShortcutKeyMaskEx. Trying getMenuShortcutKeyMask.");
       }
-    }
-    if (method != null)
-    {
-      try
+      if (method == null)
       {
-        method.setAccessible(true);
-        SHORTCUT_KEY_MASK = ((int) method.invoke(tk, new Object[0]));
-      } catch (Exception e)
+        try
+        {
+          method = tk.getClass().getMethod("getMenuShortcutKeyMask");
+        } catch (Exception e)
+        {
+          System.err.println(
+                  "Could not find Toolkit method getMenuShortcutKeyMaskEx or getMenuShortcutKeyMask.");
+          e.printStackTrace();
+        }
+      }
+      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;
       }
-    }
-    if (SHORTCUT_KEY_MASK <= 0xF)
-    {
-      // shift this into the extended region (was Java 8)
-      SHORTCUT_KEY_MASK = SHORTCUT_KEY_MASK << 6;
     }
   }
-
   /**
    * added to group mouse events into Windows and nonWindows (mac, unix, linux)
    * 
@@ -171,6 +174,18 @@ public class Platform
   }
 
   /**
+   * added to check LaF for Linux
+   * 
+   * @return
+   */
+  public static boolean isLinux()
+  {
+    return (isLinux == null
+            ? (isLinux = (System.getProperty("os.name").indexOf("Linux") >= 0))
+            : isLinux);
+  }
+
+  /**
    * 
    * @return true if HTML5 JavaScript
    */
@@ -192,7 +207,7 @@ public class Platform
   }
 
   /**
-   * Check if we are on a Microsoft plaform...
+   * Check if we are on a Microsoft platform...
    * 
    * @return true if we have to cope with another platform variation
    */
@@ -202,7 +217,7 @@ public class Platform
   }
 
   /**
-   *
+   * 
    * @return true if we are running in non-interactive no UI mode
    */
   public static boolean isHeadless()
@@ -508,6 +523,7 @@ public class Platform
   public static String getUniqueAppletID()
   {
     return (isJS ? (String) jsutil.getAppletAttribute("_uniqueId") : null);
+
   }
 
   /**
@@ -607,6 +623,7 @@ public class Platform
               "StringJS does not support FileReader parsing for JSON -- but it could...");
     }
     return JSON.parse(r);
+
   }
 
   /**
@@ -621,13 +638,11 @@ public class Platform
   public static void streamToFile(InputStream is, File outFile)
           throws IOException
   {
-
     if (isJS)
     {
       jsutil.setFileBytes(outFile, is);
       return;
     }
-
     FileOutputStream fio = new FileOutputStream(outFile);
     try
     {
@@ -660,7 +675,6 @@ public class Platform
     if (isJS)
     {
       jsutil.addDirectDatabaseCall(domain);
-
       System.out.println(
               "Platform adding known access-control-allow-origin * for domain "
                       + domain);
@@ -675,8 +689,7 @@ public class Platform
   public static void getURLCommandArguments()
   {
 
-    try
-    {
+      try {
       /**
        * Retrieve the first query field as command arguments to Jalview. Include
        * only if prior to "?j2s" or "&j2s" or "#". Assign the applet's
@@ -684,14 +697,14 @@ public class Platform
        * 
        * @j2sNative var a =
        *            decodeURI((document.location.href.replace("&","?").split("?j2s")[0]
-       *            + "?").split("?")[1].split("#")[0]); a &&
-       *            (J2S.thisApplet.__Info.args = a.split(" "));
-       * 
-       *            System.out.println("URL arguments: " + a);
+       *            + "?").split("?")[1].split("#")[0]); a && (System.out.println("URL arguments detected were "+a)) &&
+       *            (J2S.thisApplet.__Info.urlargs = a.split(" ")); 
+       *            (!J2S.thisApplet.__Info.args || J2S.thisApplet.__Info.args == "" || J2S.thisApplet.__Info.args == "??") && (J2S.thisApplet.__Info.args = a) && (System.out.println("URL arguments were passed to J2S main."));
        */
     } catch (Throwable t)
     {
     }
+
   }
 
   /**
@@ -716,7 +729,6 @@ public class Platform
     String p2 = path2.replace('\\', '/');
     return p1.equals(p2);
   }
-
   ///////////// JAL-3253 Applet additions //////////////
 
   /**
@@ -1040,5 +1052,4 @@ public class Platform
       }
     }
   }
-
 }