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