Refactoring of Jalview, JalviewAppLoader, JalviewApp, various
[jalview.git] / src / jalview / bin / AppletParams.java
index 6a23c39..a6c87b1 100644 (file)
@@ -1,10 +1,11 @@
-package jalview.bin;
 
-import jalview.gui.Preferences;
+package jalview.bin;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
-import java.util.Vector;
+
+import jalview.gui.Preferences;
 
 /**
  * Collection of all known applet tags from JalviewLite
@@ -13,7 +14,7 @@ import java.util.Vector;
  *
  */
 @SuppressWarnings("serial")
-public class AppletParams extends HashMap<String, String>
+public class AppletParams extends HashMap<String, Object>
 {
 
   private final static String[] params = { "alignpdbfiles",
@@ -37,18 +38,10 @@ public class AppletParams extends HashMap<String, String>
       "userDefinedColour", "widthScale", "windowHeight", "windowWidth",
       "wrap", };
 
-  public AppletParams(Map<String, String> info)
-  {
-    for (int i = params.length; --i >= 0;)
-    {
-      put(params[i], info.get(params[i]));
-    }
-  }
-
   public String getParam(String param, String def)
   {
-    String val = get(param);
-    return (val != null ? val : def);
+    Object val = get(param);
+    return (val != null ? val.toString() : def);
   }
 
   // <applet
@@ -103,14 +96,40 @@ public class AppletParams extends HashMap<String, String>
 
   public AppletParams()
   {
-    // TODO Auto-generated constructor stub
+  }
+
+  public static AppletParams getAppletParams(Map<String, Object> map,
+          List<String> vargs)
+  {
+    AppletParams appletParams = new AppletParams();
+    String resourcePath = getString(map, "resourcePath");
+    if (resourcePath == null)
+      resourcePath = "";
+    if (resourcePath.length() > 0 && !resourcePath.endsWith("/"))
+    {
+      resourcePath += "/";
+    }
+    for (int i = params.length; --i >= 0;)
+    {
+      String prefName = params[i];
+      Object value = map.get(prefName);
+      if (value != null)
+        addParam(vargs, prefName, value, appletParams, resourcePath);
+    }
+    return appletParams;
+  }
+
+  private static String getString(Map<String, Object> map, String key)
+  {
+    Object o = map.get(key);  
+    return (o == null ? null : o.toString());
   }
 
   public static AppletParams getAppletParams(String[] args,
-          Vector<String> vargs)
+          List<String> vargs)
   {
     AppletParams appletParams = new AppletParams();
-    String resourcePath = null;
+    String resourcePath = "";
     for (int i = args.length; --i > 0;) // > 0 is correct, not >=0
     {
       if (args[i].startsWith("name=\"Info.resourcePath\""))
@@ -129,265 +148,271 @@ public class AppletParams extends HashMap<String, String>
       if (arg.startsWith("name="))
       {
         String prefName = getAttr(arg, "name");
-        String appletName = prefName.toLowerCase();
-        String argName = prefName;
         String value = getAttr(arg, "value");
+        addParam(vargs, prefName, value, appletParams, resourcePath);
+      }
+    }
+    return appletParams;
+  }
 
-        // note that Application arguments ARE case-sensitive, but
-        // Applet.getParameter() is not.
+  private static void addParam(List<String> vargs, String prefName,
+          Object value, AppletParams appletParams, String resourcePath)
+  {
 
-        switch (appletName)
-        {
+    // note that Application arguments ARE case-sensitive, but
+    // Applet.getParameter() is not.
 
-        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;
+    String appletName = prefName.toLowerCase();
+    String argName = prefName;
+    switch (appletName)
+    {
 
-        // non-loading preferences
+    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;
 
-        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;
+    // non-loading preferences
 
-        // implemented; not tested:
+    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;
 
-        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;
+    // implemented; not tested:
 
-        // TODO: probably not relevant?
+    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;
 
-        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;
+    // TODO: probably not relevant?
 
-        // unknown:
+    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;
 
-        case "nojmol":
-        case "normaliselogo":
-        case "resolvetocodebase":
-        case "uppercase":
-        case "widthscale":
-        case "windowheight":
-        case "windowwidth":
-          argName = prefName = null;
-          break;
+    // unknown:
 
-        // TRUE/FALSE
+    case "nojmol":
+    case "normaliselogo":
+    case "resolvetocodebase":
+    case "uppercase":
+    case "widthscale":
+    case "windowheight":
+    case "windowwidth":
+      argName = prefName = null;
+      break;
 
-        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);
-        }
+    // 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.toString());
+      }
+    }
+    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.toString());
     }
-    return appletParams;
   }
 
   /**
@@ -396,9 +421,9 @@ public class AppletParams extends HashMap<String, String>
    * @param value
    * @return "true" or null
    */
-  private static String checkTF(String value)
+  private static String checkTF(Object value)
   {
-    return (value.toLowerCase() == "true" ? "true" : null);
+    return (("" + value).toLowerCase() == "true" ? "true" : null);
   }
 
   /**