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.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 final int res = av.getAbsoluteColumn(evt.getX());
115 if (evt.isPopupTrigger()) // Mac: mousePressed
117 rightMouseButtonPressed(evt, res);
119 else if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
122 * defer right-mouse click handling to mouse up on Windows
123 * (where isPopupTrigger() will answer true)
124 * but accept Cmd-click on Mac which passes isRightMouseButton
130 leftMouseButtonPressed(evt, res);
135 * Handles right mouse button press. If pressed in a selected column, opens
136 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
137 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
142 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
144 JPopupMenu pop = new JPopupMenu();
147 * grab the hidden range in case mouseMoved nulls it
149 final int[] hiddenRange = reveal;
150 if (hiddenRange != null)
152 JMenuItem item = new JMenuItem(
153 MessageManager.getString("label.reveal"));
154 item.addActionListener(new ActionListener()
157 public void actionPerformed(ActionEvent e)
159 av.showColumn(hiddenRange[0]);
162 ap.paintAlignment(true, true);
168 if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
170 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
171 item.addActionListener(new ActionListener()
174 public void actionPerformed(ActionEvent e)
176 av.showAllHiddenColumns();
179 ap.paintAlignment(true, true);
185 pop.show(this, evt.getX(), evt.getY());
187 else if (av.getColumnSelection().contains(res))
189 JMenuItem item = new JMenuItem(
190 MessageManager.getString("label.hide_columns"));
191 item.addActionListener(new ActionListener()
194 public void actionPerformed(ActionEvent e)
196 av.hideColumns(res, res);
197 if (av.getSelectionGroup() != null && av.getSelectionGroup()
198 .getSize() == av.getAlignment().getHeight())
200 av.setSelectionGroup(null);
204 ap.paintAlignment(true, true);
209 pop.show(this, evt.getX(), evt.getY());
214 * Handles left mouse button press
219 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
222 * Ctrl-click/Cmd-click adds to the selection
223 * Shift-click extends the selection
225 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
226 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
228 av.getColumnSelection().clear();
231 av.getColumnSelection().addElement(res);
232 SequenceGroup sg = new SequenceGroup(av.getAlignment().getSequences());
236 if (evt.isShiftDown())
238 int min = Math.min(av.getColumnSelection().getMin(), res);
239 int max = Math.max(av.getColumnSelection().getMax(), res);
240 for (int i = min; i < max; i++)
242 av.getColumnSelection().addElement(i);
247 av.setSelectionGroup(sg);
248 ap.paintAlignment(false, false);
253 * Action on mouseUp is to set the limit of the current selection group (if
254 * there is one) and broadcast the selection
259 public void mouseReleased(MouseEvent evt)
261 boolean wasDragging = mouseDragging;
262 mouseDragging = false;
263 ap.getSeqPanel().stopScrolling();
265 int xPos = evt.getX();
266 int res = av.getAbsoluteColumn(xPos);
268 if (!stretchingGroup)
270 if (evt.isPopupTrigger()) // Windows: mouseReleased
272 rightMouseButtonPressed(evt, res);
276 ap.paintAlignment(false, false);
281 SequenceGroup sg = av.getSelectionGroup();
285 if (res > sg.getStartRes())
289 else if (res < sg.getStartRes())
295 min = Math.min(res, min);
296 max = Math.max(res, max);
297 av.getColumnSelection().stretchGroup(res, sg, min, max);
300 stretchingGroup = false;
301 ap.paintAlignment(false, false);
302 av.isSelectionGroupChanged(true);
303 av.isColSelChanged(true);
308 * Action on dragging the mouse in the scale panel is to expand or shrink the
309 * selection group range (including any hidden columns that it spans). Note
310 * that the selection is only broadcast at the start of the drag (on
311 * mousePressed) and at the end (on mouseReleased), to avoid overload
312 * redrawing of other views.
317 public void mouseDragged(MouseEvent evt)
319 mouseDragging = true;
320 ColumnSelection cs = av.getColumnSelection();
321 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
323 int res = (evt.getX() / av.getCharWidth())
324 + av.getRanges().getStartRes();
325 res = Math.max(0, res);
326 res = hidden.visibleToAbsoluteColumn(res);
327 res = Math.min(res, av.getAlignment().getWidth() - 1);
328 min = Math.min(res, min);
329 max = Math.max(res, max);
331 SequenceGroup sg = av.getSelectionGroup();
334 stretchingGroup = true;
335 cs.stretchGroup(res, sg, min, max);
336 ap.paintAlignment(false, false);
341 public void mouseEntered(MouseEvent evt)
345 ap.getSeqPanel().stopScrolling();
350 * Action on leaving the panel bounds with mouse drag in progress is to start
351 * scrolling the alignment in the direction of the mouse. To restrict
352 * scrolling to left-right (not up-down), the y-value of the mouse position is
353 * replaced with zero.
356 public void mouseExited(MouseEvent evt)
360 ap.getSeqPanel().startScrolling(new Point(evt.getX(), 0));
365 public void mouseClicked(MouseEvent evt)
370 * Creates a tooltip when the mouse is over a hidden columns marker
373 public void mouseMoved(MouseEvent evt)
375 this.setToolTipText(null);
377 if (!av.hasHiddenColumns())
382 int res = (evt.getX() / av.getCharWidth())
383 + av.getRanges().getStartRes();
385 reveal = av.getAlignment().getHiddenColumns()
386 .getRegionWithEdgeAtRes(res);
388 ToolTipManager.sharedInstance().registerComponent(this);
390 MessageManager.getString("label.reveal_hidden_columns"));
401 public void paintComponent(Graphics g)
403 super.paintComponent(g);
406 * shouldn't get called in wrapped mode as the scale above is
407 * drawn instead by SeqCanvas.drawNorthScale
409 if (!av.getWrapAlignment())
411 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
412 getWidth(), getHeight());
416 // scalewidth will normally be screenwidth,
417 public void drawScale(Graphics g, int startx, int endx, int width,
420 Graphics2D gg = (Graphics2D) g;
421 gg.setFont(av.getFont());
425 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
426 RenderingHints.VALUE_ANTIALIAS_ON);
429 // Fill in the background
430 gg.setColor(Color.white);
431 gg.fillRect(0, 0, width, height);
432 gg.setColor(Color.black);
434 // Fill the selected columns
435 ColumnSelection cs = av.getColumnSelection();
436 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
437 int avCharWidth = av.getCharWidth();
438 int avCharHeight = av.getCharHeight();
442 gg.setColor(new Color(220, 0, 0));
444 for (int sel : cs.getSelected())
446 // TODO: JAL-2001 - provide a fast method to list visible selected in a
449 if (av.hasHiddenColumns())
451 if (hidden.isVisible(sel))
453 sel = hidden.absoluteToVisibleColumn(sel);
461 if ((sel >= startx) && (sel <= endx))
463 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
469 int widthx = 1 + endx - startx;
471 FontMetrics fm = gg.getFontMetrics(av.getFont());
472 int y = avCharHeight;
473 int yOf = fm.getDescent();
475 if (av.hasHiddenColumns())
477 // draw any hidden column markers
478 gg.setColor(Color.blue);
481 if (av.getShowHiddenMarkers())
483 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
484 startx + widthx + 1);
487 res = it.next() - startx;
491 { -1 + res * avCharWidth - avCharHeight / 4,
492 -1 + res * avCharWidth + avCharHeight / 4,
493 -1 + res * avCharWidth }, new int[]
494 { y, y, y + 2 * yOf }, 3);
498 // Draw the scale numbers
499 gg.setColor(Color.black);
502 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
505 for (ScaleMark mark : marks)
507 boolean major = mark.major;
508 int mpos = mark.column; // (i - startx - 1)
509 String mstring = mark.text;
512 if (mpos * avCharWidth > maxX)
514 gg.drawString(mstring, mpos * avCharWidth, y);
515 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
520 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
521 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
525 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
526 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
532 public void propertyChange(PropertyChangeEvent evt)
534 // Respond to viewport change events (e.g. alignment panel was scrolled)
535 // Both scrolling and resizing change viewport ranges: scrolling changes
536 // both start and end points, but resize only changes end values.
537 // Here we only want to fastpaint on a scroll, with resize using a normal
538 // paint, so scroll events are identified as changes to the horizontal or
539 // vertical start value.
540 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
541 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
542 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
544 // scroll event, repaint panel
546 // Call repaint on alignment panel so that repaints from other alignment
547 // panel components can be aggregated. Otherwise performance of the overview
548 // window and others may be adversely affected.
549 av.getAlignPanel().repaint();