JAL-4152 Remove redundant casts from getInstance usages.
[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.bin.ApplicationSingletonProvider;
27 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
28 import jalview.datamodel.AnnotatedCollectionI;
29 import jalview.datamodel.SequenceCollectionI;
30 import jalview.datamodel.SequenceI;
31 import jalview.util.ColorUtils;
32
33 import java.awt.Color;
34 import java.util.LinkedHashMap;
35 import java.util.Map;
36
37 public class ColourSchemes implements ApplicationSingletonI
38 {
39
40   /**
41    * Returns the singleton instance of this class
42    * 
43    * @return
44    */
45   public static ColourSchemes getInstance()
46   {
47     return ApplicationSingletonProvider.getInstance(ColourSchemes.class);
48   }
49
50   private ColourSchemes()
51   {
52     loadColourSchemes();
53   }
54
55   /**
56    * ColourSchemeProperty "static"
57    */
58   public Color[] rnaHelices = null;
59
60   /**
61    * delete the existing cached RNA helices colours
62    */
63   public static void resetRnaHelicesShading()
64   {
65     getInstance().rnaHelices = null;
66   }
67
68   public static void initRnaHelicesShading(int n)
69   {
70     int i = 0;
71     ColourSchemes j = getInstance();
72
73     if (j.rnaHelices == null)
74     {
75       j.rnaHelices = new Color[n + 1];
76     }
77     else if (j.rnaHelices != null && j.rnaHelices.length <= n)
78     {
79       Color[] t = new Color[n + 1];
80       System.arraycopy(j.rnaHelices, 0, t, 0, j.rnaHelices.length);
81       i = j.rnaHelices.length;
82       j.rnaHelices = t;
83     }
84     else
85     {
86       return;
87     }
88     // Generate random colors and store
89     for (; i <= n; i++)
90     {
91       j.rnaHelices[i] = ColorUtils.generateRandomColor(Color.white);
92     }
93   }
94
95   /**
96    * a map from scheme name (lower-cased) to an instance of it
97    */
98   private Map<String, ColourSchemeI> schemes;
99
100   /**
101    * Loads an instance of each standard or user-defined colour scheme
102    * 
103    * @return
104    */
105   void loadColourSchemes()
106   {
107     /*
108      * store in an order-preserving map, so items can be added to menus 
109      * in the order in which they are 'discovered'
110      */
111     schemes = new LinkedHashMap<>();
112
113     for (JalviewColourScheme cs : JalviewColourScheme.values())
114     {
115       try
116       {
117         registerColourScheme(
118                 cs.getSchemeClass().getDeclaredConstructor().newInstance());
119       } catch (InstantiationException | IllegalAccessException e)
120       {
121         System.err.println("Error instantiating colour scheme for "
122                 + cs.toString() + " " + e.getMessage());
123         e.printStackTrace();
124       } catch (ReflectiveOperationException roe)
125       {
126         roe.printStackTrace();
127       }
128     }
129   }
130
131   /**
132    * Registers a colour scheme
133    * 
134    * @param cs
135    */
136   public void registerColourScheme(ColourSchemeI cs)
137   {
138     String name = cs.getSchemeName();
139     if (name == null)
140     {
141       System.err.println("ColourScheme name may not be null");
142       return;
143     }
144
145     /*
146      * name is lower-case for non-case-sensitive lookup
147      * (name in the colour keeps its true case)
148      */
149     String lower = name.toLowerCase(Locale.ROOT);
150     if (schemes.containsKey(lower))
151     {
152       System.err
153               .println("Warning: overwriting colour scheme named " + name);
154     }
155     schemes.put(lower, cs);
156   }
157
158   /**
159    * Removes a colour scheme by name
160    * 
161    * @param name
162    */
163   public void removeColourScheme(String name)
164   {
165     if (name != null)
166     {
167       schemes.remove(name.toLowerCase(Locale.ROOT));
168     }
169   }
170
171   /**
172    * Returns an instance of the colour scheme with which the given view may be
173    * coloured
174    * 
175    * @param name
176    *          name of the colour scheme
177    * @param viewport
178    * @param forData
179    *          the data to be coloured
180    * @param optional
181    *          map from hidden representative sequences to the sequences they
182    *          represent
183    * @return
184    */
185   public ColourSchemeI getColourScheme(String name, AlignViewportI viewport,
186           AnnotatedCollectionI forData,
187           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
188   {
189     if (name == null)
190     {
191       return null;
192     }
193     ColourSchemeI cs = schemes.get(name.toLowerCase(Locale.ROOT));
194     return cs == null ? null : cs.getInstance(viewport, forData);
195   }
196
197   /**
198    * Returns an instance of the colour scheme with which the given view may be
199    * coloured
200    * 
201    * @param name
202    *          name of the colour scheme
203    * @param forData
204    *          the data to be coloured
205    * @return
206    */
207   public ColourSchemeI getColourScheme(String name,
208           AnnotatedCollectionI forData)
209   {
210     return getColourScheme(name, null, forData, null);
211   }
212
213   /**
214    * Returns an iterable set of the colour schemes, in the order in which they
215    * were added
216    * 
217    * @return
218    */
219   public Iterable<ColourSchemeI> getColourSchemes()
220   {
221     return schemes.values();
222   }
223
224   /**
225    * Answers true if there is a scheme with the given name, else false. The test
226    * is not case-sensitive.
227    * 
228    * @param name
229    * @return
230    */
231   public boolean nameExists(String name)
232   {
233     if (name == null)
234     {
235       return false;
236     }
237     return schemes.containsKey(name.toLowerCase(Locale.ROOT));
238   }
239 }