Merge branch 'bug/JAL-2621' into develop
[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.bin.Cache;
24 import jalview.renderer.OverviewRenderer;
25 import jalview.util.MessageManager;
26 import jalview.util.Platform;
27 import jalview.viewmodel.OverviewDimensions;
28 import jalview.viewmodel.OverviewDimensionsHideHidden;
29 import jalview.viewmodel.OverviewDimensionsShowHidden;
30 import jalview.viewmodel.ViewportListenerI;
31
32 import java.awt.BorderLayout;
33 import java.awt.Cursor;
34 import java.awt.Dimension;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.ComponentAdapter;
38 import java.awt.event.ComponentEvent;
39 import java.awt.event.MouseAdapter;
40 import java.awt.event.MouseEvent;
41 import java.awt.event.MouseMotionAdapter;
42 import java.beans.PropertyChangeEvent;
43 import java.beans.PropertyVetoException;
44
45 import javax.swing.JCheckBoxMenuItem;
46 import javax.swing.JInternalFrame;
47 import javax.swing.JPanel;
48 import javax.swing.JPopupMenu;
49 import javax.swing.SwingUtilities;
50
51 /**
52  * Panel displaying an overview of the full alignment, with an interactive box
53  * representing the viewport onto the alignment.
54  * 
55  * @author $author$
56  * @version $Revision$
57  */
58 public class OverviewPanel extends JPanel
59         implements Runnable, ViewportListenerI
60 {
61   private OverviewDimensions od;
62
63   private OverviewCanvas oviewCanvas;
64
65   private AlignViewport av;
66
67   private AlignmentPanel ap;
68
69   private JCheckBoxMenuItem displayToggle;
70
71   private boolean showHidden = true;
72
73   private boolean draggingBox = false;
74
75   private ProgressPanel progressPanel;
76
77   /**
78    * Creates a new OverviewPanel object.
79    * 
80    * @param alPanel
81    *          The alignment panel which is shown in the overview panel
82    */
83   public OverviewPanel(AlignmentPanel alPanel)
84   {
85     this.av = alPanel.av;
86     this.ap = alPanel;
87
88     showHidden = Cache.getDefault(Preferences.SHOW_OV_HIDDEN_AT_START,
89             false);
90     if (showHidden)
91     {
92       od = new OverviewDimensionsShowHidden(av.getRanges(),
93             (av.isShowAnnotation()
94                     && av.getAlignmentConservationAnnotation() != null));
95     }
96     else
97     {
98       od = new OverviewDimensionsHideHidden(av.getRanges(),
99               (av.isShowAnnotation()
100                       && av.getAlignmentConservationAnnotation() != null));
101     }
102
103     setLayout(new BorderLayout());
104     progressPanel = new ProgressPanel(OverviewRenderer.UPDATE,
105             MessageManager.getString("label.oview_calc"), getWidth());
106     this.add(progressPanel, BorderLayout.SOUTH);
107     oviewCanvas = new OverviewCanvas(od, av, progressPanel);
108
109     add(oviewCanvas, BorderLayout.CENTER);
110
111     av.getRanges().addPropertyChangeListener(this);
112
113     // without this the overview window does not size to fit the overview canvas
114     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
115     
116     addComponentListener(new ComponentAdapter()
117     {
118       @Override
119       public void componentResized(ComponentEvent evt)
120       {
121         // Resize is called on the initial display of the overview.
122         // This code adjusts sizes to account for the progress bar if it has not
123         // already been accounted for, which triggers another resize call for
124         // the correct sizing, at which point the overview image is updated.
125         // (This avoids a double recalculation of the image.)
126         if (getWidth() == od.getWidth() && getHeight() == od.getHeight()
127                 + progressPanel.getHeight())
128         {
129           updateOverviewImage();
130         }
131         else
132         {
133           if ((getWidth() > 0) && (getHeight() > 0))
134           {
135             od.setWidth(getWidth());
136             od.setHeight(getHeight() - progressPanel.getHeight());
137           }
138
139           setPreferredSize(new Dimension(od.getWidth(),
140                   od.getHeight() + progressPanel.getHeight()));
141         }
142       }
143
144     });
145
146     addMouseMotionListener(new MouseMotionAdapter()
147     {
148       @Override
149       public void mouseDragged(MouseEvent evt)
150       {
151         if (!SwingUtilities.isRightMouseButton(evt))
152         {
153           if (draggingBox)
154           {
155             // set the mouse position as a fixed point in the box
156             // and drag relative to that position
157             od.adjustViewportFromMouse(evt.getX(), evt.getY(),
158                     av.getAlignment().getHiddenSequences(),
159                     av.getAlignment().getHiddenColumns());
160           }
161           else
162           {
163             od.updateViewportFromMouse(evt.getX(), evt.getY(),
164                     av.getAlignment().getHiddenSequences(),
165                     av.getAlignment().getHiddenColumns());
166           }
167         }
168       }
169
170       @Override
171       public void mouseMoved(MouseEvent evt)
172       {
173         if (od.isPositionInBox(evt.getX(), evt.getY()))
174         {
175           /*
176            * using HAND_CURSOR rather than DRAG_CURSOR 
177            * as the latter is not supported on Mac
178            */
179           getParent().setCursor(
180                   Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
181         }
182         else
183         {
184           // reset cursor
185           getParent().setCursor(
186                   Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
187         }
188       }
189
190     });
191
192     addMouseListener(new MouseAdapter()
193     {
194       @Override
195       public void mousePressed(MouseEvent evt)
196       {
197         if (SwingUtilities.isRightMouseButton(evt))
198         {
199           if (!Platform.isAMac())
200           {
201             showPopupMenu(evt);
202           }
203         }
204         else
205         {
206           // don't do anything if the mouse press is in the overview's box
207           // (wait to see if it's a drag instead)
208           // otherwise update the viewport
209           if (!od.isPositionInBox(evt.getX(), evt.getY()))
210           {
211             draggingBox = false;
212
213             // display drag cursor at mouse position
214             setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
215
216             od.updateViewportFromMouse(evt.getX(), evt.getY(),
217                     av.getAlignment().getHiddenSequences(),
218                     av.getAlignment().getHiddenColumns());
219             getParent().setCursor(
220                     Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
221           }
222           else
223           {
224             draggingBox = true;
225             od.setDragPoint(evt.getX(), evt.getY(),
226                     av.getAlignment().getHiddenSequences(),
227                     av.getAlignment().getHiddenColumns());
228           }
229         }
230       }
231
232       @Override
233       public void mouseClicked(MouseEvent evt)
234       {
235         if (SwingUtilities.isRightMouseButton(evt))
236         {
237           showPopupMenu(evt);
238         }
239       }
240
241       @Override
242       public void mouseReleased(MouseEvent evt)
243       {
244         draggingBox = false;
245       }
246
247     });
248   }
249
250   /*
251    * Displays the popup menu and acts on user input
252    */
253   private void showPopupMenu(MouseEvent e)
254   {
255     JPopupMenu popup = new JPopupMenu();
256     ActionListener menuListener = new ActionListener()
257     {
258       @Override
259       public void actionPerformed(ActionEvent event)
260       {
261         // switch on/off the hidden columns view
262         toggleHiddenColumns();
263         displayToggle.setSelected(showHidden);
264       }
265     };
266     displayToggle = new JCheckBoxMenuItem(
267             MessageManager.getString("label.togglehidden"));
268     displayToggle.setEnabled(true);
269     displayToggle.setSelected(showHidden);
270     popup.add(displayToggle);
271     displayToggle.addActionListener(menuListener);
272     popup.show(this, e.getX(), e.getY());
273   }
274
275   /*
276    * Toggle overview display between showing hidden columns and hiding hidden columns
277    */
278   private void toggleHiddenColumns()
279   {
280     if (showHidden)
281     {
282       showHidden = false;
283       od = new OverviewDimensionsHideHidden(av.getRanges(),
284               (av.isShowAnnotation()
285                       && av.getAlignmentConservationAnnotation() != null));
286     }
287     else
288     {
289       showHidden = true;
290       od = new OverviewDimensionsShowHidden(av.getRanges(),
291               (av.isShowAnnotation()
292                       && av.getAlignmentConservationAnnotation() != null));
293     }
294     oviewCanvas.resetOviewDims(od);
295     updateOverviewImage();
296     setBoxPosition();
297   }
298
299   /**
300    * Updates the overview image when the related alignment panel is updated
301    */
302   public void updateOverviewImage()
303   {
304     if (oviewCanvas == null)
305     {
306       /*
307        * panel has been disposed
308        */
309       return;
310     }
311
312     if ((getWidth() > 0) && (getHeight() > 0))
313     {
314       od.setWidth(getWidth());
315       od.setHeight(getHeight() - progressPanel.getHeight());
316     }
317     
318     setPreferredSize(new Dimension(od.getWidth(),
319             od.getHeight() + progressPanel.getHeight()));
320
321     if (oviewCanvas.restartDraw())
322     {
323       return;
324     }
325
326     Thread thread = new Thread(this);
327     thread.start();
328     repaint();
329
330     
331   }
332
333   @Override
334   public void run()
335   {
336     if (oviewCanvas != null)
337     {
338       oviewCanvas.draw(av.isShowSequenceFeatures(),
339               (av.isShowAnnotation()
340                       && av.getAlignmentConservationAnnotation() != null),
341               ap.getSeqPanel().seqCanvas.getFeatureRenderer());
342       setBoxPosition();
343     }
344   }
345
346   /**
347    * Update the overview panel box when the associated alignment panel is
348    * changed
349    * 
350    */
351   private void setBoxPositionOnly()
352   {
353     if (od != null)
354     {
355       int oldX = od.getBoxX();
356       int oldY = od.getBoxY();
357       int oldWidth = od.getBoxWidth();
358       int oldHeight = od.getBoxHeight();
359       od.setBoxPosition(av.getAlignment().getHiddenSequences(),
360               av.getAlignment().getHiddenColumns());
361       repaint(oldX - 1, oldY - 1, oldWidth + 2, oldHeight + 2);
362       repaint(od.getBoxX(), od.getBoxY(), od.getBoxWidth(),
363               od.getBoxHeight());
364     }
365   }
366
367   private void setBoxPosition()
368   {
369     if (od != null)
370     {
371       od.setBoxPosition(av.getAlignment().getHiddenSequences(),
372               av.getAlignment().getHiddenColumns());
373       repaint();
374     }
375   }
376
377   @Override
378   public void propertyChange(PropertyChangeEvent evt)
379   {
380     setBoxPositionOnly();
381   }
382
383   /**
384    * Removes this object as a property change listener, and nulls references
385    */
386   protected void dispose()
387   {
388     try
389     {
390       if (av != null)
391       {
392         av.getRanges().removePropertyChangeListener(this);
393       }
394
395       oviewCanvas.dispose();
396
397       /*
398        * close the parent frame (which also removes it from the
399        * Desktop Windows menu)
400        */
401       ((JInternalFrame) SwingUtilities.getAncestorOfClass(
402               JInternalFrame.class, (this))).setClosed(true);
403     } catch (PropertyVetoException e)
404     {
405       // ignore
406     } finally
407     {
408       progressPanel = null;
409       av = null;
410       oviewCanvas = null;
411       ap = null;
412       od = null;
413     }
414   }
415 }