57070f2dbe6886c4885c74b141e1d098213fdf4b
[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.renderer.OverviewRenderer;
25 import jalview.viewmodel.OverviewDimensions;
26
27 import java.awt.AlphaComposite;
28 import java.awt.Color;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.image.BufferedImage;
34
35 import javax.swing.JComponent;
36 import javax.swing.Timer;
37
38 public class OverviewCanvas extends JComponent
39 {
40   private static final long RUNNING_TIME = 1000;
41
42   private static final int SPEED = 40;
43
44   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
45
46   // This is set true if the alignment view changes whilst
47   // the overview is being calculated
48   private volatile boolean restart = false;
49
50   private volatile boolean updaterunning = false;
51
52   private boolean dispose = false;
53
54   private BufferedImage miniMe;
55
56   private BufferedImage lastMiniMe = null;
57
58   private BufferedImage veryLastMiniMe = null;
59
60   // Can set different properties in this seqCanvas than
61   // main visible SeqCanvas
62   private SequenceRenderer sr;
63
64   private jalview.renderer.seqfeatures.FeatureRenderer fr;
65
66   private OverviewDimensions od;
67
68   private OverviewRenderer or = null;
69
70   private AlignViewportI av;
71
72   private float alpha = 0f;
73
74   private long startTime = -1;
75
76   private final Timer timer;
77
78   private ProgressPanel progressPanel;
79
80   public OverviewCanvas(OverviewDimensions overviewDims,
81           AlignViewportI alignvp, ProgressPanel pp)
82   {
83     od = overviewDims;
84     av = alignvp;
85     progressPanel = pp;
86
87     sr = new SequenceRenderer(av);
88     sr.renderGaps = false;
89     sr.forOverview = true;
90     fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
91
92     setSize(od.getWidth(), od.getHeight());
93
94     timer = new Timer(SPEED, new ActionListener()
95     {
96
97       @Override
98       public void actionPerformed(ActionEvent e)
99       {
100         if (startTime < 0)
101         {
102           startTime = System.currentTimeMillis();
103         }
104         else
105         {
106
107           long time = System.currentTimeMillis();
108           long duration = time - startTime;
109           if (duration >= RUNNING_TIME)
110           {
111             startTime = -1;
112             ((Timer) e.getSource()).stop();
113             alpha = 0f;
114           }
115           else
116           {
117             alpha = 1f - ((float) duration / (float) RUNNING_TIME);
118           }
119           repaint();
120         }
121       }
122     });
123   }
124
125   /**
126    * Update the overview dimensions object used by the canvas (e.g. if we change
127    * from showing hidden columns to hiding them or vice versa)
128    * 
129    * @param overviewDims
130    */
131   public void resetOviewDims(OverviewDimensions overviewDims)
132   {
133     od = overviewDims;
134   }
135
136   /**
137    * Signals to drawing code that the associated alignment viewport has changed
138    * and a redraw will be required
139    */
140   public boolean restartDraw()
141   {
142     synchronized (this)
143     {
144       if (updaterunning)
145       {
146         restart = true;
147         if (or != null)
148         {
149           or.setRedraw(true);
150         }
151       }
152       else
153       {
154         updaterunning = true;
155       }
156       return restart;
157     }
158   }
159
160   /**
161    * Draw the overview sequences
162    * 
163    * @param showSequenceFeatures
164    *          true if sequence features are to be shown
165    * @param showAnnotation
166    *          true if the annotation is to be shown
167    * @param transferRenderer
168    *          the renderer to transfer feature colouring from
169    */
170   public void draw(boolean showSequenceFeatures, boolean showAnnotation,
171           FeatureRenderer transferRenderer)
172   {
173     miniMe = null;
174     veryLastMiniMe = lastMiniMe;
175
176     if (showSequenceFeatures)
177     {
178       fr.transferSettings(transferRenderer);
179     }
180
181     or = new OverviewRenderer(sr, fr, od);
182     or.addPropertyChangeListener(progressPanel);
183     miniMe = or.draw(od.getRows(av.getAlignment()),
184             od.getColumns(av.getAlignment()));
185
186     Graphics mg = miniMe.getGraphics();
187
188     if (showAnnotation)
189     {
190       mg.translate(0, od.getSequencesHeight());
191       or.drawGraph(mg, av.getAlignmentConservationAnnotation(),
192               av.getCharWidth(), od.getGraphHeight(),
193               od.getColumns(av.getAlignment()));
194       mg.translate(0, -od.getSequencesHeight());
195     }
196     System.gc();
197
198     or.removePropertyChangeListener(progressPanel);
199     or = null;
200     if (restart)
201     {
202       restart = false;
203       if (!dispose)
204       {
205         draw(showSequenceFeatures, showAnnotation, transferRenderer);
206       }
207     }
208     else
209     {
210       updaterunning = false;
211       lastMiniMe = miniMe;
212       alpha = 1f;
213       timer.start();
214     }
215   }
216
217   @Override
218   public void paintComponent(Graphics g)
219   {
220     if (restart)
221     {
222       if (lastMiniMe == null)
223       {
224         g.setColor(Color.white);
225         g.fillRect(0, 0, getWidth(), getHeight());
226       }
227       else
228       {
229         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
230       }
231       g.setColor(TRANS_GREY);
232       g.fillRect(0, 0, getWidth(), getHeight());
233     }
234     else if (lastMiniMe != null)
235     {
236       // is this a resize?
237       if ((getWidth() > 0) && (getHeight() > 0)
238               && ((getWidth() != od.getWidth())
239                       || (getHeight() != od.getHeight())))
240       {
241         // if there is annotation, scale the alignment and annotation separately
242         if (od.getGraphHeight() > 0)
243         {
244           BufferedImage topImage = lastMiniMe.getSubimage(0, 0,
245                   od.getWidth(), od.getSequencesHeight());
246           BufferedImage bottomImage = lastMiniMe.getSubimage(0,
247                   od.getSequencesHeight(), od.getWidth(),
248                   od.getGraphHeight());
249
250           // must be done at this point as we rely on using old width/height
251           // above, and new width/height below
252           od.setWidth(getWidth());
253           od.setHeight(getHeight());
254
255           // stick the images back together so lastMiniMe is consistent in the
256           // event of a repaint - BUT probably not thread safe
257           lastMiniMe = new BufferedImage(od.getWidth(), od.getHeight(),
258                   BufferedImage.TYPE_INT_RGB);
259           Graphics lg = lastMiniMe.getGraphics();
260           lg.drawImage(topImage, 0, 0, od.getWidth(),
261                   od.getSequencesHeight(), null);
262           lg.drawImage(bottomImage, 0, od.getSequencesHeight(),
263                   od.getWidth(), od.getGraphHeight(), this);
264           lg.dispose();
265         }
266         else
267         {
268           od.setWidth(getWidth());
269           od.setHeight(getHeight());
270         }
271
272         // scale lastMiniMe to the new size
273         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
274
275         // make sure the box is in the right place
276         od.setBoxPosition(av.getAlignment().getHiddenSequences(),
277                 av.getAlignment().getHiddenColumns());
278       }
279       else // not a resize
280       {
281         if (alpha != 0) // this is a timer triggered dissolve
282         {
283           Graphics2D g2d = (Graphics2D) g.create();
284           
285           // draw the original image
286           g2d.drawImage(veryLastMiniMe, 0, 0, getWidth(), getHeight(),
287                   this);
288
289           // draw the new image on top with varying degrees of transparency
290           g2d.setComposite(AlphaComposite.SrcOver.derive(1f - alpha));
291           g2d.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
292
293           g2d.dispose();
294         }
295         else
296         {
297           // fall back to normal behaviour
298           g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
299         }
300       }
301     }
302
303     // draw the box
304     g.setColor(Color.red);
305     od.drawBox(g);
306   }
307
308   public void dispose()
309   {
310     dispose = true;
311     synchronized (this)
312     {
313       restart = true;
314       if (or != null)
315       {
316         or.setRedraw(true);
317       }
318     }
319   }
320 }