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.datamodel.SequenceI;
27 import jalview.renderer.ScaleRenderer;
28 import jalview.renderer.ScaleRenderer.ScaleMark;
29 import jalview.util.MessageManager;
30 import jalview.util.Platform;
31 import jalview.viewmodel.ViewportListenerI;
32 import jalview.viewmodel.ViewportRanges;
34 import java.awt.Color;
35 import java.awt.FontMetrics;
36 import java.awt.Graphics;
37 import java.awt.Graphics2D;
38 import java.awt.Point;
39 import java.awt.RenderingHints;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.MouseEvent;
43 import java.awt.event.MouseListener;
44 import java.awt.event.MouseMotionListener;
45 import java.beans.PropertyChangeEvent;
46 import java.util.Iterator;
47 import java.util.List;
49 import javax.swing.JMenuItem;
50 import javax.swing.JPanel;
51 import javax.swing.JPopupMenu;
52 import javax.swing.SwingUtilities;
53 import javax.swing.ToolTipManager;
56 * The panel containing the sequence ruler (when not in wrapped mode), and
57 * supports a range of mouse operations to select, hide or reveal columns.
59 public class ScalePanel extends JPanel
60 implements MouseMotionListener, MouseListener, ViewportListenerI
62 protected int offy = 4;
66 protected AlignViewport av;
70 boolean stretchingGroup = false;
73 * min, max hold the extent of a mouse drag action
79 boolean mouseDragging = false;
82 * holds a hidden column range when the mouse is over an adjacent column
92 public ScalePanel(AlignViewport av, AlignmentPanel ap)
97 addMouseListener(this);
98 addMouseMotionListener(this);
100 av.getRanges().addPropertyChangeListener(this);
110 public void mousePressed(MouseEvent evt)
112 int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes();
115 if (av.hasHiddenColumns())
117 x = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(x);
120 if (x >= av.getAlignment().getWidth())
122 res = av.getAlignment().getWidth() - 1;
132 if (evt.isPopupTrigger()) // Mac: mousePressed
134 rightMouseButtonPressed(evt, res);
136 else if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
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
147 leftMouseButtonPressed(evt, res);
152 * Handles right mouse button press. If pressed in a selected column, opens
153 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
154 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
159 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
161 JPopupMenu pop = new JPopupMenu();
164 JMenuItem item = new JMenuItem(
165 MessageManager.getString("label.reveal"));
166 item.addActionListener(new ActionListener()
169 public void actionPerformed(ActionEvent e)
171 av.showColumn(reveal[0]);
173 ap.paintAlignment(true, true);
179 if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
181 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
182 item.addActionListener(new ActionListener()
185 public void actionPerformed(ActionEvent e)
187 av.showAllHiddenColumns();
189 ap.paintAlignment(true, true);
195 pop.show(this, evt.getX(), evt.getY());
197 else if (av.getColumnSelection().contains(res))
199 JMenuItem item = new JMenuItem(
200 MessageManager.getString("label.hide_columns"));
201 item.addActionListener(new ActionListener()
204 public void actionPerformed(ActionEvent e)
206 av.hideColumns(res, res);
207 if (av.getSelectionGroup() != null && av.getSelectionGroup()
208 .getSize() == av.getAlignment().getHeight())
210 av.setSelectionGroup(null);
213 ap.paintAlignment(true, true);
218 pop.show(this, evt.getX(), evt.getY());
223 * Handles left mouse button press
228 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
231 * Ctrl-click/Cmd-click adds to the selection
232 * Shift-click extends the selection
234 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
235 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
237 av.getColumnSelection().clear();
240 av.getColumnSelection().addElement(res);
241 SequenceGroup sg = new SequenceGroup();
242 // try to be as quick as possible
243 SequenceI[] iVec = av.getAlignment().getSequencesArray();
244 for (int i = 0; i < iVec.length; i++)
246 sg.addSequence(iVec[i], false);
253 if (evt.isShiftDown())
255 int min = Math.min(av.getColumnSelection().getMin(), res);
256 int max = Math.max(av.getColumnSelection().getMax(), res);
257 for (int i = min; i < max; i++)
259 av.getColumnSelection().addElement(i);
264 av.setSelectionGroup(sg);
265 ap.paintAlignment(false, false);
270 * Action on mouseUp is to set the limit of the current selection group (if
271 * there is one) and broadcast the selection
276 public void mouseReleased(MouseEvent evt)
278 mouseDragging = false;
279 ap.getSeqPanel().stopScrolling();
281 // todo res calculation should be a method on AlignViewport
282 int res = (evt.getX() / av.getCharWidth())
283 + av.getRanges().getStartRes();
284 if (av.hasHiddenColumns())
286 res = av.getAlignment().getHiddenColumns()
287 .visibleToAbsoluteColumn(res);
289 res = Math.min(res, av.getAlignment().getWidth() - 1);
291 if (!stretchingGroup)
293 if (evt.isPopupTrigger()) // Windows: mouseReleased
295 rightMouseButtonPressed(evt, res);
299 ap.paintAlignment(false, false);
304 SequenceGroup sg = av.getSelectionGroup();
308 if (res > sg.getStartRes())
312 else if (res < sg.getStartRes())
317 stretchingGroup = false;
318 ap.paintAlignment(false, false);
323 * Action on dragging the mouse in the scale panel is to expand or shrink the
324 * selection group range (including any hidden columns that it spans)
329 public void mouseDragged(MouseEvent evt)
331 mouseDragging = true;
332 ColumnSelection cs = av.getColumnSelection();
333 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
335 int res = (evt.getX() / av.getCharWidth())
336 + av.getRanges().getStartRes();
337 res = Math.max(0, res);
338 res = hidden.visibleToAbsoluteColumn(res);
339 res = Math.min(res, av.getAlignment().getWidth() - 1);
340 min = Math.min(res, min);
341 max = Math.max(res, max);
343 SequenceGroup sg = av.getSelectionGroup();
346 stretchingGroup = true;
347 cs.stretchGroup(res, sg, min, max);
348 ap.paintAlignment(false, false);
353 public void mouseEntered(MouseEvent evt)
357 ap.getSeqPanel().stopScrolling();
362 * Action on leaving the panel bounds with mouse drag in progress is to start
363 * scrolling the alignment in the direction of the mouse. To restrict
364 * scrolling to left-right (not up-down), the y-value of the mouse position is
365 * replaced with zero.
368 public void mouseExited(MouseEvent evt)
372 ap.getSeqPanel().startScrolling(new Point(evt.getX(), 0));
377 public void mouseClicked(MouseEvent evt)
382 * Creates a tooltip when the mouse is over a hidden columns marker
385 public void mouseMoved(MouseEvent evt)
387 this.setToolTipText(null);
389 if (!av.hasHiddenColumns())
394 int res = (evt.getX() / av.getCharWidth())
395 + av.getRanges().getStartRes();
397 reveal = av.getAlignment().getHiddenColumns()
398 .getRegionWithEdgeAtRes(res);
400 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
402 ToolTipManager.sharedInstance().registerComponent(this);
404 MessageManager.getString("label.reveal_hidden_columns"));
415 public void paintComponent(Graphics g)
417 super.paintComponent(g);
420 * shouldn't get called in wrapped mode as the scale above is
421 * drawn instead by SeqCanvas.drawNorthScale
423 if (!av.getWrapAlignment())
425 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
426 getWidth(), getHeight());
430 // scalewidth will normally be screenwidth,
431 public void drawScale(Graphics g, int startx, int endx, int width,
434 Graphics2D gg = (Graphics2D) g;
435 gg.setFont(av.getFont());
439 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
440 RenderingHints.VALUE_ANTIALIAS_ON);
443 // Fill in the background
444 gg.setColor(Color.white);
445 gg.fillRect(0, 0, width, height);
446 gg.setColor(Color.black);
448 // Fill the selected columns
449 ColumnSelection cs = av.getColumnSelection();
450 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
451 int avCharWidth = av.getCharWidth();
452 int avCharHeight = av.getCharHeight();
456 gg.setColor(new Color(220, 0, 0));
458 for (int sel : cs.getSelected())
460 // TODO: JAL-2001 - provide a fast method to list visible selected in a
463 if (av.hasHiddenColumns())
465 if (hidden.isVisible(sel))
467 sel = hidden.absoluteToVisibleColumn(sel);
475 if ((sel >= startx) && (sel <= endx))
477 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
483 int widthx = 1 + endx - startx;
485 FontMetrics fm = gg.getFontMetrics(av.getFont());
486 int y = avCharHeight;
487 int yOf = fm.getDescent();
489 if (av.hasHiddenColumns())
491 // draw any hidden column markers
492 gg.setColor(Color.blue);
495 if (av.getShowHiddenMarkers())
497 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
498 startx + widthx + 1);
501 res = it.next() - startx;
505 { -1 + res * avCharWidth - avCharHeight / 4,
506 -1 + res * avCharWidth + avCharHeight / 4,
507 -1 + res * avCharWidth }, new int[]
508 { y, y, y + 2 * yOf }, 3);
512 // Draw the scale numbers
513 gg.setColor(Color.black);
516 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
519 for (ScaleMark mark : marks)
521 boolean major = mark.major;
522 int mpos = mark.column; // (i - startx - 1)
523 String mstring = mark.text;
526 if (mpos * avCharWidth > maxX)
528 gg.drawString(mstring, mpos * avCharWidth, y);
529 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
534 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
535 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
539 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
540 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
546 public void propertyChange(PropertyChangeEvent evt)
548 // Respond to viewport change events (e.g. alignment panel was scrolled)
549 // Both scrolling and resizing change viewport ranges: scrolling changes
550 // both start and end points, but resize only changes end values.
551 // Here we only want to fastpaint on a scroll, with resize using a normal
552 // paint, so scroll events are identified as changes to the horizontal or
553 // vertical start value.
554 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
555 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
556 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
558 // scroll event, repaint panel
560 // Call repaint on alignment panel so that repaints from other alignment
561 // panel components can be aggregated. Otherwise performance of the overview
562 // window and others may be adversely affected.
563 av.getAlignPanel().repaint();