JAL-2443 push getResidueBoxColour inside getResidueColour
[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.getResidueColour(seq, lastcol, finder);
374         }
375         else
376         {
377           color = Color.WHITE;
378         }
379
380         if (hiddenRow
381                 || (hasHiddenCols && !av.getColumnSelection().isVisible(
382                         lastcol)))
383         {
384           color = color.darker().darker();
385         }
386
387         miniMe.setRGB(col, row, color.getRGB());
388
389       }
390     }
391
392     if (av.getAlignmentConservationAnnotation() != null)
393     {
394       renderer.updateFromAlignViewport(av);
395       for (col = 0; col < width; col++)
396       {
397         if (resizeAgain)
398         {
399           break;
400         }
401         lastcol = (int) (col * sampleCol);
402         {
403           mg.translate(col, sequencesHeight);
404           renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
405                   av.getAlignmentConservationAnnotation().annotations,
406                   (int) (sampleCol) + 1, graphHeight,
407                   (int) (col * sampleCol), (int) (col * sampleCol) + 1);
408           mg.translate(-col, -sequencesHeight);
409         }
410       }
411     }
412     System.gc();
413
414     resizing = false;
415
416     if (resizeAgain)
417     {
418       resizeAgain = false;
419       updateOverviewImage();
420     }
421     else
422     {
423       lastMiniMe = miniMe;
424     }
425
426     setBoxPosition();
427   }
428
429   /**
430    * DOCUMENT ME!
431    */
432   public void setBoxPosition()
433   {
434     int fullsizeWidth = av.getAlignment().getWidth() * av.getCharWidth();
435     int fullsizeHeight = (av.getAlignment().getHeight() + av.getAlignment()
436             .getHiddenSequences().getSize())
437             * av.getCharHeight();
438
439     int startRes = av.getStartRes();
440     int endRes = av.getEndRes();
441
442     if (av.hasHiddenColumns())
443     {
444       startRes = av.getColumnSelection().adjustForHiddenColumns(startRes);
445       endRes = av.getColumnSelection().adjustForHiddenColumns(endRes);
446     }
447
448     int startSeq = av.startSeq;
449     int endSeq = av.endSeq;
450
451     if (av.hasHiddenRows())
452     {
453       startSeq = av.getAlignment().getHiddenSequences()
454               .adjustForHiddenSeqs(startSeq);
455
456       endSeq = av.getAlignment().getHiddenSequences()
457               .adjustForHiddenSeqs(endSeq);
458
459     }
460
461     scalew = (float) width / (float) fullsizeWidth;
462     scaleh = (float) sequencesHeight / (float) fullsizeHeight;
463
464     boxX = (int) (startRes * av.getCharWidth() * scalew);
465     boxY = (int) (startSeq * av.getCharHeight() * scaleh);
466
467     if (av.hasHiddenColumns())
468     {
469       boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew);
470     }
471     else
472     {
473       boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew);
474     }
475
476     boxHeight = (int) ((endSeq - startSeq) * av.getCharHeight() * scaleh);
477
478     repaint();
479   }
480
481   private BufferedImage lastMiniMe = null;
482
483   /**
484    * DOCUMENT ME!
485    * 
486    * @param g
487    *          DOCUMENT ME!
488    */
489   @Override
490   public void paintComponent(Graphics g)
491   {
492     if (resizing || resizeAgain)
493     {
494       if (lastMiniMe == null)
495       {
496         g.setColor(Color.white);
497         g.fillRect(0, 0, getWidth(), getHeight());
498       }
499       else
500       {
501         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
502       }
503       g.setColor(new Color(100, 100, 100, 25));
504       g.fillRect(0, 0, getWidth(), getHeight());
505     }
506     else if (lastMiniMe != null)
507     {
508       g.drawImage(lastMiniMe, 0, 0, this);
509       if (lastMiniMe != miniMe)
510       {
511         g.setColor(new Color(100, 100, 100, 25));
512         g.fillRect(0, 0, getWidth(), getHeight());
513       }
514     }
515     // TODO: render selected regions
516     g.setColor(Color.red);
517     g.drawRect(boxX, boxY, boxWidth, boxHeight);
518     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
519   }
520 }