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())
76 cs.getSchemeClass().getDeclaredConstructor().newInstance());
77 } catch (InstantiationException | IllegalAccessException e)
79 System.err.println("Error instantiating colour scheme for "
80 + cs.toString() + " " + e.getMessage());
82 } catch (ReflectiveOperationException roe)
84 roe.printStackTrace();
90 * Registers a colour scheme
94 public void registerColourScheme(ColourSchemeI cs)
96 String name = cs.getSchemeName();
99 System.err.println("ColourScheme name may not be null");
104 * name is lower-case for non-case-sensitive lookup
105 * (name in the colour keeps its true case)
107 String lower = name.toLowerCase();
108 if (schemes.containsKey(lower))
111 .println("Warning: overwriting colour scheme named " + name);
113 schemes.put(lower, cs);
117 * Removes a colour scheme by name
121 public void removeColourScheme(String name)
125 schemes.remove(name.toLowerCase());
130 * Returns an instance of the colour scheme with which the given view may be
134 * name of the colour scheme
137 * the data to be coloured
139 * map from hidden representative sequences to the sequences they
143 public ColourSchemeI getColourScheme(String name,
144 AlignViewportI viewport, AnnotatedCollectionI forData,
145 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
151 ColourSchemeI cs = schemes.get(name.toLowerCase());
152 return cs == null ? null
153 : cs.getInstance(viewport, forData);
157 * Returns an instance of the colour scheme with which the given view may be
161 * name of the colour scheme
163 * the data to be coloured
166 public ColourSchemeI getColourScheme(String name,
167 AnnotatedCollectionI forData)
169 return getColourScheme(name, null, forData, null);
173 * Returns an iterable set of the colour schemes, in the order in which they
178 public Iterable<ColourSchemeI> getColourSchemes()
180 return schemes.values();
184 * Answers true if there is a scheme with the given name, else false. The test
185 * is not case-sensitive.
190 public boolean nameExists(String name)
196 return schemes.containsKey(name.toLowerCase());