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