Merge branch 'Jalview-JS/jim/JAL-3253-JAL-3418' into Jalview-JS/JAL-3253-applet
[jalview.git] / src / jalview / bin / AppletParams.java
index 8beee03..6a23c39 100644 (file)
@@ -1,7 +1,10 @@
 package jalview.bin;
 
+import jalview.gui.Preferences;
+
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Vector;
 
 /**
  * Collection of all known applet tags from JalviewLite
@@ -14,7 +17,8 @@ public class AppletParams extends HashMap<String, String>
 {
 
   private final static String[] params = { "alignpdbfiles",
-      "ANNOTATIONCOLOUR_MAX", "ANNOTATIONCOLOUR_MIN", "annotations",
+      Preferences.ANNOTATIONCOLOUR_MAX, Preferences.ANNOTATIONCOLOUR_MIN,
+      "annotations",
       "APPLICATION_URL", "automaticScrolling", "centrecolumnlabels",
       "debug", "defaultColour", "defaultColourNuc", "defaultColourProt",
       "embedded", "enableSplitFrame", "externalstructureviewer", "features",
@@ -97,6 +101,306 @@ public class AppletParams extends HashMap<String, String>
     put("_code", code);
   }
 
+  public AppletParams()
+  {
+    // TODO Auto-generated constructor stub
+  }
+
+  public static AppletParams getAppletParams(String[] args,
+          Vector<String> vargs)
+  {
+    AppletParams appletParams = new AppletParams();
+    String resourcePath = null;
+    for (int i = args.length; --i > 0;) // > 0 is correct, not >=0
+    {
+      if (args[i].startsWith("name=\"Info.resourcePath\""))
+      {
+        resourcePath = getAttr(args[i], "value");
+        if (resourcePath.length() > 0 && !resourcePath.endsWith("/"))
+        {
+          resourcePath += "/";
+        }
+        break;
+      }
+    }
+    for (int i = 1; i < args.length; i++)
+    {
+      String arg = args[i].trim();
+      if (arg.startsWith("name="))
+      {
+        String prefName = getAttr(arg, "name");
+        String appletName = prefName.toLowerCase();
+        String argName = prefName;
+        String value = getAttr(arg, "value");
+
+        // note that Application arguments ARE case-sensitive, but
+        // Applet.getParameter() is not.
+
+        switch (appletName)
+        {
+
+        case "file":
+          argName = "open";
+          appletName = null;
+          value = resourcePath + value;
+          break;
+        case "file2":
+          argName = "open2";
+          prefName = null;
+          value = resourcePath + value;
+          break;
+        case "features":
+        case "jnetfile":
+        case "jpredfile":
+        case "pdbfile":
+        case "scorefile":
+        case "sequence":
+          // setting argName to null indicates that we want
+          // JalviewAppLoader to take care of this.
+          prefName = argName = null;
+          value = resourcePath + value;
+          break;
+        case "tree":
+        case "treefile":
+          // setting appletName to null indicates that we want
+          // Jalview.doMain to taken care of this as Jalview args
+          argName = "tree";
+          appletName = null;
+          value = resourcePath + value;
+          break;
+
+        // non-loading preferences
+
+        case "defaultcolour":
+          prefName = Preferences.DEFAULT_COLOUR;
+          break;
+        case "defaultcolournuc":
+          prefName = Preferences.DEFAULT_COLOUR_NUC;
+          break;
+        case "defaultcolourprot":
+          prefName = Preferences.DEFAULT_COLOUR_PROT;
+          break;
+        case "annotationcolour_max":
+          prefName = Preferences.ANNOTATIONCOLOUR_MAX;
+          break;
+        case "annotationcolour_min":
+          prefName = Preferences.ANNOTATIONCOLOUR_MIN;
+          break;
+        case "enablesplitframe":
+          prefName = Preferences.ENABLE_SPLIT_FRAME;
+          break;
+        case "centrecolumnlabels":
+          prefName = Preferences.CENTRE_COLUMN_LABELS;
+          break;
+        case "sortby":
+          prefName = Preferences.SORT_ALIGNMENT; // id, etc.
+          break;
+        case "normalisesequencelogo":
+          prefName = Preferences.NORMALISE_CONSENSUS_LOGO;
+          break;
+        case "relaxedidmatch":
+          prefName = Preferences.RELAXEDSEQIDMATCHING;
+          break;
+        case "scaleproteinascdna":
+          prefName = Preferences.SCALE_PROTEIN_TO_CDNA;
+          break;
+        case "userdefinedcolour":
+          argName = "colour";
+          prefName = Preferences.USER_DEFINED_COLOURS;
+          break;
+        case "wrap":
+          prefName = Preferences.WRAP_ALIGNMENT;
+          break;
+
+        // implemented; not tested:
+
+        case "oninit":
+          prefName = null;
+          break;
+        case "annotations":
+          value = resourcePath + value;
+          argName = null;
+          break;
+        case "hidefeaturegroups":
+          // TODO
+          break;
+        case "pdbseq":
+          argName = prefName = null;
+          break;
+        case "sortbytree":
+          prefName = Preferences.SORT_BY_TREE;
+          value = checkTF(value);
+          appletName = null; // taken care of by Jalview
+          break;
+        case "format":
+          break;
+        case "alignpdbfiles":
+          argName = prefName = null;
+          break;
+        case "separator":
+          break;
+
+        // TODO: probably not relevant?
+
+        case "rgb":
+          prefName = null; // TODO no background for application?
+          break;
+        case "externalstructureviewer":
+          break;
+        case "application_url":
+          break;
+        case "automaticscrolling":
+          break;
+        case "heightscale":
+          break;
+        case "jalviewhelpurl":
+          break;
+        case "label":
+          break;
+        case "linklabel_":
+          prefName = "linkLabel_";
+          break;
+        case "linklabel_1":
+          prefName = "linkLabel_1";
+          break;
+        case "linkurl_":
+          prefName = "linkURL_";
+          break;
+
+        // unknown:
+
+        case "nojmol":
+        case "normaliselogo":
+        case "resolvetocodebase":
+        case "uppercase":
+        case "widthscale":
+        case "windowheight":
+        case "windowwidth":
+          argName = prefName = null;
+          break;
+
+        // TRUE/FALSE
+
+        case "debug":
+          value = checkTF(value);
+          break;
+        case "embedded":
+          value = checkTF(value);
+          break;
+        case "showbutton":
+          value = checkTF(value);
+          break;
+        case "showannotation":
+          prefName = Preferences.SHOW_ANNOTATIONS;
+          value = checkTF(value);
+          break;
+        case "showconsensus":
+          prefName = Preferences.SHOW_CONSENSUS_LOGO;
+          value = checkTF(value);
+          break;
+        case "showconsensushistogram":
+          prefName = Preferences.SHOW_CONSENSUS_HISTOGRAM;
+          value = checkTF(value);
+          break;
+        case "showconservation":
+          prefName = Preferences.SHOW_CONSERVATION;
+          value = checkTF(value);
+          break;
+        case "showgroupconsensus":
+          prefName = Preferences.SHOW_GROUP_CONSENSUS;
+          value = checkTF(value);
+          break;
+        case "showgroupconservation":
+          prefName = Preferences.SHOW_GROUP_CONSERVATION;
+          value = checkTF(value);
+          break;
+        case "showoccupancy":
+          prefName = Preferences.SHOW_OCCUPANCY;
+          value = checkTF(value);
+          break;
+        case "showquality":
+          prefName = Preferences.SHOW_QUALITY;
+          value = checkTF(value);
+          break;
+        case "showsequencelogo":
+          prefName = Preferences.SHOW_CONSENSUS_LOGO;
+          value = checkTF(value);
+          break;
+        case "showfeaturegroups":
+          value = checkTF(value);
+          break;
+        case "showfeaturesettings":
+          value = checkTF(value);
+          break;
+        case "showfullid":
+          value = checkTF(value);
+          break;
+        case "showtreebootstraps":
+          value = checkTF(value);
+          break;
+        case "showtreedistances":
+          value = checkTF(value);
+          break;
+        case "showunconserved":
+          prefName = Preferences.SHOW_UNCONSERVED;
+          value = checkTF(value);
+          break;
+        case "showunlinkedtreenodes":
+          value = checkTF(value);
+          break;
+        default:
+          if (appletName.startsWith("pdbfile")
+                  || appletName.startsWith("sequence") && Character.isDigit(
+                          appletName.charAt(appletName.length() - 1)))
+          {
+            // could be pdbFile2, for example
+            prefName = argName = null;
+            value = resourcePath + value;
+            break;
+          }
+          // or one of the app preference names
+          break;
+        }
+        // put name and value into application args
+        if (value != null && argName != null)
+        {
+          vargs.add(argName);
+          if (value != "true")
+          {
+            vargs.add(value);
+          }
+        }
+        if (value == null)
+        {
+          value = "false";
+        }
+        System.out.println("AppletParams propName=" + prefName + " argName="
+                + argName + " appletName="
+                + appletName + " value=" + value);
+        if (appletName != null)
+        {
+          appletParams.put(appletName, value);
+        }
+        if (prefName != null)
+        {
+          Cache.setPropertyNoSave(prefName, value);
+        }
+      }
+    }
+    return appletParams;
+  }
+
+  /**
+   * Check for a single-argument option.
+   * 
+   * @param value
+   * @return "true" or null
+   */
+  private static String checkTF(String value)
+  {
+    return (value.toLowerCase() == "true" ? "true" : null);
+  }
+
   /**
    * Crude applet innerHTML parser
    * 
@@ -104,7 +408,7 @@ public class AppletParams extends HashMap<String, String>
    * @param attr
    * @return
    */
-  private String getAttr(String tag, String attr)
+  private static String getAttr(String tag, String attr)
   {
     int pt = tag.indexOf(attr + "=\"");
     if (pt < 0)