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.LinkedHashMap;
24 import java.util.Locale;
27 import jalview.api.AlignViewportI;
28 import jalview.datamodel.AnnotatedCollectionI;
29 import jalview.datamodel.SequenceCollectionI;
30 import jalview.datamodel.SequenceI;
32 public class ColourSchemes
35 * singleton instance of this class
37 private static ColourSchemes instance = new ColourSchemes();
40 * a map from scheme name (lower-cased) to an instance of it
42 private Map<String, ColourSchemeI> schemes;
45 * Returns the singleton instance of this class
49 public static ColourSchemes getInstance()
54 private ColourSchemes()
60 * Loads an instance of each standard or user-defined colour scheme
64 void loadColourSchemes()
67 * store in an order-preserving map, so items can be added to menus
68 * in the order in which they are 'discovered'
70 schemes = new LinkedHashMap<>();
72 for (JalviewColourScheme cs : JalviewColourScheme.values())
77 cs.getSchemeClass().getDeclaredConstructor().newInstance());
78 } catch (InstantiationException | IllegalAccessException e)
80 System.err.println("Error instantiating colour scheme for "
81 + cs.toString() + " " + e.getMessage());
83 } catch (ReflectiveOperationException roe)
85 roe.printStackTrace();
91 * Registers a colour scheme
95 public void registerColourScheme(ColourSchemeI cs)
97 String name = cs.getSchemeName();
100 System.err.println("ColourScheme name may not be null");
105 * name is lower-case for non-case-sensitive lookup
106 * (name in the colour keeps its true case)
108 String lower = getColourSchemeShortName(cs);
109 if (schemes.containsKey(lower))
112 .println("Warning: overwriting colour scheme named " + name);
114 schemes.put(lower, cs);
117 private String getColourSchemeShortName(ColourSchemeI cs)
119 return getColourSchemeShortName(cs.getSchemeName());
122 private String getColourSchemeShortName(String name)
126 return name.toLowerCase(Locale.ROOT).replaceAll("%", "pc")
127 .replaceAll("[^a-z0-9]", "-").replaceAll("--+", "-");
131 * Removes a colour scheme by name
135 public void removeColourScheme(String name)
139 schemes.remove(getColourSchemeShortName(name));
144 * Returns an instance of the colour scheme with which the given view may be
148 * name of the colour scheme
151 * the data to be coloured
153 * map from hidden representative sequences to the sequences they
157 public ColourSchemeI getColourScheme(String name, AlignViewportI viewport,
158 AnnotatedCollectionI forData,
159 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
165 ColourSchemeI cs = schemes.get(getColourSchemeShortName(name));
166 return cs == null ? null : cs.getInstance(viewport, forData);
170 * Returns an instance of the colour scheme with which the given view may be
174 * name of the colour scheme
176 * the data to be coloured
179 public ColourSchemeI getColourScheme(String name,
180 AnnotatedCollectionI forData)
182 return getColourScheme(name, null, forData, null);
186 * Returns an iterable set of the colour schemes, in the order in which they
191 public Iterable<ColourSchemeI> getColourSchemes()
193 return schemes.values();
197 * Answers true if there is a scheme with the given name, else false. The test
198 * is not case-sensitive.
203 public boolean nameExists(String name)
209 return schemes.containsKey(getColourSchemeShortName(name));