JAL-2388 Minor refactoring
[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.renderer.AnnotationRenderer;
24 import jalview.viewmodel.OverviewDimensions;
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  * Panel displaying an overview of the full alignment, with an interactive box
40  * representing the viewport onto the alignment.
41  * 
42  * @author $author$
43  * @version $Revision$
44  */
45 public class OverviewPanel extends JPanel implements Runnable
46 {
47   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
48
49   private final AnnotationRenderer renderer = new AnnotationRenderer();
50
51   private OverviewDimensions od;
52
53   private BufferedImage miniMe;
54
55   private BufferedImage lastMiniMe = null;
56
57   private AlignViewport av;
58
59   private AlignmentPanel ap;
60
61   //
62   private boolean resizing = false;
63
64   // This is set true if the user resizes whilst
65   // the overview is being calculated
66   private boolean resizeAgain = false;
67
68   // Can set different properties in this seqCanvas than
69   // main visible SeqCanvas
70   private SequenceRenderer sr;
71
72   private jalview.renderer.seqfeatures.FeatureRenderer fr;
73
74   /**
75    * Creates a new OverviewPanel object.
76    * 
77    * @param alPanel
78    *          The alignment panel which is shown in the overview panel
79    */
80   public OverviewPanel(AlignmentPanel alPanel)
81   {
82     this.av = alPanel.av;
83     this.ap = alPanel;
84     setLayout(null);
85
86     sr = new SequenceRenderer(av);
87     sr.renderGaps = false;
88     sr.forOverview = true;
89     fr = new FeatureRenderer(alPanel);
90
91     od = new OverviewDimensions(av);
92
93     addComponentListener(new ComponentAdapter()
94     {
95       @Override
96       public void componentResized(ComponentEvent evt)
97       {
98         if ((getWidth() != od.getWidth())
99                 || (getHeight() != (od.getHeight())))
100         {
101           updateOverviewImage();
102         }
103       }
104     });
105
106     addMouseMotionListener(new MouseMotionAdapter()
107     {
108       @Override
109       public void mouseDragged(MouseEvent evt)
110       {
111         if (!av.getWrapAlignment())
112         {
113           // TODO: feature: jv2.5 detect shift drag and update selection from
114           // it.
115           od.setBoxPositionByMouse(evt.getX(), evt.getY());
116           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
117         }
118       }
119     });
120
121     addMouseListener(new MouseAdapter()
122     {
123       @Override
124       public void mousePressed(MouseEvent evt)
125       {
126         if (!av.getWrapAlignment())
127         {
128           od.setBoxPositionByMouse(evt.getX(), evt.getY());
129           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
130         }
131       }
132     });
133
134     updateOverviewImage();
135   }
136
137   /**
138    * Updates the overview image when the related alignment panel is updated
139    */
140   public void updateOverviewImage()
141   {
142     if (resizing)
143     {
144       resizeAgain = true;
145       return;
146     }
147
148     resizing = true;
149
150     if ((getWidth() > 0) && (getHeight() > 0))
151     {
152       od.setWidth(getWidth()); // width = getWidth();
153       od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
154     }
155
156     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
157
158     Thread thread = new Thread(this);
159     thread.start();
160     repaint();
161   }
162
163   @Override
164   public void run()
165   {
166     miniMe = null;
167
168     if (av.isShowSequenceFeatures())
169     {
170       fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
171     }
172
173     // why do we need to set preferred size again? was set in
174     // updateOverviewImage
175     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
176
177     miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
178             BufferedImage.TYPE_INT_RGB);
179
180     Graphics mg = miniMe.getGraphics();
181     mg.setColor(Color.orange);
182     mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
183
184     // calculate scale based on current alignment width and height
185     od.updateScales();
186
187     // calculate sampleCol and sampleRow
188     // alignment width is max number of residues/bases
189     // alignment height is number of sequences
190     int alwidth = av.getAlignment().getWidth();
191     int alheight = av.getAlignment().getHeight()
192             + av.getAlignment().getHiddenSequences().getSize();
193
194     // sampleCol or sampleRow is the width/height allocated to each residue
195     // in particular, sometimes we may need more than one row/col of the
196     // BufferedImage allocated
197     // sampleCol is how much of a residue to assign to each pixel
198     // sampleRow is how many sequences to assign to each pixel
199     float sampleCol = alwidth / (float) od.getWidth();
200     float sampleRow = alheight / (float) od.getSequencesHeight();
201
202     buildImage(sampleRow, sampleCol);
203
204     if (av.getAlignmentConservationAnnotation() != null)
205     {
206       renderer.updateFromAlignViewport(av);
207       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
208       {
209         mg.translate(col, od.getSequencesHeight());
210         renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
211                 av.getAlignmentConservationAnnotation().annotations,
212                 (int) (sampleCol) + 1, od.getGraphHeight(),
213                 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
214         mg.translate(-col, -od.getSequencesHeight());
215
216       }
217     }
218     System.gc();
219
220     resizing = false;
221
222     if (resizeAgain)
223     {
224       resizeAgain = false;
225       updateOverviewImage();
226     }
227     else
228     {
229       lastMiniMe = miniMe;
230     }
231
232     setBoxPosition();
233   }
234
235   private void buildImage(float sampleRow, float sampleCol)
236   {
237     int lastcol = -1;
238     int lastrow = -1;
239     int color = Color.white.getRGB();
240
241     jalview.datamodel.SequenceI seq = null;
242     final boolean hasHiddenRows = av.hasHiddenRows();
243     final boolean hasHiddenCols = av.hasHiddenColumns();
244     boolean hiddenRow = false;
245     // get hidden row and hidden column map once at beginning.
246     // clone featureRenderer settings to avoid race conditions... if state is
247     // updated just need to refresh again
248     for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
249     {
250       boolean doCopy = true;
251       int currentrow = (int) (row * sampleRow);
252       if (currentrow != lastrow)
253       {
254         doCopy = false;
255
256         lastrow = currentrow;
257
258         hiddenRow = false;
259         // should be method(s) in Alignment
260         if (hasHiddenRows)
261         {
262           seq = av.getAlignment().getHiddenSequences()
263                   .getHiddenSequence(lastrow);
264           if (seq == null)
265           {
266             int index = av.getAlignment().getHiddenSequences()
267                     .findIndexWithoutHiddenSeqs(lastrow);
268
269             seq = av.getAlignment().getSequenceAt(index);
270           }
271           else
272           {
273             hiddenRow = true;
274           }
275         }
276         else
277         {
278           seq = av.getAlignment().getSequenceAt(lastrow);
279         }
280         // end of Alignment method(s)
281       }
282
283       if (seq == null)
284       {
285         System.out.println(lastrow + " null");
286         continue;
287       }
288
289       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
290       {
291         if (doCopy)
292         {
293           color = miniMe.getRGB(col, row - 1);
294         }
295         else if ((int) (col * sampleCol) != lastcol
296                 || (int) (row * sampleRow) != lastrow)
297         {
298           lastcol = (int) (col * sampleCol);
299           color = getColumnColourFromSequence(seq, hiddenRow, hasHiddenCols,
300                   lastcol);
301         }
302         // else if ((int) (col * sampleCol) == lastcol && (int) (row *
303         // sampleRow) == lastrow))
304         // we just use the color we already have , so don't need to set it
305
306         miniMe.setRGB(col, row, color);
307       }
308     }
309   }
310
311   /*
312    * Find the colour of a sequence at a specified column position
313    */
314   private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
315           boolean hiddenRow, boolean hasHiddenCols, int lastcol)
316   {
317     int color;
318
319     if (seq.getLength() > lastcol)
320     {
321       color = sr.getResidueBoxColour(seq, lastcol).getRGB();
322
323       if (av.isShowSequenceFeatures())
324       {
325         color = fr.findFeatureColour(color, seq, lastcol);
326       }
327     }
328     else
329     {
330       color = Color.white.getRGB(); // White
331     }
332
333     if (hiddenRow
334             || (hasHiddenCols && !av.getColumnSelection()
335                     .isVisible(lastcol)))
336     {
337       color = new Color(color).darker().darker().getRGB();
338     }
339
340     return color;
341   }
342
343   /**
344    * Update the overview panel box when the associated alignment panel is
345    * changed
346    * 
347    */
348   public void setBoxPosition()
349   {
350     od.setBoxPosition();
351     repaint();
352   }
353
354
355   @Override
356   public void paintComponent(Graphics g)
357   {
358     if (resizing || resizeAgain)
359     {
360       if (lastMiniMe == null)
361       {
362         g.setColor(Color.white);
363         g.fillRect(0, 0, getWidth(), getHeight());
364       }
365       else
366       {
367         g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
368       }
369       g.setColor(TRANS_GREY);
370       g.fillRect(0, 0, getWidth(), getHeight());
371     }
372     else if (lastMiniMe != null)
373     {
374       g.drawImage(lastMiniMe, 0, 0, this);
375       if (lastMiniMe != miniMe)
376       {
377         g.setColor(TRANS_GREY);
378         g.fillRect(0, 0, getWidth(), getHeight());
379       }
380     }
381     // TODO: render selected regions
382     g.setColor(Color.red);
383     od.drawBox(g);
384   }
385 }