1 package jalview.schemes;
3 import jalview.datamodel.AnnotatedCollectionI;
4 import jalview.datamodel.SequenceCollectionI;
5 import jalview.datamodel.SequenceI;
7 import java.util.LinkedHashMap;
10 public class ColourSchemes
13 * singleton instance of this class
15 private static ColourSchemes instance = new ColourSchemes();
18 * a map from scheme name to an instance of it
20 private Map<String, ColourSchemeI> schemes;
23 * Returns the singleton instance of this class
27 public static ColourSchemes getInstance()
32 private ColourSchemes()
38 * Loads an instance of each standard or user-defined colour scheme
42 void loadColourSchemes()
45 * store in an order-preserving map, so items can be added to menus
46 * in the order in which they are 'discovered'
48 schemes = new LinkedHashMap<String, ColourSchemeI>();
50 for (JalviewColourScheme cs : JalviewColourScheme.values())
54 registerColourScheme(cs.getSchemeClass().newInstance());
55 } catch (InstantiationException | IllegalAccessException e)
57 System.err.println("Error instantiating colour scheme for "
58 + cs.toString() + " " + e.getMessage());
64 * Registers a colour scheme
68 public void registerColourScheme(ColourSchemeI cs)
70 String name = cs.getSchemeName();
73 System.err.println("ColourScheme name may not be null");
78 * name is lower-case for non-case-sensitive lookup
79 * (name in the colour keeps its true case)
81 String lower = name.toLowerCase();
82 if (schemes.containsKey(lower))
85 .println("Warning: overwriting colour scheme named " + name);
87 schemes.put(lower, cs);
91 * Removes a colour scheme by name
95 public void removeColourScheme(String name)
101 * Returns an instance of the colour scheme with which the given view may be
105 * name of the colour scheme
107 * the data to be coloured
109 * map from hidden representative sequences to the sequences they
113 public ColourSchemeI getColourScheme(String name,
114 AnnotatedCollectionI forData,
115 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
121 ColourSchemeI cs = schemes.get(name.toLowerCase());
122 return cs == null ? null : cs.getInstance(forData, hiddenRepSequences);
126 * Returns an instance of the colour scheme with which the given view may be
130 * name of the colour scheme
132 * the data to be coloured
135 public ColourSchemeI getColourScheme(String name,
136 AnnotatedCollectionI forData)
138 return getColourScheme(name, forData, null);
142 * Returns an iterable set of the colour schemes, in the order in which they
147 public Iterable<ColourSchemeI> getColourSchemes()
149 return schemes.values();
153 * Answers true if there is a scheme with the given name, else false. The test
154 * is not case-sensitive.
159 public boolean nameExists(String name)
165 name = name.toLowerCase();
166 for (ColourSchemeI scheme : getColourSchemes())
168 if (name.equals(scheme.getSchemeName().toLowerCase()))