Merge branch 'develop' into trialMerge
[jalview.git] / src / jalview / bin / Jalview.java
index cc41c53..2595c4d 100755 (executable)
@@ -41,6 +41,8 @@ import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.ws.jws2.Jws2Discoverer;
 
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -60,8 +62,12 @@ import java.security.Policy;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Vector;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.swing.LookAndFeel;
+import javax.swing.Timer;
 import javax.swing.UIManager;
 
 import groovy.lang.Binding;
@@ -84,6 +90,29 @@ import groovy.util.GroovyScriptEngine;
  */
 public class Jalview
 {
+       /**
+        * @j2sNative
+        * 
+        *  // find first query parameter (if any) that doesn't start with j2s
+        *  // and set as space-delimited arguments to Jalview main
+        *      hr = decodeURI(document.location.href);
+        *  pos = hr.indexOf("?");
+        *  if (pos > 0)
+        *  {
+        *     q = hr.substring(pos+1);
+        *     args = q.split("&");
+        *     for (i = 0 ; i < args.length; i++)
+        *     {
+        *      arg1 = args[i];
+        *      if (!arg1.startsWith("j2s"))
+        *      {
+        *       thisApplet.__Info.args = arg1.split(" ");
+        *       break;
+        *      }
+        *     }
+        *  }
+        */
+
   /*
    * singleton instance of this class
    */
@@ -95,22 +124,25 @@ public class Jalview
 
   static
   {
-    // grab all the rights we can the JVM
-    Policy.setPolicy(new Policy()
-    {
-      @Override
-      public PermissionCollection getPermissions(CodeSource codesource)
-      {
-        Permissions perms = new Permissions();
-        perms.add(new AllPermission());
-        return (perms);
-      }
-
-      @Override
-      public void refresh()
-      {
-      }
-    });
+    if (!Platform.isJS())
+    { // BH 2018
+           // grab all the rights we can for the JVM
+           Policy.setPolicy(new Policy()
+           {
+             @Override
+             public PermissionCollection getPermissions(CodeSource codesource)
+             {
+               Permissions perms = new Permissions();
+               perms.add(new AllPermission());
+               return (perms);
+             }
+       
+             @Override
+             public void refresh()
+             {
+             }
+           });
+    }
   }
 
   /**
@@ -185,16 +217,51 @@ public class Jalview
    */
   public static void main(String[] args)
   {
+//     setLogging(); // BH - for event debugging in JavaScript
     instance = new Jalview();
     instance.doMain(args);
+}
+
+  private static void logClass(String name) 
+  {    
+         // BH - for event debugging in JavaScript
+      ConsoleHandler consoleHandler = new ConsoleHandler();
+      consoleHandler.setLevel(Level.ALL);
+      Logger logger = Logger.getLogger(name);
+      logger.setLevel(Level.ALL);
+      logger.addHandler(consoleHandler);
+  }
+
+  @SuppressWarnings("unused")
+  private static void setLogging() 
+  {
+         // BH - for event debugging in JavaScript (Java mode only)
+       if (!(/** @j2sNative true ||*/false)) 
+       {
+               Logger.getLogger("").setLevel(Level.ALL);
+        logClass("java.awt.EventDispatchThread");
+        logClass("java.awt.EventQueue");
+        logClass("java.awt.Component");
+        logClass("java.awt.focus.Component");
+        logClass("java.awt.focus.DefaultKeyboardFocusManager"); 
+       }       
+
   }
+  
+
+  
 
   /**
    * @param args
    */
   void doMain(String[] args)
   {
-    System.setSecurityManager(null);
+
+    if (!Platform.isJS())
+    {
+      System.setSecurityManager(null);
+    }
+
     System.out
             .println("Java version: " + System.getProperty("java.version"));
     System.out.println(System.getProperty("os.arch") + " "
@@ -204,17 +271,6 @@ public class Jalview
     ArgsParser aparser = new ArgsParser(args);
     boolean headless = false;
 
-    if (aparser.contains("help") || aparser.contains("h"))
-    {
-      showUsage();
-      System.exit(0);
-    }
-    if (aparser.contains("nodisplay") || aparser.contains("nogui")
-            || aparser.contains("headless"))
-    {
-      System.setProperty("java.awt.headless", "true");
-      headless = true;
-    }
     String usrPropsFile = aparser.getValue("props");
     Cache.loadProperties(usrPropsFile); // must do this before
     if (usrPropsFile != null)
@@ -223,23 +279,41 @@ public class Jalview
               "CMD [-props " + usrPropsFile + "] executed successfully!");
     }
 
-    // anything else!
-
-    final String jabawsUrl = aparser.getValue("jabaws");
-    if (jabawsUrl != null)
+    /**
+     * BH 2018 ignoring this section for JS
+     * 
+     * @j2sNative
+     */
     {
-      try
+      if (aparser.contains("help") || aparser.contains("h"))
       {
-        Jws2Discoverer.getDiscoverer().setPreferredUrl(jabawsUrl);
-        System.out.println(
-                "CMD [-jabaws " + jabawsUrl + "] executed successfully!");
-      } catch (MalformedURLException e)
+        showUsage();
+        System.exit(0);
+      }
+      if (aparser.contains("nodisplay") || aparser.contains("nogui")
+              || aparser.contains("headless"))
       {
-        System.err.println(
-                "Invalid jabaws parameter: " + jabawsUrl + " ignored");
+        System.setProperty("java.awt.headless", "true");
+        headless = true;
       }
-    }
+      // anything else!
 
+      final String jabawsUrl = aparser.getValue("jabaws");
+      if (jabawsUrl != null)
+      {
+        try
+        {
+          Jws2Discoverer.getDiscoverer().setPreferredUrl(jabawsUrl);
+          System.out.println(
+                  "CMD [-jabaws " + jabawsUrl + "] executed successfully!");
+        } catch (MalformedURLException e)
+        {
+          System.err.println(
+                  "Invalid jabaws parameter: " + jabawsUrl + " ignored");
+        }
+      }
+
+    }
     String defs = aparser.getValue("setprop");
     while (defs != null)
     {
@@ -251,9 +325,10 @@ public class Jalview
       else
       {
         System.out.println("Executing setprop argument: " + defs);
-        // DISABLED FOR SECURITY REASONS
-        // TODO: add a property to allow properties to be overriden by cli args
-        // Cache.setProperty(defs.substring(0,p), defs.substring(p+1));
+        if (Platform.isJS())
+        {
+          Cache.setProperty(defs.substring(0,p), defs.substring(p+1));
+        }
       }
       defs = aparser.getValue("setprop");
     }
@@ -285,7 +360,7 @@ public class Jalview
       System.err.println("Unexpected Look and Feel Exception");
       ex.printStackTrace();
     }
-    if (Platform.isAMac())
+    if (Platform.isAMacAndNotJS())
     {
 
       LookAndFeel lookAndFeel = ch.randelshofer.quaqua.QuaquaManager
@@ -333,58 +408,77 @@ public class Jalview
 
     if (!headless)
     {
-      desktop = new Desktop();
+      desktop = new Desktop() 
+//      {
+// // BH testing
+//       @Override
+//       protected void processEvent(AWTEvent e) {
+//               System.out.println("Jalview.java " + e);
+//               super.processEvent(e);
+//       }
+//       }
+      ;
       desktop.setInBatchMode(true); // indicate we are starting up
       desktop.setVisible(true);
-      desktop.startServiceDiscovery();
-      if (!aparser.contains("nousagestats"))
-      {
-        startUsageStats(desktop);
-      }
-      else
-      {
-        System.err.println("CMD [-nousagestats] executed successfully!");
-      }
 
-      if (!aparser.contains("noquestionnaire"))
+      /**
+       * BH 2018 JS bypass this section
+       * 
+       * @j2sNative
+       * 
+       */
       {
-        String url = aparser.getValue("questionnaire");
-        if (url != null)
+        desktop.startServiceDiscovery();
+        if (!aparser.contains("nousagestats"))
         {
-          // Start the desktop questionnaire prompter with the specified
-          // questionnaire
-          Cache.log.debug("Starting questionnaire url at " + url);
-          desktop.checkForQuestionnaire(url);
-          System.out.println(
-                  "CMD questionnaire[-" + url + "] executed successfully!");
+          startUsageStats(desktop);
         }
         else
         {
-          if (Cache.getProperty("NOQUESTIONNAIRES") == null)
+          System.err.println("CMD [-nousagestats] executed successfully!");
+        }
+
+        if (!aparser.contains("noquestionnaire"))
+        {
+          String url = aparser.getValue("questionnaire");
+          if (url != null)
           {
             // Start the desktop questionnaire prompter with the specified
             // questionnaire
-            // String defurl =
-            // "http://anaplog.compbio.dundee.ac.uk/cgi-bin/questionnaire.pl";
-            // //
-            String defurl = "http://www.jalview.org/cgi-bin/questionnaire.pl";
-            Cache.log.debug(
-                    "Starting questionnaire with default url: " + defurl);
-            desktop.checkForQuestionnaire(defurl);
+            Cache.log.debug("Starting questionnaire url at " + url);
+            desktop.checkForQuestionnaire(url);
+            System.out.println("CMD questionnaire[-" + url
+                    + "] executed successfully!");
+          }
+          else
+          {
+            if (Cache.getProperty("NOQUESTIONNAIRES") == null)
+            {
+              // Start the desktop questionnaire prompter with the specified
+              // questionnaire
+              // String defurl =
+              // "http://anaplog.compbio.dundee.ac.uk/cgi-bin/questionnaire.pl";
+              // //
+              String defurl = "http://www.jalview.org/cgi-bin/questionnaire.pl";
+              Cache.log.debug(
+                      "Starting questionnaire with default url: " + defurl);
+              desktop.checkForQuestionnaire(defurl);
+            }
           }
         }
-      }
-      else
-      {
-        System.err.println("CMD [-noquestionnaire] executed successfully!");
-      }
+        else
+        {
+          System.err
+                  .println("CMD [-noquestionnaire] executed successfully!");
+        }
 
-      if (!aparser.contains("nonews"))
-      {
-        desktop.checkForNews();
-      }
+        if (!aparser.contains("nonews"))
+        {
+          desktop.checkForNews();
+        }
 
-      BioJsHTMLOutput.updateBioJS();
+        BioJsHTMLOutput.updateBioJS();
+      }
     }
 
     String file = null, data = null;
@@ -501,7 +595,7 @@ public class Jalview
       }
       System.out.println("CMD [-open " + file + "] executed successfully!");
 
-      if (!file.startsWith("http://"))
+      if (!Platform.isJS() && !file.startsWith("http://"))
       {
         if (!(new File(file)).exists())
         {
@@ -513,7 +607,7 @@ public class Jalview
         }
       }
 
-      protocol = AppletFormatAdapter.checkProtocol(file);
+        protocol = AppletFormatAdapter.checkProtocol(file);
 
       try
       {
@@ -700,7 +794,8 @@ public class Jalview
             continue;
           }
 
-          if (af.saveAlignment(file, format))
+          af.saveAlignment(file, format);
+          if (af.isSaveAlignmentSuccessful())
           {
             System.out.println("Written alignment in " + format
                     + " format to " + file);
@@ -724,7 +819,8 @@ public class Jalview
     // And the user
     // ////////////////////
 
-    if (!headless && file == null && vamsasImport == null
+    if (/** @j2sNative false && */ // BH 2018
+    !headless && file == null && vamsasImport == null
             && jalview.bin.Cache.getDefault("SHOW_STARTUP_FILE", true))
     {
       file = jalview.bin.Cache.getDefault("STARTUP_FILE",