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 @SuppressWarnings("serial")
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);
118 res = Math.min(x, av.getAlignment().getWidth() - 1);
123 if (evt.isPopupTrigger()) // Mac: mousePressed
125 rightMouseButtonPressed(evt, res);
128 if (Platform.isWinRightButton(evt))
131 * defer right-mouse click handling to mouse up on Windows
132 * (where isPopupTrigger() will answer true)
133 * but accept Cmd-click on Mac which passes isRightMouseButton
137 leftMouseButtonPressed(evt, res);
141 * Handles right mouse button press. If pressed in a selected column, opens
142 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
143 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
148 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
150 JPopupMenu pop = buildPopupMenu(res);
151 if (pop.getSubElements().length > 0)
153 pop.show(this, evt.getX(), evt.getY());
158 * Builds a popup menu with 'Hide' or 'Reveal' options, or both, or neither
161 * column number (0..)
164 protected JPopupMenu buildPopupMenu(final int res)
166 JPopupMenu pop = new JPopupMenu();
169 * logic here depends on 'reveal', set in mouseMoved;
170 * grab the hidden range in case mouseMoved nulls it later
172 final int[] hiddenRange = reveal;
173 if (hiddenRange != null)
175 JMenuItem item = new JMenuItem(
176 MessageManager.getString("label.reveal"));
177 item.addActionListener(new ActionListener()
180 public void actionPerformed(ActionEvent e)
182 av.showColumn(hiddenRange[0]);
185 ap.paintAlignment(true, true);
191 if (av.getAlignment().getHiddenColumns()
192 .hasMultiHiddenColumnRegions())
194 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
195 item.addActionListener(new ActionListener()
198 public void actionPerformed(ActionEvent e)
200 av.showAllHiddenColumns();
203 ap.paintAlignment(true, true);
211 if (av.getColumnSelection().contains(res))
213 JMenuItem item = new JMenuItem(
214 MessageManager.getString("label.hide_columns"));
215 item.addActionListener(new ActionListener()
218 public void actionPerformed(ActionEvent e)
220 av.hideColumns(res, res);
221 if (av.getSelectionGroup() != null && av.getSelectionGroup()
222 .getSize() == av.getAlignment().getHeight())
224 av.setSelectionGroup(null);
228 ap.paintAlignment(true, true);
238 * Handles left mouse button press
243 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
246 * Ctrl-click/Cmd-click adds to the selection
247 * Shift-click extends the selection
249 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
250 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
252 av.getColumnSelection().clear();
255 av.getColumnSelection().addElement(res);
256 SequenceGroup sg = new SequenceGroup(av.getAlignment().getSequences());
260 if (evt.isShiftDown())
262 int min = Math.min(av.getColumnSelection().getMin(), res);
263 int max = Math.max(av.getColumnSelection().getMax(), res);
264 for (int i = min; i < max; i++)
266 av.getColumnSelection().addElement(i);
271 av.setSelectionGroup(sg);
272 ap.paintAlignment(false, false);
277 * Action on mouseUp is to set the limit of the current selection group (if
278 * there is one) and broadcast the selection
283 public void mouseReleased(MouseEvent evt)
285 boolean wasDragging = mouseDragging;
286 mouseDragging = false;
287 ap.getSeqPanel().stopScrolling();
289 // todo res calculation should be a method on AlignViewport
290 int xCords = Math.max(0, evt.getX()); // prevent negative X coordinates
291 ViewportRanges ranges = av.getRanges();
292 int res = (xCords / av.getCharWidth()) + ranges.getStartRes();
293 res = Math.min(res, ranges.getEndRes());
294 if (av.hasHiddenColumns())
296 res = av.getAlignment().getHiddenColumns()
297 .visibleToAbsoluteColumn(res);
299 res = Math.max(0, res);
301 if (!stretchingGroup)
303 if (evt.isPopupTrigger()) // Windows: mouseReleased
305 rightMouseButtonPressed(evt, res);
309 ap.paintAlignment(false, false);
314 SequenceGroup sg = av.getSelectionGroup();
318 if (res > sg.getStartRes())
322 else if (res < sg.getStartRes())
328 min = Math.min(res, min);
329 max = Math.max(res, max);
330 av.getColumnSelection().stretchGroup(res, sg, min, max);
333 stretchingGroup = false;
334 ap.paintAlignment(false, false);
335 av.isSelectionGroupChanged(true);
336 av.isColSelChanged(true);
341 * Action on dragging the mouse in the scale panel is to expand or shrink the
342 * selection group range (including any hidden columns that it spans). Note
343 * that the selection is only broadcast at the start of the drag (on
344 * mousePressed) and at the end (on mouseReleased), to avoid overload
345 * redrawing of other views.
350 public void mouseDragged(MouseEvent evt)
352 mouseDragging = true;
353 ColumnSelection cs = av.getColumnSelection();
354 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
356 int res = (evt.getX() / av.getCharWidth())
357 + av.getRanges().getStartRes();
358 res = Math.max(0, res);
359 res = hidden.visibleToAbsoluteColumn(res);
360 res = Math.min(res, av.getAlignment().getWidth() - 1);
361 min = Math.min(res, min);
362 max = Math.max(res, max);
364 SequenceGroup sg = av.getSelectionGroup();
367 stretchingGroup = true;
368 cs.stretchGroup(res, sg, min, max);
369 ap.paintAlignment(false, false);
374 public void mouseEntered(MouseEvent evt)
378 mouseDragging = false;
379 ap.getSeqPanel().stopScrolling();
384 * Action on leaving the panel bounds with mouse drag in progress is to start
385 * scrolling the alignment in the direction of the mouse. To restrict
386 * scrolling to left-right (not up-down), the y-value of the mouse position is
387 * replaced with zero.
390 public void mouseExited(MouseEvent evt)
394 ap.getSeqPanel().startScrolling(new Point(evt.getX(), 0));
399 public void mouseClicked(MouseEvent evt)
404 * Creates a tooltip when the mouse is over a hidden columns marker
407 public void mouseMoved(MouseEvent evt)
409 this.setToolTipText(null);
411 if (!av.hasHiddenColumns())
416 int res = (evt.getX() / av.getCharWidth())
417 + av.getRanges().getStartRes();
419 reveal = av.getAlignment().getHiddenColumns()
420 .getRegionWithEdgeAtRes(res);
422 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
424 ToolTipManager.sharedInstance().registerComponent(this);
426 MessageManager.getString("label.reveal_hidden_columns"));
437 public void paintComponent(Graphics g)
440 * shouldn't get called in wrapped mode as the scale above is
441 * drawn instead by SeqCanvas.drawNorthScale
443 if (!av.getWrapAlignment())
445 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
446 getWidth(), getHeight());
450 public void drawScale(Graphics g, int startx, int endx, int width,
453 Graphics2D gg = (Graphics2D) g;
454 gg.setFont(av.getFont());
458 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
459 RenderingHints.VALUE_ANTIALIAS_ON);
462 // Fill in the background
463 gg.setColor(Color.white);
464 gg.fillRect(0, 0, width, height);
465 gg.setColor(Color.black);
467 // Fill the selected columns
468 ColumnSelection cs = av.getColumnSelection();
469 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
470 int avCharWidth = av.getCharWidth();
471 int avCharHeight = av.getCharHeight();
475 gg.setColor(new Color(220, 0, 0));
477 for (int sel : cs.getSelected())
479 // TODO: JAL-2001 - provide a fast method to list visible selected in a
482 if (av.hasHiddenColumns())
484 if (hidden.isVisible(sel))
486 sel = hidden.absoluteToVisibleColumn(sel);
494 if ((sel >= startx) && (sel <= endx))
496 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
502 int widthx = 1 + endx - startx;
504 FontMetrics fm = gg.getFontMetrics(av.getFont());
505 int y = avCharHeight;
506 int yOf = fm.getDescent();
508 if (av.hasHiddenColumns())
510 // draw any hidden column markers
511 gg.setColor(Color.blue);
514 if (av.getShowHiddenMarkers())
516 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
517 startx + widthx + 1);
520 res = it.next() - startx;
524 { -1 + res * avCharWidth - avCharHeight / 4,
525 -1 + res * avCharWidth + avCharHeight / 4,
526 -1 + res * avCharWidth },
528 { y, y, y + 2 * yOf }, 3);
532 // Draw the scale numbers
533 gg.setColor(Color.black);
536 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
539 for (ScaleMark mark : marks)
541 boolean major = mark.major;
542 int mpos = mark.column; // (i - startx - 1)
543 String mstring = mark.text;
546 if (mpos * avCharWidth > maxX)
548 gg.drawString(mstring, mpos * avCharWidth, y);
549 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
554 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
555 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
559 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
560 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
566 public void propertyChange(PropertyChangeEvent evt)
568 switch (evt.getPropertyName())
570 case ViewportRanges.STARTRES:
571 case ViewportRanges.STARTRESANDSEQ:
572 case ViewportRanges.MOVE_VIEWPORT:
573 // scroll event, repaint panel
575 // Call repaint on alignment panel so that repaints from other alignment
576 // panel components can be aggregated. Otherwise performance of the
578 // window and others may be adversely affected.
581 // BH: This is actually quite strange. AlignmentPanel is taking care of
582 // all of this with fast paint, so why indirectly trigger a repaint from
583 // the ScalePanel? Where do we see this behavior necessary?
584 // I have set this to check for a trigger from some other ViewportRanges,
585 // but I don't actually think that is possible.
587 if (evt.getSource() != av.getRanges())
589 av.getAlignPanel().repaint();
595 boolean isMouseDragging()
597 return mouseDragging;