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)) {
203 if (SwingUtilities.isRightMouseButton(evt)) {
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()))
213 // display drag cursor at mouse position
214 setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
216 od.updateViewportFromMouse(evt.getX(), evt.getY(),
217 av.getAlignment().getHiddenSequences(),
218 av.getAlignment().getHiddenColumns());
219 getParent().setCursor(
220 Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
225 od.setDragPoint(evt.getX(), evt.getY(),
226 av.getAlignment().getHiddenSequences(),
227 av.getAlignment().getHiddenColumns());
232 public void mouseClicked(MouseEvent evt)
234 if (SwingUtilities.isRightMouseButton(evt))
241 public void mouseReleased(MouseEvent evt)
249 * Javascript does not call componentResized on initial display,
250 * so do the update here
254 updateOverviewImage();
259 * Displays the popup menu and acts on user input
261 protected void showPopupMenu(MouseEvent e)
263 JPopupMenu popup = new JPopupMenu();
264 ActionListener menuListener = new ActionListener()
267 public void actionPerformed(ActionEvent event)
269 // switch on/off the hidden columns view
270 toggleHiddenColumns();
271 displayToggle.setSelected(showHidden);
274 displayToggle = new JCheckBoxMenuItem(
275 MessageManager.getString("label.togglehidden"));
276 displayToggle.setEnabled(true);
277 displayToggle.setSelected(showHidden);
278 popup.add(displayToggle);
279 displayToggle.addActionListener(menuListener);
280 popup.show(this, e.getX(), e.getY());
284 * Toggle overview display between showing hidden columns and hiding hidden columns
286 protected void toggleHiddenColumns()
291 od = new OverviewDimensionsHideHidden(av.getRanges(),
292 (av.isShowAnnotation()
293 && av.getAlignmentConservationAnnotation() != null));
298 od = new OverviewDimensionsShowHidden(av.getRanges(),
299 (av.isShowAnnotation()
300 && av.getAlignmentConservationAnnotation() != null));
302 oviewCanvas.resetOviewDims(od);
303 updateOverviewImage();
308 * Updates the overview image when the related alignment panel is updated
310 public void updateOverviewImage()
312 if (oviewCanvas == null)
315 * panel has been disposed
320 if ((getWidth() > 0) && (getHeight() > 0))
322 od.setWidth(getWidth());
323 od.setHeight(getHeight() - progressPanel.getHeight());
326 setPreferredSize(new Dimension(od.getWidth(),
327 od.getHeight() + progressPanel.getHeight()));
329 if (oviewCanvas.restartDraw())
334 Thread thread = new Thread(this);
344 if (oviewCanvas != null)
346 oviewCanvas.draw(av.isShowSequenceFeatures(),
347 (av.isShowAnnotation()
348 && av.getAlignmentConservationAnnotation() != null),
349 ap.getSeqPanel().seqCanvas.getFeatureRenderer());
355 * Update the overview panel box when the associated alignment panel is
359 private void setBoxPositionOnly()
363 int oldX = od.getBoxX();
364 int oldY = od.getBoxY();
365 int oldWidth = od.getBoxWidth();
366 int oldHeight = od.getBoxHeight();
367 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
368 av.getAlignment().getHiddenColumns());
369 repaint(oldX - 1, oldY - 1, oldWidth + 2, oldHeight + 2);
370 repaint(od.getBoxX(), od.getBoxY(), od.getBoxWidth(),
375 private void setBoxPosition()
379 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
380 av.getAlignment().getHiddenColumns());
386 public void propertyChange(PropertyChangeEvent evt)
388 setBoxPositionOnly();
392 * Removes this object as a property change listener, and nulls references
394 protected void dispose()
400 av.getRanges().removePropertyChangeListener(this);
403 oviewCanvas.dispose();
406 * close the parent frame (which also removes it from the
407 * Desktop Windows menu)
409 ((JInternalFrame) SwingUtilities.getAncestorOfClass(
410 JInternalFrame.class, (this))).setClosed(true);
411 } catch (PropertyVetoException e)
416 progressPanel = null;