From e67769b1bb24f3817d67d2acaebbcb0edc73381b Mon Sep 17 00:00:00 2001 From: BobHanson Date: Mon, 1 Jun 2020 16:39:06 -0500 Subject: [PATCH] JAL-3262 from JAL-3253-applet AppletParams + Cache.setPropertyNoSave --- src/jalview/bin/AppletParams.java | 447 +++++++++++++++++++++++++++++++++++++ src/jalview/bin/Cache.java | 52 +++++ 2 files changed, 499 insertions(+) create mode 100644 src/jalview/bin/AppletParams.java diff --git a/src/jalview/bin/AppletParams.java b/src/jalview/bin/AppletParams.java new file mode 100644 index 0000000..6a23c39 --- /dev/null +++ b/src/jalview/bin/AppletParams.java @@ -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 +{ + + 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 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); + } + + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + public AppletParams(String outerHTML) + { + String[] tokens = outerHTML.split(" 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 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; + } + // + 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(" \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " "); + } + +} \ No newline at end of file diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index 788cb02..c532b7a 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -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; + } + + + } -- 1.7.10.2