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 public class OverviewPanel extends JPanel
59 implements Runnable, ViewportListenerI
61 private OverviewDimensions od;
63 private OverviewCanvas oviewCanvas;
65 private AlignViewport av;
67 private AlignmentPanel ap;
69 private JCheckBoxMenuItem displayToggle;
71 private boolean showHidden = true;
73 private boolean draggingBox = false;
75 private ProgressPanel progressPanel;
78 * Creates a new OverviewPanel object.
81 * The alignment panel which is shown in the overview panel
83 public OverviewPanel(AlignmentPanel alPanel)
88 showHidden = Cache.getDefault(Preferences.SHOW_OV_HIDDEN_AT_START,
92 od = new OverviewDimensionsShowHidden(av.getRanges(),
93 (av.isShowAnnotation()
94 && av.getAlignmentConservationAnnotation() != null));
98 od = new OverviewDimensionsHideHidden(av.getRanges(),
99 (av.isShowAnnotation()
100 && av.getAlignmentConservationAnnotation() != null));
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);
109 add(oviewCanvas, BorderLayout.CENTER);
111 av.getRanges().addPropertyChangeListener(this);
113 // without this the overview window does not size to fit the overview canvas
114 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
116 addComponentListener(new ComponentAdapter()
119 public void componentResized(ComponentEvent evt)
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())
129 updateOverviewImage();
133 if ((getWidth() > 0) && (getHeight() > 0))
135 od.setWidth(getWidth());
136 od.setHeight(getHeight() - progressPanel.getHeight());
139 setPreferredSize(new Dimension(od.getWidth(),
140 od.getHeight() + progressPanel.getHeight()));
146 addMouseMotionListener(new MouseMotionAdapter()
149 public void mouseDragged(MouseEvent evt)
151 if (!SwingUtilities.isRightMouseButton(evt))
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());
163 od.updateViewportFromMouse(evt.getX(), evt.getY(),
164 av.getAlignment().getHiddenSequences(),
165 av.getAlignment().getHiddenColumns());
171 public void mouseMoved(MouseEvent evt)
173 if (od.isPositionInBox(evt.getX(), evt.getY()))
175 // display drag cursor at mouse position
176 setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
181 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
186 addMouseListener(new MouseAdapter()
189 public void mousePressed(MouseEvent evt)
191 if (SwingUtilities.isRightMouseButton(evt))
193 if (!Platform.isAMac())
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()))
206 od.updateViewportFromMouse(evt.getX(), evt.getY(),
207 av.getAlignment().getHiddenSequences(),
208 av.getAlignment().getHiddenColumns());
213 od.setDragPoint(evt.getX(), evt.getY(),
214 av.getAlignment().getHiddenSequences(),
215 av.getAlignment().getHiddenColumns());
221 public void mouseClicked(MouseEvent evt)
223 if (SwingUtilities.isRightMouseButton(evt))
232 * Displays the popup menu and acts on user input
234 private void showPopupMenu(MouseEvent e)
236 JPopupMenu popup = new JPopupMenu();
237 ActionListener menuListener = new ActionListener()
240 public void actionPerformed(ActionEvent event)
242 // switch on/off the hidden columns view
243 toggleHiddenColumns();
244 displayToggle.setSelected(showHidden);
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());
257 * Toggle overview display between showing hidden columns and hiding hidden columns
259 private void toggleHiddenColumns()
264 od = new OverviewDimensionsHideHidden(av.getRanges(),
265 (av.isShowAnnotation()
266 && av.getAlignmentConservationAnnotation() != null));
271 od = new OverviewDimensionsShowHidden(av.getRanges(),
272 (av.isShowAnnotation()
273 && av.getAlignmentConservationAnnotation() != null));
275 oviewCanvas.resetOviewDims(od);
276 updateOverviewImage();
281 * Updates the overview image when the related alignment panel is updated
283 public void updateOverviewImage()
285 if (oviewCanvas == null)
288 * panel has been disposed
293 if ((getWidth() > 0) && (getHeight() > 0))
295 od.setWidth(getWidth());
296 od.setHeight(getHeight() - progressPanel.getHeight());
299 setPreferredSize(new Dimension(od.getWidth(),
300 od.getHeight() + progressPanel.getHeight()));
302 if (oviewCanvas.restartDraw())
307 Thread thread = new Thread(this);
317 if (oviewCanvas != null)
319 oviewCanvas.draw(av.isShowSequenceFeatures(),
320 (av.isShowAnnotation()
321 && av.getAlignmentConservationAnnotation() != null),
322 ap.getSeqPanel().seqCanvas.getFeatureRenderer());
328 * Update the overview panel box when the associated alignment panel is
332 private void setBoxPositionOnly()
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(),
348 private void setBoxPosition()
352 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
353 av.getAlignment().getHiddenColumns());
359 public void propertyChange(PropertyChangeEvent evt)
361 setBoxPositionOnly();
365 * Removes this object as a property change listener, and nulls references
367 protected void dispose()
373 av.getRanges().removePropertyChangeListener(this);
376 oviewCanvas.dispose();
379 * close the parent frame (which also removes it from the
380 * Desktop Windows menu)
382 ((JInternalFrame) SwingUtilities.getAncestorOfClass(
383 JInternalFrame.class, (this))).setClosed(true);
384 } catch (PropertyVetoException e)
389 progressPanel = null;