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