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.ToolTipManager;
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);
118 if (x >= av.getAlignment().getWidth())
120 res = av.getAlignment().getWidth() - 1;
130 if (evt.isPopupTrigger()) // Mac: mousePressed
132 rightMouseButtonPressed(evt, res);
135 if (Platform.isWinRightButton(evt))
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
144 leftMouseButtonPressed(evt, res);
148 * Handles right mouse button press. If pressed in a selected column, opens
149 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
150 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
155 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
157 JPopupMenu pop = new JPopupMenu();
160 * grab the hidden range in case mouseMoved nulls it
162 final int[] hiddenRange = reveal;
163 if (hiddenRange != null)
165 JMenuItem item = new JMenuItem(
166 MessageManager.getString("label.reveal"));
167 item.addActionListener(new ActionListener()
170 public void actionPerformed(ActionEvent e)
172 av.showColumn(hiddenRange[0]);
175 ap.paintAlignment(true, true);
181 if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
183 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
184 item.addActionListener(new ActionListener()
187 public void actionPerformed(ActionEvent e)
189 av.showAllHiddenColumns();
192 ap.paintAlignment(true, true);
198 pop.show(this, evt.getX(), evt.getY());
200 else if (av.getColumnSelection().contains(res))
202 JMenuItem item = new JMenuItem(
203 MessageManager.getString("label.hide_columns"));
204 item.addActionListener(new ActionListener()
207 public void actionPerformed(ActionEvent e)
209 av.hideColumns(res, res);
210 if (av.getSelectionGroup() != null && av.getSelectionGroup()
211 .getSize() == av.getAlignment().getHeight())
213 av.setSelectionGroup(null);
217 ap.paintAlignment(true, true);
222 pop.show(this, evt.getX(), evt.getY());
227 * Handles left mouse button press
232 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
235 * Ctrl-click/Cmd-click adds to the selection
236 * Shift-click extends the selection
238 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
239 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
241 av.getColumnSelection().clear();
244 av.getColumnSelection().addElement(res);
245 SequenceGroup sg = new SequenceGroup(av.getAlignment().getSequences());
249 if (evt.isShiftDown())
251 int min = Math.min(av.getColumnSelection().getMin(), res);
252 int max = Math.max(av.getColumnSelection().getMax(), res);
253 for (int i = min; i < max; i++)
255 av.getColumnSelection().addElement(i);
260 av.setSelectionGroup(sg);
261 ap.paintAlignment(false, false);
266 * Action on mouseUp is to set the limit of the current selection group (if
267 * there is one) and broadcast the selection
272 public void mouseReleased(MouseEvent evt)
274 boolean wasDragging = mouseDragging;
275 mouseDragging = false;
276 ap.getSeqPanel().stopScrolling();
278 // todo res calculation should be a method on AlignViewport
279 int xCords = Math.max(0, evt.getX()); // prevent negative X coordinates
280 ViewportRanges ranges = av.getRanges();
281 int res = (xCords / av.getCharWidth())
282 + ranges.getStartRes();
283 res = Math.min(res, ranges.getEndRes());
284 if (av.hasHiddenColumns())
286 res = av.getAlignment().getHiddenColumns()
287 .visibleToAbsoluteColumn(res);
289 res = Math.max(0, res);
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())
318 min = Math.min(res, min);
319 max = Math.max(res, max);
320 av.getColumnSelection().stretchGroup(res, sg, min, max);
323 stretchingGroup = false;
324 ap.paintAlignment(false, false);
325 av.isSelectionGroupChanged(true);
326 av.isColSelChanged(true);
331 * Action on dragging the mouse in the scale panel is to expand or shrink the
332 * selection group range (including any hidden columns that it spans). Note
333 * that the selection is only broadcast at the start of the drag (on
334 * mousePressed) and at the end (on mouseReleased), to avoid overload
335 * redrawing of other views.
340 public void mouseDragged(MouseEvent evt)
342 mouseDragging = true;
343 ColumnSelection cs = av.getColumnSelection();
344 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
346 int res = (evt.getX() / av.getCharWidth())
347 + av.getRanges().getStartRes();
348 res = Math.max(0, res);
349 res = hidden.visibleToAbsoluteColumn(res);
350 res = Math.min(res, av.getAlignment().getWidth() - 1);
351 min = Math.min(res, min);
352 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);
364 public void mouseEntered(MouseEvent evt)
368 mouseDragging = false;
369 ap.getSeqPanel().stopScrolling();
374 * Action on leaving the panel bounds with mouse drag in progress is to start
375 * scrolling the alignment in the direction of the mouse. To restrict
376 * scrolling to left-right (not up-down), the y-value of the mouse position is
377 * replaced with zero.
380 public void mouseExited(MouseEvent evt)
384 ap.getSeqPanel().startScrolling(new Point(evt.getX(), 0));
389 public void mouseClicked(MouseEvent evt)
394 * Creates a tooltip when the mouse is over a hidden columns marker
397 public void mouseMoved(MouseEvent evt)
399 this.setToolTipText(null);
401 if (!av.hasHiddenColumns())
406 int res = (evt.getX() / av.getCharWidth())
407 + av.getRanges().getStartRes();
409 reveal = av.getAlignment().getHiddenColumns()
410 .getRegionWithEdgeAtRes(res);
412 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
414 ToolTipManager.sharedInstance().registerComponent(this);
416 MessageManager.getString("label.reveal_hidden_columns"));
427 public void paintComponent(Graphics g)
429 //super.paintComponent(g); // BH 2019
432 * shouldn't get called in wrapped mode as the scale above is
433 * drawn instead by SeqCanvas.drawNorthScale
435 if (!av.getWrapAlignment())
437 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
438 getWidth(), getHeight());
442 // scalewidth will normally be screenwidth,
443 public void drawScale(Graphics g, int startx, int endx, int width,
446 Graphics2D gg = (Graphics2D) g;
447 gg.setFont(av.getFont());
451 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
452 RenderingHints.VALUE_ANTIALIAS_ON);
455 // Fill in the background
456 gg.setColor(Color.white);
457 gg.fillRect(0, 0, width, height);
458 gg.setColor(Color.black);
460 // Fill the selected columns
461 ColumnSelection cs = av.getColumnSelection();
462 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
463 int avCharWidth = av.getCharWidth();
464 int avCharHeight = av.getCharHeight();
468 gg.setColor(new Color(220, 0, 0));
470 for (int sel : cs.getSelected())
472 // TODO: JAL-2001 - provide a fast method to list visible selected in a
475 if (av.hasHiddenColumns())
477 if (hidden.isVisible(sel))
479 sel = hidden.absoluteToVisibleColumn(sel);
487 if ((sel >= startx) && (sel <= endx))
489 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
495 int widthx = 1 + endx - startx;
497 FontMetrics fm = gg.getFontMetrics(av.getFont());
498 int y = avCharHeight;
499 int yOf = fm.getDescent();
501 if (av.hasHiddenColumns())
503 // draw any hidden column markers
504 gg.setColor(Color.blue);
507 if (av.getShowHiddenMarkers())
509 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
510 startx + widthx + 1);
513 res = it.next() - startx;
517 { -1 + res * avCharWidth - avCharHeight / 4,
518 -1 + res * avCharWidth + avCharHeight / 4,
519 -1 + res * avCharWidth }, new int[]
520 { y, y, y + 2 * yOf }, 3);
524 // Draw the scale numbers
525 gg.setColor(Color.black);
528 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
531 for (ScaleMark mark : marks)
533 boolean major = mark.major;
534 int mpos = mark.column; // (i - startx - 1)
535 String mstring = mark.text;
538 if (mpos * avCharWidth > maxX)
540 gg.drawString(mstring, mpos * avCharWidth, y);
541 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
546 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
547 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
551 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
552 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
558 public void propertyChange(PropertyChangeEvent evt)
560 // Respond to viewport change events (e.g. alignment panel was scrolled)
561 // Both scrolling and resizing change viewport ranges: scrolling changes
562 // both start and end points, but resize only changes end values.
563 // Here we only want to fastpaint on a scroll, with resize using a normal
564 // paint, so scroll events are identified as changes to the horizontal or
565 // vertical start value.
566 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
567 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
568 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
570 // scroll event, repaint panel
572 // Call repaint on alignment panel so that repaints from other alignment
573 // panel components can be aggregated. Otherwise performance of the overview
574 // window and others may be adversely affected.
575 av.getAlignPanel().repaint();