JAL-3446 removes all JalviewJSApp dependency from jalview.bin.Jalview;
[jalview.git] / src / jalview / bin / Jalview.java
index af9d06e..ac44bfe 100755 (executable)
@@ -119,11 +119,6 @@ public class Jalview implements ApplicationSingletonI
   {
   }
 
-  static
-  {
-    Platform.getURLCommandArguments();
-  }
-
   private boolean headless;
 
   private Desktop desktop;
@@ -132,8 +127,8 @@ public class Jalview implements ApplicationSingletonI
 
   public String appletResourcePath;
 
-  JalviewJSApp app; // JalviewJS-specific JavaScript interface
-  
+  public String j2sAppletID;
+
   private boolean noCalculation, noMenuBar, noStatus;
 
   private boolean noAnnotation;
@@ -160,12 +155,9 @@ public class Jalview implements ApplicationSingletonI
 
   static
   {
-    if (!Platform.isJS())
-    /**
-     * Java only
-     * 
-     * @j2sIgnore
-     */
+    if (Platform.isJS()) {
+        Platform.getURLCommandArguments();
+    } else /** @j2sIgnore */
     {
       // grab all the rights we can for the JVM
       Policy.setPolicy(new Policy()
@@ -308,11 +300,12 @@ public class Jalview implements ApplicationSingletonI
 
     if (isJS)
     {
-        app = new JalviewJSApp(aparser);
-        Preferences.setAppletDefaults();
-        Cache.loadProperties(usrPropsFile); // again, because we
-        // might be changing defaults here?
-      appletResourcePath = (String) aparser.getAppletValue("resourcepath", null, true);
+      j2sAppletID = Platform.getAppID(null);
+      Preferences.setAppletDefaults();
+      Cache.loadProperties(usrPropsFile); // again, because we
+      // might be changing defaults here?
+      appletResourcePath = (String) aparser.getAppletValue("resourcepath",
+              null, true);
     }
     else
     /**
@@ -451,7 +444,7 @@ public class Jalview implements ApplicationSingletonI
      * configure 'full' SO model if preferences say to, else use the default (full SO)
      * - as JS currently doesn't have OBO parsing, it must use 'Lite' version
      */
-    boolean soDefault = !Platform.isJS();
+    boolean soDefault = !isJS;
     if (Cache.getDefault("USE_FULL_SO", soDefault))
     {
       SequenceOntologyFactory.setSequenceOntology(new SequenceOntology());
@@ -470,7 +463,7 @@ public class Jalview implements ApplicationSingletonI
       }
       desktop.setVisible(true);
 
-      if (!Platform.isJS())
+      if (!isJS)
       /**
        * Java only
        * 
@@ -529,12 +522,13 @@ public class Jalview implements ApplicationSingletonI
         BioJsHTMLOutput.updateBioJS();
       }
     }
-
     parseArguments(aparser, true);
-    
   }
 
   /**
+   * Parse all command-line String[] arguments as well as all JavaScript-derived parameters from Info.
+   * 
+   * We allow for this method to be run from JavaScript. Basically allowing simple scripting.
    * 
    * @param aparser
    * @param isStartup
@@ -574,17 +568,22 @@ public class Jalview implements ApplicationSingletonI
 
     String file = aparser.getValue("open", true);
 
-    if (file == null && desktop == null)
+    if (!isJS && file == null && desktop == null)
     {
       System.out.println("No files to open!");
       System.exit(1);
     }
 
+    // time to open a file.
+
     long progress = -1;
     DataSourceType protocol = null;
     FileLoader fileLoader = new FileLoader(!headless);
     FileFormatI format = null;
     // Finally, deal with the remaining input data.
+    JalviewJSApp jsApp = null;
+    AlignFrame af = null;
+
     if (file != null)
     {
       if (!headless)
@@ -616,7 +615,8 @@ public class Jalview implements ApplicationSingletonI
           }
         }
       }
-      String fileFormat = (isJS ? (String) aparser.getAppletValue("format", null, true)
+      String fileFormat = (isJS
+              ? (String) aparser.getAppletValue("format", null, true)
               : null);
       protocol = AppletFormatAdapter.checkProtocol(file);
       try
@@ -657,8 +657,8 @@ public class Jalview implements ApplicationSingletonI
         System.out.println("CMD [nocalculation] executed successfully!");
       }
 
-      AlignFrame af = new FileLoader(!headless).LoadFileWaitTillLoaded(file,
-              protocol, format);
+      af = new FileLoader(!headless).LoadFileWaitTillLoaded(file, protocol,
+              format);
       if (af == null)
       {
         System.out.println("error");
@@ -819,10 +819,11 @@ public class Jalview implements ApplicationSingletonI
         // TODO - load PDB structure(s) to alignment JAL-629
         // (associate with identical sequence in alignment, or a specified
         // sequence)
-        
+
         if (isJS)
         {
-          app.load(af);
+          jsApp = new JalviewJSApp(aparser);
+          jsApp.load(af);
         }
         else
         /**
@@ -846,67 +847,67 @@ public class Jalview implements ApplicationSingletonI
         }
       }
     }
-
-    AlignFrame startUpAlframe = null;
-    // We'll only open the default file if the desktop is visible.
-    // And the user
-    // ////////////////////
-
-    if (!isJS && !headless && file == null
-            && Cache.getDefault("SHOW_STARTUP_FILE", true))
-    /**
-     * Java only
-     * 
-     * @j2sIgnore
-     */
+    else
     {
-      file = Cache.getDefault("STARTUP_FILE",
-              Cache.getDefault("www.jalview.org", "http://www.jalview.org")
-                      + "/examples/exampleFile_2_7.jar");
-      if (file.equals(
-              "http://www.jalview.org/examples/exampleFile_2_3.jar"))
+      if (!isJS && !headless && Cache.getDefault("SHOW_STARTUP_FILE", true))
+      /**
+       * Java only
+       * 
+       * @j2sIgnore
+       */
       {
-        // hardwire upgrade of the startup file
-        file.replace("_2_3.jar", "_2_7.jar");
-        // and remove the stale setting
-        Cache.removeProperty("STARTUP_FILE");
-      }
 
-      protocol = DataSourceType.FILE;
+        // We'll only open the default file if the desktop is visible.
+        // And the user
+        // ////////////////////
 
-      if (file.indexOf("http:") > -1)
-      {
-        protocol = DataSourceType.URL;
-      }
+        file = Cache.getDefault("STARTUP_FILE",
+                Cache.getDefault("www.jalview.org",
+                        "http://www.jalview.org")
+                        + "/examples/exampleFile_2_7.jar");
+        if (file.equals(
+                "http://www.jalview.org/examples/exampleFile_2_3.jar"))
+        {
+          // hardwire upgrade of the startup file
+          file.replace("_2_3.jar", "_2_7.jar");
+          // and remove the stale setting
+          Cache.removeProperty("STARTUP_FILE");
+        }
 
-      if (file.endsWith(".jar"))
-      {
-        format = FileFormat.Jalview;
-      }
-      else
-      {
-        try
+        protocol = DataSourceType.FILE;
+
+        if (file.indexOf("http:") > -1)
         {
-          format = new IdentifyFile().identify(file, protocol);
-        } catch (FileFormatException e)
+          protocol = DataSourceType.URL;
+        }
+
+        if (file.endsWith(".jar"))
         {
-          // TODO what?
+          format = FileFormat.Jalview;
+        }
+        else
+        {
+          try
+          {
+            format = new IdentifyFile().identify(file, protocol);
+          } catch (FileFormatException e)
+          {
+            // TODO what?
+          }
         }
-      }
 
-      startUpAlframe = fileLoader.LoadFileWaitTillLoaded(file, protocol,
-              format);
+        af = fileLoader.LoadFileWaitTillLoaded(file, protocol, format);
 
+      }
     }
-
     // extract groovy arguments before anything else.
     // Once all other stuff is done, execute any groovy scripts (in order)
-    if (groovyscript != null)
+    if (!isJS && groovyscript != null)
     {
       if (Cache.groovyJarsPresent())
       {
         System.out.println("Executing script " + groovyscript);
-        executeGroovyScript(groovyscript, startUpAlframe);
+        executeGroovyScript(groovyscript, af);
       }
       else
       {
@@ -928,7 +929,7 @@ public class Jalview implements ApplicationSingletonI
 
     if (isJS && isStartup)
     {
-      app.callInitCallback();
+      jsApp.callInitCallback();
     }
   }
   
@@ -1007,7 +1008,7 @@ public class Jalview implements ApplicationSingletonI
                 "Creating image map: " + imageFile.getAbsolutePath());
         continue;
       }
-      if (!Platform.isJS())
+      if (!Platform.isJS()) /** @j2sIgnore */
       {
         // skipping outputFormat?
         System.out.println("Unknown arg: " + outputFormat);      
@@ -1024,13 +1025,12 @@ public class Jalview implements ApplicationSingletonI
                   + " format!!");
         }
       }
-      while (aparser.getSize() > 0)
-      {
-        System.out.println("Unknown arg: " + aparser.nextValue());
-      }
       break;
     }
-
+    while (aparser.getSize() > 0)
+    {
+      System.out.println("Unknown arg: " + aparser.nextValue());
+    }
   }
 
   private static void showUsage()
@@ -1267,32 +1267,10 @@ public class Jalview implements ApplicationSingletonI
   }
 
   
-  /**
-   * Get the SwingJS applet ID and combine that with the frameType
-   * 
-   * @param frameType
-   *          "alignment", "desktop", etc., or null
-   * @return
-   */
-  public static String getAppID(String frameType)
-  {
-    return (Platform.isJS() ? getInstance().app.getAppID(frameType) : null);
-  }
-
-  public String doSendCallback(Object callback, Object[] data) {
-    return (Platform.isJS() ? app.doSendCallback(callback, data) : null);
-  }
-
   public void notifyWorker(AlignCalcWorkerI worker, String status)
   {
     // System.out.println("Jalview worker " + worker.getClass().getSimpleName()
     // + " " + status);
   }
-
-  public Object arrayToSeparatorList(String[] seqs)
-  {
-    return (Platform.isJS() ? app.arrayToSeparatorList(seqs) : null);
-  }
   
 }