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.Color;
24 import java.awt.FontMetrics;
25 import java.awt.Graphics;
26 import java.awt.Graphics2D;
27 import java.awt.Point;
28 import java.awt.RenderingHints;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.MouseEvent;
32 import java.awt.event.MouseListener;
33 import java.awt.event.MouseMotionListener;
34 import java.beans.PropertyChangeEvent;
35 import java.util.Iterator;
36 import java.util.List;
38 import javax.swing.JMenuItem;
39 import javax.swing.JPanel;
40 import javax.swing.JPopupMenu;
41 import javax.swing.ToolTipManager;
43 import jalview.datamodel.ColumnSelection;
44 import jalview.datamodel.HiddenColumns;
45 import jalview.datamodel.SequenceGroup;
46 import jalview.renderer.ScaleRenderer;
47 import jalview.renderer.ScaleRenderer.ScaleMark;
48 import jalview.util.MessageManager;
49 import jalview.util.Platform;
50 import jalview.viewmodel.ViewportListenerI;
51 import jalview.viewmodel.ViewportRanges;
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 res = ap.getSeqPanel().findColumn(evt);
119 if (evt.isPopupTrigger()) // Mac: mousePressed
121 rightMouseButtonPressed(evt, res);
124 if (Platform.isWinRightButton(evt))
127 * defer right-mouse click handling to mouse up on Windows
128 * (where isPopupTrigger() will answer true)
129 * but accept Cmd-click on Mac which passes isRightMouseButton
133 leftMouseButtonPressed(evt, res);
137 * Handles right mouse button press. If pressed in a selected column, opens
138 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
139 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
144 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
146 JPopupMenu pop = buildPopupMenu(res);
147 if (pop.getSubElements().length > 0)
149 pop.show(this, evt.getX(), evt.getY());
154 * Builds a popup menu with 'Hide' or 'Reveal' options, or both, or neither
157 * column number (0..)
160 protected JPopupMenu buildPopupMenu(final int res)
162 JPopupMenu pop = new JPopupMenu();
165 * logic here depends on 'reveal', set in mouseMoved;
166 * grab the hidden range in case mouseMoved nulls it later
168 final int[] hiddenRange = reveal;
169 if (hiddenRange != null)
171 JMenuItem item = new JMenuItem(
172 MessageManager.getString("label.reveal"));
173 item.addActionListener(new ActionListener()
176 public void actionPerformed(ActionEvent e)
178 av.showColumn(hiddenRange[0]);
181 ap.paintAlignment(true, true);
187 if (av.getAlignment().getHiddenColumns()
188 .hasMultiHiddenColumnRegions())
190 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
191 item.addActionListener(new ActionListener()
194 public void actionPerformed(ActionEvent e)
196 av.showAllHiddenColumns();
199 ap.paintAlignment(true, true);
207 if (av.getColumnSelection().contains(res))
209 JMenuItem item = new JMenuItem(
210 MessageManager.getString("label.hide_columns"));
211 item.addActionListener(new ActionListener()
214 public void actionPerformed(ActionEvent e)
216 av.hideColumns(res, res);
217 if (av.getSelectionGroup() != null && av.getSelectionGroup()
218 .getSize() == av.getAlignment().getHeight())
220 av.setSelectionGroup(null);
224 ap.paintAlignment(true, true);
234 * Handles left mouse button press
239 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
242 * Ctrl-click/Cmd-click adds to the selection
243 * Shift-click extends the selection
245 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
246 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
248 av.getColumnSelection().clear();
251 av.getColumnSelection().addElement(res);
252 SequenceGroup sg = new SequenceGroup(av.getAlignment().getSequences());
256 if (evt.isShiftDown())
258 int min = Math.min(av.getColumnSelection().getMin(), res);
259 int max = Math.max(av.getColumnSelection().getMax(), res);
260 for (int i = min; i < max; i++)
262 av.getColumnSelection().addElement(i);
267 av.setSelectionGroup(sg);
268 ap.paintAlignment(false, false);
269 PaintRefresher.Refresh(this,av.getSequenceSetId());
274 * Action on mouseUp is to set the limit of the current selection group (if
275 * there is one) and broadcast the selection
280 public void mouseReleased(MouseEvent evt)
282 boolean wasDragging = mouseDragging;
283 mouseDragging = false;
284 ap.getSeqPanel().stopScrolling();
286 int res = ap.getSeqPanel().findColumn(evt);
288 if (!stretchingGroup)
295 if (evt.isPopupTrigger()) // Windows: mouseReleased
297 rightMouseButtonPressed(evt, res);
301 ap.paintAlignment(false, false);
306 SequenceGroup sg = av.getSelectionGroup();
308 if (res != -1 && sg != null)
310 if (res > sg.getStartRes())
314 else if (res < sg.getStartRes())
320 min = Math.min(res, min);
321 max = Math.max(res, max);
322 av.getColumnSelection().stretchGroup(res, sg, min, max);
325 stretchingGroup = false;
326 ap.paintAlignment(false, false);
327 av.isSelectionGroupChanged(true);
328 av.isColSelChanged(true);
329 PaintRefresher.Refresh(ap, av.getSequenceSetId());
334 * Action on dragging the mouse in the scale panel is to expand or shrink the
335 * selection group range (including any hidden columns that it spans). Note
336 * that the selection is only broadcast at the start of the drag (on
337 * mousePressed) and at the end (on mouseReleased), to avoid overload
338 * redrawing of other views.
343 public void mouseDragged(MouseEvent evt)
346 int res = ap.getSeqPanel().findColumn(evt);
350 mouseDragging = true;
351 ColumnSelection cs = av.getColumnSelection();
352 min = Math.min(res, min);
353 max = Math.max(res, max);
354 SequenceGroup sg = av.getSelectionGroup();
357 stretchingGroup = true;
358 cs.stretchGroup(res, sg, min, max);
359 ap.paintAlignment(false, false);
360 PaintRefresher.Refresh(ap, av.getSequenceSetId());
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 final int res = ap.getSeqPanel().findColumn(evt);
409 highlightAllStructPos(res);
410 if (!av.hasHiddenColumns()) {
413 reveal = av.getAlignment().getHiddenColumns()
414 .getRegionWithEdgeAtRes(res);
415 ToolTipManager.sharedInstance().registerComponent(this);
417 MessageManager.getString("label.reveal_hidden_columns"));
421 public void highlightAllStructPos(int col)
423 ap.getStructureSelectionManager().highlightPositionsOnMany(
424 ap.av.getAlignment().getSequencesArray(), new int[]
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 },
530 { y, y, y + 2 * yOf }, 3);
534 // Draw the scale numbers
535 gg.setColor(Color.black);
538 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
541 for (ScaleMark mark : marks)
543 boolean major = mark.major;
544 int mpos = mark.column; // (i - startx - 1)
545 String mstring = mark.text;
548 if (mpos * avCharWidth > maxX)
550 gg.drawString(mstring, mpos * avCharWidth, y);
551 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
556 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
557 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
561 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
562 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
568 public void propertyChange(PropertyChangeEvent evt)
570 // Respond to viewport change events (e.g. alignment panel was scrolled)
571 // Both scrolling and resizing change viewport ranges: scrolling changes
572 // both start and end points, but resize only changes end values.
573 // Here we only want to fastpaint on a scroll, with resize using a normal
574 // paint, so scroll events are identified as changes to the horizontal or
575 // vertical start value.
576 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
577 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
578 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
580 // scroll event, repaint panel
582 // Call repaint on alignment panel so that repaints from other alignment
583 // panel components can be aggregated. Otherwise performance of the
585 // window and others may be adversely affected.
586 av.getAlignPanel().repaint();