b28139fd430454fe2bbf957e62b5f32def957fa1
[jalview.git] / src / jalview / schemes / ColourSchemes.java
1 package jalview.schemes;
2
3 import jalview.datamodel.AnnotatedCollectionI;
4 import jalview.datamodel.SequenceCollectionI;
5 import jalview.datamodel.SequenceI;
6
7 import java.util.LinkedHashMap;
8 import java.util.Map;
9
10 public class ColourSchemes
11 {
12   /*
13    * singleton instance of this class
14    */
15   private static ColourSchemes instance = new ColourSchemes();
16
17   /*
18    * a map from scheme name (lower-cased) to an instance of it
19    */
20   private Map<String, ColourSchemeI> schemes;
21
22   /**
23    * Returns the singleton instance of this class
24    * 
25    * @return
26    */
27   public static ColourSchemes getInstance()
28   {
29     return instance;
30   }
31
32   private ColourSchemes()
33   {
34     loadColourSchemes();
35   }
36
37   /**
38    * Loads an instance of each standard or user-defined colour scheme
39    * 
40    * @return
41    */
42   void loadColourSchemes()
43   {
44     /*
45      * store in an order-preserving map, so items can be added to menus 
46      * in the order in which they are 'discovered'
47      */
48     schemes = new LinkedHashMap<String, ColourSchemeI>();
49
50     for (JalviewColourScheme cs : JalviewColourScheme.values())
51     {
52       try
53       {
54         registerColourScheme(cs.getSchemeClass().newInstance());
55       } catch (InstantiationException | IllegalAccessException e)
56       {
57         System.err.println("Error instantiating colour scheme for "
58                 + cs.toString() + " " + e.getMessage());
59       }
60     }
61   }
62
63   /**
64    * Registers a colour scheme
65    * 
66    * @param cs
67    */
68   public void registerColourScheme(ColourSchemeI cs)
69   {
70     String name = cs.getSchemeName();
71     if (name == null)
72     {
73       System.err.println("ColourScheme name may not be null");
74       return;
75     }
76
77     /*
78      * name is lower-case for non-case-sensitive lookup
79      * (name in the colour keeps its true case)
80      */
81     String lower = name.toLowerCase();
82     if (schemes.containsKey(lower))
83     {
84       System.err
85               .println("Warning: overwriting colour scheme named " + name);
86     }
87     schemes.put(lower, cs);
88   }
89
90   /**
91    * Removes a colour scheme by name
92    * 
93    * @param name
94    */
95   public void removeColourScheme(String name)
96   {
97     if (name != null)
98     {
99       schemes.remove(name.toLowerCase());
100     }
101   }
102
103   /**
104    * Returns an instance of the colour scheme with which the given view may be
105    * coloured
106    * 
107    * @param name
108    *          name of the colour scheme
109    * @param forData
110    *          the data to be coloured
111    * @param optional
112    *          map from hidden representative sequences to the sequences they
113    *          represent
114    * @return
115    */
116   public ColourSchemeI getColourScheme(String name,
117           AnnotatedCollectionI forData,
118           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
119   {
120     if (name == null)
121     {
122       return null;
123     }
124     ColourSchemeI cs = schemes.get(name.toLowerCase());
125     return cs == null ? null : cs.getInstance(forData, hiddenRepSequences);
126   }
127
128   /**
129    * Returns an instance of the colour scheme with which the given view may be
130    * coloured
131    * 
132    * @param name
133    *          name of the colour scheme
134    * @param forData
135    *          the data to be coloured
136    * @return
137    */
138   public ColourSchemeI getColourScheme(String name,
139           AnnotatedCollectionI forData)
140   {
141     return getColourScheme(name, forData, null);
142   }
143
144   /**
145    * Returns an iterable set of the colour schemes, in the order in which they
146    * were added
147    * 
148    * @return
149    */
150   public Iterable<ColourSchemeI> getColourSchemes()
151   {
152     return schemes.values();
153   }
154
155   /**
156    * Answers true if there is a scheme with the given name, else false. The test
157    * is not case-sensitive.
158    * 
159    * @param name
160    * @return
161    */
162   public boolean nameExists(String name)
163   {
164     if (name == null)
165     {
166       return false;
167     }
168     return schemes.containsKey(name.toLowerCase());
169   }
170 }