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