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 private Dimension dim;
78 private boolean showProgress = !Platform.isJS();
80 protected ProgressPanel progressPanel;
84 * Creates the appropriate type of OverviewDimensions, with the desired size
86 private void createOverviewDimensions()
88 boolean showAnnotation = (av.isShowAnnotation()
89 && av.getAlignmentConservationAnnotation() != null);
92 od = new OverviewDimensionsShowHidden(av.getRanges(), showAnnotation,
97 od = new OverviewDimensionsHideHidden(av.getRanges(), showAnnotation,
102 public OverviewPanel(AlignmentPanel alPanel, Dimension dim)
104 this.av = alPanel.av;
108 showHidden = Cache.getDefault(Preferences.SHOW_OV_HIDDEN_AT_START,
110 createOverviewDimensions();
111 setLayout(new BorderLayout());
112 progressPanel = new ProgressPanel(OverviewRenderer.UPDATE,
113 MessageManager.getString("label.oview_calc"), getWidth());
114 this.add(progressPanel, BorderLayout.SOUTH);
115 oviewCanvas = new OverviewCanvas(od, av, progressPanel);
117 add(oviewCanvas, BorderLayout.CENTER);
119 av.getRanges().addPropertyChangeListener(this);
121 // without this the overview window does not size to fit the overview canvas
122 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
124 addComponentListener(new ComponentAdapter()
127 public void componentResized(ComponentEvent evt)
129 // Resize is called on the initial display of the overview.
130 // This code adjusts sizes to account for the progress bar if it has not
131 // already been accounted for, which triggers another resize call for
132 // the correct sizing, at which point the overview image is updated.
133 // (This avoids a double recalculation of the image.)
134 if (getWidth() == od.getWidth() && getHeight() == od.getHeight()
135 + progressPanel.getHeight())
137 updateOverviewImage();
141 if ((getWidth() > 0) && (getHeight() > 0))
143 od.setWidth(getWidth());
144 od.setHeight(getHeight() - progressPanel.getHeight());
147 setPreferredSize(new Dimension(od.getWidth(),
148 od.getHeight() + progressPanel.getHeight()));
154 addMouseMotionListener(new MouseMotionAdapter()
157 public void mouseDragged(MouseEvent evt)
159 if (!SwingUtilities.isRightMouseButton(evt))
163 // set the mouse position as a fixed point in the box
164 // and drag relative to that position
165 od.adjustViewportFromMouse(evt.getX(), evt.getY(),
166 av.getAlignment().getHiddenSequences(),
167 av.getAlignment().getHiddenColumns());
171 od.updateViewportFromMouse(evt.getX(), evt.getY(),
172 av.getAlignment().getHiddenSequences(),
173 av.getAlignment().getHiddenColumns());
179 public void mouseMoved(MouseEvent evt)
181 if (od.isPositionInBox(evt.getX(), evt.getY()))
184 * using HAND_CURSOR rather than DRAG_CURSOR
185 * as the latter is not supported on Mac
187 getParent().setCursor(
188 Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
193 getParent().setCursor(
194 Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
200 addMouseListener(new MouseAdapter()
203 public void mousePressed(MouseEvent evt)
206 if (Platform.isWinRightButton(evt)) {
210 if (SwingUtilities.isRightMouseButton(evt)) {
213 // don't do anything if the mouse press is in the overview's box
214 // (wait to see if it's a drag instead)
215 // otherwise update the viewport
216 if (!od.isPositionInBox(evt.getX(), evt.getY()))
220 // display drag cursor at mouse position
221 setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
223 od.updateViewportFromMouse(evt.getX(), evt.getY(),
224 av.getAlignment().getHiddenSequences(),
225 av.getAlignment().getHiddenColumns());
226 getParent().setCursor(
227 Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
232 od.setDragPoint(evt.getX(), evt.getY(),
233 av.getAlignment().getHiddenSequences(),
234 av.getAlignment().getHiddenColumns());
239 public void mouseClicked(MouseEvent evt)
241 if (SwingUtilities.isRightMouseButton(evt))
248 public void mouseReleased(MouseEvent evt)
256 * Javascript does not call componentResized on initial display,
257 * so do the update here
261 updateOverviewImage();
266 * Displays the popup menu and acts on user input
268 protected void showPopupMenu(MouseEvent e)
270 JPopupMenu popup = new JPopupMenu();
271 ActionListener menuListener = new ActionListener()
274 public void actionPerformed(ActionEvent event)
276 // switch on/off the hidden columns view
277 toggleHiddenColumns();
278 displayToggle.setSelected(showHidden);
281 displayToggle = new JCheckBoxMenuItem(
282 MessageManager.getString("label.togglehidden"));
283 displayToggle.setEnabled(true);
284 displayToggle.setSelected(showHidden);
285 popup.add(displayToggle);
286 displayToggle.addActionListener(menuListener);
287 popup.show(this, e.getX(), e.getY());
291 * Toggle overview display between showing hidden columns and hiding hidden columns
293 protected void toggleHiddenColumns()
295 showHidden = !showHidden;
296 createOverviewDimensions();
297 oviewCanvas.resetOviewDims(od);
298 updateOverviewImage();
303 * Updates the overview image when the related alignment panel is updated
305 public void updateOverviewImage()
307 if (oviewCanvas == null)
310 * panel has been disposed
315 if ((getWidth() > 0) && (getHeight() > 0))
317 od.setWidth(getWidth());
318 od.setHeight(getHeight() - progressPanel.getHeight());
321 setPreferredSize(new Dimension(od.getWidth(),
322 od.getHeight() + progressPanel.getHeight()));
324 if (oviewCanvas.restartDraw())
329 Thread thread = new Thread(this);
339 if (oviewCanvas != null)
341 oviewCanvas.draw(av.isShowSequenceFeatures(),
342 (av.isShowAnnotation()
343 && av.getAlignmentConservationAnnotation() != null),
344 ap.getSeqPanel().seqCanvas.getFeatureRenderer());
350 * Update the overview panel box when the associated alignment panel is
354 private void setBoxPositionOnly()
358 int oldX = od.getBoxX();
359 int oldY = od.getBoxY();
360 int oldWidth = od.getBoxWidth();
361 int oldHeight = od.getBoxHeight();
362 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
363 av.getAlignment().getHiddenColumns());
364 repaint(oldX - 1, oldY - 1, oldWidth + 2, oldHeight + 2);
365 repaint(od.getBoxX(), od.getBoxY(), od.getBoxWidth(),
370 private void setBoxPosition()
374 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
375 av.getAlignment().getHiddenColumns());
381 public void propertyChange(PropertyChangeEvent evt)
383 setBoxPositionOnly();
387 * Removes this object as a property change listener, and nulls references
389 protected void dispose()
395 av.getRanges().removePropertyChangeListener(this);
398 oviewCanvas.dispose();
401 * close the parent frame (which also removes it from the
402 * Desktop Windows menu)
404 ((JInternalFrame) SwingUtilities.getAncestorOfClass(
405 JInternalFrame.class, (this))).setClosed(true);
406 } catch (PropertyVetoException e)
411 progressPanel = null;