JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / schemes / ColourSchemes.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.schemes;
22
23 import java.util.Locale;
24
25 import jalview.api.AlignViewportI;
26 import jalview.datamodel.AnnotatedCollectionI;
27 import jalview.datamodel.SequenceCollectionI;
28 import jalview.datamodel.SequenceI;
29
30 import java.util.LinkedHashMap;
31 import java.util.Map;
32
33 public class ColourSchemes
34 {
35   /*
36    * singleton instance of this class
37    */
38   private static ColourSchemes instance = new ColourSchemes();
39
40   /*
41    * a map from scheme name (lower-cased) to an instance of it
42    */
43   private Map<String, ColourSchemeI> schemes;
44
45   /**
46    * Returns the singleton instance of this class
47    * 
48    * @return
49    */
50   public static ColourSchemes getInstance()
51   {
52     return instance;
53   }
54
55   private ColourSchemes()
56   {
57     loadColourSchemes();
58   }
59
60   /**
61    * Loads an instance of each standard or user-defined colour scheme
62    * 
63    * @return
64    */
65   void loadColourSchemes()
66   {
67     /*
68      * store in an order-preserving map, so items can be added to menus 
69      * in the order in which they are 'discovered'
70      */
71     schemes = new LinkedHashMap<>();
72
73     for (JalviewColourScheme cs : JalviewColourScheme.values())
74     {
75       try
76       {
77         registerColourScheme(
78                 cs.getSchemeClass().getDeclaredConstructor().newInstance());
79       } catch (InstantiationException | IllegalAccessException e)
80       {
81         System.err.println("Error instantiating colour scheme for "
82                 + cs.toString() + " " + e.getMessage());
83         e.printStackTrace();
84       } catch (ReflectiveOperationException roe)
85       {
86         roe.printStackTrace();
87       }
88     }
89   }
90
91   /**
92    * Registers a colour scheme
93    * 
94    * @param cs
95    */
96   public void registerColourScheme(ColourSchemeI cs)
97   {
98     String name = cs.getSchemeName();
99     if (name == null)
100     {
101       System.err.println("ColourScheme name may not be null");
102       return;
103     }
104
105     /*
106      * name is lower-case for non-case-sensitive lookup
107      * (name in the colour keeps its true case)
108      */
109     String lower = name.toLowerCase(Locale.ROOT);
110     if (schemes.containsKey(lower))
111     {
112       System.err
113               .println("Warning: overwriting colour scheme named " + name);
114     }
115     schemes.put(lower, cs);
116   }
117
118   /**
119    * Removes a colour scheme by name
120    * 
121    * @param name
122    */
123   public void removeColourScheme(String name)
124   {
125     if (name != null)
126     {
127       schemes.remove(name.toLowerCase(Locale.ROOT));
128     }
129   }
130
131   /**
132    * Returns an instance of the colour scheme with which the given view may be
133    * coloured
134    * 
135    * @param name
136    *          name of the colour scheme
137    * @param viewport
138    * @param forData
139    *          the data to be coloured
140    * @param optional
141    *          map from hidden representative sequences to the sequences they
142    *          represent
143    * @return
144    */
145   public ColourSchemeI getColourScheme(String name, AlignViewportI viewport,
146           AnnotatedCollectionI forData,
147           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
148   {
149     if (name == null)
150     {
151       return null;
152     }
153     ColourSchemeI cs = schemes.get(name.toLowerCase(Locale.ROOT));
154     return cs == null ? null : cs.getInstance(viewport, forData);
155   }
156
157   /**
158    * Returns an instance of the colour scheme with which the given view may be
159    * coloured
160    * 
161    * @param name
162    *          name of the colour scheme
163    * @param forData
164    *          the data to be coloured
165    * @return
166    */
167   public ColourSchemeI getColourScheme(String name,
168           AnnotatedCollectionI forData)
169   {
170     return getColourScheme(name, null, forData, null);
171   }
172
173   /**
174    * Returns an iterable set of the colour schemes, in the order in which they
175    * were added
176    * 
177    * @return
178    */
179   public Iterable<ColourSchemeI> getColourSchemes()
180   {
181     return schemes.values();
182   }
183
184   /**
185    * Answers true if there is a scheme with the given name, else false. The test
186    * is not case-sensitive.
187    * 
188    * @param name
189    * @return
190    */
191   public boolean nameExists(String name)
192   {
193     if (name == null)
194     {
195       return false;
196     }
197     return schemes.containsKey(name.toLowerCase(Locale.ROOT));
198   }
199 }