JAL-3442 (Aug 28 commit) option to use/not use cache
[jalview.git] / src / jalview / renderer / OverviewResColourFinder.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.renderer;
22
23 import jalview.datamodel.SequenceGroup;
24 import jalview.datamodel.SequenceI;
25 import jalview.renderer.seqfeatures.FeatureColourFinder;
26 import jalview.util.Comparison;
27
28 import java.awt.Color;
29
30 public class OverviewResColourFinder extends ResidueColourFinder
31 {
32   final int GAP_COLOUR; // default colour to use at gaps
33
34   final int RESIDUE_COLOUR; // default colour to use at residues
35
36   final Color HIDDEN_COLOUR; // colour for hidden regions
37
38   boolean useLegacy = false;
39
40   public static final Color OVERVIEW_DEFAULT_GAP = Color.lightGray;
41
42   public static final Color OVERVIEW_DEFAULT_LEGACY_GAP = Color.white;
43
44   public static final Color OVERVIEW_DEFAULT_HIDDEN = Color.darkGray
45           .darker();
46
47   /**
48    * Constructor without colour settings (used by applet)
49    */
50   public OverviewResColourFinder()
51   {
52     this(false, OVERVIEW_DEFAULT_GAP, OVERVIEW_DEFAULT_HIDDEN);
53   }
54
55   /**
56    * Constructor with colour settings
57    * 
58    * @param useLegacyColouring
59    *          whether to use legacy gap colouring (white gaps, grey residues)
60    * @param gapCol
61    *          gap colour if not legacy
62    * @param hiddenCol
63    *          hidden region colour (transparency applied by rendering code)
64    */
65   public OverviewResColourFinder(boolean useLegacyColouring, Color gapCol,
66           Color hiddenCol)
67   {
68     if (useLegacyColouring)
69     {
70       GAP_COLOUR = Color.white.getRGB();
71       RESIDUE_COLOUR = Color.lightGray.getRGB();
72     }
73     else
74     {
75       GAP_COLOUR = gapCol.getRGB();
76       RESIDUE_COLOUR = Color.white.getRGB();
77     }
78     HIDDEN_COLOUR = hiddenCol;
79   }
80
81   @Override
82   /**
83    * for Test suite only.
84    */
85   public Color getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
86   {
87     return new Color(getBoxColourInt(shader, seq, i));
88   }
89
90   public int getBoxColourInt(ResidueShaderI shader, SequenceI seq, int i)
91   {
92     char currentChar = seq.getCharAt(i);
93     // In the overview window, gaps are coloured grey, unless the colour scheme
94     // specifies a gap colour, in which case gaps honour the colour scheme
95     // settings
96     boolean isGap = Comparison.isGap(currentChar);
97     if (shader.getColourScheme() == null)
98     {
99       return (isGap ? GAP_COLOUR : RESIDUE_COLOUR);
100     }
101     return (isGap && !shader.getColourScheme().hasGapColour() ? GAP_COLOUR
102             : shader.findColourInt(currentChar, i, seq));
103   }
104
105   /**
106    * For test suite only.
107    */
108   @Override
109   public Color getResidueColour(boolean showBoxes, ResidueShaderI shader,
110           SequenceGroup[] allGroups, final SequenceI seq, int i,
111           FeatureColourFinder finder)
112   {
113     return new Color(getResidueColourInt(showBoxes, shader, allGroups, seq,
114             i, finder));
115   }
116
117   private boolean useCache = //
118           true // option to use cache
119   // false // option to not use cache
120   ;
121
122   public int getResidueColourInt(boolean showBoxes, ResidueShaderI shader,
123           SequenceGroup[] allGroups, final SequenceI seq, int i,
124           FeatureColourFinder finder)
125   {
126
127
128     int c = (useCache ? seq.getColor(i) : 0);
129     if (c != 0)
130     {
131       return c;
132     }
133
134     int col = getResidueBoxColourInt(showBoxes, shader, allGroups, seq, i);
135
136     // if there's a FeatureColourFinder we might override the residue colour
137     // here with feature colouring
138     c = (finder == null || finder.noFeaturesDisplayed() ? col
139             : finder.findFeatureColourInt(col, seq, i));
140     return (useCache ? seq.setColor(i, c) : c);
141   }
142
143   /**
144    * For test suite only.
145    */
146   @Override
147   protected Color getResidueBoxColour(boolean showBoxes,
148           ResidueShaderI shader, SequenceGroup[] allGroups, SequenceI seq,
149           int i)
150   {
151     return new Color(
152             getResidueBoxColourInt(showBoxes, shader, allGroups, seq, i));
153   }
154
155   /**
156    * In the overview, the showBoxes setting is ignored, as the overview displays
157    * the colours regardless.
158    */
159   protected int getResidueBoxColourInt(boolean showBoxes,
160           ResidueShaderI shader, SequenceGroup[] allGroups, SequenceI seq,
161           int i)
162   {
163     SequenceGroup currentSequenceGroup = getCurrentSequenceGroup(allGroups,
164             i);
165     ResidueShaderI currentShader = (currentSequenceGroup == null ? shader
166             : currentSequenceGroup.getGroupColourScheme());
167     return getBoxColourInt(currentShader, seq, i);
168   }
169
170   /**
171    * Supply hidden colour
172    * 
173    * @return colour of hidden regions
174    */
175   protected Color getHiddenColour()
176   {
177     return HIDDEN_COLOUR;
178   }
179 }