JAL-3369 JAL-3253-applet adds embedded dim checking for overview frame
[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.bin.Cache;
25 import jalview.datamodel.AlignmentI;
26 import jalview.renderer.OverviewRenderer;
27 import jalview.renderer.OverviewResColourFinder;
28 import jalview.viewmodel.OverviewDimensions;
29
30 import java.awt.Color;
31 import java.awt.Dimension;
32 import java.awt.Graphics;
33 import java.awt.image.BufferedImage;
34
35 import javax.swing.JPanel;
36
37 @SuppressWarnings("serial")
38 public class OverviewCanvas extends JPanel
39 {
40   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
41
42   // This is set true if the alignment view changes whilst
43   // the overview is being calculated
44   private volatile boolean restart = false;
45
46   private volatile boolean updaterunning = false;
47
48   private boolean disposed = false;
49
50   private BufferedImage lastMiniMe = null;
51
52   // Can set different properties in this seqCanvas than
53   // main visible SeqCanvas
54   private SequenceRenderer sr;
55
56   private jalview.renderer.seqfeatures.FeatureRenderer fr;
57
58   private OverviewDimensions od;
59
60   private OverviewRenderer or = null;
61
62   private AlignViewportI av;
63
64   private OverviewResColourFinder cf;
65
66   private ProgressPanel progressPanel;
67
68   private boolean showSequenceFeatures;
69
70   private boolean showAnnotation;
71
72   private jalview.api.FeatureRenderer featureRenderer;
73
74   private OverviewPanel panel;
75
76   public OverviewCanvas(OverviewPanel panel,
77           OverviewDimensions overviewDims,
78           AlignViewportI alignvp, ProgressPanel pp)
79   {
80     this.panel = panel;
81     od = overviewDims;
82     av = alignvp;
83     progressPanel = pp;
84
85     sr = new SequenceRenderer(av);
86     sr.renderGaps = false;
87     fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
88
89     boolean useLegacy = Cache.getDefault(Preferences.USE_LEGACY_GAP, false);
90     Color gapCol = Cache.getDefaultColour(Preferences.GAP_COLOUR,
91             jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_GAP);
92     Color hiddenCol = Cache.getDefaultColour(Preferences.HIDDEN_COLOUR,
93             jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_HIDDEN);
94     cf = new OverviewResColourFinder(useLegacy, gapCol, hiddenCol);
95
96     setSize(od.getWidth(), od.getHeight());
97     setPreferredSize(getSize()); // BH 2019.07.29 added
98   }
99
100   /**
101    * Update the overview dimensions object used by the canvas (e.g. if we change
102    * from showing hidden columns to hiding them or vice versa)
103    * 
104    * @param overviewDims
105    */
106   public void resetOviewDims(OverviewDimensions overviewDims)
107   {
108     od = overviewDims;
109   }
110
111   /**
112    * Signals to drawing code that the associated alignment viewport has changed
113    * and a redraw will be required
114    */
115   public boolean restartDraw()
116   {
117     synchronized (this)
118     {
119       if (updaterunning)
120       {
121         setRestart("restartDraw");
122       }
123       else
124       {
125         updaterunning = true;
126       }
127       return restart;
128     }
129   }
130
131   private void setRestart(String why)
132   {
133     // System.out.println("OC restart true " + why);
134     restart = true;
135     if (or != null)
136     {
137       or.setRedraw(true);
138     }
139   }
140
141   /**
142    * Draw the overview sequences
143    * 
144    * @param showSequenceFeatures
145    *          true if sequence features are to be shown
146    * @param showAnnotation
147    *          true if the annotation is to be shown
148    * @param featureRenderer
149    *          the renderer to transfer feature colouring from
150    */
151   public void draw(boolean showSequenceFeatures, boolean showAnnotation,
152           jalview.api.FeatureRenderer featureRenderer)
153   {
154     this.showSequenceFeatures = showSequenceFeatures;
155     this.showAnnotation = showAnnotation;
156     this.featureRenderer = featureRenderer;
157
158     // System.out.println("OC draw " + ++ndraw + " showseqf="
159     // + showSequenceFeatures + " showAnno=" + showAnnotation);
160
161     if (showSequenceFeatures)
162     {
163       fr.transferSettings(featureRenderer);
164     }
165
166     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
167
168     AlignmentI al = av.getAlignment();
169     or = new OverviewRenderer(panel.ap, fr, od, al,
170             av.getResidueShading(), cf,
171             progressPanel != null);
172     if (progressPanel != null)
173     {
174       or.addPropertyChangeListener(progressPanel);
175     }
176     or.draw(od.getRows(al), od.getColumns(al));
177   }
178
179   void finalizeDraw(BufferedImage miniMe)
180   {
181     Graphics mg = miniMe.getGraphics();
182     if (showAnnotation)
183     {
184       mg.translate(0, od.getSequencesHeight());
185       or.drawGraph(mg, av.getAlignmentConservationAnnotation(),
186               od.getGraphHeight(), od.getColumns(av.getAlignment()));
187       mg.translate(0, -od.getSequencesHeight());
188     }
189     mg.dispose(); // BH 2019
190     if (progressPanel != null)
191     {
192       or.removePropertyChangeListener(progressPanel);
193     }
194     or = null;
195     if (restart)
196     {
197       restart = false;
198       if (!disposed)
199       {
200         draw(showSequenceFeatures, showAnnotation, featureRenderer);
201       }
202     }
203     else
204     {
205       updaterunning = false;
206       lastMiniMe = miniMe;
207       repaint();
208     }
209
210   }
211   @Override
212   public void paintComponent(Graphics g)
213   {
214
215     int w = getWidth();
216     int h = getHeight();
217     if (w == 0 || od.getBoxWidth() <= 0)
218     {
219       // BH 2019.07.27 removes two unnecessary paints, since boxwidth can be -1
220       // or 0 during early-stage painting
221       return;
222     }
223
224     boolean drawMe = (lastMiniMe != null);
225     if (restart)
226     {
227       if (drawMe)
228       {
229         g.drawImage(lastMiniMe, 0, 0, w, h, this);
230       }
231       else
232       {
233         g.setColor(Color.white);
234         g.fillRect(0, 0, w, h);
235       }
236       g.setColor(TRANS_GREY);
237       g.fillRect(0, 0, w, h);
238       drawMe = false;
239     }
240     else if (drawMe)
241     {
242       // is this a resize?
243       if (w != od.getWidth() ||  h != od.getHeight()) {
244       // if there is annotation, scale the alignment and annotation
245       // separately
246       if (od.getGraphHeight() <= 0 && od.getSequencesHeight() <= 0)
247       {
248         od.setWidth(w);
249         od.setHeight(h);
250         return;
251       }
252         // System.out.println("OC new subimages");
253       BufferedImage topImage = lastMiniMe.getSubimage(0, 0, od.getWidth(),
254               od.getSequencesHeight());
255       BufferedImage bottomImage = lastMiniMe.getSubimage(0,
256               od.getSequencesHeight(), od.getWidth(), od.getGraphHeight());
257
258       // must be done at this point as we rely on using old width/height
259       // above, and new width/height below
260       od.setWidth(w);
261       od.setHeight(h);
262
263       // stick the images back together so lastMiniMe is consistent in the
264       // event of a repaint - BUT probably not thread safe
265       // System.out.println("OC new lastminime " + w + " " + h);
266       lastMiniMe = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
267       Graphics lg = lastMiniMe.getGraphics();
268       lg.drawImage(topImage, 0, 0, w, od.getSequencesHeight(), null);
269       lg.drawImage(bottomImage, 0, od.getSequencesHeight(), w,
270               od.getGraphHeight(), this);
271       lg.dispose();
272       // BH 2019: removed -- this is now taken care of using vpbox in
273       // OverviewDimension
274       // // make sure the box is in the right place
275       // od.setBoxPosition(av.getAlignment().getHiddenSequences(),
276       // av.getAlignment().getHiddenColumns());
277       }
278     }
279
280     if (drawMe)
281     {
282       g.drawImage(lastMiniMe, 0, 0, w, h, this);
283     }
284     // draw the box
285     g.setColor(Color.red);
286     // System.out.println("OC paintComponent nd=" + ndraw + " nr=" + nrepaint
287     // + " np=" + ++npaint);
288     od.drawBox(g);
289   }
290
291   private int ndraw, npaint, nrepaint;
292
293   // @Override
294   // public void repaint()
295   // {
296   // System.out.println("OC repaint " + (++nrepaint));
297   // super.repaint();
298   // }
299
300   public void dispose()
301   {
302     disposed = true;
303     od = null;
304     synchronized (this)
305     {
306       setRestart("dispose");
307     }
308   }
309
310 }