JAL-2587 Removed duplicate events fired to progress bar
[jalview.git] / src / jalview / renderer / OverviewRenderer.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.api.AlignmentColsCollectionI;
24 import jalview.api.AlignmentRowsCollectionI;
25 import jalview.api.RendererListenerI;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.SequenceI;
29 import jalview.renderer.seqfeatures.FeatureColourFinder;
30 import jalview.renderer.seqfeatures.FeatureRenderer;
31 import jalview.viewmodel.OverviewDimensions;
32
33 import java.awt.Color;
34 import java.awt.Graphics;
35 import java.awt.image.BufferedImage;
36 import java.beans.PropertyChangeSupport;
37
38 public class OverviewRenderer
39 {
40   public static final String UPDATE = "OverviewUpdate";
41
42   private static final int MAX_PROGRESS = 100;
43
44   private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
45           this);
46
47   private FeatureColourFinder finder;
48
49   private jalview.api.SequenceRenderer sr;
50
51   // image to render on
52   private BufferedImage miniMe;
53
54   // raw number of pixels to allocate to each column
55   private float pixelsPerCol;
56
57   // raw number of pixels to allocate to each row
58   private float pixelsPerSeq;
59
60   // height in pixels of graph
61   private int graphHeight;
62
63   // flag to indicate whether to halt drawing
64   private volatile boolean redraw = false;
65
66   public OverviewRenderer(jalview.api.SequenceRenderer seqRenderer,
67           FeatureRenderer fr, OverviewDimensions od)
68   {
69     sr = seqRenderer;
70     finder = new FeatureColourFinder(fr);
71
72     pixelsPerCol = od.getPixelsPerCol();
73     pixelsPerSeq = od.getPixelsPerSeq();
74     graphHeight = od.getGraphHeight();
75     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
76             BufferedImage.TYPE_INT_RGB);
77   }
78
79   /**
80    * Draw alignment rows and columns onto an image
81    * 
82    * @param rit
83    *          Iterator over rows to be drawn
84    * @param cit
85    *          Iterator over columns to be drawn
86    * @return image containing the drawing
87    */
88   public BufferedImage draw(AlignmentRowsCollectionI rows,
89           AlignmentColsCollectionI cols)
90   {
91     int rgbcolor = Color.white.getRGB();
92     int seqIndex = 0;
93     int pixelRow = 0;
94     int alignmentHeight = miniMe.getHeight() - graphHeight;
95     int totalPixels = miniMe.getWidth() * alignmentHeight;
96
97     int lastRowUpdate = 0;
98     int lastUpdate = 0;
99     changeSupport.firePropertyChange(UPDATE, -1, 0);
100
101     for (int alignmentRow : rows)
102     {
103       if (redraw)
104       {
105         break;
106       }
107
108       // get details of this alignment row
109       boolean hidden = rows.isHidden(alignmentRow);
110       SequenceI seq = rows.getSequence(alignmentRow);
111
112       // calculate where this row extends to in pixels
113       int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
114               miniMe.getHeight() - 1);
115
116       int colIndex = 0;
117       int pixelCol = 0;
118       for (int alignmentCol : cols)
119       {
120         if (redraw)
121         {
122           break;
123         }
124
125         // calculate where this column extends to in pixels
126         int endCol = Math.min(
127                 Math.round((colIndex + 1) * pixelsPerCol) - 1,
128                 miniMe.getWidth() - 1);
129
130         // don't do expensive colour determination if we're not going to use it
131         // NB this is important to avoid performance issues in the overview
132         // panel
133         if (pixelCol <= endCol)
134         {
135           // determine the colour based on the sequence and column position
136           rgbcolor = getColumnColourFromSequence(seq,
137                   hidden || cols.isHidden(alignmentCol), alignmentCol,
138                   finder);
139
140           // fill in the appropriate number of pixels
141           for (int row = pixelRow; row <= endRow; ++row)
142           {
143             for (int col = pixelCol; col <= endCol; ++col)
144             {
145               miniMe.setRGB(col, row, rgbcolor);
146             }
147           }
148
149           // store last update value
150           lastUpdate = sendProgressUpdate(
151                   (pixelCol + 1) * (endRow - pixelRow), totalPixels,
152                   lastRowUpdate, lastUpdate);
153           pixelCol = endCol + 1;
154         }
155         colIndex++;
156       }
157
158       if (pixelRow != endRow + 1)
159       {
160         // store row offset and last update value
161         lastRowUpdate = sendProgressUpdate(endRow + 1, alignmentHeight, 0,
162                 lastUpdate);
163         lastUpdate = lastRowUpdate;
164         pixelRow = endRow + 1;
165       }
166       seqIndex++;
167     }
168
169     // final update to progress bar if present
170     if (redraw)
171     {
172       sendProgressUpdate(pixelRow - 1, alignmentHeight, 0, 0);
173     }
174     else
175     {
176       sendProgressUpdate(alignmentHeight, miniMe.getHeight(), 0, 0);
177     }
178     return miniMe;
179   }
180
181   /*
182    * Calculate progress update value and fire event
183    * @param rowOffset number of rows to offset calculation by
184    * @return new rowOffset - return value only to be used when at end of a row
185    */
186   private int sendProgressUpdate(int position, int maximum, int rowOffset,
187           int lastUpdate)
188   {
189     int newUpdate = rowOffset
190             + Math.round(MAX_PROGRESS * ((float) position / maximum));
191     if (newUpdate > lastUpdate)
192     {
193       changeSupport.firePropertyChange(UPDATE, rowOffset, newUpdate);
194       return newUpdate;
195     }
196     return newUpdate;
197   }
198
199   /*
200    * Find the colour of a sequence at a specified column position
201    */
202   private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
203           boolean isHidden, int lastcol, FeatureColourFinder fcfinder)
204   {
205     Color color = Color.white;
206
207     if ((seq != null) && (seq.getLength() > lastcol))
208     {
209       color = sr.getResidueColour(seq, lastcol, fcfinder);
210     }
211
212     if (isHidden)
213     {
214       color = color.darker().darker();
215     }
216
217     return color.getRGB();
218   }
219
220   /**
221    * Draw the alignment annotation in the overview panel
222    * 
223    * @param g
224    *          the graphics object to draw on
225    * @param anno
226    *          alignment annotation information
227    * @param charWidth
228    *          alignment character width value
229    * @param y
230    *          y-position for the annotation graph
231    * @param cols
232    *          the collection of columns used in the overview panel
233    */
234   public void drawGraph(Graphics g, AlignmentAnnotation anno, int charWidth,
235           int y, AlignmentColsCollectionI cols)
236   {
237     Annotation[] annotations = anno.annotations;
238     g.setColor(Color.white);
239     g.fillRect(0, 0, miniMe.getWidth(), y);
240
241     int height;
242     int colIndex = 0;
243     int pixelCol = 0;
244     for (int alignmentCol : cols)
245     {
246       if (redraw)
247       {
248         changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1, 0);
249         break;
250       }
251
252       if (alignmentCol >= annotations.length)
253       {
254         break; // no more annotations to draw here
255       }
256       else
257       {
258         int endCol = Math.min(
259                 Math.round((colIndex + 1) * pixelsPerCol) - 1,
260                 miniMe.getWidth() - 1);
261
262         if (annotations[alignmentCol] != null)
263         {
264           if (annotations[alignmentCol].colour == null)
265           {
266             g.setColor(Color.black);
267           }
268           else
269           {
270             g.setColor(annotations[alignmentCol].colour);
271           }
272
273           height = (int) ((annotations[alignmentCol].value / anno.graphMax) * y);
274           if (height > y)
275           {
276             height = y;
277           }
278
279           g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
280         }
281
282         pixelCol = endCol + 1;
283         colIndex++;
284       }
285     }
286     changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1,
287             MAX_PROGRESS);
288   }
289
290   public void setRedraw(boolean b)
291   {
292     synchronized (this)
293     {
294       redraw = b;
295     }
296   }
297
298   public void addPropertyChangeListener(RendererListenerI listener)
299   {
300     changeSupport.addPropertyChangeListener(listener);
301   }
302
303   public void removePropertyChangeListener(RendererListenerI listener)
304   {
305     changeSupport.removePropertyChangeListener(listener);
306   }
307 }