JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[jalview.git] / src / jalview / util / BrowserLauncher.java
index 2404cb6..ca2e55e 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
- * Copyright (C) 2015 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -28,9 +28,14 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 
 /**
- * BrowserLauncher is a class that provides one static method, openURL, which
- * opens the default web browser for the current user of the system to the given
- * URL. It may support other protocols depending on the system -- mailto, ftp,
+ * BrowserLauncher is a class that provides two static methods:
+ * 
+ * openURL(String url), which opens the default web browser for the current user
+ * of the system to the given URL, and
+ * 
+ * resetBrowser(), which allows switching browsers in Java.
+ * 
+ * openURL may support other protocols depending on the system -- mailto, ftp,
  * etc. -- but that has not been rigorously tested and is not guaranteed to
  * work.
  * <p>
@@ -69,16 +74,24 @@ import java.lang.reflect.Method;
  * functionality or any adverse or unexpected effects of using this software.
  * <p>
  * Credits: <br>
- * Steven Spencer, JavaWorld magazine (<a
- * href="http://www.javaworld.com/javaworld/javatips/jw-javatip66.html">Java Tip
- * 66</a>) <br>
+ * Steven Spencer, JavaWorld magazine
+ * (<a href="http://www.javaworld.com/javaworld/javatips/jw-javatip66.html">Java
+ * Tip 66</a>) <br>
  * Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea
  * Cantatore, Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk
  * 
- * @author Eric Albert (<a
- *         href="mailto:ejalbert@cs.stanford.edu">ejalbert@cs.stanford.edu</a>)
+ * @author Eric Albert (<a href=
+ *         "mailto:ejalbert@cs.stanford.edu">ejalbert@cs.stanford.edu</a>)
  * @version 1.4b1 (Released June 20, 2001)
  */
+/*
+ * 
+ * SwingJS note: Do not use methods in this class directly. Use
+ * 
+ * Platform.openURL(String url) only.
+ * 
+ * JavaScript does not see this class.
+ */
 public class BrowserLauncher
 {
   /**
@@ -101,22 +114,22 @@ public class BrowserLauncher
   private static boolean loadedWithoutErrors;
 
   /** The com.apple.mrj.MRJFileUtils class */
-  private static Class mrjFileUtilsClass;
+  private static Class<?> mrjFileUtilsClass;
 
   /** The com.apple.mrj.MRJOSType class */
-  private static Class mrjOSTypeClass;
+  private static Class<?> mrjOSTypeClass;
 
   /** The com.apple.MacOS.AEDesc class */
-  private static Class aeDescClass;
+  private static Class<?> aeDescClass;
 
   /** The &lt;init&gt;(int) method of com.apple.MacOS.AETarget */
-  private static Constructor aeTargetConstructor;
+  private static Constructor<?> aeTargetConstructor;
 
   /** The &lt;init&gt;(int, int, int) method of com.apple.MacOS.AppleEvent */
-  private static Constructor appleEventConstructor;
+  private static Constructor<?> appleEventConstructor;
 
   /** The &lt;init&gt;(String) method of com.apple.MacOS.AEDesc */
-  private static Constructor aeDescConstructor;
+  private static Constructor<?> aeDescConstructor;
 
   /** The findFolder method of com.apple.mrj.MRJFileUtils */
   private static Method findFolder;
@@ -233,6 +246,7 @@ public class BrowserLauncher
    */
   static
   {
+
     loadedWithoutErrors = true;
 
     String osName = System.getProperty("os.name");
@@ -325,30 +339,37 @@ public class BrowserLauncher
    */
   private static boolean loadClasses()
   {
+
     switch (jvm)
     {
     case MRJ_2_0:
 
       try
       {
-        Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
-        Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
-        Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
-        Class aeClass = Class.forName("com.apple.MacOS.ae");
+        Class<?> aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
+        Class<?> osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
+        Class<?> appleEventClass = Class
+                .forName("com.apple.MacOS.AppleEvent");
+        Class<?> aeClass = Class.forName("com.apple.MacOS.ae");
         aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
 
         aeTargetConstructor = aeTargetClass
-                .getDeclaredConstructor(new Class[] { int.class });
+                .getDeclaredConstructor(new Class[]
+                { int.class });
         appleEventConstructor = appleEventClass
-                .getDeclaredConstructor(new Class[] { int.class, int.class,
-                    aeTargetClass, int.class, int.class });
+                .getDeclaredConstructor(new Class[]
+                { int.class, int.class, aeTargetClass, int.class,
+                    int.class });
         aeDescConstructor = aeDescClass
-                .getDeclaredConstructor(new Class[] { String.class });
+                .getDeclaredConstructor(new Class[]
+                { String.class });
 
         makeOSType = osUtilsClass.getDeclaredMethod("makeOSType",
-                new Class[] { String.class });
+                new Class[]
+                { String.class });
         putParameter = appleEventClass.getDeclaredMethod("putParameter",
-                new Class[] { int.class, aeDescClass });
+                new Class[]
+                { int.class, aeDescClass });
         sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply",
                 new Class[] {});
 
@@ -399,11 +420,14 @@ public class BrowserLauncher
                 .getDeclaredField("kSystemFolderType");
         kSystemFolderType = systemFolderField.get(null);
         findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder",
-                new Class[] { mrjOSTypeClass });
-        getFileCreator = mrjFileUtilsClass.getDeclaredMethod(
-                "getFileCreator", new Class[] { File.class });
+                new Class[]
+                { mrjOSTypeClass });
+        getFileCreator = mrjFileUtilsClass
+                .getDeclaredMethod("getFileCreator", new Class[]
+                { File.class });
         getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType",
-                new Class[] { File.class });
+                new Class[]
+                { File.class });
       } catch (ClassNotFoundException cnfe)
       {
         errorMessage = cnfe.getMessage();
@@ -437,11 +461,13 @@ public class BrowserLauncher
 
       try
       {
-        Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
-        Constructor constructor = linker
-                .getConstructor(new Class[] { Class.class });
+        Class<?> linker = Class.forName("com.apple.mrj.jdirect.Linker");
+        Constructor<?> constructor = linker
+                .getConstructor(new Class[]
+                { Class.class });
         linkage = constructor
-                .newInstance(new Object[] { BrowserLauncher.class });
+                .newInstance(new Object[]
+                { BrowserLauncher.class });
       } catch (ClassNotFoundException cnfe)
       {
         errorMessage = cnfe.getMessage();
@@ -477,7 +503,8 @@ public class BrowserLauncher
       {
         mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
         openURL = mrjFileUtilsClass.getDeclaredMethod("openURL",
-                new Class[] { String.class });
+                new Class[]
+                { String.class });
       } catch (ClassNotFoundException cnfe)
       {
         errorMessage = cnfe.getMessage();
@@ -524,14 +551,18 @@ public class BrowserLauncher
       try
       {
         Integer finderCreatorCode = (Integer) makeOSType.invoke(null,
-                new Object[] { FINDER_CREATOR });
+                new Object[]
+                { FINDER_CREATOR });
         Object aeTarget = aeTargetConstructor
-                .newInstance(new Object[] { finderCreatorCode });
+                .newInstance(new Object[]
+                { finderCreatorCode });
         Integer gurlType = (Integer) makeOSType.invoke(null,
-                new Object[] { GURL_EVENT });
-        Object appleEvent = appleEventConstructor.newInstance(new Object[] {
-            gurlType, gurlType, aeTarget, kAutoGenerateReturnID,
-            kAnyTransactionID });
+                new Object[]
+                { GURL_EVENT });
+        Object appleEvent = appleEventConstructor
+                .newInstance(new Object[]
+                { gurlType, gurlType, aeTarget, kAutoGenerateReturnID,
+                    kAnyTransactionID });
 
         // Don't set browser = appleEvent because then the next time we call
         // locateBrowser(), we'll get the same AppleEvent, to which we'll
@@ -569,7 +600,8 @@ public class BrowserLauncher
       try
       {
         systemFolder = (File) findFolder.invoke(null,
-                new Object[] { kSystemFolderType });
+                new Object[]
+                { kSystemFolderType });
       } catch (IllegalArgumentException iare)
       {
         browser = null;
@@ -615,7 +647,8 @@ public class BrowserLauncher
           if (FINDER_TYPE.equals(fileType.toString()))
           {
             Object fileCreator = getFileCreator.invoke(null,
-                    new Object[] { file });
+                    new Object[]
+                    { file });
 
             if (FINDER_CREATOR.equals(fileCreator.toString()))
             {
@@ -671,7 +704,6 @@ public class BrowserLauncher
 
       break;
     }
-
     return browser;
   }
 
@@ -694,10 +726,16 @@ public class BrowserLauncher
    */
   public static void openURL(String url) throws IOException
   {
+    if (Platform.isJS())
+    {
+      Platform.openURL(url);
+      return;
+    }
     if (!loadedWithoutErrors)
     {
-      throw new IOException(MessageManager.formatMessage(
-              "exception.browser_not_found", new String[] { errorMessage }));
+      throw new IOException(MessageManager
+              .formatMessage("exception.browser_not_found", new String[]
+              { errorMessage }));
     }
 
     Object browser = locateBrowser();
@@ -705,8 +743,8 @@ public class BrowserLauncher
     if (browser == null)
     {
       throw new IOException(MessageManager.formatMessage(
-              "exception.browser_unable_to_locate",
-              new String[] { errorMessage }));
+              "exception.browser_unable_to_locate", new String[]
+              { errorMessage }));
     }
 
     switch (jvm)
@@ -719,13 +757,15 @@ public class BrowserLauncher
       {
         aeDesc = aeDescConstructor.newInstance(new Object[] { url });
         putParameter.invoke(browser,
-                new Object[] { keyDirectObject, aeDesc });
+                new Object[]
+                { keyDirectObject, aeDesc });
         sendNoReply.invoke(browser, new Object[] {});
       } catch (InvocationTargetException ite)
       {
         throw new IOException(MessageManager.formatMessage(
                 "exception.invocation_target_exception_creating_aedesc",
-                new String[] { ite.getMessage() }));
+                new String[]
+                { ite.getMessage() }));
       } catch (IllegalAccessException iae)
       {
         throw new IOException(MessageManager.formatMessage(
@@ -771,15 +811,15 @@ public class BrowserLauncher
         else
         {
           throw new IOException(MessageManager.formatMessage(
-                  "exception.unable_to_launch_url", new String[] { Integer
-                          .valueOf(result).toString() }));
+                  "exception.unable_to_launch_url", new String[]
+                  { Integer.valueOf(result).toString() }));
         }
       }
       else
       {
         throw new IOException(MessageManager.formatMessage(
-                "exception.unable_to_create_internet_config",
-                new String[] { Integer.valueOf(result).toString() }));
+                "exception.unable_to_create_internet_config", new String[]
+                { Integer.valueOf(result).toString() }));
       }
 
       break;
@@ -792,13 +832,13 @@ public class BrowserLauncher
       } catch (InvocationTargetException ite)
       {
         throw new IOException(MessageManager.formatMessage(
-                "exception.invocation_target_calling_url",
-                new String[] { ite.getMessage() }));
+                "exception.invocation_target_calling_url", new String[]
+                { ite.getMessage() }));
       } catch (IllegalAccessException iae)
       {
         throw new IOException(MessageManager.formatMessage(
-                "exception.illegal_access_calling_url",
-                new String[] { iae.getMessage() }));
+                "exception.illegal_access_calling_url", new String[]
+                { iae.getMessage() }));
       }
 
       break;
@@ -808,8 +848,9 @@ public class BrowserLauncher
 
       // Add quotes around the URL to allow ampersands and other special
       // characters to work.
-      Process process = Runtime.getRuntime().exec(
-              new String[] { (String) browser, FIRST_WINDOWS_PARAMETER,
+      Process process = Runtime.getRuntime()
+              .exec(new String[]
+              { (String) browser, FIRST_WINDOWS_PARAMETER,
                   SECOND_WINDOWS_PARAMETER, THIRD_WINDOWS_PARAMETER,
                   '"' + url + '"' });
 
@@ -823,8 +864,8 @@ public class BrowserLauncher
       } catch (InterruptedException ie)
       {
         throw new IOException(MessageManager.formatMessage(
-                "exception.interrupted_launching_browser",
-                new String[] { ie.getMessage() }));
+                "exception.interrupted_launching_browser", new String[]
+                { ie.getMessage() }));
       }
 
       break;
@@ -842,10 +883,9 @@ public class BrowserLauncher
        * NETSCAPE_REMOTE_PARAMETER+" "+ NETSCAPE_OPEN_PARAMETER_START + url +
        * NETSCAPE_OPEN_NEW_WINDOW + NETSCAPE_OPEN_PARAMETER_END);
        */
-      process = Runtime.getRuntime().exec(
-              new String[] {
-                  (String) browser,
-                  NETSCAPE_REMOTE_PARAMETER,
+      process = Runtime.getRuntime()
+              .exec(new String[]
+              { (String) browser, NETSCAPE_REMOTE_PARAMETER,
 
                   NETSCAPE_OPEN_PARAMETER_START + url
                           + NETSCAPE_OPEN_NEW_WINDOW
@@ -862,22 +902,21 @@ public class BrowserLauncher
       } catch (InterruptedException ie)
       {
         throw new IOException(MessageManager.formatMessage(
-                "exception.interrupted_launching_browser",
-                new String[] { ie.getMessage() }));
+                "exception.interrupted_launching_browser", new String[]
+                { ie.getMessage() }));
       }
 
       break;
 
     default:
-
       // This should never occur, but if it does, we'll try the simplest thing
       // possible
       Runtime.getRuntime().exec(new String[] { (String) browser, url });
-
       break;
     }
   }
 
+
   /**
    * Methods required for Mac OS X. The presence of native methods does not
    * cause any problems on other platforms.