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