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