JAL-2778 Reduced cursor set/reset in overview mouse move
[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             true);
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 (!draggingBox)
174         // don't bother changing the cursor if we're dragging the box
175         // as we can't have moved inside or out of the box in that case
176         {
177           if (od.isPositionInBox(evt.getX(), evt.getY()))
178           {
179             // display drag cursor at mouse position
180             setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
181           }
182           else
183           {
184             // reset cursor
185             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
186           }
187         }
188       }
189     });
190
191     addMouseListener(new MouseAdapter()
192     {
193       @Override
194       public void mousePressed(MouseEvent evt)
195       {
196         if (SwingUtilities.isRightMouseButton(evt))
197         {
198           if (!Platform.isAMac())
199           {
200             showPopupMenu(evt);
201           }
202         }
203         else
204         {
205           // don't do anything if the mouse press is in the overview's box
206           // (wait to see if it's a drag instead)
207           // otherwise update the viewport
208           if (!od.isPositionInBox(evt.getX(), evt.getY()))
209           {
210             draggingBox = false;
211             od.updateViewportFromMouse(evt.getX(), evt.getY(),
212                     av.getAlignment().getHiddenSequences(),
213                     av.getAlignment().getHiddenColumns());
214           }
215           else
216           {
217             draggingBox = true;
218             od.setDragPoint(evt.getX(), evt.getY(),
219                     av.getAlignment().getHiddenSequences(),
220                     av.getAlignment().getHiddenColumns());
221           }
222         }
223       }
224
225       @Override
226       public void mouseClicked(MouseEvent evt)
227       {
228         if (SwingUtilities.isRightMouseButton(evt))
229         {
230           showPopupMenu(evt);
231         }
232       }
233     });
234   }
235
236   /*
237    * Displays the popup menu and acts on user input
238    */
239   private void showPopupMenu(MouseEvent e)
240   {
241     JPopupMenu popup = new JPopupMenu();
242     ActionListener menuListener = new ActionListener()
243     {
244       @Override
245       public void actionPerformed(ActionEvent event)
246       {
247         // switch on/off the hidden columns view
248         toggleHiddenColumns();
249         displayToggle.setSelected(showHidden);
250       }
251     };
252     displayToggle = new JCheckBoxMenuItem(
253             MessageManager.getString("label.togglehidden"));
254     displayToggle.setEnabled(true);
255     displayToggle.setSelected(showHidden);
256     popup.add(displayToggle);
257     displayToggle.addActionListener(menuListener);
258     popup.show(this, e.getX(), e.getY());
259   }
260
261   /*
262    * Toggle overview display between showing hidden columns and hiding hidden columns
263    */
264   private void toggleHiddenColumns()
265   {
266     if (showHidden)
267     {
268       showHidden = false;
269       od = new OverviewDimensionsHideHidden(av.getRanges(),
270               (av.isShowAnnotation()
271                       && av.getAlignmentConservationAnnotation() != null));
272     }
273     else
274     {
275       showHidden = true;
276       od = new OverviewDimensionsShowHidden(av.getRanges(),
277               (av.isShowAnnotation()
278                       && av.getAlignmentConservationAnnotation() != null));
279     }
280     oviewCanvas.resetOviewDims(od);
281     updateOverviewImage();
282     setBoxPosition();
283   }
284
285   /**
286    * Updates the overview image when the related alignment panel is updated
287    */
288   public void updateOverviewImage()
289   {
290     if (oviewCanvas == null)
291     {
292       /*
293        * panel has been disposed
294        */
295       return;
296     }
297
298     if ((getWidth() > 0) && (getHeight() > 0))
299     {
300       od.setWidth(getWidth());
301       od.setHeight(getHeight() - progressPanel.getHeight());
302     }
303     
304     setPreferredSize(new Dimension(od.getWidth(),
305             od.getHeight() + progressPanel.getHeight()));
306
307     if (oviewCanvas.restartDraw())
308     {
309       return;
310     }
311
312     Thread thread = new Thread(this);
313     thread.start();
314     repaint();
315
316     
317   }
318
319   @Override
320   public void run()
321   {
322     if (oviewCanvas != null)
323     {
324       oviewCanvas.draw(av.isShowSequenceFeatures(),
325               (av.isShowAnnotation()
326                       && av.getAlignmentConservationAnnotation() != null),
327               ap.getSeqPanel().seqCanvas.getFeatureRenderer());
328       setBoxPosition();
329     }
330   }
331
332   /**
333    * Update the overview panel box when the associated alignment panel is
334    * changed
335    * 
336    */
337   private void setBoxPositionOnly()
338   {
339     if (od != null)
340     {
341       int oldX = od.getBoxX();
342       int oldY = od.getBoxY();
343       int oldWidth = od.getBoxWidth();
344       int oldHeight = od.getBoxHeight();
345       od.setBoxPosition(av.getAlignment().getHiddenSequences(),
346               av.getAlignment().getHiddenColumns());
347       repaint(oldX - 1, oldY - 1, oldWidth + 2, oldHeight + 2);
348       repaint(od.getBoxX(), od.getBoxY(), od.getBoxWidth(),
349               od.getBoxHeight());
350     }
351   }
352
353   private void setBoxPosition()
354   {
355     if (od != null)
356     {
357       od.setBoxPosition(av.getAlignment().getHiddenSequences(),
358               av.getAlignment().getHiddenColumns());
359       repaint();
360     }
361   }
362
363   @Override
364   public void propertyChange(PropertyChangeEvent evt)
365   {
366     setBoxPositionOnly();
367   }
368
369   /**
370    * Removes this object as a property change listener, and nulls references
371    */
372   protected void dispose()
373   {
374     try
375     {
376       if (av != null)
377       {
378         av.getRanges().removePropertyChangeListener(this);
379       }
380
381       oviewCanvas.dispose();
382
383       /*
384        * close the parent frame (which also removes it from the
385        * Desktop Windows menu)
386        */
387       ((JInternalFrame) SwingUtilities.getAncestorOfClass(
388               JInternalFrame.class, (this))).setClosed(true);
389     } catch (PropertyVetoException e)
390     {
391       // ignore
392     } finally
393     {
394       progressPanel = null;
395       av = null;
396       oviewCanvas = null;
397       ap = null;
398       od = null;
399     }
400   }
401 }