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)
81 .errPrintln("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 jalview.bin.Console.errPrintln("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 = getColourSchemeShortName(cs);
110 if (schemes.containsKey(lower))
113 .println("Warning: overwriting colour scheme named " + name);
115 schemes.put(lower, cs);
118 private String getColourSchemeShortName(ColourSchemeI cs)
120 return getColourSchemeShortName(cs.getSchemeName());
123 private String getColourSchemeShortName(String name)
127 return name.toLowerCase(Locale.ROOT).replaceAll("%", "pc")
128 .replaceAll("[^a-z0-9]", "-").replaceAll("--+", "-");
132 * Removes a colour scheme by name
136 public void removeColourScheme(String name)
140 schemes.remove(getColourSchemeShortName(name));
145 * Returns an instance of the colour scheme with which the given view may be
149 * name of the colour scheme
152 * the data to be coloured
154 * map from hidden representative sequences to the sequences they
158 public ColourSchemeI getColourScheme(String name, AlignViewportI viewport,
159 AnnotatedCollectionI forData,
160 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
166 ColourSchemeI cs = schemes.get(getColourSchemeShortName(name));
167 return cs == null ? null : cs.getInstance(viewport, forData);
171 * Returns an instance of the colour scheme with which the given view may be
175 * name of the colour scheme
177 * the data to be coloured
180 public ColourSchemeI getColourScheme(String name,
181 AnnotatedCollectionI forData)
183 return getColourScheme(name, null, forData, null);
187 * Returns an iterable set of the colour schemes, in the order in which they
192 public Iterable<ColourSchemeI> getColourSchemes()
194 return schemes.values();
198 * Answers true if there is a scheme with the given name, else false. The test
199 * is not case-sensitive.
204 public boolean nameExists(String name)
210 return schemes.containsKey(getColourSchemeShortName(name));