JAL-3262 from JAL-3253-applet AppletParams + Cache.setPropertyNoSave
[jalview.git] / src / jalview / bin / AppletParams.java
1 package jalview.bin;
2
3 import jalview.gui.Preferences;
4
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Vector;
8
9 /**
10  * Collection of all known applet tags from JalviewLite
11  * 
12  * @author hansonr
13  *
14  */
15 @SuppressWarnings("serial")
16 public class AppletParams extends HashMap<String, String>
17 {
18
19   private final static String[] params = { "alignpdbfiles",
20       Preferences.ANNOTATIONCOLOUR_MAX, Preferences.ANNOTATIONCOLOUR_MIN,
21       "annotations",
22       "APPLICATION_URL", "automaticScrolling", "centrecolumnlabels",
23       "debug", "defaultColour", "defaultColourNuc", "defaultColourProt",
24       "embedded", "enableSplitFrame", "externalstructureviewer", "features",
25       "file", "file2", "format", "heightScale", "hidefeaturegroups",
26       "jalviewhelpurl", "jnetfile", "jpredfile", "label", "linkLabel_",
27       "linkLabel_1", "linkURL_", "nojmol", "normaliseLogo",
28       "normaliseSequenceLogo", "oninit", "PDBFILE", "PDBSEQ",
29       "relaxedidmatch", "resolvetocodebase", "RGB", "scaleProteinAsCdna",
30       "scoreFile", "separator", "sequence", "showAnnotation", "showbutton",
31       "showConsensus", "showConsensusHistogram", "showConservation",
32       "showfeaturegroups", "showFeatureSettings", "showFullId",
33       "showGroupConsensus", "showGroupConservation", "showOccupancy",
34       "showQuality", "showSequenceLogo", "showTreeBootstraps",
35       "showTreeDistances", "showUnconserved", "showUnlinkedTreeNodes",
36       "sortBy", "sortByTree", "tree", "treeFile", "upperCase",
37       "userDefinedColour", "widthScale", "windowHeight", "windowWidth",
38       "wrap", };
39
40   public AppletParams(Map<String, String> info)
41   {
42     for (int i = params.length; --i >= 0;)
43     {
44       put(params[i], info.get(params[i]));
45     }
46   }
47
48   public String getParam(String param, String def)
49   {
50     String val = get(param);
51     return (val != null ? val : def);
52   }
53
54   // <applet
55   // code="jalview.bin.JalviewLite" width="140" height="35"
56   // archive="jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar">
57   // <param name="permissions" value="sandbox"/>
58   // <param name="file" value="uniref50.fa"/>
59   // <param name="treeFile" value="ferredoxin.nw"/>
60   // <param name="userDefinedColour" value="C=yellow; R,K,H=FF5555;
61   // D,E=5555FF"/>
62   // <param name="sortByTree" value="True"/>
63   // <param name="showSequenceLogo" value="true"/>
64   // <param name="showGroupConsensus" value="true"/>
65   // <param name="showFullId" value="false"/>
66   // <param name="linkLabel_1" value="Uniprot"/>
67   // <param name="linkUrl_1"
68   // value="http://www.uniprot.org/uniprot/$SEQUENCE_ID$"/>
69   // <param name="linkLabel_2" value="EMBL-EBI Search"/>
70   // <param name="linkUrl_2"
71   // value="http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$"/>
72   // <param name="APPLICATION_URL"
73   // value="http://www.jalview.org/services/launchApp"/>
74   // </applet>
75   //
76   public AppletParams(String outerHTML)
77   {
78     String[] tokens = outerHTML.split("<param");
79     outerHTML = tokens[0];
80     String code = getAttr(outerHTML, "code");
81     if (!code.equals("jalview.bin.JalviewLite"))
82     {
83       return;
84     }
85     for (int i = tokens.length; --i > 0;)
86     {
87       String param = tokens[i];
88       String key = getAttr(param, "name");
89       if (key != null)
90       {
91         String value = getAttr(param, "value");
92         System.out.println("AppletParams " + key + " = \"" + value + "\"");
93         put(key, value);
94       }
95     }
96     put("_width", getAttr(outerHTML, "width"));
97     put("_height", getAttr(outerHTML, "height"));
98     put("_id", getAttr(outerHTML, "id"));
99     put("_name", getAttr(outerHTML, "name"));
100     put("_archive", getAttr(outerHTML, "archive"));
101     put("_code", code);
102   }
103
104   public AppletParams()
105   {
106     // TODO Auto-generated constructor stub
107   }
108
109   public static AppletParams getAppletParams(String[] args,
110           Vector<String> vargs)
111   {
112     AppletParams appletParams = new AppletParams();
113     String resourcePath = null;
114     for (int i = args.length; --i > 0;) // > 0 is correct, not >=0
115     {
116       if (args[i].startsWith("name=\"Info.resourcePath\""))
117       {
118         resourcePath = getAttr(args[i], "value");
119         if (resourcePath.length() > 0 && !resourcePath.endsWith("/"))
120         {
121           resourcePath += "/";
122         }
123         break;
124       }
125     }
126     for (int i = 1; i < args.length; i++)
127     {
128       String arg = args[i].trim();
129       if (arg.startsWith("name="))
130       {
131         String prefName = getAttr(arg, "name");
132         String appletName = prefName.toLowerCase();
133         String argName = prefName;
134         String value = getAttr(arg, "value");
135
136         // note that Application arguments ARE case-sensitive, but
137         // Applet.getParameter() is not.
138
139         switch (appletName)
140         {
141
142         case "file":
143           argName = "open";
144           appletName = null;
145           value = resourcePath + value;
146           break;
147         case "file2":
148           argName = "open2";
149           prefName = null;
150           value = resourcePath + value;
151           break;
152         case "features":
153         case "jnetfile":
154         case "jpredfile":
155         case "pdbfile":
156         case "scorefile":
157         case "sequence":
158           // setting argName to null indicates that we want
159           // JalviewAppLoader to take care of this.
160           prefName = argName = null;
161           value = resourcePath + value;
162           break;
163         case "tree":
164         case "treefile":
165           // setting appletName to null indicates that we want
166           // Jalview.doMain to taken care of this as Jalview args
167           argName = "tree";
168           appletName = null;
169           value = resourcePath + value;
170           break;
171
172         // non-loading preferences
173
174         case "defaultcolour":
175           prefName = Preferences.DEFAULT_COLOUR;
176           break;
177         case "defaultcolournuc":
178           prefName = Preferences.DEFAULT_COLOUR_NUC;
179           break;
180         case "defaultcolourprot":
181           prefName = Preferences.DEFAULT_COLOUR_PROT;
182           break;
183         case "annotationcolour_max":
184           prefName = Preferences.ANNOTATIONCOLOUR_MAX;
185           break;
186         case "annotationcolour_min":
187           prefName = Preferences.ANNOTATIONCOLOUR_MIN;
188           break;
189         case "enablesplitframe":
190           prefName = Preferences.ENABLE_SPLIT_FRAME;
191           break;
192         case "centrecolumnlabels":
193           prefName = Preferences.CENTRE_COLUMN_LABELS;
194           break;
195         case "sortby":
196           prefName = Preferences.SORT_ALIGNMENT; // id, etc.
197           break;
198         case "normalisesequencelogo":
199           prefName = Preferences.NORMALISE_CONSENSUS_LOGO;
200           break;
201         case "relaxedidmatch":
202           prefName = Preferences.RELAXEDSEQIDMATCHING;
203           break;
204         case "scaleproteinascdna":
205           prefName = Preferences.SCALE_PROTEIN_TO_CDNA;
206           break;
207         case "userdefinedcolour":
208           argName = "colour";
209           prefName = Preferences.USER_DEFINED_COLOURS;
210           break;
211         case "wrap":
212           prefName = Preferences.WRAP_ALIGNMENT;
213           break;
214
215         // implemented; not tested:
216
217         case "oninit":
218           prefName = null;
219           break;
220         case "annotations":
221           value = resourcePath + value;
222           argName = null;
223           break;
224         case "hidefeaturegroups":
225           // TODO
226           break;
227         case "pdbseq":
228           argName = prefName = null;
229           break;
230         case "sortbytree":
231           prefName = Preferences.SORT_BY_TREE;
232           value = checkTF(value);
233           appletName = null; // taken care of by Jalview
234           break;
235         case "format":
236           break;
237         case "alignpdbfiles":
238           argName = prefName = null;
239           break;
240         case "separator":
241           break;
242
243         // TODO: probably not relevant?
244
245         case "rgb":
246           prefName = null; // TODO no background for application?
247           break;
248         case "externalstructureviewer":
249           break;
250         case "application_url":
251           break;
252         case "automaticscrolling":
253           break;
254         case "heightscale":
255           break;
256         case "jalviewhelpurl":
257           break;
258         case "label":
259           break;
260         case "linklabel_":
261           prefName = "linkLabel_";
262           break;
263         case "linklabel_1":
264           prefName = "linkLabel_1";
265           break;
266         case "linkurl_":
267           prefName = "linkURL_";
268           break;
269
270         // unknown:
271
272         case "nojmol":
273         case "normaliselogo":
274         case "resolvetocodebase":
275         case "uppercase":
276         case "widthscale":
277         case "windowheight":
278         case "windowwidth":
279           argName = prefName = null;
280           break;
281
282         // TRUE/FALSE
283
284         case "debug":
285           value = checkTF(value);
286           break;
287         case "embedded":
288           value = checkTF(value);
289           break;
290         case "showbutton":
291           value = checkTF(value);
292           break;
293         case "showannotation":
294           prefName = Preferences.SHOW_ANNOTATIONS;
295           value = checkTF(value);
296           break;
297         case "showconsensus":
298           prefName = Preferences.SHOW_CONSENSUS_LOGO;
299           value = checkTF(value);
300           break;
301         case "showconsensushistogram":
302           prefName = Preferences.SHOW_CONSENSUS_HISTOGRAM;
303           value = checkTF(value);
304           break;
305         case "showconservation":
306           prefName = Preferences.SHOW_CONSERVATION;
307           value = checkTF(value);
308           break;
309         case "showgroupconsensus":
310           prefName = Preferences.SHOW_GROUP_CONSENSUS;
311           value = checkTF(value);
312           break;
313         case "showgroupconservation":
314           prefName = Preferences.SHOW_GROUP_CONSERVATION;
315           value = checkTF(value);
316           break;
317         case "showoccupancy":
318           prefName = Preferences.SHOW_OCCUPANCY;
319           value = checkTF(value);
320           break;
321         case "showquality":
322           prefName = Preferences.SHOW_QUALITY;
323           value = checkTF(value);
324           break;
325         case "showsequencelogo":
326           prefName = Preferences.SHOW_CONSENSUS_LOGO;
327           value = checkTF(value);
328           break;
329         case "showfeaturegroups":
330           value = checkTF(value);
331           break;
332         case "showfeaturesettings":
333           value = checkTF(value);
334           break;
335         case "showfullid":
336           value = checkTF(value);
337           break;
338         case "showtreebootstraps":
339           value = checkTF(value);
340           break;
341         case "showtreedistances":
342           value = checkTF(value);
343           break;
344         case "showunconserved":
345           prefName = Preferences.SHOW_UNCONSERVED;
346           value = checkTF(value);
347           break;
348         case "showunlinkedtreenodes":
349           value = checkTF(value);
350           break;
351         default:
352           if (appletName.startsWith("pdbfile")
353                   || appletName.startsWith("sequence") && Character.isDigit(
354                           appletName.charAt(appletName.length() - 1)))
355           {
356             // could be pdbFile2, for example
357             prefName = argName = null;
358             value = resourcePath + value;
359             break;
360           }
361           // or one of the app preference names
362           break;
363         }
364         // put name and value into application args
365         if (value != null && argName != null)
366         {
367           vargs.add(argName);
368           if (value != "true")
369           {
370             vargs.add(value);
371           }
372         }
373         if (value == null)
374         {
375           value = "false";
376         }
377         System.out.println("AppletParams propName=" + prefName + " argName="
378                 + argName + " appletName="
379                 + appletName + " value=" + value);
380         if (appletName != null)
381         {
382           appletParams.put(appletName, value);
383         }
384         if (prefName != null)
385         {
386           Cache.setPropertyNoSave(prefName, value);
387         }
388       }
389     }
390     return appletParams;
391   }
392
393   /**
394    * Check for a single-argument option.
395    * 
396    * @param value
397    * @return "true" or null
398    */
399   private static String checkTF(String value)
400   {
401     return (value.toLowerCase() == "true" ? "true" : null);
402   }
403
404   /**
405    * Crude applet innerHTML parser
406    * 
407    * @param tag
408    * @param attr
409    * @return
410    */
411   private static String getAttr(String tag, String attr)
412   {
413     int pt = tag.indexOf(attr + "=\"");
414     if (pt < 0)
415     {
416       System.out
417               .println("AppletParams did not read " + attr + " in " + tag);
418       return null;
419     }
420     // <param name="sortByTree" value="True"/>
421     int pt1 = pt + attr.length() + 2;
422     int pt2 = tag.indexOf("\"", pt1);
423     return (pt < 0 ? null : tag.substring(pt1, pt2));
424   }
425
426   public static void main(String[] args)
427   {
428     new AppletParams("<applet\r\n"
429             + "    code=\"jalview.bin.JalviewLite\" width=\"140\" height=\"35\"\r\n"
430             + "    archive=\"jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar\">  \r\n"
431             + "  <param name=\"permissions\" value=\"sandbox\"/>\r\n"
432             + "  <param name=\"file\" value=\"uniref50.fa\"/>\r\n"
433             + "  <param name=\"treeFile\" value=\"ferredoxin.nw\"/>\r\n"
434             + "  <param name=\"userDefinedColour\" value=\"C=yellow; R,K,H=FF5555; D,E=5555FF\"/>\r\n"
435             + "  <param name=\"sortByTree\" value=\"True\"/>\r\n"
436             + "  <param name=\"showSequenceLogo\" value=\"true\"/>\r\n"
437             + "  <param name=\"showGroupConsensus\" value=\"true\"/>\r\n"
438             + "  <param name=\"showFullId\" value=\"false\"/>\r\n"
439             + "    <param name=\"linkLabel_1\" value=\"Uniprot\"/>\r\n"
440             + "    <param name=\"linkUrl_1\" value=\"http://www.uniprot.org/uniprot/$SEQUENCE_ID$\"/>\r\n"
441             + "    <param name=\"linkLabel_2\" value=\"EMBL-EBI Search\"/>\r\n"
442             + "    <param name=\"linkUrl_2\" value=\"http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$\"/>\r\n"
443             + "    <param name=\"APPLICATION_URL\" value=\"http://www.jalview.org/services/launchApp\"/>\r\n"
444             + "     </applet>");
445   }
446
447 }