2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.schemes;
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.AnnotatedCollectionI;
25 import jalview.datamodel.SequenceCollectionI;
26 import jalview.datamodel.SequenceI;
28 import java.util.LinkedHashMap;
31 public class ColourSchemes
34 * singleton instance of this class
36 private static ColourSchemes instance = new ColourSchemes();
39 * a map from scheme name (lower-cased) to an instance of it
41 private Map<String, ColourSchemeI> schemes;
44 * Returns the singleton instance of this class
48 public static ColourSchemes getInstance()
53 private ColourSchemes()
59 * Loads an instance of each standard or user-defined colour scheme
63 void loadColourSchemes()
66 * store in an order-preserving map, so items can be added to menus
67 * in the order in which they are 'discovered'
69 schemes = new LinkedHashMap<>();
71 for (JalviewColourScheme cs : JalviewColourScheme.values())
75 registerColourScheme(cs.getSchemeClass().newInstance());
76 } catch (InstantiationException | IllegalAccessException e)
78 System.err.println("Error instantiating colour scheme for "
79 + cs.toString() + " " + e.getMessage());
86 * Registers a colour scheme
90 public void registerColourScheme(ColourSchemeI cs)
92 String name = cs.getSchemeName();
95 System.err.println("ColourScheme name may not be null");
100 * name is lower-case for non-case-sensitive lookup
101 * (name in the colour keeps its true case)
103 String lower = name.toLowerCase();
104 if (schemes.containsKey(lower))
107 .println("Warning: overwriting colour scheme named " + name);
109 schemes.put(lower, cs);
113 * Removes a colour scheme by name
117 public void removeColourScheme(String name)
121 schemes.remove(name.toLowerCase());
126 * Returns an instance of the colour scheme with which the given view may be
130 * name of the colour scheme
133 * the data to be coloured
135 * map from hidden representative sequences to the sequences they
139 public ColourSchemeI getColourScheme(String name,
140 AlignViewportI viewport, AnnotatedCollectionI forData,
141 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
147 ColourSchemeI cs = schemes.get(name.toLowerCase());
148 return cs == null ? null
149 : cs.getInstance(viewport, forData);
153 * Returns an instance of the colour scheme with which the given view may be
157 * name of the colour scheme
159 * the data to be coloured
162 public ColourSchemeI getColourScheme(String name,
163 AnnotatedCollectionI forData)
165 return getColourScheme(name, null, forData, null);
169 * Returns an iterable set of the colour schemes, in the order in which they
174 public Iterable<ColourSchemeI> getColourSchemes()
176 return schemes.values();
180 * Answers true if there is a scheme with the given name, else false. The test
181 * is not case-sensitive.
186 public boolean nameExists(String name)
192 return schemes.containsKey(name.toLowerCase());