JAL-3262 from JAL-3253-applet AppletParams + Cache.setPropertyNoSave
authorBobHanson <hansonr@stolaf.edu>
Mon, 1 Jun 2020 21:39:06 +0000 (16:39 -0500)
committerBobHanson <hansonr@stolaf.edu>
Mon, 1 Jun 2020 21:39:06 +0000 (16:39 -0500)
src/jalview/bin/AppletParams.java [new file with mode: 0644]
src/jalview/bin/Cache.java

diff --git a/src/jalview/bin/AppletParams.java b/src/jalview/bin/AppletParams.java
new file mode 100644 (file)
index 0000000..6a23c39
--- /dev/null
@@ -0,0 +1,447 @@
+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
+ * 
+ * @author hansonr
+ *
+ */
+@SuppressWarnings("serial")
+public class AppletParams extends HashMap<String, String>
+{
+
+  private final static String[] params = { "alignpdbfiles",
+      Preferences.ANNOTATIONCOLOUR_MAX, Preferences.ANNOTATIONCOLOUR_MIN,
+      "annotations",
+      "APPLICATION_URL", "automaticScrolling", "centrecolumnlabels",
+      "debug", "defaultColour", "defaultColourNuc", "defaultColourProt",
+      "embedded", "enableSplitFrame", "externalstructureviewer", "features",
+      "file", "file2", "format", "heightScale", "hidefeaturegroups",
+      "jalviewhelpurl", "jnetfile", "jpredfile", "label", "linkLabel_",
+      "linkLabel_1", "linkURL_", "nojmol", "normaliseLogo",
+      "normaliseSequenceLogo", "oninit", "PDBFILE", "PDBSEQ",
+      "relaxedidmatch", "resolvetocodebase", "RGB", "scaleProteinAsCdna",
+      "scoreFile", "separator", "sequence", "showAnnotation", "showbutton",
+      "showConsensus", "showConsensusHistogram", "showConservation",
+      "showfeaturegroups", "showFeatureSettings", "showFullId",
+      "showGroupConsensus", "showGroupConservation", "showOccupancy",
+      "showQuality", "showSequenceLogo", "showTreeBootstraps",
+      "showTreeDistances", "showUnconserved", "showUnlinkedTreeNodes",
+      "sortBy", "sortByTree", "tree", "treeFile", "upperCase",
+      "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);
+  }
+
+  // <applet
+  // code="jalview.bin.JalviewLite" width="140" height="35"
+  // archive="jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar">
+  // <param name="permissions" value="sandbox"/>
+  // <param name="file" value="uniref50.fa"/>
+  // <param name="treeFile" value="ferredoxin.nw"/>
+  // <param name="userDefinedColour" value="C=yellow; R,K,H=FF5555;
+  // D,E=5555FF"/>
+  // <param name="sortByTree" value="True"/>
+  // <param name="showSequenceLogo" value="true"/>
+  // <param name="showGroupConsensus" value="true"/>
+  // <param name="showFullId" value="false"/>
+  // <param name="linkLabel_1" value="Uniprot"/>
+  // <param name="linkUrl_1"
+  // value="http://www.uniprot.org/uniprot/$SEQUENCE_ID$"/>
+  // <param name="linkLabel_2" value="EMBL-EBI Search"/>
+  // <param name="linkUrl_2"
+  // value="http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$"/>
+  // <param name="APPLICATION_URL"
+  // value="http://www.jalview.org/services/launchApp"/>
+  // </applet>
+  //
+  public AppletParams(String outerHTML)
+  {
+    String[] tokens = outerHTML.split("<param");
+    outerHTML = tokens[0];
+    String code = getAttr(outerHTML, "code");
+    if (!code.equals("jalview.bin.JalviewLite"))
+    {
+      return;
+    }
+    for (int i = tokens.length; --i > 0;)
+    {
+      String param = tokens[i];
+      String key = getAttr(param, "name");
+      if (key != null)
+      {
+        String value = getAttr(param, "value");
+        System.out.println("AppletParams " + key + " = \"" + value + "\"");
+        put(key, value);
+      }
+    }
+    put("_width", getAttr(outerHTML, "width"));
+    put("_height", getAttr(outerHTML, "height"));
+    put("_id", getAttr(outerHTML, "id"));
+    put("_name", getAttr(outerHTML, "name"));
+    put("_archive", getAttr(outerHTML, "archive"));
+    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
+   * 
+   * @param tag
+   * @param attr
+   * @return
+   */
+  private static String getAttr(String tag, String attr)
+  {
+    int pt = tag.indexOf(attr + "=\"");
+    if (pt < 0)
+    {
+      System.out
+              .println("AppletParams did not read " + attr + " in " + tag);
+      return null;
+    }
+    // <param name="sortByTree" value="True"/>
+    int pt1 = pt + attr.length() + 2;
+    int pt2 = tag.indexOf("\"", pt1);
+    return (pt < 0 ? null : tag.substring(pt1, pt2));
+  }
+
+  public static void main(String[] args)
+  {
+    new AppletParams("<applet\r\n"
+            + "    code=\"jalview.bin.JalviewLite\" width=\"140\" height=\"35\"\r\n"
+            + "    archive=\"jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar\">  \r\n"
+            + "  <param name=\"permissions\" value=\"sandbox\"/>\r\n"
+            + "  <param name=\"file\" value=\"uniref50.fa\"/>\r\n"
+            + "  <param name=\"treeFile\" value=\"ferredoxin.nw\"/>\r\n"
+            + "  <param name=\"userDefinedColour\" value=\"C=yellow; R,K,H=FF5555; D,E=5555FF\"/>\r\n"
+            + "  <param name=\"sortByTree\" value=\"True\"/>\r\n"
+            + "  <param name=\"showSequenceLogo\" value=\"true\"/>\r\n"
+            + "  <param name=\"showGroupConsensus\" value=\"true\"/>\r\n"
+            + "  <param name=\"showFullId\" value=\"false\"/>\r\n"
+            + "    <param name=\"linkLabel_1\" value=\"Uniprot\"/>\r\n"
+            + "    <param name=\"linkUrl_1\" value=\"http://www.uniprot.org/uniprot/$SEQUENCE_ID$\"/>\r\n"
+            + "    <param name=\"linkLabel_2\" value=\"EMBL-EBI Search\"/>\r\n"
+            + "    <param name=\"linkUrl_2\" value=\"http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$\"/>\r\n"
+            + "    <param name=\"APPLICATION_URL\" value=\"http://www.jalview.org/services/launchApp\"/>\r\n"
+            + "     </applet>");
+  }
+
+}
\ No newline at end of file
index 788cb02..c532b7a 100755 (executable)
@@ -1174,4 +1174,56 @@ public class Cache
     // eg 'built from Source' or update channel
     return jalview.bin.Cache.getDefault("INSTALLATION", "unknown");
   }
+  
+  /**
+   * 
+   * For AppletParams
+   * 
+   * Sets a property value for the running application, without saving it to the
+   * properties file
+   * 
+   * @param key
+   * @param obj
+   */
+  public static void setPropertyNoSave(String key, String obj)
+  {
+    /* for JalviewJS should be 
+    
+    getInstance().
+    
+   */
+    setPropertyImpl(key, obj, false);
+  }
+
+  /**
+   * Sets a property value, and optionally also saves the current properties to
+   * file
+   * 
+   * @param key
+   * @param obj
+   * @param andSave
+   * @return
+   */
+  private /* for JalviewJS should not be static */ static Object setPropertyImpl(String key, String obj, boolean andSave)
+  {
+    Object oldValue = null;
+    try
+    {
+      oldValue = applicationProperties.setProperty(key, obj);
+      if (andSave && !propsAreReadOnly && propertiesFile != null)
+      {
+        FileOutputStream out = new FileOutputStream(propertiesFile);
+        applicationProperties.store(out, "---JalviewX Properties File---");
+        out.close();
+      }
+    } catch (Exception ex)
+    {
+      System.out.println(
+              "Error setting property: " + key + " " + obj + "\n" + ex);
+    }
+    return oldValue;
+  }
+
+  
+  
 }