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