33ec966d60d2ccf7ab18084eb39aba31bd3d54f3
[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 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     schemes.remove(name);
98   }
99   
100   /**
101    * Returns an instance of the colour scheme with which the given view may be
102    * coloured
103    * 
104    * @param name
105    *          name of the colour scheme
106    * @param forData
107    *          the data to be coloured
108    * @param optional
109    *          map from hidden representative sequences to the sequences they
110    *          represent
111    * @return
112    */
113   public ColourSchemeI getColourScheme(String name,
114           AnnotatedCollectionI forData,
115           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
116   {
117     if (name == null)
118     {
119       return null;
120     }
121     ColourSchemeI cs = schemes.get(name.toLowerCase());
122     return cs == null ? null : cs.getInstance(forData, hiddenRepSequences);
123   }
124
125   /**
126    * Returns an instance of the colour scheme with which the given view may be
127    * coloured
128    * 
129    * @param name
130    *          name of the colour scheme
131    * @param forData
132    *          the data to be coloured
133    * @return
134    */
135   public ColourSchemeI getColourScheme(String name,
136           AnnotatedCollectionI forData)
137   {
138     return getColourScheme(name, forData, null);
139   }
140
141   /**
142    * Returns an iterable set of the colour schemes, in the order in which they
143    * were added
144    * 
145    * @return
146    */
147   public Iterable<ColourSchemeI> getColourSchemes()
148   {
149     return schemes.values();
150   }
151 }