2e505213daf9be240f9531813678ab328379ebde
[jalview.git] / src / jalview / gui / OverviewPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.awt.image.*;
24 import javax.swing.*;
25
26 /**
27  * DOCUMENT ME!
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class OverviewPanel extends JPanel implements Runnable
33 {
34   BufferedImage miniMe;
35
36   AlignViewport av;
37
38   AlignmentPanel ap;
39
40   float scalew = 1f;
41
42   float scaleh = 1f;
43
44   int width;
45
46   int sequencesHeight;
47
48   int graphHeight = 20;
49
50   int boxX = -1;
51
52   int boxY = -1;
53
54   int boxWidth = -1;
55
56   int boxHeight = -1;
57
58   boolean resizing = false;
59
60   // Can set different properties in this seqCanvas than
61   // main visible SeqCanvas
62   SequenceRenderer sr;
63
64   FeatureRenderer fr;
65
66   /**
67    * Creates a new OverviewPanel object.
68    * 
69    * @param ap
70    *                DOCUMENT ME!
71    */
72   public OverviewPanel(AlignmentPanel ap)
73   {
74     this.av = ap.av;
75     this.ap = ap;
76     setLayout(null);
77
78     sr = new SequenceRenderer(av);
79     sr.renderGaps = false;
80     sr.forOverview = true;
81     fr = new FeatureRenderer(ap);
82
83     // scale the initial size of overviewpanel to shape of alignment
84     float initialScale = (float) av.alignment.getWidth()
85             / (float) av.alignment.getHeight();
86
87     if (av.conservation == null)
88     {
89       graphHeight = 0;
90     }
91
92     if (av.alignment.getWidth() > av.alignment.getHeight())
93     {
94       // wider
95       width = 400;
96       sequencesHeight = (int) (400f / initialScale);
97       if (sequencesHeight < 40)
98       {
99         sequencesHeight = 40;
100       }
101     }
102     else
103     {
104       // taller
105       width = (int) (400f * initialScale);
106       sequencesHeight = 300;
107
108       if (width < 120)
109       {
110         width = 120;
111       }
112     }
113
114     addComponentListener(new ComponentAdapter()
115     {
116       public void componentResized(ComponentEvent evt)
117       {
118         if ((getWidth() != width)
119                 || (getHeight() != (sequencesHeight + graphHeight)))
120         {
121           updateOverviewImage();
122         }
123       }
124     });
125
126     addMouseMotionListener(new MouseMotionAdapter()
127     {
128       public void mouseDragged(MouseEvent evt)
129       {
130         if (!av.wrapAlignment)
131         {
132           // TODO: feature: jv2.5 detect shift drag and update selection from 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())
392             * av.getCharHeight();
393
394     int startRes = av.getStartRes();
395     int endRes = av.getEndRes();
396
397     if (av.hasHiddenColumns)
398     {
399       startRes = av.getColumnSelection().adjustForHiddenColumns(startRes);
400       endRes = av.getColumnSelection().adjustForHiddenColumns(endRes);
401     }
402
403     int startSeq = av.startSeq;
404     int endSeq = av.endSeq;
405
406     if (av.hasHiddenRows)
407     {
408       startSeq = av.alignment.getHiddenSequences().adjustForHiddenSeqs(
409               startSeq);
410
411       endSeq = av.alignment.getHiddenSequences()
412               .adjustForHiddenSeqs(endSeq);
413
414     }
415
416     scalew = (float) width / (float) fullsizeWidth;
417     scaleh = (float) sequencesHeight / (float) fullsizeHeight;
418
419     boxX = (int) (startRes * av.getCharWidth() * scalew);
420     boxY = (int) (startSeq * av.getCharHeight() * scaleh);
421
422     if (av.hasHiddenColumns)
423     {
424       boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew);
425     }
426     else
427     {
428       boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew);
429     }
430
431     boxHeight = (int) ((endSeq - startSeq) * av.getCharHeight() * scaleh);
432
433     repaint();
434   }
435
436   /**
437    * DOCUMENT ME!
438    * 
439    * @param g
440    *                DOCUMENT ME!
441    */
442   public void paintComponent(Graphics g)
443   {
444     if (resizing)
445     {
446       g.setColor(Color.white);
447       g.fillRect(0, 0, getWidth(), getHeight());
448     }
449     else if (miniMe != null)
450     {
451       g.drawImage(miniMe, 0, 0, this);
452     }
453
454     g.setColor(Color.red);
455     g.drawRect(boxX, boxY, boxWidth, boxHeight);
456     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
457
458   }
459 }