7fdb610771300f71bc74af9e8f78944a540a3257
[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.renderer.seqfeatures.FeatureColourFinder;
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  * DOCUMENT ME!
40  * 
41  * @author $author$
42  * @version $Revision$
43  */
44 public class OverviewPanel extends JPanel implements Runnable
45 {
46   BufferedImage miniMe;
47
48   AlignViewport av;
49
50   AlignmentPanel ap;
51
52   final AnnotationRenderer renderer = new AnnotationRenderer();
53
54   float scalew = 1f;
55
56   float scaleh = 1f;
57
58   int width;
59
60   int sequencesHeight;
61
62   int graphHeight = 20;
63
64   int boxX = -1;
65
66   int boxY = -1;
67
68   int boxWidth = -1;
69
70   int boxHeight = -1;
71
72   boolean resizing = false;
73
74   // Can set different properties in this seqCanvas than
75   // main visible SeqCanvas
76   SequenceRenderer sr;
77
78   jalview.renderer.seqfeatures.FeatureRenderer fr;
79
80   /**
81    * Creates a new OverviewPanel object.
82    * 
83    * @param ap
84    *          DOCUMENT ME!
85    */
86   public OverviewPanel(AlignmentPanel ap)
87   {
88     this.av = ap.av;
89     this.ap = ap;
90     setLayout(null);
91
92     sr = new SequenceRenderer(av);
93     sr.renderGaps = false;
94     sr.forOverview = true;
95     fr = new FeatureRenderer(ap);
96
97     // scale the initial size of overviewpanel to shape of alignment
98     float initialScale = (float) av.getAlignment().getWidth()
99             / (float) av.getAlignment().getHeight();
100
101     if (av.getAlignmentConservationAnnotation() == null)
102     {
103       graphHeight = 0;
104     }
105
106     if (av.getAlignment().getWidth() > av.getAlignment().getHeight())
107     {
108       // wider
109       width = 400;
110       sequencesHeight = (int) (400f / initialScale);
111       if (sequencesHeight < 40)
112       {
113         sequencesHeight = 40;
114       }
115     }
116     else
117     {
118       // taller
119       width = (int) (400f * initialScale);
120       sequencesHeight = 300;
121
122       if (width < 120)
123       {
124         width = 120;
125       }
126     }
127
128     addComponentListener(new ComponentAdapter()
129     {
130       @Override
131       public void componentResized(ComponentEvent evt)
132       {
133         if ((getWidth() != width)
134                 || (getHeight() != (sequencesHeight + graphHeight)))
135         {
136           updateOverviewImage();
137         }
138       }
139     });
140
141     addMouseMotionListener(new MouseMotionAdapter()
142     {
143       @Override
144       public void mouseDragged(MouseEvent evt)
145       {
146         if (!av.getWrapAlignment())
147         {
148           // TODO: feature: jv2.5 detect shift drag and update selection from
149           // it.
150           boxX = evt.getX();
151           boxY = evt.getY();
152           checkValid();
153         }
154       }
155     });
156
157     addMouseListener(new MouseAdapter()
158     {
159       @Override
160       public void mousePressed(MouseEvent evt)
161       {
162         if (!av.getWrapAlignment())
163         {
164           boxX = evt.getX();
165           boxY = evt.getY();
166           checkValid();
167         }
168       }
169     });
170
171     updateOverviewImage();
172   }
173
174   /**
175    * DOCUMENT ME!
176    */
177   void checkValid()
178   {
179     if (boxY < 0)
180     {
181       boxY = 0;
182     }
183
184     if (boxY > (sequencesHeight - boxHeight))
185     {
186       boxY = sequencesHeight - boxHeight + 1;
187     }
188
189     if (boxX < 0)
190     {
191       boxX = 0;
192     }
193
194     if (boxX > (width - boxWidth))
195     {
196       if (av.hasHiddenColumns())
197       {
198         // Try smallest possible box
199         boxWidth = (int) ((av.endRes - av.startRes + 1) * av.getCharWidth() * scalew);
200       }
201       boxX = width - boxWidth;
202     }
203
204     int col = (int) (boxX / scalew / av.getCharWidth());
205     int row = (int) (boxY / scaleh / av.getCharHeight());
206
207     if (av.hasHiddenColumns())
208     {
209       if (!av.getColumnSelection().isVisible(col))
210       {
211         return;
212       }
213
214       col = av.getColumnSelection().findColumnPosition(col);
215     }
216
217     if (av.hasHiddenRows())
218     {
219       row = av.getAlignment().getHiddenSequences()
220               .findIndexWithoutHiddenSeqs(row);
221     }
222
223     ap.setScrollValues(col, row);
224
225   }
226
227   /**
228    * DOCUMENT ME!
229    */
230   public void updateOverviewImage()
231   {
232     if (resizing)
233     {
234       resizeAgain = true;
235       return;
236     }
237
238     resizing = true;
239
240     if ((getWidth() > 0) && (getHeight() > 0))
241     {
242       width = getWidth();
243       sequencesHeight = getHeight() - graphHeight;
244     }
245
246     setPreferredSize(new Dimension(width, sequencesHeight + graphHeight));
247
248     Thread thread = new Thread(this);
249     thread.start();
250     repaint();
251   }
252
253   // This is set true if the user resizes whilst
254   // the overview is being calculated
255   boolean resizeAgain = false;
256
257   /**
258    * DOCUMENT ME!
259    */
260   @Override
261   public void run()
262   {
263     miniMe = null;
264
265     if (av.isShowSequenceFeatures())
266     {
267       fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
268     }
269
270     int alwidth = av.getAlignment().getWidth();
271     int alheight = av.getAlignment().getHeight()
272             + av.getAlignment().getHiddenSequences().getSize();
273
274     setPreferredSize(new Dimension(width, sequencesHeight + graphHeight));
275
276     int fullsizeWidth = alwidth * av.getCharWidth();
277     int fullsizeHeight = alheight * av.getCharHeight();
278
279     scalew = (float) width / (float) fullsizeWidth;
280     scaleh = (float) sequencesHeight / (float) fullsizeHeight;
281
282     miniMe = new BufferedImage(width, sequencesHeight + graphHeight,
283             BufferedImage.TYPE_INT_RGB);
284
285     Graphics mg = miniMe.getGraphics();
286     mg.setColor(Color.orange);
287     mg.fillRect(0, 0, width, miniMe.getHeight());
288
289     float sampleCol = (float) alwidth / (float) width;
290     float sampleRow = (float) alheight / (float) sequencesHeight;
291
292     int lastcol = -1, lastrow = -1;
293     Color color = Color.white;
294     int row, col;
295     jalview.datamodel.SequenceI seq;
296     final boolean hasHiddenRows = av.hasHiddenRows(), hasHiddenCols = av
297             .hasHiddenColumns();
298     boolean hiddenRow = false;
299     // get hidden row and hidden column map once at beginning.
300     // clone featureRenderer settings to avoid race conditions... if state is
301     // updated just need to refresh again
302
303     FeatureColourFinder finder = new FeatureColourFinder(fr);
304
305     for (row = 0; row < sequencesHeight; row++)
306     {
307       if (resizeAgain)
308       {
309         break;
310       }
311       if ((int) (row * sampleRow) == lastrow)
312       {
313         // No need to recalculate the colours,
314         // Just copy from the row above
315         for (col = 0; col < width; col++)
316         {
317           if (resizeAgain)
318           {
319             break;
320           }
321           miniMe.setRGB(col, row, miniMe.getRGB(col, row - 1));
322         }
323         continue;
324       }
325
326       lastrow = (int) (row * sampleRow);
327
328       hiddenRow = false;
329       if (hasHiddenRows)
330       {
331         seq = av.getAlignment().getHiddenSequences()
332                 .getHiddenSequence(lastrow);
333         if (seq == null)
334         {
335           int index = av.getAlignment().getHiddenSequences()
336                   .findIndexWithoutHiddenSeqs(lastrow);
337
338           seq = av.getAlignment().getSequenceAt(index);
339         }
340         else
341         {
342           hiddenRow = true;
343         }
344       }
345       else
346       {
347         seq = av.getAlignment().getSequenceAt(lastrow);
348       }
349
350       if (seq == null)
351       {
352         System.out.println(lastrow + " null");
353         continue;
354       }
355
356       for (col = 0; col < width; col++)
357       {
358         if (resizeAgain)
359         {
360           break;
361         }
362         if ((int) (col * sampleCol) == lastcol
363                 && (int) (row * sampleRow) == lastrow)
364         {
365           miniMe.setRGB(col, row, color.getRGB());
366           continue;
367         }
368
369         lastcol = (int) (col * sampleCol);
370
371         if (seq.getLength() > lastcol)
372         {
373           color = sr.getResidueBoxColour(seq, lastcol);
374
375           if (av.isShowSequenceFeatures())
376           {
377             color = finder.findFeatureColour(color, seq, lastcol);
378           }
379         }
380         else
381         {
382           color = Color.WHITE;
383         }
384
385         if (hiddenRow
386                 || (hasHiddenCols && !av.getColumnSelection().isVisible(
387                         lastcol)))
388         {
389           color = color.darker().darker();
390         }
391
392         miniMe.setRGB(col, row, color.getRGB());
393
394       }
395     }
396
397     if (av.getAlignmentConservationAnnotation() != null)
398     {
399       renderer.updateFromAlignViewport(av);
400       for (col = 0; col < width; col++)
401       {
402         if (resizeAgain)
403         {
404           break;
405         }
406         lastcol = (int) (col * sampleCol);
407         {
408           mg.translate(col, sequencesHeight);
409           renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
410                   av.getAlignmentConservationAnnotation().annotations,
411                   (int) (sampleCol) + 1, graphHeight,
412                   (int) (col * sampleCol), (int) (col * sampleCol) + 1);
413           mg.translate(-col, -sequencesHeight);
414         }
415       }
416     }
417     System.gc();
418
419     resizing = false;
420
421     if (resizeAgain)
422     {
423       resizeAgain = false;
424       updateOverviewImage();
425     }
426     else
427     {
428       lastMiniMe = miniMe;
429     }
430
431     setBoxPosition();
432   }
433
434   /**
435    * DOCUMENT ME!
436    */
437   public void setBoxPosition()
438   {
439     int fullsizeWidth = av.getAlignment().getWidth() * av.getCharWidth();
440     int fullsizeHeight = (av.getAlignment().getHeight() + av.getAlignment()
441             .getHiddenSequences().getSize())
442             * av.getCharHeight();
443
444     int startRes = av.getStartRes();
445     int endRes = av.getEndRes();
446
447     if (av.hasHiddenColumns())
448     {
449       startRes = av.getColumnSelection().adjustForHiddenColumns(startRes);
450       endRes = av.getColumnSelection().adjustForHiddenColumns(endRes);
451     }
452
453     int startSeq = av.startSeq;
454     int endSeq = av.endSeq;
455
456     if (av.hasHiddenRows())
457     {
458       startSeq = av.getAlignment().getHiddenSequences()
459               .adjustForHiddenSeqs(startSeq);
460
461       endSeq = av.getAlignment().getHiddenSequences()
462               .adjustForHiddenSeqs(endSeq);
463
464     }
465
466     scalew = (float) width / (float) fullsizeWidth;
467     scaleh = (float) sequencesHeight / (float) fullsizeHeight;
468
469     boxX = (int) (startRes * av.getCharWidth() * scalew);
470     boxY = (int) (startSeq * av.getCharHeight() * scaleh);
471
472     if (av.hasHiddenColumns())
473     {
474       boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew);
475     }
476     else
477     {
478       boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew);
479     }
480
481     boxHeight = (int) ((endSeq - startSeq) * av.getCharHeight() * scaleh);
482
483     repaint();
484   }
485
486   private BufferedImage lastMiniMe = null;
487
488   /**
489    * DOCUMENT ME!
490    * 
491    * @param g
492    *          DOCUMENT ME!
493    */
494   @Override
495   public void paintComponent(Graphics g)
496   {
497     if (resizing || resizeAgain)
498     {
499       if (lastMiniMe == null)
500       {
501         g.setColor(Color.white);
502         g.fillRect(0, 0, getWidth(), getHeight());
503       }
504       else
505       {
506         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
507       }
508       g.setColor(new Color(100, 100, 100, 25));
509       g.fillRect(0, 0, getWidth(), getHeight());
510     }
511     else if (lastMiniMe != null)
512     {
513       g.drawImage(lastMiniMe, 0, 0, this);
514       if (lastMiniMe != miniMe)
515       {
516         g.setColor(new Color(100, 100, 100, 25));
517         g.fillRect(0, 0, getWidth(), getHeight());
518       }
519     }
520     // TODO: render selected regions
521     g.setColor(Color.red);
522     g.drawRect(boxX, boxY, boxWidth, boxHeight);
523     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
524   }
525 }