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 res = ap.getSeqPanel().findAlignmentColumn(evt);
115 if (evt.isPopupTrigger()) // Mac: mousePressed
117 rightMouseButtonPressed(evt, res);
120 if (Platform.isWinRightButton(evt))
123 * defer right-mouse click handling to mouse up on Windows
124 * (where isPopupTrigger() will answer true)
125 * but accept Cmd-click on Mac which passes isRightMouseButton
129 leftMouseButtonPressed(evt, res);
133 * Handles right mouse button press. If pressed in a selected column, opens
134 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
135 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
140 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
142 JPopupMenu pop = buildPopupMenu(res);
143 if (pop.getSubElements().length > 0)
145 pop.show(this, evt.getX(), evt.getY());
150 * Builds a popup menu with 'Hide' or 'Reveal' options, or both, or neither
153 * column number (0..)
156 protected JPopupMenu buildPopupMenu(final int res)
158 JPopupMenu pop = new JPopupMenu();
161 * logic here depends on 'reveal', set in mouseMoved;
162 * grab the hidden range in case mouseMoved nulls it later
164 final int[] hiddenRange = reveal;
165 if (hiddenRange != null)
167 JMenuItem item = new JMenuItem(
168 MessageManager.getString("label.reveal"));
169 item.addActionListener(new ActionListener()
172 public void actionPerformed(ActionEvent e)
174 av.showColumn(hiddenRange[0]);
177 ap.paintAlignment(true, true);
183 if (av.getAlignment().getHiddenColumns()
184 .hasMultiHiddenColumnRegions())
186 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
187 item.addActionListener(new ActionListener()
190 public void actionPerformed(ActionEvent e)
192 av.showAllHiddenColumns();
195 ap.paintAlignment(true, true);
203 if (av.getColumnSelection().contains(res))
205 JMenuItem item = new JMenuItem(
206 MessageManager.getString("label.hide_columns"));
207 item.addActionListener(new ActionListener()
210 public void actionPerformed(ActionEvent e)
212 av.hideColumns(res, res);
213 if (av.getSelectionGroup() != null && av.getSelectionGroup()
214 .getSize() == av.getAlignment().getHeight())
216 av.setSelectionGroup(null);
220 ap.paintAlignment(true, true);
230 * Handles left mouse button press
235 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
238 * Ctrl-click/Cmd-click adds to the selection
239 * Shift-click extends the selection
241 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
242 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
244 av.getColumnSelection().clear();
247 av.getColumnSelection().addElement(res);
248 SequenceGroup sg = new SequenceGroup(av.getAlignment().getSequences());
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);
265 PaintRefresher.Refresh(this, av.getSequenceSetId());
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 boolean wasDragging = mouseDragging;
279 mouseDragging = false;
280 ap.getSeqPanel().stopScrolling();
282 int res = ap.getSeqPanel().findAlignmentColumn(evt);
284 if (!stretchingGroup)
286 if (evt.isPopupTrigger()) // Windows: mouseReleased
288 rightMouseButtonPressed(evt, res);
292 ap.paintAlignment(false, false);
297 SequenceGroup sg = av.getSelectionGroup();
301 if (res > sg.getStartRes())
305 else if (res < sg.getStartRes())
311 min = Math.min(res, min);
312 max = Math.max(res, max);
313 av.getColumnSelection().stretchGroup(res, sg, min, max);
316 stretchingGroup = false;
317 ap.paintAlignment(false, false);
318 av.isSelectionGroupChanged(true);
319 av.isColSelChanged(true);
320 PaintRefresher.Refresh(ap, av.getSequenceSetId());
325 * Action on dragging the mouse in the scale panel is to expand or shrink the
326 * selection group range (including any hidden columns that it spans). Note
327 * that the selection is only broadcast at the start of the drag (on
328 * mousePressed) and at the end (on mouseReleased), to avoid overload
329 * redrawing of other views.
334 public void mouseDragged(MouseEvent evt)
336 mouseDragging = true;
337 int res = ap.getSeqPanel().findAlignmentColumn(evt);
339 ColumnSelection cs = av.getColumnSelection();
341 min = Math.min(res, min);
342 max = Math.max(res, max);
344 SequenceGroup sg = av.getSelectionGroup();
347 stretchingGroup = true;
348 cs.stretchGroup(res, sg, min, max);
349 ap.paintAlignment(false, false);
350 PaintRefresher.Refresh(ap, av.getSequenceSetId());
355 public void mouseEntered(MouseEvent evt)
359 mouseDragging = false;
360 ap.getSeqPanel().stopScrolling();
365 * Action on leaving the panel bounds with mouse drag in progress is to start
366 * scrolling the alignment in the direction of the mouse. To restrict
367 * scrolling to left-right (not up-down), the y-value of the mouse position is
368 * replaced with zero.
371 public void mouseExited(MouseEvent evt)
375 ap.getSeqPanel().startScrolling(new Point(evt.getX(), 0));
380 public void mouseClicked(MouseEvent evt)
385 * Creates a tooltip when the mouse is over a hidden columns marker
388 public void mouseMoved(MouseEvent evt)
390 this.setToolTipText(null);
392 final int res = ap.getSeqPanel().findAlignmentColumn(evt);
394 highlightAllStructPos(res);
395 if (!av.hasHiddenColumns())
399 reveal = av.getAlignment().getHiddenColumns()
400 .getRegionWithEdgeAtRes(av.getAlignment().getHiddenColumns()
401 .absoluteToVisibleColumn(res));
406 ToolTipManager.sharedInstance().registerComponent(this);
408 MessageManager.getString("label.reveal_hidden_columns"));
412 public void highlightAllStructPos(int col)
414 ap.getStructureSelectionManager().highlightPositionsOnMany(
415 ap.av.getAlignment().getSequencesArray(), new int[]
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 },
521 { y, y, y + 2 * yOf }, 3);
525 // Draw the scale numbers
526 gg.setColor(Color.black);
529 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
532 for (ScaleMark mark : marks)
534 boolean major = mark.major;
535 int mpos = mark.column; // (i - startx - 1)
536 String mstring = mark.text;
539 if (mpos * avCharWidth > maxX)
541 gg.drawString(mstring, mpos * avCharWidth, y);
542 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
547 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
548 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
552 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
553 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
559 public void propertyChange(PropertyChangeEvent evt)
561 // Respond to viewport change events (e.g. alignment panel was scrolled)
562 // Both scrolling and resizing change viewport ranges: scrolling changes
563 // both start and end points, but resize only changes end values.
564 // Here we only want to fastpaint on a scroll, with resize using a normal
565 // paint, so scroll events are identified as changes to the horizontal or
566 // vertical start value.
567 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
568 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
569 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
571 // scroll event, repaint panel
573 // Call repaint on alignment panel so that repaints from other alignment
574 // panel components can be aggregated. Otherwise performance of the
576 // window and others may be adversely affected.
577 av.getAlignPanel().repaint();