JAL-2738 copy to spikes/mungo
[jalview.git] / src / jalview / schemes / ColourSchemes.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.schemes;
22
23 import jalview.datamodel.AnnotatedCollectionI;
24 import jalview.datamodel.SequenceCollectionI;
25 import jalview.datamodel.SequenceI;
26
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29
30 public class ColourSchemes
31 {
32   /*
33    * singleton instance of this class
34    */
35   private static ColourSchemes instance = new ColourSchemes();
36
37   /*
38    * a map from scheme name (lower-cased) to an instance of it
39    */
40   private Map<String, ColourSchemeI> schemes;
41
42   /**
43    * Returns the singleton instance of this class
44    * 
45    * @return
46    */
47   public static ColourSchemes getInstance()
48   {
49     return instance;
50   }
51
52   private ColourSchemes()
53   {
54     loadColourSchemes();
55   }
56
57   /**
58    * Loads an instance of each standard or user-defined colour scheme
59    * 
60    * @return
61    */
62   void loadColourSchemes()
63   {
64     /*
65      * store in an order-preserving map, so items can be added to menus 
66      * in the order in which they are 'discovered'
67      */
68     schemes = new LinkedHashMap<String, ColourSchemeI>();
69
70     for (JalviewColourScheme cs : JalviewColourScheme.values())
71     {
72       try
73       {
74         registerColourScheme(cs.getSchemeClass().newInstance());
75       } catch (InstantiationException | IllegalAccessException e)
76       {
77         System.err.println("Error instantiating colour scheme for "
78                 + cs.toString() + " " + e.getMessage());
79       }
80     }
81   }
82
83   /**
84    * Registers a colour scheme
85    * 
86    * @param cs
87    */
88   public void registerColourScheme(ColourSchemeI cs)
89   {
90     String name = cs.getSchemeName();
91     if (name == null)
92     {
93       System.err.println("ColourScheme name may not be null");
94       return;
95     }
96
97     /*
98      * name is lower-case for non-case-sensitive lookup
99      * (name in the colour keeps its true case)
100      */
101     String lower = name.toLowerCase();
102     if (schemes.containsKey(lower))
103     {
104       System.err
105               .println("Warning: overwriting colour scheme named " + name);
106     }
107     schemes.put(lower, cs);
108   }
109
110   /**
111    * Removes a colour scheme by name
112    * 
113    * @param name
114    */
115   public void removeColourScheme(String name)
116   {
117     if (name != null)
118     {
119       schemes.remove(name.toLowerCase());
120     }
121   }
122
123   /**
124    * Returns an instance of the colour scheme with which the given view may be
125    * coloured
126    * 
127    * @param name
128    *          name of the colour scheme
129    * @param forData
130    *          the data to be coloured
131    * @param optional
132    *          map from hidden representative sequences to the sequences they
133    *          represent
134    * @return
135    */
136   public ColourSchemeI getColourScheme(String name,
137           AnnotatedCollectionI forData,
138           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
139   {
140     if (name == null)
141     {
142       return null;
143     }
144     ColourSchemeI cs = schemes.get(name.toLowerCase());
145     return cs == null ? null : cs.getInstance(forData, hiddenRepSequences);
146   }
147
148   /**
149    * Returns an instance of the colour scheme with which the given view may be
150    * coloured
151    * 
152    * @param name
153    *          name of the colour scheme
154    * @param forData
155    *          the data to be coloured
156    * @return
157    */
158   public ColourSchemeI getColourScheme(String name,
159           AnnotatedCollectionI forData)
160   {
161     return getColourScheme(name, forData, null);
162   }
163
164   /**
165    * Returns an iterable set of the colour schemes, in the order in which they
166    * were added
167    * 
168    * @return
169    */
170   public Iterable<ColourSchemeI> getColourSchemes()
171   {
172     return schemes.values();
173   }
174
175   /**
176    * Answers true if there is a scheme with the given name, else false. The test
177    * is not case-sensitive.
178    * 
179    * @param name
180    * @return
181    */
182   public boolean nameExists(String name)
183   {
184     if (name == null)
185     {
186       return false;
187     }
188     return schemes.containsKey(name.toLowerCase());
189   }
190 }