2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
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;
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;
52 * Panel displaying an overview of the full alignment, with an interactive box
53 * representing the viewport onto the alignment.
58 @SuppressWarnings("serial")
59 public class OverviewPanel extends JPanel
60 implements Runnable, ViewportListenerI
62 protected OverviewDimensions od;
64 private OverviewCanvas oviewCanvas;
66 protected AlignViewport av;
68 private AlignmentPanel ap;
70 protected JCheckBoxMenuItem displayToggle;
72 protected boolean showHidden = true;
74 protected boolean draggingBox = false;
76 protected ProgressPanel progressPanel;
79 * Creates a new OverviewPanel object.
82 * The alignment panel which is shown in the overview panel
84 public OverviewPanel(AlignmentPanel alPanel)
89 showHidden = Cache.getDefault(Preferences.SHOW_OV_HIDDEN_AT_START,
93 od = new OverviewDimensionsShowHidden(av.getRanges(),
94 (av.isShowAnnotation()
95 && av.getAlignmentConservationAnnotation() != null));
99 od = new OverviewDimensionsHideHidden(av.getRanges(),
100 (av.isShowAnnotation()
101 && av.getAlignmentConservationAnnotation() != null));
104 setLayout(new BorderLayout());
105 progressPanel = new ProgressPanel(OverviewRenderer.UPDATE,
106 MessageManager.getString("label.oview_calc"), getWidth());
107 this.add(progressPanel, BorderLayout.SOUTH);
108 oviewCanvas = new OverviewCanvas(od, av, progressPanel);
110 add(oviewCanvas, BorderLayout.CENTER);
112 av.getRanges().addPropertyChangeListener(this);
114 // without this the overview window does not size to fit the overview canvas
115 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
117 addComponentListener(new ComponentAdapter()
120 public void componentResized(ComponentEvent evt)
122 // Resize is called on the initial display of the overview.
123 // This code adjusts sizes to account for the progress bar if it has not
124 // already been accounted for, which triggers another resize call for
125 // the correct sizing, at which point the overview image is updated.
126 // (This avoids a double recalculation of the image.)
127 if (getWidth() == od.getWidth() && getHeight() == od.getHeight()
128 + progressPanel.getHeight())
130 updateOverviewImage();
134 if ((getWidth() > 0) && (getHeight() > 0))
136 od.setWidth(getWidth());
137 od.setHeight(getHeight() - progressPanel.getHeight());
140 setPreferredSize(new Dimension(od.getWidth(),
141 od.getHeight() + progressPanel.getHeight()));
147 addMouseMotionListener(new MouseMotionAdapter()
150 public void mouseDragged(MouseEvent evt)
152 if (!SwingUtilities.isRightMouseButton(evt))
156 // set the mouse position as a fixed point in the box
157 // and drag relative to that position
158 od.adjustViewportFromMouse(evt.getX(), evt.getY(),
159 av.getAlignment().getHiddenSequences(),
160 av.getAlignment().getHiddenColumns());
164 od.updateViewportFromMouse(evt.getX(), evt.getY(),
165 av.getAlignment().getHiddenSequences(),
166 av.getAlignment().getHiddenColumns());
172 public void mouseMoved(MouseEvent evt)
174 if (od.isPositionInBox(evt.getX(), evt.getY()))
177 * using HAND_CURSOR rather than DRAG_CURSOR
178 * as the latter is not supported on Mac
180 getParent().setCursor(
181 Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
186 getParent().setCursor(
187 Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
193 addMouseListener(new MouseAdapter()
196 public void mousePressed(MouseEvent evt)
199 if (Platform.isWinRightButton(evt))
204 if (SwingUtilities.isRightMouseButton(evt))
208 // don't do anything if the mouse press is in the overview's box
209 // (wait to see if it's a drag instead)
210 // otherwise update the viewport
211 if (!od.isPositionInBox(evt.getX(), evt.getY()))
215 // display drag cursor at mouse position
216 setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
218 od.updateViewportFromMouse(evt.getX(), evt.getY(),
219 av.getAlignment().getHiddenSequences(),
220 av.getAlignment().getHiddenColumns());
221 getParent().setCursor(
222 Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
227 od.setDragPoint(evt.getX(), evt.getY(),
228 av.getAlignment().getHiddenSequences(),
229 av.getAlignment().getHiddenColumns());
234 public void mouseClicked(MouseEvent evt)
236 if (SwingUtilities.isRightMouseButton(evt))
243 public void mouseReleased(MouseEvent evt)
251 * Javascript does not call componentResized on initial display,
252 * so do the update here
256 updateOverviewImage();
261 * Displays the popup menu and acts on user input
263 protected void showPopupMenu(MouseEvent e)
265 JPopupMenu popup = new JPopupMenu();
266 ActionListener menuListener = new ActionListener()
269 public void actionPerformed(ActionEvent event)
271 // switch on/off the hidden columns view
272 toggleHiddenColumns();
273 displayToggle.setSelected(showHidden);
276 displayToggle = new JCheckBoxMenuItem(
277 MessageManager.getString("label.togglehidden"));
278 displayToggle.setEnabled(true);
279 displayToggle.setSelected(showHidden);
280 popup.add(displayToggle);
281 displayToggle.addActionListener(menuListener);
282 popup.show(this, e.getX(), e.getY());
286 * Toggle overview display between showing hidden columns and hiding hidden columns
288 protected void toggleHiddenColumns()
293 od = new OverviewDimensionsHideHidden(av.getRanges(),
294 (av.isShowAnnotation()
295 && av.getAlignmentConservationAnnotation() != null));
300 od = new OverviewDimensionsShowHidden(av.getRanges(),
301 (av.isShowAnnotation()
302 && av.getAlignmentConservationAnnotation() != null));
304 oviewCanvas.resetOviewDims(od);
305 updateOverviewImage();
310 * Updates the overview image when the related alignment panel is updated
312 public void updateOverviewImage()
314 if (oviewCanvas == null)
317 * panel has been disposed
322 if ((getWidth() > 0) && (getHeight() > 0))
324 od.setWidth(getWidth());
325 od.setHeight(getHeight() - progressPanel.getHeight());
328 setPreferredSize(new Dimension(od.getWidth(),
329 od.getHeight() + progressPanel.getHeight()));
331 if (oviewCanvas.restartDraw())
336 Thread thread = new Thread(this);
345 if (oviewCanvas != null)
347 oviewCanvas.draw(av.isShowSequenceFeatures(),
348 (av.isShowAnnotation()
349 && av.getAlignmentConservationAnnotation() != null),
350 ap.getSeqPanel().seqCanvas.getFeatureRenderer());
356 * Update the overview panel box when the associated alignment panel is
360 private void setBoxPositionOnly()
364 int oldX = od.getBoxX();
365 int oldY = od.getBoxY();
366 int oldWidth = od.getBoxWidth();
367 int oldHeight = od.getBoxHeight();
368 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
369 av.getAlignment().getHiddenColumns());
370 repaint(oldX - 1, oldY - 1, oldWidth + 2, oldHeight + 2);
371 repaint(od.getBoxX(), od.getBoxY(), od.getBoxWidth(),
376 private void setBoxPosition()
380 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
381 av.getAlignment().getHiddenColumns());
387 public void propertyChange(PropertyChangeEvent evt)
389 setBoxPositionOnly();
393 * Removes this object as a property change listener, and nulls references
395 protected void dispose()
401 av.getRanges().removePropertyChangeListener(this);
404 oviewCanvas.dispose();
407 * close the parent frame (which also removes it from the
408 * Desktop Windows menu)
410 ((JInternalFrame) SwingUtilities
411 .getAncestorOfClass(JInternalFrame.class, (this)))
413 } catch (PropertyVetoException e)
418 progressPanel = null;