0b97682a43799274fd24212524e5dee5db41c529
[jalview.git] / src / jalview / gui / OverviewPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import jalview.renderer.AnnotationRenderer;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.awt.image.*;
25 import javax.swing.*;
26
27 /**
28  * DOCUMENT ME!
29  * 
30  * @author $author$
31  * @version $Revision$
32  */
33 public class OverviewPanel extends JPanel implements Runnable
34 {
35   BufferedImage miniMe;
36
37   AlignViewport av;
38
39   AlignmentPanel ap;
40
41   final AnnotationRenderer renderer = new AnnotationRenderer();
42
43   float scalew = 1f;
44
45   float scaleh = 1f;
46
47   int width;
48
49   int sequencesHeight;
50
51   int graphHeight = 20;
52
53   int boxX = -1;
54
55   int boxY = -1;
56
57   int boxWidth = -1;
58
59   int boxHeight = -1;
60
61   boolean resizing = false;
62
63   // Can set different properties in this seqCanvas than
64   // main visible SeqCanvas
65   SequenceRenderer sr;
66
67   FeatureRenderer fr;
68
69   /**
70    * Creates a new OverviewPanel object.
71    * 
72    * @param ap
73    *          DOCUMENT ME!
74    */
75   public OverviewPanel(AlignmentPanel ap)
76   {
77     this.av = ap.av;
78     this.ap = ap;
79     setLayout(null);
80
81     sr = new SequenceRenderer(av);
82     sr.renderGaps = false;
83     sr.forOverview = true;
84     fr = new FeatureRenderer(ap);
85
86     // scale the initial size of overviewpanel to shape of alignment
87     float initialScale = (float) av.getAlignment().getWidth()
88             / (float) av.getAlignment().getHeight();
89
90     if (av.getAlignmentConservationAnnotation() == null)
91     {
92       graphHeight = 0;
93     }
94
95     if (av.getAlignment().getWidth() > av.getAlignment().getHeight())
96     {
97       // wider
98       width = 400;
99       sequencesHeight = (int) (400f / initialScale);
100       if (sequencesHeight < 40)
101       {
102         sequencesHeight = 40;
103       }
104     }
105     else
106     {
107       // taller
108       width = (int) (400f * initialScale);
109       sequencesHeight = 300;
110
111       if (width < 120)
112       {
113         width = 120;
114       }
115     }
116
117     addComponentListener(new ComponentAdapter()
118     {
119       @Override
120       public void componentResized(ComponentEvent evt)
121       {
122         if ((getWidth() != width)
123                 || (getHeight() != (sequencesHeight + graphHeight)))
124         {
125           updateOverviewImage();
126         }
127       }
128     });
129
130     addMouseMotionListener(new MouseMotionAdapter()
131     {
132       @Override
133       public void mouseDragged(MouseEvent evt)
134       {
135         if (!av.wrapAlignment)
136         {
137           // TODO: feature: jv2.5 detect shift drag and update selection from
138           // it.
139           boxX = evt.getX();
140           boxY = evt.getY();
141           checkValid();
142         }
143       }
144     });
145
146     addMouseListener(new MouseAdapter()
147     {
148       @Override
149       public void mousePressed(MouseEvent evt)
150       {
151         if (!av.wrapAlignment)
152         {
153           boxX = evt.getX();
154           boxY = evt.getY();
155           checkValid();
156         }
157       }
158     });
159
160     updateOverviewImage();
161   }
162
163   /**
164    * DOCUMENT ME!
165    */
166   void checkValid()
167   {
168     if (boxY < 0)
169     {
170       boxY = 0;
171     }
172
173     if (boxY > (sequencesHeight - boxHeight))
174     {
175       boxY = sequencesHeight - boxHeight + 1;
176     }
177
178     if (boxX < 0)
179     {
180       boxX = 0;
181     }
182
183     if (boxX > (width - boxWidth))
184     {
185       if (av.hasHiddenColumns())
186       {
187         // Try smallest possible box
188         boxWidth = (int) ((av.endRes - av.startRes + 1) * av.getCharWidth() * scalew);
189       }
190       boxX = width - boxWidth;
191     }
192
193     int col = (int) (boxX / scalew / av.getCharWidth());
194     int row = (int) (boxY / scaleh / av.getCharHeight());
195
196     if (av.hasHiddenColumns())
197     {
198       if (!av.getColumnSelection().isVisible(col))
199       {
200         return;
201       }
202
203       col = av.getColumnSelection().findColumnPosition(col);
204     }
205
206     if (av.hasHiddenRows())
207     {
208       row = av.getAlignment().getHiddenSequences()
209               .findIndexWithoutHiddenSeqs(row);
210     }
211
212     ap.setScrollValues(col, row);
213
214   }
215
216   /**
217    * DOCUMENT ME!
218    */
219   public void updateOverviewImage()
220   {
221     if (resizing)
222     {
223       resizeAgain = true;
224       return;
225     }
226
227     resizing = true;
228
229     if ((getWidth() > 0) && (getHeight() > 0))
230     {
231       width = getWidth();
232       sequencesHeight = getHeight() - graphHeight;
233     }
234
235     setPreferredSize(new Dimension(width, sequencesHeight + graphHeight));
236
237     Thread thread = new Thread(this);
238     thread.start();
239     repaint();
240   }
241
242   // This is set true if the user resizes whilst
243   // the overview is being calculated
244   boolean resizeAgain = false;
245
246   /**
247    * DOCUMENT ME!
248    */
249   @Override
250   public void run()
251   {
252     miniMe = null;
253
254     if (av.showSequenceFeatures)
255     {
256       fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
257     }
258
259     int alwidth = av.getAlignment().getWidth();
260     int alheight = av.getAlignment().getHeight()
261             + av.getAlignment().getHiddenSequences().getSize();
262
263     setPreferredSize(new Dimension(width, sequencesHeight + graphHeight));
264
265     int fullsizeWidth = alwidth * av.getCharWidth();
266     int fullsizeHeight = alheight * av.getCharHeight();
267
268     scalew = (float) width / (float) fullsizeWidth;
269     scaleh = (float) sequencesHeight / (float) fullsizeHeight;
270
271     miniMe = new BufferedImage(width, sequencesHeight + graphHeight,
272             BufferedImage.TYPE_INT_RGB);
273
274     Graphics mg = miniMe.getGraphics();
275     mg.setColor(Color.orange);
276     mg.fillRect(0, 0, width, miniMe.getHeight());
277
278     float sampleCol = (float) alwidth / (float) width;
279     float sampleRow = (float) alheight / (float) sequencesHeight;
280
281     int lastcol = -1, lastrow = -1;
282     int color = Color.white.getRGB();
283     int row, col;
284     jalview.datamodel.SequenceI seq;
285     boolean hiddenRow = false;
286     for (row = 0; row < sequencesHeight; row++)
287     {
288       if ((int) (row * sampleRow) == lastrow)
289       {
290         // No need to recalculate the colours,
291         // Just copy from the row above
292         for (col = 0; col < width; col++)
293         {
294           miniMe.setRGB(col, row, miniMe.getRGB(col, row - 1));
295         }
296         continue;
297       }
298
299       lastrow = (int) (row * sampleRow);
300
301       hiddenRow = false;
302       if (av.hasHiddenRows())
303       {
304         seq = av.getAlignment().getHiddenSequences()
305                 .getHiddenSequence(lastrow);
306         if (seq == null)
307         {
308           int index = av.getAlignment().getHiddenSequences()
309                   .findIndexWithoutHiddenSeqs(lastrow);
310
311           seq = av.getAlignment().getSequenceAt(index);
312         }
313         else
314         {
315           hiddenRow = true;
316         }
317       }
318       else
319       {
320         seq = av.getAlignment().getSequenceAt(lastrow);
321       }
322
323       if (seq == null)
324       {
325         System.out.println(lastrow + " null");
326         continue;
327       }
328
329       for (col = 0; col < width; col++)
330       {
331         if ((int) (col * sampleCol) == lastcol
332                 && (int) (row * sampleRow) == lastrow)
333         {
334           miniMe.setRGB(col, row, color);
335           continue;
336         }
337
338         lastcol = (int) (col * sampleCol);
339
340         if (seq.getLength() > lastcol)
341         {
342           color = sr.getResidueBoxColour(seq, lastcol).getRGB();
343
344           if (av.showSequenceFeatures)
345           {
346             color = fr.findFeatureColour(color, seq, lastcol);
347           }
348         }
349         else
350         {
351           color = -1; // White
352         }
353
354         if (hiddenRow
355                 || (av.hasHiddenColumns() && !av.getColumnSelection()
356                         .isVisible(lastcol)))
357         {
358           color = new Color(color).darker().darker().getRGB();
359         }
360
361         miniMe.setRGB(col, row, color);
362
363       }
364     }
365
366     if (av.getAlignmentConservationAnnotation() != null)
367     {
368       renderer.updateFromAlignViewport(av);
369       for (col = 0; col < width; col++)
370       {
371         lastcol = (int) (col * sampleCol);
372         {
373           mg.translate(col, sequencesHeight);
374           renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
375                   av.getAlignmentConservationAnnotation().annotations,
376                   (int) (sampleCol) + 1, graphHeight,
377                   (int) (col * sampleCol), (int) (col * sampleCol) + 1);
378           mg.translate(-col, -sequencesHeight);
379         }
380       }
381     }
382     System.gc();
383
384     resizing = false;
385
386     setBoxPosition();
387
388     if (resizeAgain)
389     {
390       resizeAgain = false;
391       updateOverviewImage();
392     }
393   }
394
395   /**
396    * DOCUMENT ME!
397    */
398   public void setBoxPosition()
399   {
400     int fullsizeWidth = av.getAlignment().getWidth() * av.getCharWidth();
401     int fullsizeHeight = (av.getAlignment().getHeight() + av.getAlignment()
402             .getHiddenSequences().getSize())
403             * av.getCharHeight();
404
405     int startRes = av.getStartRes();
406     int endRes = av.getEndRes();
407
408     if (av.hasHiddenColumns())
409     {
410       startRes = av.getColumnSelection().adjustForHiddenColumns(startRes);
411       endRes = av.getColumnSelection().adjustForHiddenColumns(endRes);
412     }
413
414     int startSeq = av.startSeq;
415     int endSeq = av.endSeq;
416
417     if (av.hasHiddenRows())
418     {
419       startSeq = av.getAlignment().getHiddenSequences()
420               .adjustForHiddenSeqs(startSeq);
421
422       endSeq = av.getAlignment().getHiddenSequences()
423               .adjustForHiddenSeqs(endSeq);
424
425     }
426
427     scalew = (float) width / (float) fullsizeWidth;
428     scaleh = (float) sequencesHeight / (float) fullsizeHeight;
429
430     boxX = (int) (startRes * av.getCharWidth() * scalew);
431     boxY = (int) (startSeq * av.getCharHeight() * scaleh);
432
433     if (av.hasHiddenColumns())
434     {
435       boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew);
436     }
437     else
438     {
439       boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew);
440     }
441
442     boxHeight = (int) ((endSeq - startSeq) * av.getCharHeight() * scaleh);
443
444     repaint();
445   }
446
447   /**
448    * DOCUMENT ME!
449    * 
450    * @param g
451    *          DOCUMENT ME!
452    */
453   @Override
454   public void paintComponent(Graphics g)
455   {
456     if (resizing)
457     {
458       g.setColor(Color.white);
459       g.fillRect(0, 0, getWidth(), getHeight());
460     }
461     else if (miniMe != null)
462     {
463       g.drawImage(miniMe, 0, 0, this);
464     }
465
466     g.setColor(Color.red);
467     g.drawRect(boxX, boxY, boxWidth, boxHeight);
468     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
469
470   }
471 }