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