JAL-2388 Remove OverviewDimensions dependency on gui classes
[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     boolean showAnnotation = false;
79     // TODO: in applet this was getSequenceConsensusHash()
80     // check if it makes any functional difference: hconsensus or conservation
81     if (av.getAlignmentConservationAnnotation() == null)
82     {
83       showAnnotation = true;
84     }
85
86     od = new OverviewDimensions(av.getPosProps(), showAnnotation);
87
88     setSize(new Dimension(od.getWidth(), od.getHeight()));
89     addComponentListener(new ComponentAdapter()
90     {
91
92       @Override
93       public void componentResized(ComponentEvent evt)
94       {
95         if ((getWidth() != od.getWidth())
96                 || (getHeight() != (od.getHeight())))
97         {
98           updateOverviewImage();
99         }
100       }
101     });
102
103     addMouseMotionListener(this);
104
105     addMouseListener(this);
106
107     updateOverviewImage();
108
109   }
110
111   @Override
112   public void mouseEntered(MouseEvent evt)
113   {
114   }
115
116   @Override
117   public void mouseExited(MouseEvent evt)
118   {
119   }
120
121   @Override
122   public void mouseClicked(MouseEvent evt)
123   {
124   }
125
126   @Override
127   public void mouseMoved(MouseEvent evt)
128   {
129   }
130
131   @Override
132   public void mousePressed(MouseEvent evt)
133   {
134     mouseAction(evt);
135   }
136
137   @Override
138   public void mouseReleased(MouseEvent evt)
139   {
140     mouseAction(evt);
141   }
142
143   @Override
144   public void mouseDragged(MouseEvent evt)
145   {
146     mouseAction(evt);
147   }
148
149   private void mouseAction(MouseEvent evt)
150   {
151     od.updateViewportFromMouse(evt.getX(), evt.getY(), av.getAlignment()
152             .getHiddenSequences(), av.getColumnSelection(), av
153             .getPosProps());
154     ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
155     ap.paintAlignment(false);
156   }
157
158   /**
159    * DOCUMENT ME!
160    */
161   public void updateOverviewImage()
162   {
163     if (resizing)
164     {
165       resizeAgain = true;
166       return;
167     }
168
169     if (av.isShowSequenceFeatures())
170     {
171       fr.transferSettings(ap.seqPanel.seqCanvas.fr);
172     }
173
174     resizing = true;
175
176     if ((getWidth() > 0) && (getHeight() > 0))
177     {
178       od.setWidth(getWidth()); // width = getWidth();
179       od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
180     }
181     setSize(new Dimension(od.getWidth(), od.getHeight()));
182
183     Thread thread = new Thread(this);
184     thread.start();
185     repaint();
186   }
187
188   @Override
189   public void run()
190   {
191     miniMe = null;
192
193     if (av.isShowSequenceFeatures())
194     {
195       fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
196     }
197
198     if (getSize().width > 0 && getSize().height > 0)
199     {
200       od.setWidth(getSize().width);
201       od.setHeight(getSize().height - od.getGraphHeight());
202     }
203
204     setSize(new Dimension(od.getWidth(), od.getHeight()));
205
206     miniMe = nullFrame.createImage(od.getWidth(), od.getHeight());
207     offscreen = nullFrame.createImage(od.getWidth(), od.getHeight());
208
209     Graphics mg = miniMe.getGraphics();
210
211     // od.updateScales();
212
213     int alwidth = av.getAlignment().getWidth();
214     int alheight = av.getAlignment().getAbsoluteHeight();
215     float sampleCol = alwidth / (float) od.getWidth();
216     float sampleRow = alheight / (float) od.getSequencesHeight();
217
218     buildImage(sampleRow, sampleCol, mg);
219
220     if (av.getAlignmentConservationAnnotation() != null)
221     {
222       for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
223       {
224         mg.translate(col, od.getSequencesHeight());
225         ap.annotationPanel.renderer.drawGraph(mg,
226                 av.getAlignmentConservationAnnotation(),
227                 av.getAlignmentConservationAnnotation().annotations,
228                 (int) (sampleCol) + 1, od.getGraphHeight(),
229                 (int) (col * sampleCol), (int) (col * sampleCol) + 1);
230         mg.translate(-col, -od.getSequencesHeight());
231       }
232     }
233     System.gc();
234
235     resizing = false;
236
237     setBoxPosition();
238
239     if (resizeAgain)
240     {
241       resizeAgain = false;
242       updateOverviewImage();
243     }
244   }
245
246   private void buildImage(float sampleRow, float sampleCol, Graphics mg)
247   {
248     int lastcol = 0;
249     int lastrow = 0;
250     int xstart = 0;
251     int ystart = 0;
252     Color color = Color.yellow;
253     int sameRow = 0;
254     int sameCol = 0;
255
256     jalview.datamodel.SequenceI seq = null;
257
258     final boolean hasHiddenCols = av.hasHiddenColumns();
259     boolean hiddenRow = false;
260
261     for (int row = 0; row <= od.getSequencesHeight() && !resizeAgain; row++)
262     {
263       if ((int) (row * sampleRow) == lastrow)
264       {
265         sameRow++;
266       }
267       else
268       {
269         // get the sequence which would be at alignment index 'lastrow' if no
270         // columns were hidden, and determine whether it is hidden or not
271         hiddenRow = av.getAlignment().isHidden(lastrow);
272         seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
273
274         for (int col = 0; col < od.getWidth(); col++)
275         {
276           if ((int) (col * sampleCol) == lastcol
277                   && (int) (row * sampleRow) == lastrow)
278           {
279             sameCol++;
280           }
281           else
282           {
283             lastcol = (int) (col * sampleCol);
284
285             color = getColumnColourFromSequence(seq, hiddenRow,
286                     hasHiddenCols, lastcol);
287
288             mg.setColor(color);
289             if (sameCol == 1 && sameRow == 1)
290             {
291               mg.drawLine(xstart, ystart, xstart, ystart);
292             }
293             else
294             {
295               mg.fillRect(xstart, ystart, sameCol, sameRow);
296             }
297
298             xstart = col;
299             sameCol = 1;
300           }
301         }
302         lastrow = (int) (row * sampleRow);
303         ystart = row;
304         sameRow = 1;
305       }
306     }
307
308   }
309
310   /*
311    * Find the colour of a sequence at a specified column position
312    */
313   private Color getColumnColourFromSequence(
314           jalview.datamodel.SequenceI seq, boolean hiddenRow,
315           boolean hasHiddenCols, int lastcol)
316   {
317     Color color;
318     if (seq.getLength() > lastcol)
319     {
320       color = sr.getResidueBoxColour(seq, lastcol);
321
322       if (av.isShowSequenceFeatures())
323       {
324         color = fr.findFeatureColour(color, seq, lastcol);
325       }
326     }
327     else
328     {
329       color = Color.white; // White
330     }
331
332     if (hiddenRow
333             || (hasHiddenCols && !av.getColumnSelection()
334                     .isVisible(lastcol)))
335     {
336       color = color.darker().darker();
337     }
338     return color;
339   }
340
341   /**
342    * Update the overview panel box when the associated alignment panel is
343    * changed
344    * 
345    */
346   public void setBoxPosition()
347   {
348     od.setBoxPosition(av.getAlignment()
349             .getHiddenSequences(), av.getColumnSelection(), av.getPosProps());
350     repaint();
351   }
352
353   @Override
354   public void update(Graphics g)
355   {
356     paint(g);
357   }
358
359   @Override
360   public void paint(Graphics g)
361   {
362     Graphics og = offscreen.getGraphics();
363     if (miniMe != null)
364     {
365       og.drawImage(miniMe, 0, 0, this);
366       og.setColor(Color.red);
367       od.drawBox(og);
368       g.drawImage(offscreen, 0, 0, this);
369     }
370   }
371
372 }