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