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 java.util.Locale;
25 import jalview.api.AlignViewportI;
26 import jalview.datamodel.AnnotatedCollectionI;
27 import jalview.datamodel.SequenceCollectionI;
28 import jalview.datamodel.SequenceI;
30 import java.util.LinkedHashMap;
33 public class ColourSchemes
36 * singleton instance of this class
38 private static ColourSchemes instance = new ColourSchemes();
41 * a map from scheme name (lower-cased) to an instance of it
43 private Map<String, ColourSchemeI> schemes;
46 * Returns the singleton instance of this class
50 public static ColourSchemes getInstance()
55 private ColourSchemes()
61 * Loads an instance of each standard or user-defined colour scheme
65 void loadColourSchemes()
68 * store in an order-preserving map, so items can be added to menus
69 * in the order in which they are 'discovered'
71 schemes = new LinkedHashMap<>();
73 for (JalviewColourScheme cs : JalviewColourScheme.values())
78 cs.getSchemeClass().getDeclaredConstructor().newInstance());
79 } catch (InstantiationException | IllegalAccessException e)
81 System.err.println("Error instantiating colour scheme for "
82 + cs.toString() + " " + e.getMessage());
84 } catch (ReflectiveOperationException roe)
86 roe.printStackTrace();
92 * Registers a colour scheme
96 public void registerColourScheme(ColourSchemeI cs)
98 String name = cs.getSchemeName();
101 System.err.println("ColourScheme name may not be null");
106 * name is lower-case for non-case-sensitive lookup
107 * (name in the colour keeps its true case)
109 String lower = name.toLowerCase(Locale.ROOT);
110 if (schemes.containsKey(lower))
113 .println("Warning: overwriting colour scheme named " + name);
115 schemes.put(lower, cs);
119 * Removes a colour scheme by name
123 public void removeColourScheme(String name)
127 schemes.remove(name.toLowerCase(Locale.ROOT));
132 * Returns an instance of the colour scheme with which the given view may be
136 * name of the colour scheme
139 * the data to be coloured
141 * map from hidden representative sequences to the sequences they
145 public ColourSchemeI getColourScheme(String name, AlignViewportI viewport,
146 AnnotatedCollectionI forData,
147 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
153 ColourSchemeI cs = schemes.get(name.toLowerCase(Locale.ROOT));
154 return cs == null ? null : cs.getInstance(viewport, forData);
158 * Returns an instance of the colour scheme with which the given view may be
162 * name of the colour scheme
164 * the data to be coloured
167 public ColourSchemeI getColourScheme(String name,
168 AnnotatedCollectionI forData)
170 return getColourScheme(name, null, forData, null);
174 * Returns an iterable set of the colour schemes, in the order in which they
179 public Iterable<ColourSchemeI> getColourSchemes()
181 return schemes.values();
185 * Answers true if there is a scheme with the given name, else false. The test
186 * is not case-sensitive.
191 public boolean nameExists(String name)
197 return schemes.containsKey(name.toLowerCase(Locale.ROOT));