JAL-2587 Adjusting height for progress bar
[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 = 2000;
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 BufferedImage miniMe;
53
54   private BufferedImage lastMiniMe = null;
55
56   private BufferedImage veryLastMiniMe = null;
57
58   // Can set different properties in this seqCanvas than
59   // main visible SeqCanvas
60   private SequenceRenderer sr;
61
62   private jalview.renderer.seqfeatures.FeatureRenderer fr;
63
64   private OverviewDimensions od;
65
66   private OverviewRenderer or = null;
67
68   private AlignViewportI av;
69
70   private float alpha = 0f;
71
72   private long startTime = -1;
73
74   private final Timer timer;
75
76   private ProgressPanel progressPanel;
77
78   public OverviewCanvas(OverviewDimensions overviewDims,
79           AlignViewportI alignvp, ProgressPanel pp)
80   {
81     od = overviewDims;
82     av = alignvp;
83     progressPanel = pp;
84
85     sr = new SequenceRenderer(av);
86     sr.renderGaps = false;
87     sr.forOverview = true;
88     fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
89
90     setSize(od.getWidth(), od.getHeight());
91
92     timer = new Timer(SPEED, new ActionListener()
93     {
94
95       @Override
96       public void actionPerformed(ActionEvent e)
97       {
98         if (startTime < 0)
99         {
100           startTime = System.currentTimeMillis();
101         }
102         else
103         {
104
105           long time = System.currentTimeMillis();
106           long duration = time - startTime;
107           if (duration >= RUNNING_TIME)
108           {
109             startTime = -1;
110             ((Timer) e.getSource()).stop();
111             alpha = 0f;
112           }
113           else
114           {
115             alpha = 1f - ((float) duration / (float) RUNNING_TIME);
116           }
117           repaint();
118         }
119       }
120     });
121   }
122
123   /**
124    * Update the overview dimensions object used by the canvas (e.g. if we change
125    * from showing hidden columns to hiding them or vice versa)
126    * 
127    * @param overviewDims
128    */
129   public void resetOviewDims(OverviewDimensions overviewDims)
130   {
131     od = overviewDims;
132   }
133
134   /**
135    * Signals to drawing code that the associated alignment viewport has changed
136    * and a redraw will be required
137    */
138   public boolean restartDraw()
139   {
140     synchronized (this)
141     {
142       if (updaterunning)
143       {
144         restart = true;
145         if (or != null)
146         {
147           or.setRedraw(true);
148         }
149       }
150       else
151       {
152         updaterunning = true;
153       }
154       return restart;
155     }
156   }
157
158   /**
159    * Draw the overview sequences
160    * 
161    * @param showSequenceFeatures
162    *          true if sequence features are to be shown
163    * @param showAnnotation
164    *          true if the annotation is to be shown
165    * @param transferRenderer
166    *          the renderer to transfer feature colouring from
167    */
168   public void draw(boolean showSequenceFeatures, boolean showAnnotation,
169           FeatureRenderer transferRenderer)
170   {
171     // System.out.println(this.getHeight());
172     // setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
173
174     miniMe = null;
175     veryLastMiniMe = lastMiniMe;
176
177     if (showSequenceFeatures)
178     {
179       fr.transferSettings(transferRenderer);
180     }
181
182     or = new OverviewRenderer(sr, fr, od);
183     or.addPropertyChangeListener(progressPanel);
184     miniMe = or.draw(od.getRows(av.getAlignment()),
185             od.getColumns(av.getAlignment()));
186
187     Graphics mg = miniMe.getGraphics();
188
189     if (showAnnotation)
190     {
191       mg.translate(0, od.getSequencesHeight());
192       or.drawGraph(mg, av.getAlignmentConservationAnnotation(),
193               av.getCharWidth(), od.getGraphHeight(),
194               od.getColumns(av.getAlignment()));
195       mg.translate(0, -od.getSequencesHeight());
196     }
197     System.gc();
198
199     or.removePropertyChangeListener(progressPanel);
200     if (restart)
201     {
202       restart = false;
203       draw(showSequenceFeatures, showAnnotation, transferRenderer);
204     }
205     else
206     {
207       updaterunning = false;
208       lastMiniMe = miniMe;
209       alpha = 1f;
210       timer.start();
211     }
212   }
213
214   @Override
215   public void paintComponent(Graphics g)
216   {
217
218     if (restart)
219     {
220       if (lastMiniMe == null)
221       {
222         g.setColor(Color.white);
223         g.fillRect(0, 0, getWidth(), getHeight());
224       }
225       else
226       {
227         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
228       }
229       g.setColor(TRANS_GREY);
230       g.fillRect(0, 0, getWidth(), getHeight());
231     }
232     else if (lastMiniMe != null)
233     {
234       if ((getWidth() > 0) && (getHeight() > 0)
235               && ((getWidth() != od.getWidth())
236                       || (getHeight() != od.getHeight())))
237       {
238         // scale the alignment and annotation separately *** if there is
239         // annotation ***
240         if (od.getGraphHeight() > 0)
241         {
242           BufferedImage topImage = lastMiniMe.getSubimage(0, 0,
243                   od.getWidth(), od.getSequencesHeight());
244           BufferedImage bottomImage = lastMiniMe.getSubimage(0,
245                   od.getSequencesHeight(), od.getWidth(),
246                   od.getGraphHeight());
247
248           // must be done at this point as we rely on using old width/height
249           // above, and new width/height below
250           od.setWidth(getWidth());
251           od.setHeight(getHeight());
252
253           // stick the images back together so lastMiniMe is consistent in the
254           // event of a repaint - BUT probably not thread safe
255           lastMiniMe = new BufferedImage(od.getWidth(), od.getHeight(),
256                   BufferedImage.TYPE_INT_RGB);
257           Graphics lg = lastMiniMe.getGraphics();
258           lg.drawImage(topImage, 0, 0, od.getWidth(),
259                   od.getSequencesHeight(), null);
260           lg.drawImage(bottomImage, 0, od.getSequencesHeight(),
261                   od.getWidth(), od.getGraphHeight(), this);
262           lg.dispose();
263         }
264         else
265         {
266           System.out.println("Resetting height from/to: " + od.getHeight()
267                   + " " + getHeight());
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
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 if (lastMiniMe != miniMe)
296         {
297           g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
298           g.setColor(TRANS_GREY);
299           g.fillRect(0, 0, getWidth(), getHeight());
300         }*/
301         else
302         {
303           // fall back to normal behaviour
304           g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
305         }
306
307       }
308
309     }
310
311     // draw the box
312     g.setColor(Color.red);
313     od.drawBox(g);
314   }
315 }