2fbdeabf2395258ad4874d331e956b6a7b49f1a7
[jalview.git] / src / jalview / appletgui / 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.appletgui;
22
23 import jalview.datamodel.SequenceI;
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.Frame;
30 import java.awt.Graphics;
31 import java.awt.Image;
32
33 import javax.swing.JComponent;
34
35 public class OverviewCanvas extends JComponent
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 OverviewDimensions od;
44
45   private Image miniMe;
46
47   private Image offscreen;
48
49   private AlignViewport av;
50
51   // Can set different properties in this seqCanvas than
52   // main visible SeqCanvas
53   private SequenceRenderer sr;
54
55   private FeatureRenderer fr;
56
57   private Frame nullFrame;
58
59   public OverviewCanvas(OverviewDimensions overviewDims,
60           AlignViewport alignvp)
61   {
62     od = overviewDims;
63     av = alignvp;
64
65     nullFrame = new Frame();
66     nullFrame.addNotify();
67
68     sr = new SequenceRenderer(av);
69     sr.graphics = nullFrame.getGraphics();
70     sr.renderGaps = false;
71     sr.forOverview = true;
72     fr = new FeatureRenderer(av);
73   }
74
75   /*
76    * Signals to drawing code that the associated alignment viewport
77    * has changed and a redraw will be required
78    */
79   public boolean restartDraw()
80   {
81     synchronized (this)
82     {
83       if (updaterunning)
84       {
85         restart = true;
86       }
87       else
88       {
89         updaterunning = true;
90       }
91       return restart;
92     }
93   }
94
95   public void draw(boolean showSequenceFeatures, boolean showAnnotation,
96           AlignmentPanel ap)
97   {
98     miniMe = null;
99
100     if (av.isShowSequenceFeatures())
101     {
102       fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
103     }
104
105     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
106
107     miniMe = nullFrame.createImage(od.getWidth(), od.getHeight());
108     offscreen = nullFrame.createImage(od.getWidth(), od.getHeight());
109
110     Graphics mg = miniMe.getGraphics();
111
112     int alwidth = av.getAlignment().getWidth();
113     int alheight = av.getAlignment().getAbsoluteHeight();
114     float sampleCol = alwidth / (float) od.getWidth();
115     float sampleRow = alheight / (float) od.getSequencesHeight();
116
117     buildImage(sampleRow, sampleCol, mg);
118
119     // check for conservation annotation to make sure overview works for DNA too
120     if (showAnnotation)
121     {
122       for (int col = 0; col < od.getWidth() && !restart; col++)
123       {
124         mg.translate(col, od.getSequencesHeight());
125         ap.annotationPanel.renderer.drawGraph(mg,
126                 av.getAlignmentConservationAnnotation(),
127                 av.getAlignmentConservationAnnotation().annotations,
128                 (int) (sampleCol) + 1, od.getGraphHeight(),
129                 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
130         mg.translate(-col, -od.getSequencesHeight());
131       }
132     }
133     System.gc();
134
135     if (restart)
136     {
137       restart = false;
138       draw(showSequenceFeatures, showAnnotation, ap);
139     }
140     else
141     {
142       updaterunning = false;
143     }
144   }
145
146   /*
147    * Build the overview panel image
148    */
149   private void buildImage(float sampleRow, float sampleCol, Graphics mg)
150   {
151     int lastcol = 0;
152     int lastrow = 0;
153     int xstart = 0;
154     int ystart = 0;
155     Color color = Color.yellow;
156     int sameRow = 0;
157     int sameCol = 0;
158
159     SequenceI seq = null;
160     FeatureColourFinder finder = new FeatureColourFinder(fr);
161
162     final boolean hasHiddenCols = av.hasHiddenColumns();
163     boolean hiddenRow = false;
164
165     for (int row = 0; row < od.getSequencesHeight() && !restart; row++)
166     {
167       if ((int) (row * sampleRow) == lastrow)
168       {
169         sameRow++;
170       }
171       else
172       {
173         // get the sequence which would be at alignment index 'lastrow' if no
174         // rows were hidden, and determine whether it is hidden or not
175         hiddenRow = av.getAlignment().isHidden(lastrow);
176         seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
177
178         for (int col = 0; col < od.getWidth(); col++)
179         {
180           if ((int) (col * sampleCol) == lastcol
181                   && (int) (row * sampleRow) == lastrow)
182           {
183             sameCol++;
184           }
185           else
186           {
187             lastcol = (int) (col * sampleCol);
188
189             color = getColumnColourFromSequence(seq, hiddenRow,
190                     hasHiddenCols, lastcol, finder);
191
192             mg.setColor(color);
193             if (sameCol == 1 && sameRow == 1)
194             {
195               mg.drawLine(xstart, ystart, xstart, ystart);
196             }
197             else
198             {
199               mg.fillRect(xstart, ystart, sameCol, sameRow);
200             }
201
202             xstart = col;
203             sameCol = 1;
204           }
205         }
206         lastrow = (int) (row * sampleRow);
207         ystart = row;
208         sameRow = 1;
209       }
210     }
211   }
212
213   /*
214    * Find the colour of a sequence at a specified column position
215    */
216   private Color getColumnColourFromSequence(
217           jalview.datamodel.SequenceI seq, boolean hiddenRow,
218           boolean hasHiddenCols, int lastcol, FeatureColourFinder finder)
219   {
220     Color color = Color.white;
221     if (seq.getLength() > lastcol)
222     {
223       color = sr.getResidueColour(seq, lastcol, finder);
224     }
225
226     if (hiddenRow
227             || (hasHiddenCols && !av.getAlignment().getHiddenColumns()
228                     .isVisible(lastcol)))
229     {
230       color = color.darker().darker();
231     }
232     return color;
233   }
234
235   @Override
236   public void update(Graphics g)
237   {
238     paint(g);
239   }
240
241   @Override
242   public void paint(Graphics g)
243   {
244     Graphics og = offscreen.getGraphics();
245     if (miniMe != null)
246     {
247       og.drawImage(miniMe, 0, 0, this);
248       og.setColor(Color.red);
249       od.drawBox(og);
250       g.drawImage(offscreen, 0, 0, this);
251     }
252   }
253
254 }