0d3f05843153687745eb48b37c67c8f6f9638f54
[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.viewmodel.OverviewDimensions;
24
25 import java.awt.Color;
26 import java.awt.Dimension;
27 import java.awt.Frame;
28 import java.awt.Graphics;
29 import java.awt.Image;
30 import java.awt.Panel;
31 import java.awt.event.ComponentAdapter;
32 import java.awt.event.ComponentEvent;
33 import java.awt.event.MouseEvent;
34 import java.awt.event.MouseListener;
35 import java.awt.event.MouseMotionListener;
36
37 public class OverviewPanel extends Panel implements Runnable,
38         MouseMotionListener, MouseListener
39 {
40   private OverviewDimensions od;
41
42   private Image miniMe;
43
44   private Image offscreen;
45
46   private AlignViewport av;
47
48   private AlignmentPanel ap;
49
50   private boolean resizing = false;
51
52   // This is set true if the user resizes whilst
53   // the overview is being calculated
54   private boolean resizeAgain = false;
55
56   // Can set different properties in this seqCanvas than
57   // main visible SeqCanvas
58   private SequenceRenderer sr;
59
60   private FeatureRenderer fr;
61
62   private Frame nullFrame;
63
64   public OverviewPanel(AlignmentPanel ap)
65   {
66     this.av = ap.av;
67     this.ap = ap;
68     setLayout(null);
69     nullFrame = new Frame();
70     nullFrame.addNotify();
71
72     sr = new SequenceRenderer(av);
73     sr.graphics = nullFrame.getGraphics();
74     sr.renderGaps = false;
75     sr.forOverview = true;
76     fr = new FeatureRenderer(av);
77
78     od = new OverviewDimensions(av);
79
80     setSize(new Dimension(od.getWidth(), od.getHeight()));
81     addComponentListener(new ComponentAdapter()
82     {
83
84       @Override
85       public void componentResized(ComponentEvent evt)
86       {
87         if ((getWidth() != od.getWidth())
88                 || (getHeight() != (od.getHeight())))
89         {
90           updateOverviewImage();
91         }
92       }
93     });
94
95     addMouseMotionListener(this);
96
97     addMouseListener(this);
98
99     updateOverviewImage();
100
101   }
102
103   @Override
104   public void mouseEntered(MouseEvent evt)
105   {
106   }
107
108   @Override
109   public void mouseExited(MouseEvent evt)
110   {
111   }
112
113   @Override
114   public void mouseClicked(MouseEvent evt)
115   {
116   }
117
118   @Override
119   public void mouseMoved(MouseEvent evt)
120   {
121   }
122
123   @Override
124   public void mousePressed(MouseEvent evt)
125   {
126     od.setBoxPositionByMouse(evt.getX(), evt.getY());
127     ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
128     ap.paintAlignment(false);
129   }
130
131   @Override
132   public void mouseReleased(MouseEvent evt)
133   {
134     od.setBoxPositionByMouse(evt.getX(), evt.getY());
135     ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
136     ap.paintAlignment(false);
137   }
138
139   @Override
140   public void mouseDragged(MouseEvent evt)
141   {
142     od.setBoxPositionByMouse(evt.getX(), evt.getY());
143     ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
144     ap.paintAlignment(false);
145   }
146
147   /**
148    * DOCUMENT ME!
149    */
150   public void updateOverviewImage()
151   {
152     if (resizing)
153     {
154       resizeAgain = true;
155       return;
156     }
157
158     if (av.isShowSequenceFeatures())
159     {
160       fr.transferSettings(ap.seqPanel.seqCanvas.fr);
161     }
162
163     resizing = true;
164
165     if ((getWidth() > 0) && (getHeight() > 0))
166     {
167       od.setWidth(getWidth()); // width = getWidth();
168       od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
169     }
170     setSize(new Dimension(od.getWidth(), od.getHeight()));
171
172     Thread thread = new Thread(this);
173     thread.start();
174     repaint();
175   }
176
177   @Override
178   public void run()
179   {
180     miniMe = null;
181
182     if (av.isShowSequenceFeatures())
183     {
184       fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
185     }
186
187     if (getSize().width > 0 && getSize().height > 0)
188     {
189       od.setWidth(getSize().width);
190       od.setHeight(getSize().height - od.getGraphHeight());
191     }
192
193     setSize(new Dimension(od.getWidth(), od.getHeight()));
194
195     miniMe = nullFrame.createImage(od.getWidth(), od.getHeight());
196     offscreen = nullFrame.createImage(od.getWidth(), od.getHeight());
197
198     Graphics mg = miniMe.getGraphics();
199
200     od.updateScales();
201
202     int alwidth = av.getAlignment().getWidth();
203     int alheight = av.getAlignment().getHeight()
204             + av.getAlignment().getHiddenSequences().getSize();
205     float sampleCol = alwidth / (float) od.getWidth();
206     float sampleRow = alheight / (float) od.getSequencesHeight();
207
208     buildImage(sampleRow, sampleCol, mg);
209
210     if (av.getAlignmentConservationAnnotation() != null)
211     {
212       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
213       {
214         mg.translate(col, od.getSequencesHeight());
215         ap.annotationPanel.renderer.drawGraph(mg,
216                 av.getAlignmentConservationAnnotation(),
217                 av.getAlignmentConservationAnnotation().annotations,
218                 (int) (sampleCol) + 1, od.getGraphHeight(),
219                 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
220         mg.translate(-col, -od.getSequencesHeight());
221       }
222     }
223     System.gc();
224
225     resizing = false;
226
227     setBoxPosition();
228
229     if (resizeAgain)
230     {
231       resizeAgain = false;
232       updateOverviewImage();
233     }
234   }
235
236   private void buildImage(float sampleRow, float sampleCol, Graphics mg)
237   {
238     int lastcol = 0;
239     int lastrow = 0;
240     int xstart = 0;
241     int ystart = 0;
242     Color color = Color.yellow;
243     int sameRow = 0;
244     int sameCol = 0;
245
246     jalview.datamodel.SequenceI seq = null;
247     final boolean hasHiddenRows = av.hasHiddenRows();
248     final boolean hasHiddenCols = av.hasHiddenColumns();
249     boolean hiddenRow = false;
250
251     for (int row = 0; row <= od.getSequencesHeight() && !resizeAgain; row++)
252     {
253       if ((int) (row * sampleRow) == lastrow)
254       {
255         sameRow++;
256       }
257       else
258       {
259         // this should largely be a method in Alignment
260         hiddenRow = false;
261         if (hasHiddenRows)
262         {
263           // look for sequence in hidden rows
264           seq = av.getAlignment().getHiddenSequences()
265                   .getHiddenSequence(lastrow);
266           if (seq == null)
267           {
268             // didn't find in hidden rows, adjust the index and retrieve from
269             // full row set
270             int index = av.getAlignment().getHiddenSequences()
271                     .findIndexWithoutHiddenSeqs(lastrow);
272
273             seq = av.getAlignment().getSequenceAt(index);
274           }
275           else
276           {
277             // found in hidden rows, ergo this row is hidden
278             hiddenRow = true;
279           }
280         }
281         else
282         {
283           seq = av.getAlignment().getSequenceAt(lastrow);
284         }
285         // end of Alignment method
286
287         for (int col = 0; col < od.getWidth(); col++)
288         {
289           if ((int) (col * sampleCol) == lastcol
290                   && (int) (row * sampleRow) == lastrow)
291           {
292             sameCol++;
293           }
294           else
295           {
296             lastcol = (int) (col * sampleCol);
297
298             color = getColumnColourFromSequence(seq, hiddenRow,
299                     hasHiddenCols, lastcol);
300
301             mg.setColor(color);
302             if (sameCol == 1 && sameRow == 1)
303             {
304               mg.drawLine(xstart, ystart, xstart, ystart);
305             }
306             else
307             {
308               mg.fillRect(xstart, ystart, sameCol, sameRow);
309             }
310
311             xstart = col;
312             sameCol = 1;
313           }
314         }
315         lastrow = (int) (row * sampleRow);
316         ystart = row;
317         sameRow = 1;
318       }
319     }
320
321   }
322
323   /*
324    * Find the colour of a sequence at a specified column position
325    */
326   private Color getColumnColourFromSequence(
327           jalview.datamodel.SequenceI seq, boolean hiddenRow,
328           boolean hasHiddenCols, int lastcol)
329   {
330     Color color;
331     if (seq.getLength() > lastcol)
332     {
333       color = sr.getResidueBoxColour(seq, lastcol);
334
335       if (av.isShowSequenceFeatures())
336       {
337         color = fr.findFeatureColour(color, seq, lastcol);
338       }
339     }
340     else
341     {
342       color = Color.white; // White
343     }
344
345     if (hiddenRow
346             || (hasHiddenCols && !av.getColumnSelection()
347                     .isVisible(lastcol)))
348     {
349       color = color.darker().darker();
350     }
351     return color;
352   }
353
354   /**
355    * Update the overview panel box when the associated alignment panel is
356    * changed
357    * 
358    */
359   public void setBoxPosition()
360   {
361     od.setBoxPosition();
362     repaint();
363   }
364
365   @Override
366   public void update(Graphics g)
367   {
368     paint(g);
369   }
370
371   @Override
372   public void paint(Graphics g)
373   {
374     Graphics og = offscreen.getGraphics();
375     if (miniMe != null)
376     {
377       og.drawImage(miniMe, 0, 0, this);
378       og.setColor(Color.red);
379       od.drawBox(og);
380       g.drawImage(offscreen, 0, 0, this);
381     }
382   }
383
384 }