Jalview-JS/JAL-3253-applet adding more applet parameters and setting
[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 appName = getAttr(arg, "name");
132         String value = getAttr(arg, "value");
133         String appletName = appName.toLowerCase();
134
135         // note that Application arguments ARE case-sensitive, but
136         // Applet.getParameter() is not.
137
138         switch (appletName)
139         {
140
141         // tested, working:
142
143         case "file":
144           appName = "open";
145           value = resourcePath + value;
146           appletName = null;
147           break;
148         case "file2":
149           value = resourcePath + value;
150           appName = null;
151           break;
152         case "jnetfile":
153           value = resourcePath + value;
154           appName = null;
155           break;
156         case "jpredfile":
157           value = resourcePath + value;
158           appName = null;
159           break;
160         case "features":
161           value = resourcePath + value;
162           appName = null;
163           break;
164         case "pdbfile":
165           value = resourcePath + value;
166           appName = null;
167           break;
168         case "tree":
169         case "treefile":
170           appName = "tree";
171           value = resourcePath + value;
172           appletName = null; // taken care of by Jalview
173           break;
174         case "defaultcolour":
175           appName = Preferences.DEFAULT_COLOUR;
176           break;
177         case "defaultcolournuc":
178           appName = Preferences.DEFAULT_COLOUR_NUC;
179           break;
180         case "defaultcolourprot":
181           appName = Preferences.DEFAULT_COLOUR_PROT;
182           break;
183
184         // implemented; not tested:
185
186         case "oninit":
187           appName = null;
188           break;
189         case "annotations":
190           value = resourcePath + value;
191           appName = null;
192           break;
193         case "annotationcolour_max":
194           appName = Preferences.ANNOTATIONCOLOUR_MAX;
195           break;
196         case "annotationcolour_min":
197           appName = Preferences.ANNOTATIONCOLOUR_MIN;
198           break;
199         case "enablesplitframe":
200           appName = Preferences.ENABLE_SPLIT_FRAME;
201           break;
202         case "hidefeaturegroups":
203           break;
204         case "centrecolumnlabels":
205           appName = null;
206           break;
207         case "pdbseq":
208           appName = null;
209           break;
210         case "sortby":
211           appName = Preferences.SORT_ALIGNMENT; // id, etc.
212           break;
213         case "sortbytree":
214           appName = Preferences.SORT_BY_TREE;
215           value = checkTF(value);
216           appletName = null; // taken care of by Jalview
217           break;
218         case "format":
219           break;
220
221         // probably not relevant:
222
223         case "externalstructureviewer":
224           break;
225         case "alignpdbfiles":
226           break;
227         case "application_url":
228           appName = "APPLICATION_URL";
229           break;
230         case "automaticscrolling":
231           appName = "automaticScrolling";
232           break;
233         case "heightscale":
234           appName = "heightScale";
235           break;
236         case "jalviewhelpurl":
237           break;
238         case "label":
239           break;
240         case "linklabel_":
241           appName = "linkLabel_";
242           break;
243         case "linklabel_1":
244           appName = "linkLabel_1";
245           break;
246         case "linkurl_":
247           appName = "linkURL_";
248           break;
249         case "rgb":
250           appName = null; // no background for application
251           break;
252
253         // unknown:
254
255         case "nojmol":
256           break;
257         case "normaliselogo":
258           appName = Preferences.NORMALISE_LOGO;
259           break;
260         case "normalisesequencelogo":
261           appName = Preferences.NORMALISE_CONSENSUS_LOGO;
262           break;
263         case "relaxedidmatch":
264           break;
265         case "resolvetocodebase":
266           break;
267         case "scaleproteinascdna":
268           appName = "scaleProteinAsCdna";
269           break;
270         case "separator":
271           break;
272         case "sequence":
273           break;
274         case "uppercase":
275           appName = "upperCase";
276           break;
277         case "userdefinedcolour":
278           appName = "colour";
279           break;
280         case "widthscale":
281           appName = "widthScale";
282           break;
283         case "windowheight":
284           appName = "windowHeight";
285           break;
286         case "windowwidth":
287           appName = "windowWidth";
288           break;
289         case "wrap":
290           appName = Preferences.WRAP_ALIGNMENT;
291           break;
292         case "scorefile":
293           appName = "scoreFile";
294           value = resourcePath + value;
295           break;
296
297         // TRUE/FALSE
298
299         case "debug":
300           value = checkTF(value);
301           break;
302         case "embedded":
303           value = checkTF(value);
304           break;
305         case "showbutton":
306           value = checkTF(value);
307           break;
308         case "showannotation":
309           appName = Preferences.SHOW_ANNOTATIONS;
310           value = checkTF(value);
311           break;
312         case "showconsensus":
313           appName = Preferences.SHOW_CONSENSUS_LOGO;
314           value = checkTF(value);
315           break;
316         case "showconsensushistogram":
317           appName = Preferences.SHOW_CONSENSUS_HISTOGRAM;
318           value = checkTF(value);
319           break;
320         case "showconservation":
321           appName = Preferences.SHOW_CONSERVATION;
322           value = checkTF(value);
323           break;
324         case "showgroupconsensus":
325           appName = Preferences.SHOW_GROUP_CONSENSUS;
326           value = checkTF(value);
327           break;
328         case "showgroupconservation":
329           appName = Preferences.SHOW_GROUP_CONSERVATION;
330           value = checkTF(value);
331           break;
332         case "showoccupancy":
333           appName = Preferences.SHOW_OCCUPANCY;
334           value = checkTF(value);
335           break;
336         case "showquality":
337           appName = Preferences.SHOW_QUALITY;
338           value = checkTF(value);
339           break;
340         case "showsequencelogo":
341           appName = Preferences.SHOW_CONSENSUS_LOGO;
342           value = checkTF(value);
343           break;
344         case "showfeaturegroups":
345           value = checkTF(value);
346           break;
347         case "showfeaturesettings":
348           appName = "showFeatureSettings";
349           value = checkTF(value);
350           break;
351         case "showfullid":
352           appName = "showFullId";
353           value = checkTF(value);
354           break;
355         case "showtreebootstraps":
356           appName = "showTreeBootstraps";
357           value = checkTF(value);
358           break;
359         case "showtreedistances":
360           appName = "showTreeDistances";
361           value = checkTF(value);
362           break;
363         case "showunconserved":
364           appName = Preferences.SHOW_UNCONSERVED;
365           value = checkTF(value);
366           break;
367         case "showunlinkedtreenodes":
368           appName = "showUnlinkedTreeNodes";
369           value = checkTF(value);
370           break;
371         default:
372           // could be pdbFile2, for example
373           // or one of the app preference names
374           break;
375         }
376         if (value != null)
377         {
378           vargs.add(appName);
379           if (value != "true")
380           {
381             vargs.add(value);
382           }
383         }
384         if (value == null)
385         {
386           value = "false";
387         }
388         System.out.println("AppletParams appName=" + appName + "appletName="
389                 + appletName + " value=" + value);
390         Cache.setPropertyNoSave(appName, value);
391         if (appletName != null)
392         {
393           appletParams.put(appletName, value);
394         }
395       }
396     }
397     return appletParams;
398   }
399
400   /**
401    * Check for a single-argument option.
402    * 
403    * @param value
404    * @return "true" or null
405    */
406   private static String checkTF(String value)
407   {
408     return (value.toLowerCase() == "true" ? "true" : null);
409   }
410
411   /**
412    * Crude applet innerHTML parser
413    * 
414    * @param tag
415    * @param attr
416    * @return
417    */
418   private static String getAttr(String tag, String attr)
419   {
420     int pt = tag.indexOf(attr + "=\"");
421     if (pt < 0)
422     {
423       System.out
424               .println("AppletParams did not read " + attr + " in " + tag);
425       return null;
426     }
427     // <param name="sortByTree" value="True"/>
428     int pt1 = pt + attr.length() + 2;
429     int pt2 = tag.indexOf("\"", pt1);
430     return (pt < 0 ? null : tag.substring(pt1, pt2));
431   }
432
433   public static void main(String[] args)
434   {
435     new AppletParams("<applet\r\n"
436             + "    code=\"jalview.bin.JalviewLite\" width=\"140\" height=\"35\"\r\n"
437             + "    archive=\"jalviewApplet.jar,JmolApplet-14.6.4_2016.10.26.jar,java-json.jar,json_simple-1.1.jar\">  \r\n"
438             + "  <param name=\"permissions\" value=\"sandbox\"/>\r\n"
439             + "  <param name=\"file\" value=\"uniref50.fa\"/>\r\n"
440             + "  <param name=\"treeFile\" value=\"ferredoxin.nw\"/>\r\n"
441             + "  <param name=\"userDefinedColour\" value=\"C=yellow; R,K,H=FF5555; D,E=5555FF\"/>\r\n"
442             + "  <param name=\"sortByTree\" value=\"True\"/>\r\n"
443             + "  <param name=\"showSequenceLogo\" value=\"true\"/>\r\n"
444             + "  <param name=\"showGroupConsensus\" value=\"true\"/>\r\n"
445             + "  <param name=\"showFullId\" value=\"false\"/>\r\n"
446             + "    <param name=\"linkLabel_1\" value=\"Uniprot\"/>\r\n"
447             + "    <param name=\"linkUrl_1\" value=\"http://www.uniprot.org/uniprot/$SEQUENCE_ID$\"/>\r\n"
448             + "    <param name=\"linkLabel_2\" value=\"EMBL-EBI Search\"/>\r\n"
449             + "    <param name=\"linkUrl_2\" value=\"http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$\"/>\r\n"
450             + "    <param name=\"APPLICATION_URL\" value=\"http://www.jalview.org/services/launchApp\"/>\r\n"
451             + "     </applet>");
452   }
453
454 }