JAL-2388 Rationalised overview panel drawing thread
[jalview.git] / src / jalview / gui / OverviewCanvas.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.gui;
22
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.SequenceI;
25 import jalview.renderer.AnnotationRenderer;
26 import jalview.renderer.seqfeatures.FeatureColourFinder;
27 import jalview.viewmodel.OverviewDimensions;
28
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.Graphics;
32 import java.awt.image.BufferedImage;
33
34 import javax.swing.JComponent;
35
36
37 public class OverviewCanvas extends JComponent
38 {
39   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
40
41   // This is set true if the alignment view changes whilst
42   // the overview is being calculated
43   private volatile boolean restart = false;
44
45   private BufferedImage miniMe;
46
47   private BufferedImage lastMiniMe = null;
48
49
50
51   // Can set different properties in this seqCanvas than
52   // main visible SeqCanvas
53   private SequenceRenderer sr;
54
55   private jalview.renderer.seqfeatures.FeatureRenderer fr;
56
57   private final AnnotationRenderer renderer = new AnnotationRenderer();
58
59   OverviewDimensions od;
60
61   AlignViewport av;
62
63   AlignmentPanel ap;
64
65   public OverviewCanvas(OverviewDimensions overviewDims,
66           AlignViewportI alignvp, AlignmentPanel alignp)
67   {
68     od = overviewDims;
69     av = alignp.av;
70     ap = alignp;
71
72     sr = new SequenceRenderer(av);
73     sr.renderGaps = false;
74     sr.forOverview = true;
75     fr = new FeatureRenderer(ap);
76   }
77
78   /*
79    * Signals to drawing code that the associated alignment viewport
80    * has changed and a redraw will be required
81    */
82   public void restartDraw()
83   {
84     restart = true;
85   }
86
87   public void draw(boolean showSequenceFeatures, boolean showAnnotation)
88   {
89     miniMe = null;
90
91     if (showSequenceFeatures)
92     {
93       fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
94     }
95
96     // why do we need to set preferred size again? was set in
97     // updateOverviewImage
98     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
99
100     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
101             BufferedImage.TYPE_INT_RGB);
102
103     Graphics mg = miniMe.getGraphics();
104     mg.setColor(Color.orange);
105     mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
106
107     // calculate sampleCol and sampleRow
108     // alignment width is max number of residues/bases
109     // alignment height is number of sequences
110     int alwidth = av.getAlignment().getWidth();
111     int alheight = av.getAlignment().getAbsoluteHeight();
112
113     // sampleCol or sampleRow is the width/height allocated to each residue
114     // in particular, sometimes we may need more than one row/col of the
115     // BufferedImage allocated
116     // sampleCol is how much of a residue to assign to each pixel
117     // sampleRow is how many sequences to assign to each pixel
118     float sampleCol = alwidth / (float) od.getWidth();
119     float sampleRow = alheight / (float) od.getSequencesHeight();
120
121     buildImage(sampleRow, sampleCol);
122
123     if (showAnnotation)
124     {
125       renderer.updateFromAlignViewport(av);
126       for (int col = 0; col < od.getWidth() && !restart; col++)
127       {
128         mg.translate(col, od.getSequencesHeight());
129         renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
130                 av.getAlignmentConservationAnnotation().annotations,
131                 (int) (sampleCol) + 1, od.getGraphHeight(),
132                 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
133         mg.translate(-col, -od.getSequencesHeight());
134
135       }
136     }
137     System.gc();
138
139     if (restart)
140     {
141       restart = false;
142       draw(showSequenceFeatures, showAnnotation);
143     }
144     else
145     {
146       lastMiniMe = miniMe;
147     }
148   }
149
150   @Override
151   public void paintComponent(Graphics g)
152   {
153     if (restart)
154     {
155       if (lastMiniMe == null)
156       {
157         g.setColor(Color.white);
158         g.fillRect(0, 0, getWidth(), getHeight());
159       }
160       else
161       {
162         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
163       }
164       g.setColor(TRANS_GREY);
165       g.fillRect(0, 0, getWidth(), getHeight());
166     }
167     else if (lastMiniMe != null)
168     {
169       g.drawImage(lastMiniMe, 0, 0, this);
170       if (lastMiniMe != miniMe)
171       {
172         g.setColor(TRANS_GREY);
173         g.fillRect(0, 0, getWidth(), getHeight());
174       }
175     }
176
177     g.setColor(Color.red);
178     od.drawBox(g);
179   }
180
181   /*
182    * Build the overview panel image
183    */
184   private void buildImage(float sampleRow, float sampleCol)
185   {
186     int lastcol = -1;
187     int lastrow = -1;
188     int rgbcolor = Color.white.getRGB();
189
190     SequenceI seq = null;
191     FeatureColourFinder finder = new FeatureColourFinder(fr);
192
193     final boolean hasHiddenCols = av.hasHiddenColumns();
194     boolean hiddenRow = false;
195     // get hidden row and hidden column map once at beginning.
196     // clone featureRenderer settings to avoid race conditions... if state is
197     // updated just need to refresh again
198     for (int row = 0; row < od.getSequencesHeight() && !restart; row++)
199     {
200       boolean doCopy = true;
201       int currentrow = (int) (row * sampleRow);
202       if (currentrow != lastrow)
203       {
204         doCopy = false;
205
206         lastrow = currentrow;
207
208         // get the sequence which would be at alignment index 'lastrow' if no
209         // rows were hidden, and determine whether it is hidden or not
210         hiddenRow = av.getAlignment().isHidden(lastrow);
211         seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
212       }
213
214       for (int col = 0; col < od.getWidth() && !restart; col++)
215       {
216         if (doCopy)
217         {
218           rgbcolor = miniMe.getRGB(col, row - 1);
219         }
220         else if ((int) (col * sampleCol) != lastcol
221                 || (int) (row * sampleRow) != lastrow)
222         {
223           lastcol = (int) (col * sampleCol);
224           rgbcolor = getColumnColourFromSequence(seq, hiddenRow,
225                   hasHiddenCols, lastcol, finder);
226         }
227         // else we just use the color we already have , so don't need to set it
228
229         miniMe.setRGB(col, row, rgbcolor);
230       }
231     }
232   }
233
234   /*
235    * Find the colour of a sequence at a specified column position
236    */
237   private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
238           boolean hiddenRow, boolean hasHiddenCols, int lastcol,
239           FeatureColourFinder finder)
240   {
241     Color color = Color.white;
242
243     if ((seq != null) && (seq.getLength() > lastcol))
244     {
245       color = sr.getResidueColour(seq, lastcol, finder);
246     }
247
248     if (hiddenRow
249             || (hasHiddenCols && !av.getColumnSelection()
250                     .isVisible(lastcol)))
251     {
252       color = color.darker().darker();
253     }
254
255     return color.getRGB();
256   }
257
258 }