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