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.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);
135 else if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
138 * defer right-mouse click handling to mouse up on Windows
139 * (where isPopupTrigger() will answer true)
140 * but accept Cmd-click on Mac which passes isRightMouseButton
146 leftMouseButtonPressed(evt, res);
151 * Handles right mouse button press. If pressed in a selected column, opens
152 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
153 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
158 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
160 JPopupMenu pop = new JPopupMenu();
163 JMenuItem item = new JMenuItem(
164 MessageManager.getString("label.reveal"));
165 item.addActionListener(new ActionListener()
168 public void actionPerformed(ActionEvent e)
170 av.showColumn(reveal[0]);
172 ap.paintAlignment(true, true);
178 if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
180 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
181 item.addActionListener(new ActionListener()
184 public void actionPerformed(ActionEvent e)
186 av.showAllHiddenColumns();
188 ap.paintAlignment(true, true);
194 pop.show(this, evt.getX(), evt.getY());
196 else if (av.getColumnSelection().contains(res))
198 JMenuItem item = new JMenuItem(
199 MessageManager.getString("label.hide_columns"));
200 item.addActionListener(new ActionListener()
203 public void actionPerformed(ActionEvent e)
205 av.hideColumns(res, res);
206 if (av.getSelectionGroup() != null && av.getSelectionGroup()
207 .getSize() == av.getAlignment().getHeight())
209 av.setSelectionGroup(null);
212 ap.paintAlignment(true, true);
217 pop.show(this, evt.getX(), evt.getY());
222 * Handles left mouse button press
227 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
230 * Ctrl-click/Cmd-click adds to the selection
231 * Shift-click extends the selection
233 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
234 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
236 av.getColumnSelection().clear();
239 av.getColumnSelection().addElement(res);
240 SequenceGroup sg = new SequenceGroup();
241 // try to be as quick as possible
242 SequenceI[] iVec = av.getAlignment().getSequencesArray();
243 for (int i = 0; i < iVec.length; i++)
245 sg.addSequence(iVec[i], false);
252 if (evt.isShiftDown())
254 int min = Math.min(av.getColumnSelection().getMin(), res);
255 int max = Math.max(av.getColumnSelection().getMax(), res);
256 for (int i = min; i < max; i++)
258 av.getColumnSelection().addElement(i);
263 av.setSelectionGroup(sg);
264 ap.paintAlignment(false, false);
275 public void mouseReleased(MouseEvent evt)
277 mouseDragging = false;
279 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);
290 if (res >= av.getAlignment().getWidth())
292 res = av.getAlignment().getWidth() - 1;
295 if (!stretchingGroup)
297 if (evt.isPopupTrigger()) // Windows: mouseReleased
299 rightMouseButtonPressed(evt, res);
303 ap.paintAlignment(false, false);
308 SequenceGroup sg = av.getSelectionGroup();
312 if (res > sg.getStartRes())
316 else if (res < sg.getStartRes())
321 stretchingGroup = false;
322 ap.paintAlignment(false, false);
327 * Action on dragging the mouse in the scale panel is to expand or shrink the
328 * selection group range (including any hidden columns that it spans)
333 public void mouseDragged(MouseEvent evt)
335 mouseDragging = true;
336 ColumnSelection cs = av.getColumnSelection();
337 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
339 int res = (evt.getX() / av.getCharWidth())
340 + av.getRanges().getStartRes();
341 res = Math.max(0, res);
342 res = hidden.visibleToAbsoluteColumn(res);
343 res = Math.min(res, av.getAlignment().getWidth() - 1);
344 min = Math.min(res, min);
345 max = Math.max(res, max);
347 SequenceGroup sg = av.getSelectionGroup();
350 stretchingGroup = true;
351 cs.stretchGroup(res, sg, min, max);
352 ap.paintAlignment(false, false);
357 public void mouseEntered(MouseEvent evt)
361 ap.getSeqPanel().scrollCanvas(null);
366 public void mouseExited(MouseEvent evt)
370 ap.getSeqPanel().scrollCanvas(evt);
375 public void mouseClicked(MouseEvent evt)
380 * Creates a tooltip when the mouse is over a hidden columns marker
383 public void mouseMoved(MouseEvent evt)
385 this.setToolTipText(null);
387 if (!av.hasHiddenColumns())
392 int res = (evt.getX() / av.getCharWidth())
393 + av.getRanges().getStartRes();
395 reveal = av.getAlignment().getHiddenColumns()
396 .getRegionWithEdgeAtRes(res);
398 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
400 ToolTipManager.sharedInstance().registerComponent(this);
402 MessageManager.getString("label.reveal_hidden_columns"));
413 public void paintComponent(Graphics g)
415 super.paintComponent(g);
418 * shouldn't get called in wrapped mode as the scale above is
419 * drawn instead by SeqCanvas.drawNorthScale
421 if (!av.getWrapAlignment())
423 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
424 getWidth(), getHeight());
428 // scalewidth will normally be screenwidth,
429 public void drawScale(Graphics g, int startx, int endx, int width,
432 Graphics2D gg = (Graphics2D) g;
433 gg.setFont(av.getFont());
437 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
438 RenderingHints.VALUE_ANTIALIAS_ON);
441 // Fill in the background
442 gg.setColor(Color.white);
443 gg.fillRect(0, 0, width, height);
444 gg.setColor(Color.black);
446 // Fill the selected columns
447 ColumnSelection cs = av.getColumnSelection();
448 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
449 int avCharWidth = av.getCharWidth();
450 int avCharHeight = av.getCharHeight();
454 gg.setColor(new Color(220, 0, 0));
456 for (int sel : cs.getSelected())
458 // TODO: JAL-2001 - provide a fast method to list visible selected in a
461 if (av.hasHiddenColumns())
463 if (hidden.isVisible(sel))
465 sel = hidden.absoluteToVisibleColumn(sel);
473 if ((sel >= startx) && (sel <= endx))
475 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
481 int widthx = 1 + endx - startx;
483 FontMetrics fm = gg.getFontMetrics(av.getFont());
484 int y = avCharHeight;
485 int yOf = fm.getDescent();
487 if (av.hasHiddenColumns())
489 // draw any hidden column markers
490 gg.setColor(Color.blue);
493 if (av.getShowHiddenMarkers())
495 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
496 startx + widthx + 1);
499 res = it.next() - startx;
503 { -1 + res * avCharWidth - avCharHeight / 4,
504 -1 + res * avCharWidth + avCharHeight / 4,
505 -1 + res * avCharWidth }, new int[]
506 { y, y, y + 2 * yOf }, 3);
510 // Draw the scale numbers
511 gg.setColor(Color.black);
514 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
517 for (ScaleMark mark : marks)
519 boolean major = mark.major;
520 int mpos = mark.column; // (i - startx - 1)
521 String mstring = mark.text;
524 if (mpos * avCharWidth > maxX)
526 gg.drawString(mstring, mpos * avCharWidth, y);
527 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
532 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
533 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
537 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
538 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
544 public void propertyChange(PropertyChangeEvent evt)
546 // Respond to viewport change events (e.g. alignment panel was scrolled)
547 // Both scrolling and resizing change viewport ranges: scrolling changes
548 // both start and end points, but resize only changes end values.
549 // Here we only want to fastpaint on a scroll, with resize using a normal
550 // paint, so scroll events are identified as changes to the horizontal or
551 // vertical start value.
552 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
553 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
554 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
556 // scroll event, repaint panel
558 // Call repaint on alignment panel so that repaints from other alignment
559 // panel components can be aggregated. Otherwise performance of the overview
560 // window and others may be adversely affected.
561 av.getAlignPanel().repaint();