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