JAL-2388 Tidies
[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.seqfeatures.FeatureColourFinder;
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 AlignViewportI av;
57
58   public OverviewCanvas(OverviewDimensions overviewDims,
59           AlignViewportI alignvp)
60   {
61     od = overviewDims;
62     av = alignvp;
63
64     sr = new SequenceRenderer(av);
65     sr.renderGaps = false;
66     sr.forOverview = true;
67     fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
68   }
69
70   /**
71    * Update the overview dimensions object used by the canvas (e.g. if we change
72    * from showing hidden columns to hiding them or vice versa)
73    * 
74    * @param overviewDims
75    */
76   public void resetOviewDims(OverviewDimensions overviewDims)
77   {
78     od = overviewDims;
79   }
80
81   /**
82    * Signals to drawing code that the associated alignment viewport has changed
83    * and a redraw will be required
84    */
85   public boolean restartDraw()
86   {
87     synchronized (this)
88     {
89       if (updaterunning)
90       {
91         restart = true;
92       }
93       else
94       {
95         updaterunning = true;
96       }
97       return restart;
98     }
99   }
100
101   /**
102    * Draw the overview sequences
103    * 
104    * @param showSequenceFeatures
105    *          true if sequence features are to be shown
106    * @param showAnnotation
107    *          true if the annotation is to be shown
108    * @param transferRenderer
109    *          the renderer to transfer feature colouring from
110    */
111   public void draw(boolean showSequenceFeatures, boolean showAnnotation,
112           FeatureRenderer transferRenderer)
113   {
114     miniMe = null;
115
116     if (showSequenceFeatures)
117     {
118       fr.transferSettings(transferRenderer);
119     }
120     FeatureColourFinder finder = new FeatureColourFinder(fr);
121
122     // why do we need to set preferred size again? was set in
123     // updateOverviewImage
124     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
125
126     OverviewRenderer or = new OverviewRenderer(sr, finder, od);
127     miniMe = or.draw(od.getRows(av.getRanges(), av.getAlignment()),
128             od.getColumns(av.getRanges(), av.getColumnSelection()));
129
130     Graphics mg = miniMe.getGraphics();
131
132     if (showAnnotation)
133     {
134       mg.translate(0, od.getSequencesHeight());
135       or.drawGraph(mg, av.getAlignmentConservationAnnotation(),
136               av.getCharWidth(), od.getGraphHeight(),
137               od.getColumns(av.getRanges(), av.getColumnSelection()));
138       mg.translate(0, -od.getSequencesHeight());
139     }
140     System.gc();
141
142     if (restart)
143     {
144       restart = false;
145       draw(showSequenceFeatures, showAnnotation, transferRenderer);
146     }
147     else
148     {
149       updaterunning = false;
150       lastMiniMe = miniMe;
151     }
152   }
153
154   @Override
155   public void paintComponent(Graphics g)
156   {
157     if (restart)
158     {
159       if (lastMiniMe == null)
160       {
161         g.setColor(Color.white);
162         g.fillRect(0, 0, getWidth(), getHeight());
163       }
164       else
165       {
166         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
167       }
168       g.setColor(TRANS_GREY);
169       g.fillRect(0, 0, getWidth(), getHeight());
170     }
171     else if (lastMiniMe != null)
172     {
173       g.drawImage(lastMiniMe, 0, 0, this);
174       if (lastMiniMe != miniMe)
175       {
176         g.setColor(TRANS_GREY);
177         g.fillRect(0, 0, getWidth(), getHeight());
178       }
179     }
180
181     g.setColor(Color.red);
182     od.drawBox(g);
183   }
184
185 }