JAL-2388 Corrected overview panel behaviour, updated tests
[jalview.git] / src / jalview / gui / OverviewPanel.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.renderer.AnnotationRenderer;
24 import jalview.viewmodel.OverviewDimensions;
25
26 import java.awt.Color;
27 import java.awt.Dimension;
28 import java.awt.Graphics;
29 import java.awt.event.ComponentAdapter;
30 import java.awt.event.ComponentEvent;
31 import java.awt.event.MouseAdapter;
32 import java.awt.event.MouseEvent;
33 import java.awt.event.MouseMotionAdapter;
34 import java.awt.image.BufferedImage;
35
36 import javax.swing.JPanel;
37
38 /**
39  * Panel displaying an overview of the full alignment, with an interactive box
40  * representing the viewport onto the alignment.
41  * 
42  * @author $author$
43  * @version $Revision$
44  */
45 public class OverviewPanel extends JPanel implements Runnable
46 {
47   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
48
49   private final AnnotationRenderer renderer = new AnnotationRenderer();
50
51   private OverviewDimensions od;
52
53   private BufferedImage miniMe;
54
55   private BufferedImage lastMiniMe = null;
56
57   private AlignViewport av;
58
59   private AlignmentPanel ap;
60
61   //
62   private boolean resizing = false;
63
64   // This is set true if the user resizes whilst
65   // the overview is being calculated
66   private boolean resizeAgain = false;
67
68   // Can set different properties in this seqCanvas than
69   // main visible SeqCanvas
70   private SequenceRenderer sr;
71
72   private jalview.renderer.seqfeatures.FeatureRenderer fr;
73
74   /**
75    * Creates a new OverviewPanel object.
76    * 
77    * @param alPanel
78    *          The alignment panel which is shown in the overview panel
79    */
80   public OverviewPanel(AlignmentPanel alPanel)
81   {
82     this.av = alPanel.av;
83     this.ap = alPanel;
84     setLayout(null);
85
86     sr = new SequenceRenderer(av);
87     sr.renderGaps = false;
88     sr.forOverview = true;
89     fr = new FeatureRenderer(alPanel);
90
91     boolean showAnnotation = false;
92     // TODO: in applet this was getSequenceConsensusHash()
93     // check if it makes any functional difference: hconsensus or conservation
94     if (av.getAlignmentConservationAnnotation() == null)
95     {
96       showAnnotation = true;
97     }
98     od = new OverviewDimensions(av, showAnnotation);
99
100     addComponentListener(new ComponentAdapter()
101     {
102       @Override
103       public void componentResized(ComponentEvent evt)
104       {
105         if ((getWidth() != od.getWidth())
106                 || (getHeight() != (od.getHeight())))
107         {
108           updateOverviewImage();
109         }
110       }
111     });
112
113     addMouseMotionListener(new MouseMotionAdapter()
114     {
115       @Override
116       public void mouseDragged(MouseEvent evt)
117       {
118         if (!av.getWrapAlignment())
119         {
120           // TODO: feature: jv2.5 detect shift drag and update selection from
121           // it.
122           od.updateViewportFromMouse(evt.getX(), evt.getY());
123           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
124         }
125       }
126     });
127
128     addMouseListener(new MouseAdapter()
129     {
130       @Override
131       public void mousePressed(MouseEvent evt)
132       {
133         if (!av.getWrapAlignment())
134         {
135           od.updateViewportFromMouse(evt.getX(), evt.getY());
136           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
137         }
138       }
139     });
140
141     updateOverviewImage();
142   }
143
144   /**
145    * Updates the overview image when the related alignment panel is updated
146    */
147   public void updateOverviewImage()
148   {
149     if (resizing)
150     {
151       resizeAgain = true;
152       return;
153     }
154
155     resizing = true;
156
157     if ((getWidth() > 0) && (getHeight() > 0))
158     {
159       od.setWidth(getWidth()); // width = getWidth();
160       od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
161     }
162
163     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
164
165     Thread thread = new Thread(this);
166     thread.start();
167     repaint();
168   }
169
170   @Override
171   public void run()
172   {
173     miniMe = null;
174
175     if (av.isShowSequenceFeatures())
176     {
177       fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
178     }
179
180     // why do we need to set preferred size again? was set in
181     // updateOverviewImage
182     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
183
184     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
185             BufferedImage.TYPE_INT_RGB);
186
187     Graphics mg = miniMe.getGraphics();
188     mg.setColor(Color.orange);
189     mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
190
191     // calculate sampleCol and sampleRow
192     // alignment width is max number of residues/bases
193     // alignment height is number of sequences
194     int alwidth = av.getAlignment().getWidth();
195     int alheight = av.getAlignment().getAbsoluteHeight();
196
197     // sampleCol or sampleRow is the width/height allocated to each residue
198     // in particular, sometimes we may need more than one row/col of the
199     // BufferedImage allocated
200     // sampleCol is how much of a residue to assign to each pixel
201     // sampleRow is how many sequences to assign to each pixel
202     float sampleCol = alwidth / (float) od.getWidth();
203     float sampleRow = alheight / (float) od.getSequencesHeight();
204
205     buildImage(sampleRow, sampleCol);
206
207     if (av.getAlignmentConservationAnnotation() != null)
208     {
209       renderer.updateFromAlignViewport(av);
210       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
211       {
212         mg.translate(col, od.getSequencesHeight());
213         renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
214                 av.getAlignmentConservationAnnotation().annotations,
215                 (int) (sampleCol) + 1, od.getGraphHeight(),
216                 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
217         mg.translate(-col, -od.getSequencesHeight());
218
219       }
220     }
221     System.gc();
222
223     resizing = false;
224
225     if (resizeAgain)
226     {
227       resizeAgain = false;
228       updateOverviewImage();
229     }
230     else
231     {
232       lastMiniMe = miniMe;
233     }
234
235     setBoxPosition();
236   }
237
238   private void buildImage(float sampleRow, float sampleCol)
239   {
240     int lastcol = -1;
241     int lastrow = -1;
242     int color = Color.white.getRGB();
243
244     jalview.datamodel.SequenceI seq = null;
245
246     final boolean hasHiddenCols = av.hasHiddenColumns();
247     boolean hiddenRow = false;
248     // get hidden row and hidden column map once at beginning.
249     // clone featureRenderer settings to avoid race conditions... if state is
250     // updated just need to refresh again
251     for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
252     {
253       boolean doCopy = true;
254       int currentrow = (int) (row * sampleRow);
255       if (currentrow != lastrow)
256       {
257         doCopy = false;
258
259         lastrow = currentrow;
260
261         // get the sequence which would be at alignment index 'lastrow' if no
262         // columns were hidden, and determine whether it is hidden or not
263         hiddenRow = av.getAlignment().isHidden(lastrow);
264         seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
265       }
266
267       if (seq == null)
268       {
269         System.out.println(lastrow + " null");
270         continue;
271       }
272
273       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
274       {
275         if (doCopy)
276         {
277           color = miniMe.getRGB(col, row - 1);
278         }
279         else if ((int) (col * sampleCol) != lastcol
280                 || (int) (row * sampleRow) != lastrow)
281         {
282           lastcol = (int) (col * sampleCol);
283           color = getColumnColourFromSequence(seq, hiddenRow, hasHiddenCols,
284                   lastcol);
285         }
286         // else if ((int) (col * sampleCol) == lastcol && (int) (row *
287         // sampleRow) == lastrow))
288         // we just use the color we already have , so don't need to set it
289
290         miniMe.setRGB(col, row, color);
291       }
292     }
293   }
294
295   /*
296    * Find the colour of a sequence at a specified column position
297    */
298   private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
299           boolean hiddenRow, boolean hasHiddenCols, int lastcol)
300   {
301     int color;
302
303     if (seq.getLength() > lastcol)
304     {
305       color = sr.getResidueBoxColour(seq, lastcol).getRGB();
306
307       if (av.isShowSequenceFeatures())
308       {
309         color = fr.findFeatureColour(color, seq, lastcol);
310       }
311     }
312     else
313     {
314       color = Color.white.getRGB(); // White
315     }
316
317     if (hiddenRow
318             || (hasHiddenCols && !av.getColumnSelection()
319                     .isVisible(lastcol)))
320     {
321       color = new Color(color).darker().darker().getRGB();
322     }
323
324     return color;
325   }
326
327   /**
328    * Update the overview panel box when the associated alignment panel is
329    * changed
330    * 
331    */
332   public void setBoxPosition()
333   {
334     od.setBoxPosition();
335     repaint();
336   }
337
338
339   @Override
340   public void paintComponent(Graphics g)
341   {
342     if (resizing || resizeAgain)
343     {
344       if (lastMiniMe == null)
345       {
346         g.setColor(Color.white);
347         g.fillRect(0, 0, getWidth(), getHeight());
348       }
349       else
350       {
351         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
352       }
353       g.setColor(TRANS_GREY);
354       g.fillRect(0, 0, getWidth(), getHeight());
355     }
356     else if (lastMiniMe != null)
357     {
358       g.drawImage(lastMiniMe, 0, 0, this);
359       if (lastMiniMe != miniMe)
360       {
361         g.setColor(TRANS_GREY);
362         g.fillRect(0, 0, getWidth(), getHeight());
363       }
364     }
365     // TODO: render selected regions
366     g.setColor(Color.red);
367     od.drawBox(g);
368   }
369 }