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