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