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 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()
191 .hasMultiHiddenColumnRegions())
193 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
194 item.addActionListener(new ActionListener()
197 public void actionPerformed(ActionEvent e)
199 av.showAllHiddenColumns();
202 ap.paintAlignment(true, true);
210 if (av.getColumnSelection().contains(res))
212 JMenuItem item = new JMenuItem(
213 MessageManager.getString("label.hide_columns"));
214 item.addActionListener(new ActionListener()
217 public void actionPerformed(ActionEvent e)
219 av.hideColumns(res, res);
220 if (av.getSelectionGroup() != null && av.getSelectionGroup()
221 .getSize() == av.getAlignment().getHeight())
223 av.setSelectionGroup(null);
227 ap.paintAlignment(true, true);
237 * Handles left mouse button press
242 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
245 * Ctrl-click/Cmd-click adds to the selection
246 * Shift-click extends the selection
248 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
249 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
251 av.getColumnSelection().clear();
254 av.getColumnSelection().addElement(res);
255 SequenceGroup sg = new SequenceGroup(av.getAlignment().getSequences());
259 if (evt.isShiftDown())
261 int min = Math.min(av.getColumnSelection().getMin(), res);
262 int max = Math.max(av.getColumnSelection().getMax(), res);
263 for (int i = min; i < max; i++)
265 av.getColumnSelection().addElement(i);
270 av.setSelectionGroup(sg);
271 ap.paintAlignment(false, false);
276 * Action on mouseUp is to set the limit of the current selection group (if
277 * there is one) and broadcast the selection
282 public void mouseReleased(MouseEvent evt)
284 boolean wasDragging = mouseDragging;
285 mouseDragging = false;
286 ap.getSeqPanel().stopScrolling();
288 // todo res calculation should be a method on AlignViewport
289 int xCords = Math.max(0, evt.getX()); // prevent negative X coordinates
290 ViewportRanges ranges = av.getRanges();
291 int res = (xCords / av.getCharWidth()) + 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);
336 PaintRefresher.Refresh(ap, av.getSequenceSetId());
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())
413 int res = (evt.getX() / av.getCharWidth())
414 + av.getRanges().getStartRes();
415 highlightAllStructPos(res);
419 int res = (evt.getX() / av.getCharWidth())
420 + av.getRanges().getStartRes();
422 reveal = av.getAlignment().getHiddenColumns()
423 .getRegionWithEdgeAtRes(res);
425 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
426 highlightAllStructPos(res);
427 ToolTipManager.sharedInstance().registerComponent(this);
429 MessageManager.getString("label.reveal_hidden_columns"));
433 public void highlightAllStructPos(int col)
435 ap.getStructureSelectionManager().highlightPositionsOnMany(
436 ap.av.getAlignment().getSequencesArray(), new int[]
448 public void paintComponent(Graphics g)
450 // super.paintComponent(g); // BH 2019
453 * shouldn't get called in wrapped mode as the scale above is
454 * drawn instead by SeqCanvas.drawNorthScale
456 if (!av.getWrapAlignment())
458 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
459 getWidth(), getHeight());
463 // scalewidth will normally be screenwidth,
464 public void drawScale(Graphics g, int startx, int endx, int width,
467 Graphics2D gg = (Graphics2D) g;
468 gg.setFont(av.getFont());
472 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
473 RenderingHints.VALUE_ANTIALIAS_ON);
476 // Fill in the background
477 gg.setColor(Color.white);
478 gg.fillRect(0, 0, width, height);
479 gg.setColor(Color.black);
481 // Fill the selected columns
482 ColumnSelection cs = av.getColumnSelection();
483 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
484 int avCharWidth = av.getCharWidth();
485 int avCharHeight = av.getCharHeight();
489 gg.setColor(new Color(220, 0, 0));
491 for (int sel : cs.getSelected())
493 // TODO: JAL-2001 - provide a fast method to list visible selected in a
496 if (av.hasHiddenColumns())
498 if (hidden.isVisible(sel))
500 sel = hidden.absoluteToVisibleColumn(sel);
508 if ((sel >= startx) && (sel <= endx))
510 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
516 int widthx = 1 + endx - startx;
518 FontMetrics fm = gg.getFontMetrics(av.getFont());
519 int y = avCharHeight;
520 int yOf = fm.getDescent();
522 if (av.hasHiddenColumns())
524 // draw any hidden column markers
525 gg.setColor(Color.blue);
528 if (av.getShowHiddenMarkers())
530 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
531 startx + widthx + 1);
534 res = it.next() - startx;
538 { -1 + res * avCharWidth - avCharHeight / 4,
539 -1 + res * avCharWidth + avCharHeight / 4,
540 -1 + res * avCharWidth },
542 { y, y, y + 2 * yOf }, 3);
546 // Draw the scale numbers
547 gg.setColor(Color.black);
550 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
553 for (ScaleMark mark : marks)
555 boolean major = mark.major;
556 int mpos = mark.column; // (i - startx - 1)
557 String mstring = mark.text;
560 if (mpos * avCharWidth > maxX)
562 gg.drawString(mstring, mpos * avCharWidth, y);
563 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
568 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
569 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
573 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
574 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
580 public void propertyChange(PropertyChangeEvent evt)
582 // Respond to viewport change events (e.g. alignment panel was scrolled)
583 // Both scrolling and resizing change viewport ranges: scrolling changes
584 // both start and end points, but resize only changes end values.
585 // Here we only want to fastpaint on a scroll, with resize using a normal
586 // paint, so scroll events are identified as changes to the horizontal or
587 // vertical start value.
588 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
589 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
590 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
592 // scroll event, repaint panel
594 // Call repaint on alignment panel so that repaints from other alignment
595 // panel components can be aggregated. Otherwise performance of the
597 // window and others may be adversely affected.
598 av.getAlignPanel().repaint();