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.SwingUtilities;
52 import javax.swing.ToolTipManager;
55 * The panel containing the sequence ruler (when not in wrapped mode), and
56 * supports a range of mouse operations to select, hide or reveal columns.
58 public class ScalePanel extends JPanel
59 implements MouseMotionListener, MouseListener, ViewportListenerI
61 protected int offy = 4;
65 protected AlignViewport av;
69 boolean stretchingGroup = false;
72 * min, max hold the extent of a mouse drag action
78 boolean mouseDragging = false;
81 * holds a hidden column range when the mouse is over an adjacent column
91 public ScalePanel(AlignViewport av, AlignmentPanel ap)
96 addMouseListener(this);
97 addMouseMotionListener(this);
99 av.getRanges().addPropertyChangeListener(this);
109 public void mousePressed(MouseEvent evt)
111 int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes();
114 if (av.hasHiddenColumns())
116 x = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(x);
119 if (x >= av.getAlignment().getWidth())
121 res = av.getAlignment().getWidth() - 1;
131 if (evt.isPopupTrigger()) // Mac: mousePressed
133 rightMouseButtonPressed(evt, res);
136 if (Platform.isWinRightButton(evt))
139 * defer right-mouse click handling to mouse up on Windows
140 * (where isPopupTrigger() will answer true)
141 * but accept Cmd-click on Mac which passes isRightMouseButton
145 leftMouseButtonPressed(evt, res);
149 * Handles right mouse button press. If pressed in a selected column, opens
150 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
151 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
156 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
158 JPopupMenu pop = new JPopupMenu();
161 * grab the hidden range in case mouseMoved nulls it
163 final int[] hiddenRange = reveal;
164 if (hiddenRange != null)
166 JMenuItem item = new JMenuItem(
167 MessageManager.getString("label.reveal"));
168 item.addActionListener(new ActionListener()
171 public void actionPerformed(ActionEvent e)
173 av.showColumn(hiddenRange[0]);
176 ap.paintAlignment(true, true);
182 if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
184 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
185 item.addActionListener(new ActionListener()
188 public void actionPerformed(ActionEvent e)
190 av.showAllHiddenColumns();
193 ap.paintAlignment(true, true);
199 pop.show(this, evt.getX(), evt.getY());
201 else if (av.getColumnSelection().contains(res))
203 JMenuItem item = new JMenuItem(
204 MessageManager.getString("label.hide_columns"));
205 item.addActionListener(new ActionListener()
208 public void actionPerformed(ActionEvent e)
210 av.hideColumns(res, res);
211 if (av.getSelectionGroup() != null && av.getSelectionGroup()
212 .getSize() == av.getAlignment().getHeight())
214 av.setSelectionGroup(null);
218 ap.paintAlignment(true, true);
223 pop.show(this, evt.getX(), evt.getY());
228 * Handles left mouse button press
233 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
236 * Ctrl-click/Cmd-click adds to the selection
237 * Shift-click extends the selection
239 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
240 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
242 av.getColumnSelection().clear();
245 av.getColumnSelection().addElement(res);
246 SequenceGroup sg = new SequenceGroup(av.getAlignment().getSequences());
250 if (evt.isShiftDown())
252 int min = Math.min(av.getColumnSelection().getMin(), res);
253 int max = Math.max(av.getColumnSelection().getMax(), res);
254 for (int i = min; i < max; i++)
256 av.getColumnSelection().addElement(i);
261 av.setSelectionGroup(sg);
262 ap.paintAlignment(false, false);
267 * Action on mouseUp is to set the limit of the current selection group (if
268 * there is one) and broadcast the selection
273 public void mouseReleased(MouseEvent evt)
275 boolean wasDragging = mouseDragging;
276 mouseDragging = false;
277 ap.getSeqPanel().stopScrolling();
279 // todo res calculation should be a method on AlignViewport
280 int xCords = Math.max(0, evt.getX()); // prevent negative X coordinates
281 int res = (xCords / av.getCharWidth())
282 + av.getRanges().getStartRes();
284 if (av.hasHiddenColumns())
286 res = av.getAlignment().getHiddenColumns()
287 .visibleToAbsoluteColumn(res);
289 res = Math.min(res, av.getRanges().getEndRes());
290 res = Math.max(0, res);
292 if (!stretchingGroup)
294 if (evt.isPopupTrigger()) // Windows: mouseReleased
296 rightMouseButtonPressed(evt, res);
300 ap.paintAlignment(false, false);
305 SequenceGroup sg = av.getSelectionGroup();
309 if (res > sg.getStartRes())
313 else if (res < sg.getStartRes())
319 min = Math.min(res, min);
320 max = Math.max(res, max);
321 av.getColumnSelection().stretchGroup(res, sg, min, max);
324 stretchingGroup = false;
325 ap.paintAlignment(false, false);
326 av.isSelectionGroupChanged(true);
327 av.isColSelChanged(true);
332 * Action on dragging the mouse in the scale panel is to expand or shrink the
333 * selection group range (including any hidden columns that it spans). Note
334 * that the selection is only broadcast at the start of the drag (on
335 * mousePressed) and at the end (on mouseReleased), to avoid overload
336 * redrawing of other views.
341 public void mouseDragged(MouseEvent evt)
343 mouseDragging = true;
344 ColumnSelection cs = av.getColumnSelection();
345 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
347 int res = (evt.getX() / av.getCharWidth())
348 + av.getRanges().getStartRes();
349 res = Math.max(0, res);
350 res = hidden.visibleToAbsoluteColumn(res);
351 res = Math.min(res, av.getAlignment().getWidth() - 1);
352 min = Math.min(res, min);
353 max = Math.max(res, max);
355 SequenceGroup sg = av.getSelectionGroup();
358 stretchingGroup = true;
359 cs.stretchGroup(res, sg, min, max);
360 ap.paintAlignment(false, false);
365 public void mouseEntered(MouseEvent evt)
369 mouseDragging = false;
370 ap.getSeqPanel().stopScrolling();
375 * Action on leaving the panel bounds with mouse drag in progress is to start
376 * scrolling the alignment in the direction of the mouse. To restrict
377 * scrolling to left-right (not up-down), the y-value of the mouse position is
378 * replaced with zero.
381 public void mouseExited(MouseEvent evt)
385 ap.getSeqPanel().startScrolling(new Point(evt.getX(), 0));
390 public void mouseClicked(MouseEvent evt)
395 * Creates a tooltip when the mouse is over a hidden columns marker
398 public void mouseMoved(MouseEvent evt)
400 this.setToolTipText(null);
402 if (!av.hasHiddenColumns())
407 int res = (evt.getX() / av.getCharWidth())
408 + av.getRanges().getStartRes();
410 reveal = av.getAlignment().getHiddenColumns()
411 .getRegionWithEdgeAtRes(res);
413 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
415 ToolTipManager.sharedInstance().registerComponent(this);
417 MessageManager.getString("label.reveal_hidden_columns"));
428 public void paintComponent(Graphics g)
430 //super.paintComponent(g); // BH 2019
433 * shouldn't get called in wrapped mode as the scale above is
434 * drawn instead by SeqCanvas.drawNorthScale
436 if (!av.getWrapAlignment())
438 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
439 getWidth(), getHeight());
443 // scalewidth will normally be screenwidth,
444 public void drawScale(Graphics g, int startx, int endx, int width,
447 Graphics2D gg = (Graphics2D) g;
448 gg.setFont(av.getFont());
452 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
453 RenderingHints.VALUE_ANTIALIAS_ON);
456 // Fill in the background
457 gg.setColor(Color.white);
458 gg.fillRect(0, 0, width, height);
459 gg.setColor(Color.black);
461 // Fill the selected columns
462 ColumnSelection cs = av.getColumnSelection();
463 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
464 int avCharWidth = av.getCharWidth();
465 int avCharHeight = av.getCharHeight();
469 gg.setColor(new Color(220, 0, 0));
471 for (int sel : cs.getSelected())
473 // TODO: JAL-2001 - provide a fast method to list visible selected in a
476 if (av.hasHiddenColumns())
478 if (hidden.isVisible(sel))
480 sel = hidden.absoluteToVisibleColumn(sel);
488 if ((sel >= startx) && (sel <= endx))
490 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
496 int widthx = 1 + endx - startx;
498 FontMetrics fm = gg.getFontMetrics(av.getFont());
499 int y = avCharHeight;
500 int yOf = fm.getDescent();
502 if (av.hasHiddenColumns())
504 // draw any hidden column markers
505 gg.setColor(Color.blue);
508 if (av.getShowHiddenMarkers())
510 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
511 startx + widthx + 1);
514 res = it.next() - startx;
518 { -1 + res * avCharWidth - avCharHeight / 4,
519 -1 + res * avCharWidth + avCharHeight / 4,
520 -1 + res * avCharWidth }, new int[]
521 { y, y, y + 2 * yOf }, 3);
525 // Draw the scale numbers
526 gg.setColor(Color.black);
529 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
532 for (ScaleMark mark : marks)
534 boolean major = mark.major;
535 int mpos = mark.column; // (i - startx - 1)
536 String mstring = mark.text;
539 if (mpos * avCharWidth > maxX)
541 gg.drawString(mstring, mpos * avCharWidth, y);
542 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
547 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
548 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
552 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
553 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
559 public void propertyChange(PropertyChangeEvent evt)
561 // Respond to viewport change events (e.g. alignment panel was scrolled)
562 // Both scrolling and resizing change viewport ranges: scrolling changes
563 // both start and end points, but resize only changes end values.
564 // Here we only want to fastpaint on a scroll, with resize using a normal
565 // paint, so scroll events are identified as changes to the horizontal or
566 // vertical start value.
567 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
568 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
569 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
571 // scroll event, repaint panel
573 // Call repaint on alignment panel so that repaints from other alignment
574 // panel components can be aggregated. Otherwise performance of the overview
575 // window and others may be adversely affected.
576 av.getAlignPanel().repaint();