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 java.awt.BorderLayout;
24 import java.awt.Cursor;
25 import java.awt.Dimension;
26 import java.awt.Rectangle;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.ComponentAdapter;
30 import java.awt.event.ComponentEvent;
31 import java.awt.event.MouseAdapter;
32 import java.awt.event.MouseEvent;
33 import java.awt.event.MouseMotionAdapter;
34 import java.beans.PropertyChangeEvent;
35 import java.beans.PropertyVetoException;
37 import javax.swing.JCheckBoxMenuItem;
38 import javax.swing.JInternalFrame;
39 import javax.swing.JPanel;
40 import javax.swing.JPopupMenu;
41 import javax.swing.SwingUtilities;
43 import jalview.renderer.OverviewRenderer;
44 import jalview.util.MessageManager;
45 import jalview.util.Platform;
46 import jalview.viewmodel.OverviewDimensions;
47 import jalview.viewmodel.OverviewDimensionsHideHidden;
48 import jalview.viewmodel.OverviewDimensionsShowHidden;
49 import jalview.viewmodel.ViewportListenerI;
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 private JInternalFrame internalFrame;
72 protected JCheckBoxMenuItem displayToggle;
74 protected boolean showHidden = true;
76 protected boolean draggingBox = false;
78 protected ProgressPanel progressPanel;
81 * Creates a new OverviewPanel object.
84 * The alignment panel which is shown in the overview panel
89 public OverviewPanel(AlignmentPanel alPanel, JInternalFrame frame,
94 this.internalFrame = frame;
96 showHidden = isShowHidden;
99 od = new OverviewDimensionsShowHidden(av.getRanges(),
100 (av.isShowAnnotation()
101 && av.getAlignmentConservationAnnotation() != null));
105 od = new OverviewDimensionsHideHidden(av.getRanges(),
106 (av.isShowAnnotation()
107 && av.getAlignmentConservationAnnotation() != null));
110 setLayout(new BorderLayout());
111 progressPanel = new ProgressPanel(OverviewRenderer.UPDATE,
112 MessageManager.getString("label.oview_calc"), getWidth());
113 this.add(progressPanel, BorderLayout.SOUTH);
114 oviewCanvas = new OverviewCanvas(od, av, progressPanel);
116 add(oviewCanvas, BorderLayout.CENTER);
118 av.getRanges().addPropertyChangeListener(this);
120 // without this the overview window does not size to fit the overview canvas
121 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
123 addComponentListener(new ComponentAdapter()
126 public void componentResized(ComponentEvent evt)
128 // Resize is called on the initial display of the overview.
129 // This code adjusts sizes to account for the progress bar if it has not
130 // already been accounted for, which triggers another resize call for
131 // the correct sizing, at which point the overview image is updated.
132 // (This avoids a double recalculation of the image.)
133 if (getWidth() == od.getWidth() && getHeight() == od.getHeight()
134 + progressPanel.getHeight())
136 updateOverviewImage();
140 if ((getWidth() > 0) && (getHeight() > 0))
142 od.setWidth(getWidth());
143 od.setHeight(getHeight() - progressPanel.getHeight());
146 setPreferredSize(new Dimension(od.getWidth(),
147 od.getHeight() + progressPanel.getHeight()));
153 addMouseMotionListener(new MouseMotionAdapter()
156 public void mouseDragged(MouseEvent evt)
158 if (!SwingUtilities.isRightMouseButton(evt))
162 // set the mouse position as a fixed point in the box
163 // and drag relative to that position
164 od.adjustViewportFromMouse(evt.getX(), evt.getY(),
165 av.getAlignment().getHiddenSequences(),
166 av.getAlignment().getHiddenColumns());
170 od.updateViewportFromMouse(evt.getX(), evt.getY(),
171 av.getAlignment().getHiddenSequences(),
172 av.getAlignment().getHiddenColumns());
178 public void mouseMoved(MouseEvent evt)
180 if (od.isPositionInBox(evt.getX(), evt.getY()))
183 * using HAND_CURSOR rather than DRAG_CURSOR
184 * as the latter is not supported on Mac
186 getParent().setCursor(
187 Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
192 getParent().setCursor(
193 Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
199 addMouseListener(new MouseAdapter()
202 public void mousePressed(MouseEvent evt)
205 if (Platform.isWinRightButton(evt))
210 if (SwingUtilities.isRightMouseButton(evt))
214 // don't do anything if the mouse press is in the overview's box
215 // (wait to see if it's a drag instead)
216 // otherwise update the viewport
217 if (!od.isPositionInBox(evt.getX(), evt.getY()))
221 // display drag cursor at mouse position
222 setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
224 od.updateViewportFromMouse(evt.getX(), evt.getY(),
225 av.getAlignment().getHiddenSequences(),
226 av.getAlignment().getHiddenColumns());
227 getParent().setCursor(
228 Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
233 od.setDragPoint(evt.getX(), evt.getY(),
234 av.getAlignment().getHiddenSequences(),
235 av.getAlignment().getHiddenColumns());
240 public void mouseClicked(MouseEvent evt)
242 if (SwingUtilities.isRightMouseButton(evt))
249 public void mouseReleased(MouseEvent evt)
257 * Javascript does not call componentResized on initial display,
258 * so do the update here
262 updateOverviewImage();
267 * Displays the popup menu and acts on user input
269 protected void showPopupMenu(MouseEvent e)
271 JPopupMenu popup = new JPopupMenu();
272 ActionListener menuListener = new ActionListener()
275 public void actionPerformed(ActionEvent event)
277 // switch on/off the hidden columns view
278 toggleHiddenColumns();
279 displayToggle.setSelected(showHidden);
282 displayToggle = new JCheckBoxMenuItem(
283 MessageManager.getString("label.togglehidden"));
284 displayToggle.setEnabled(true);
285 displayToggle.setSelected(showHidden);
286 popup.add(displayToggle);
287 displayToggle.addActionListener(menuListener);
288 popup.show(this, e.getX(), e.getY());
292 * Toggle overview display between showing hidden columns and hiding hidden columns
294 protected void toggleHiddenColumns()
299 od = new OverviewDimensionsHideHidden(av.getRanges(),
300 (av.isShowAnnotation()
301 && av.getAlignmentConservationAnnotation() != null));
306 od = new OverviewDimensionsShowHidden(av.getRanges(),
307 (av.isShowAnnotation()
308 && av.getAlignmentConservationAnnotation() != null));
310 oviewCanvas.resetOviewDims(od);
311 updateOverviewImage();
316 * Updates the overview image when the related alignment panel is updated
318 public void updateOverviewImage()
320 if (oviewCanvas == null)
323 * panel has been disposed
328 if ((getWidth() > 0) && (getHeight() > 0))
330 od.setWidth(getWidth());
331 od.setHeight(getHeight() - progressPanel.getHeight());
334 setPreferredSize(new Dimension(od.getWidth(),
335 od.getHeight() + progressPanel.getHeight()));
337 if (oviewCanvas.restartDraw())
342 Thread thread = new Thread(this);
351 if (oviewCanvas != null)
353 oviewCanvas.draw(av.isShowSequenceFeatures(),
354 (av.isShowAnnotation()
355 && av.getAlignmentConservationAnnotation() != null),
356 ap.getSeqPanel().seqCanvas.getFeatureRenderer());
362 * Update the overview panel box when the associated alignment panel is
366 private void setBoxPositionOnly()
370 int oldX = od.getBoxX();
371 int oldY = od.getBoxY();
372 int oldWidth = od.getBoxWidth();
373 int oldHeight = od.getBoxHeight();
374 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
375 av.getAlignment().getHiddenColumns());
376 repaint(oldX - 1, oldY - 1, oldWidth + 2, oldHeight + 2);
377 repaint(od.getBoxX(), od.getBoxY(), od.getBoxWidth(),
382 private void setBoxPosition()
386 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
387 av.getAlignment().getHiddenColumns());
393 public void propertyChange(PropertyChangeEvent evt)
395 setBoxPositionOnly();
399 * Removes this object as a property change listener, and nulls references
401 protected void dispose()
407 av.getRanges().removePropertyChangeListener(this);
410 oviewCanvas.dispose();
413 * close the parent frame (which also removes it from the
414 * Desktop Windows menu)
416 ((JInternalFrame) SwingUtilities
417 .getAncestorOfClass(JInternalFrame.class, (this)))
419 } catch (PropertyVetoException e)
424 progressPanel = null;
429 internalFrame = null;
433 public boolean isShowHiddenRegions()
438 public OverviewCanvas getCanvas()
444 * Sets the title of the enclosing frame
448 public void setTitle(String title)
450 internalFrame.setTitle(title);
454 * Returns the title of the enclosing frame
458 public String getTitle()
460 return internalFrame.getTitle();
464 * Sets the bounds of the frame holding the Overview panel
471 public void setFrameBounds(int xpos, int ypos, int width, int height)
473 internalFrame.setBounds(xpos, ypos, width, height);
477 * Returns the bounds of the enclosing frame
481 public Rectangle getFrameBounds()
483 return internalFrame.getBounds();
487 * Closes the frame containing the overview panel
491 internalFrame.dispose();