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