fixes launchApp bug where user defined colourscheme defined in applet parameter does...
[jalview.git] / src / jalview / schemes / ColourSchemeProperty.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.schemes;
20
21 import java.awt.*;
22
23 /**
24  * ColourSchemeProperty Binds names to hardwired colourschemes and tries to deal
25  * intelligently with mapping unknown names to user defined colourschemes (that
26  * exist or can be created from the string representation of the colourscheme
27  * name - either a hex RGB triplet or a named colour under java.awt.color )
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class ColourSchemeProperty
33 {
34   /** DOCUMENT ME!! */
35   public static final int CLUSTAL = 0;
36
37   /** DOCUMENT ME!! */
38   public static final int BLOSUM = 1;
39
40   /** DOCUMENT ME!! */
41   public static final int PID = 2;
42
43   /** DOCUMENT ME!! */
44   public static final int ZAPPO = 3;
45
46   /** DOCUMENT ME!! */
47   public static final int TAYLOR = 4;
48
49   /** DOCUMENT ME!! */
50   public static final int HYDROPHOBIC = 5;
51
52   /** DOCUMENT ME!! */
53   public static final int HELIX = 6;
54
55   /** DOCUMENT ME!! */
56   public static final int STRAND = 7;
57
58   /** DOCUMENT ME!! */
59   public static final int TURN = 8;
60
61   /** DOCUMENT ME!! */
62   public static final int BURIED = 9;
63
64   /** DOCUMENT ME!! */
65   public static final int NUCLEOTIDE = 10;
66
67   /** DOCUMENT ME!! */
68   public static final int USER_DEFINED = 11;
69
70   /** No Colourscheme Index */
71   public static final int NONE = 12;
72
73   /** Undefined Colourscheme Index */
74   public static final int UNDEFINED = 13;
75
76   /**
77    * DOCUMENT ME!
78    * 
79    * @param name
80    *          DOCUMENT ME!
81    * 
82    * @return DOCUMENT ME!
83    */
84   public static int getColourIndexFromName(String name)
85   {
86     int ret = UNDEFINED;
87
88     if (name.equalsIgnoreCase("Clustal"))
89     {
90       ret = CLUSTAL;
91     }
92     else if (name.equalsIgnoreCase("Blosum62"))
93     {
94       ret = BLOSUM;
95     }
96     else if (name.equalsIgnoreCase("% Identity"))
97     {
98       ret = PID;
99     }
100     else if (name.equalsIgnoreCase("Zappo"))
101     {
102       ret = ZAPPO;
103     }
104     else if (name.equalsIgnoreCase("Taylor"))
105     {
106       ret = TAYLOR;
107     }
108     else if (name.equalsIgnoreCase("Hydrophobic"))
109     {
110       ret = HYDROPHOBIC;
111     }
112     else if (name.equalsIgnoreCase("Helix Propensity"))
113     {
114       ret = HELIX;
115     }
116     else if (name.equalsIgnoreCase("Strand Propensity"))
117     {
118       ret = STRAND;
119     }
120     else if (name.equalsIgnoreCase("Turn Propensity"))
121     {
122       ret = TURN;
123     }
124     else if (name.equalsIgnoreCase("Buried Index"))
125     {
126       ret = BURIED;
127     }
128     else if (name.equalsIgnoreCase("Nucleotide"))
129     {
130       ret = NUCLEOTIDE;
131     }
132     else if (name.equalsIgnoreCase("User Defined"))
133     {
134       ret = USER_DEFINED;
135     }
136     else if (name.equalsIgnoreCase("None"))
137     {
138       ret = NONE;
139     }
140
141     return ret;
142   }
143
144   /**
145    * DOCUMENT ME!
146    * 
147    * @param cs
148    *          DOCUMENT ME!
149    * 
150    * @return DOCUMENT ME!
151    */
152   public static String getColourName(ColourSchemeI cs)
153   {
154
155     int index = NONE;
156
157     if (cs instanceof ClustalxColourScheme)
158     {
159       index = CLUSTAL;
160     }
161     else if (cs instanceof Blosum62ColourScheme)
162     {
163       index = BLOSUM;
164     }
165     else if (cs instanceof PIDColourScheme)
166     {
167       index = PID;
168     }
169     else if (cs instanceof ZappoColourScheme)
170     {
171       index = ZAPPO;
172     }
173     else if (cs instanceof TaylorColourScheme)
174     {
175       index = TAYLOR;
176     }
177     else if (cs instanceof HydrophobicColourScheme)
178     {
179       index = HYDROPHOBIC;
180     }
181     else if (cs instanceof HelixColourScheme)
182     {
183       index = HELIX;
184     }
185     else if (cs instanceof StrandColourScheme)
186     {
187       index = STRAND;
188     }
189     else if (cs instanceof TurnColourScheme)
190     {
191       index = TURN;
192     }
193     else if (cs instanceof BuriedColourScheme)
194     {
195       index = BURIED;
196     }
197     else if (cs instanceof NucleotideColourScheme)
198     {
199       index = NUCLEOTIDE;
200     }
201     else if (cs instanceof UserColourScheme)
202     {
203       if ((((UserColourScheme) cs).getName() != null)
204               && (((UserColourScheme) cs).getName().length() > 0))
205       {
206         return ((UserColourScheme) cs).getName();
207       }
208       // get default colourscheme name
209       index = USER_DEFINED;
210     }
211
212     return getColourName(index);
213   }
214
215   /**
216    * DOCUMENT ME!
217    * 
218    * @param index
219    *          DOCUMENT ME!
220    * 
221    * @return DOCUMENT ME!
222    */
223   public static String getColourName(int index)
224   {
225     String ret = null;
226
227     switch (index)
228     {
229     case CLUSTAL:
230       ret = "Clustal";
231
232       break;
233
234     case BLOSUM:
235       ret = "Blosum62";
236
237       break;
238
239     case PID:
240       ret = "% Identity";
241
242       break;
243
244     case ZAPPO:
245       ret = "Zappo";
246
247       break;
248
249     case TAYLOR:
250       ret = "Taylor";
251       break;
252
253     case HYDROPHOBIC:
254       ret = "Hydrophobic";
255
256       break;
257
258     case HELIX:
259       ret = "Helix Propensity";
260
261       break;
262
263     case STRAND:
264       ret = "Strand Propensity";
265
266       break;
267
268     case TURN:
269       ret = "Turn Propensity";
270
271       break;
272
273     case BURIED:
274       ret = "Buried Index";
275
276       break;
277
278     case NUCLEOTIDE:
279       ret = "Nucleotide";
280
281       break;
282
283     case USER_DEFINED:
284       ret = "User Defined";
285
286       break;
287
288     default:
289       ret = "None";
290
291       break;
292     }
293
294     return ret;
295   }
296
297   /**
298    * DOCUMENT ME!
299    * 
300    * @param al
301    *          DOCUMENT ME!
302    * @param name
303    *          DOCUMENT ME!
304    * 
305    * @return DOCUMENT ME!
306    */
307   public static ColourSchemeI getColour(jalview.datamodel.AlignmentI al,
308           String name)
309   {
310     return getColour(al.getSequences(), al.getWidth(), name);
311   }
312
313   /**
314    * retrieve or create colourscheme associated with name
315    * 
316    * @param seqs
317    *          sequences to colour
318    * @param width
319    *          range of sequences to colour
320    * @param name
321    *          colourscheme name, applet colour parameter specification, or
322    *          string to parse as colour for new coloursheme
323    * @return Valid Colourscheme
324    */
325   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
326           String name)
327   {
328     int colindex = getColourIndexFromName(name);
329     if (colindex == UNDEFINED)
330     {
331       if (name.indexOf('=') == -1)
332       {
333         // try to build a colour from the string directly
334         try
335         {
336           return new UserColourScheme(name);
337         } catch (Exception e)
338         {
339           // System.err.println("Ignoring unknown colourscheme name");
340         }
341       }
342       else
343       {
344         // try to parse the string as a residue colourscheme
345         try
346         {
347           // fix the launchApp user defined coloursheme transfer bug
348           jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
349                   "white");
350           ucs.parseAppletParameter(name);
351
352         } catch (Exception e)
353         {
354           // System.err.println("Ignoring exception when parsing colourscheme as applet-parameter");
355         }
356       }
357     }
358     return getColour(seqs, width, getColourIndexFromName(name));
359   }
360
361   /**
362    * DOCUMENT ME!
363    * 
364    * @param seqs
365    *          DOCUMENT ME!
366    * @param width
367    *          DOCUMENT ME!
368    * @param index
369    *          DOCUMENT ME!
370    * 
371    * @return DOCUMENT ME!
372    */
373   public static ColourSchemeI getColour(java.util.Vector seqs, int width,
374           int index)
375   {
376     ColourSchemeI cs = null;
377
378     switch (index)
379     {
380     case CLUSTAL:
381       cs = new ClustalxColourScheme(seqs, width);
382
383       break;
384
385     case BLOSUM:
386       cs = new Blosum62ColourScheme();
387
388       break;
389
390     case PID:
391       cs = new PIDColourScheme();
392
393       break;
394
395     case ZAPPO:
396       cs = new ZappoColourScheme();
397
398       break;
399
400     case TAYLOR:
401       cs = new TaylorColourScheme();
402       break;
403
404     case HYDROPHOBIC:
405       cs = new HydrophobicColourScheme();
406
407       break;
408
409     case HELIX:
410       cs = new HelixColourScheme();
411
412       break;
413
414     case STRAND:
415       cs = new StrandColourScheme();
416
417       break;
418
419     case TURN:
420       cs = new TurnColourScheme();
421
422       break;
423
424     case BURIED:
425       cs = new BuriedColourScheme();
426
427       break;
428
429     case NUCLEOTIDE:
430       cs = new NucleotideColourScheme();
431
432       break;
433
434     case USER_DEFINED:
435       Color[] col = new Color[24];
436       for (int i = 0; i < 24; i++)
437       {
438         col[i] = Color.white;
439       }
440       cs = new UserColourScheme(col);
441       break;
442
443     default:
444       break;
445     }
446
447     return cs;
448   }
449
450   public static Color getAWTColorFromName(String name)
451   {
452     Color col = null;
453     name = name.toLowerCase();
454     if (name.equals("black"))
455     {
456       col = Color.black;
457     }
458     else if (name.equals("blue"))
459     {
460       col = Color.blue;
461     }
462     else if (name.equals("cyan"))
463     {
464       col = Color.cyan;
465     }
466     else if (name.equals("darkGray"))
467     {
468       col = Color.darkGray;
469     }
470     else if (name.equals("gray"))
471     {
472       col = Color.gray;
473     }
474     else if (name.equals("green"))
475     {
476       col = Color.green;
477     }
478     else if (name.equals("lightGray"))
479     {
480       col = Color.lightGray;
481     }
482     else if (name.equals("magenta"))
483     {
484       col = Color.magenta;
485     }
486     else if (name.equals("orange"))
487     {
488       col = Color.orange;
489     }
490     else if (name.equals("pink"))
491     {
492       col = Color.pink;
493     }
494     else if (name.equals("red"))
495     {
496       col = Color.red;
497     }
498     else if (name.equals("white"))
499     {
500       col = Color.white;
501     }
502     else if (name.equals("yellow"))
503     {
504       col = Color.yellow;
505     }
506
507     return col;
508   }
509 }