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.datamodel.ColumnSelection;
24 import jalview.datamodel.HiddenColumns;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.renderer.ScaleRenderer;
27 import jalview.renderer.ScaleRenderer.ScaleMark;
28 import jalview.util.MessageManager;
29 import jalview.util.Platform;
30 import jalview.viewmodel.ViewportListenerI;
31 import jalview.viewmodel.ViewportRanges;
33 import java.awt.Color;
34 import java.awt.FontMetrics;
35 import java.awt.Graphics;
36 import java.awt.Graphics2D;
37 import java.awt.Point;
38 import java.awt.RenderingHints;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.MouseEvent;
42 import java.awt.event.MouseListener;
43 import java.awt.event.MouseMotionListener;
44 import java.beans.PropertyChangeEvent;
45 import java.util.Iterator;
46 import java.util.List;
48 import javax.swing.JMenuItem;
49 import javax.swing.JPanel;
50 import javax.swing.JPopupMenu;
51 import javax.swing.ToolTipManager;
54 * The panel containing the sequence ruler (when not in wrapped mode), and
55 * supports a range of mouse operations to select, hide or reveal columns.
57 public class ScalePanel extends JPanel
58 implements MouseMotionListener, MouseListener, ViewportListenerI
60 protected int offy = 4;
64 protected AlignViewport av;
68 boolean stretchingGroup = false;
71 * min, max hold the extent of a mouse drag action
77 boolean mouseDragging = false;
80 * holds a hidden column range when the mouse is over an adjacent column
90 public ScalePanel(AlignViewport av, AlignmentPanel ap)
95 addMouseListener(this);
96 addMouseMotionListener(this);
98 av.getRanges().addPropertyChangeListener(this);
108 public void mousePressed(MouseEvent evt)
110 int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes();
113 if (av.hasHiddenColumns())
115 x = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(x);
117 res = Math.min(x, av.getAlignment().getWidth() - 1);
122 if (evt.isPopupTrigger()) // Mac: mousePressed
124 rightMouseButtonPressed(evt, res);
127 if (Platform.isWinRightButton(evt))
130 * defer right-mouse click handling to mouse up on Windows
131 * (where isPopupTrigger() will answer true)
132 * but accept Cmd-click on Mac which passes isRightMouseButton
136 leftMouseButtonPressed(evt, res);
140 * Handles right mouse button press. If pressed in a selected column, opens
141 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
142 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
147 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
149 JPopupMenu pop = buildPopupMenu(res);
150 if (pop.getSubElements().length > 0)
152 pop.show(this, evt.getX(), evt.getY());
157 * Builds a popup menu with 'Hide' or 'Reveal' options, or both, or neither
160 * column number (0..)
163 protected JPopupMenu buildPopupMenu(final int res)
165 JPopupMenu pop = new JPopupMenu();
168 * logic here depends on 'reveal', set in mouseMoved;
169 * grab the hidden range in case mouseMoved nulls it later
171 final int[] hiddenRange = reveal;
172 if (hiddenRange != null)
174 JMenuItem item = new JMenuItem(
175 MessageManager.getString("label.reveal"));
176 item.addActionListener(new ActionListener()
179 public void actionPerformed(ActionEvent e)
181 av.showColumn(hiddenRange[0]);
184 ap.paintAlignment(true, true);
190 if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
192 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
193 item.addActionListener(new ActionListener()
196 public void actionPerformed(ActionEvent e)
198 av.showAllHiddenColumns();
201 ap.paintAlignment(true, true);
209 if (av.getColumnSelection().contains(res))
211 JMenuItem item = new JMenuItem(
212 MessageManager.getString("label.hide_columns"));
213 item.addActionListener(new ActionListener()
216 public void actionPerformed(ActionEvent e)
218 av.hideColumns(res, res);
219 if (av.getSelectionGroup() != null && av.getSelectionGroup()
220 .getSize() == av.getAlignment().getHeight())
222 av.setSelectionGroup(null);
226 ap.paintAlignment(true, true);
236 * Handles left mouse button press
241 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
244 * Ctrl-click/Cmd-click adds to the selection
245 * Shift-click extends the selection
247 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
248 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
250 av.getColumnSelection().clear();
253 av.getColumnSelection().addElement(res);
254 SequenceGroup sg = new SequenceGroup(av.getAlignment().getSequences());
258 if (evt.isShiftDown())
260 int min = Math.min(av.getColumnSelection().getMin(), res);
261 int max = Math.max(av.getColumnSelection().getMax(), res);
262 for (int i = min; i < max; i++)
264 av.getColumnSelection().addElement(i);
269 av.setSelectionGroup(sg);
270 ap.paintAlignment(false, false);
275 * Action on mouseUp is to set the limit of the current selection group (if
276 * there is one) and broadcast the selection
281 public void mouseReleased(MouseEvent evt)
283 boolean wasDragging = mouseDragging;
284 mouseDragging = false;
285 ap.getSeqPanel().stopScrolling();
287 // todo res calculation should be a method on AlignViewport
288 int xCords = Math.max(0, evt.getX()); // prevent negative X coordinates
289 ViewportRanges ranges = av.getRanges();
290 int res = (xCords / av.getCharWidth())
291 + ranges.getStartRes();
292 res = Math.min(res, ranges.getEndRes());
293 if (av.hasHiddenColumns())
295 res = av.getAlignment().getHiddenColumns()
296 .visibleToAbsoluteColumn(res);
298 res = Math.max(0, res);
300 if (!stretchingGroup)
302 if (evt.isPopupTrigger()) // Windows: mouseReleased
304 rightMouseButtonPressed(evt, res);
308 ap.paintAlignment(false, false);
313 SequenceGroup sg = av.getSelectionGroup();
317 if (res > sg.getStartRes())
321 else if (res < sg.getStartRes())
327 min = Math.min(res, min);
328 max = Math.max(res, max);
329 av.getColumnSelection().stretchGroup(res, sg, min, max);
332 stretchingGroup = false;
333 ap.paintAlignment(false, false);
334 av.isSelectionGroupChanged(true);
335 av.isColSelChanged(true);
340 * Action on dragging the mouse in the scale panel is to expand or shrink the
341 * selection group range (including any hidden columns that it spans). Note
342 * that the selection is only broadcast at the start of the drag (on
343 * mousePressed) and at the end (on mouseReleased), to avoid overload
344 * redrawing of other views.
349 public void mouseDragged(MouseEvent evt)
351 mouseDragging = true;
352 ColumnSelection cs = av.getColumnSelection();
353 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
355 int res = (evt.getX() / av.getCharWidth())
356 + av.getRanges().getStartRes();
357 res = Math.max(0, res);
358 res = hidden.visibleToAbsoluteColumn(res);
359 res = Math.min(res, av.getAlignment().getWidth() - 1);
360 min = Math.min(res, min);
361 max = Math.max(res, max);
363 SequenceGroup sg = av.getSelectionGroup();
366 stretchingGroup = true;
367 cs.stretchGroup(res, sg, min, max);
368 ap.paintAlignment(false, false);
373 public void mouseEntered(MouseEvent evt)
377 mouseDragging = false;
378 ap.getSeqPanel().stopScrolling();
383 * Action on leaving the panel bounds with mouse drag in progress is to start
384 * scrolling the alignment in the direction of the mouse. To restrict
385 * scrolling to left-right (not up-down), the y-value of the mouse position is
386 * replaced with zero.
389 public void mouseExited(MouseEvent evt)
393 ap.getSeqPanel().startScrolling(new Point(evt.getX(), 0));
398 public void mouseClicked(MouseEvent evt)
403 * Creates a tooltip when the mouse is over a hidden columns marker
406 public void mouseMoved(MouseEvent evt)
408 this.setToolTipText(null);
410 if (!av.hasHiddenColumns())
415 int res = (evt.getX() / av.getCharWidth())
416 + av.getRanges().getStartRes();
418 reveal = av.getAlignment().getHiddenColumns()
419 .getRegionWithEdgeAtRes(res);
421 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
423 ToolTipManager.sharedInstance().registerComponent(this);
425 MessageManager.getString("label.reveal_hidden_columns"));
436 public void paintComponent(Graphics g)
438 //super.paintComponent(g); // BH 2019
441 * shouldn't get called in wrapped mode as the scale above is
442 * drawn instead by SeqCanvas.drawNorthScale
444 if (!av.getWrapAlignment())
446 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
447 getWidth(), getHeight());
451 // scalewidth will normally be screenwidth,
452 public void drawScale(Graphics g, int startx, int endx, int width,
455 Graphics2D gg = (Graphics2D) g;
456 gg.setFont(av.getFont());
460 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
461 RenderingHints.VALUE_ANTIALIAS_ON);
464 // Fill in the background
465 gg.setColor(Color.white);
466 gg.fillRect(0, 0, width, height);
467 gg.setColor(Color.black);
469 // Fill the selected columns
470 ColumnSelection cs = av.getColumnSelection();
471 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
472 int avCharWidth = av.getCharWidth();
473 int avCharHeight = av.getCharHeight();
477 gg.setColor(new Color(220, 0, 0));
479 for (int sel : cs.getSelected())
481 // TODO: JAL-2001 - provide a fast method to list visible selected in a
484 if (av.hasHiddenColumns())
486 if (hidden.isVisible(sel))
488 sel = hidden.absoluteToVisibleColumn(sel);
496 if ((sel >= startx) && (sel <= endx))
498 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
504 int widthx = 1 + endx - startx;
506 FontMetrics fm = gg.getFontMetrics(av.getFont());
507 int y = avCharHeight;
508 int yOf = fm.getDescent();
510 if (av.hasHiddenColumns())
512 // draw any hidden column markers
513 gg.setColor(Color.blue);
516 if (av.getShowHiddenMarkers())
518 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
519 startx + widthx + 1);
522 res = it.next() - startx;
526 { -1 + res * avCharWidth - avCharHeight / 4,
527 -1 + res * avCharWidth + avCharHeight / 4,
528 -1 + res * avCharWidth }, new int[]
529 { y, y, y + 2 * yOf }, 3);
533 // Draw the scale numbers
534 gg.setColor(Color.black);
537 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
540 for (ScaleMark mark : marks)
542 boolean major = mark.major;
543 int mpos = mark.column; // (i - startx - 1)
544 String mstring = mark.text;
547 if (mpos * avCharWidth > maxX)
549 gg.drawString(mstring, mpos * avCharWidth, y);
550 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
555 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
556 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
560 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
561 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
567 public void propertyChange(PropertyChangeEvent evt)
569 // Respond to viewport change events (e.g. alignment panel was scrolled)
570 // Both scrolling and resizing change viewport ranges: scrolling changes
571 // both start and end points, but resize only changes end values.
572 // Here we only want to fastpaint on a scroll, with resize using a normal
573 // paint, so scroll events are identified as changes to the horizontal or
574 // vertical start value.
575 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
576 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
577 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
579 // scroll event, repaint panel
581 // Call repaint on alignment panel so that repaints from other alignment
582 // panel components can be aggregated. Otherwise performance of the overview
583 // window and others may be adversely affected.
584 av.getAlignPanel().repaint();