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 * grab the hidden range in case mouseMoved nulls it
165 final int[] hiddenRange = reveal;
166 if (hiddenRange != null)
168 JMenuItem item = new JMenuItem(
169 MessageManager.getString("label.reveal"));
170 item.addActionListener(new ActionListener()
173 public void actionPerformed(ActionEvent e)
175 av.showColumn(hiddenRange[0]);
177 ap.paintAlignment(true, true);
183 if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
185 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
186 item.addActionListener(new ActionListener()
189 public void actionPerformed(ActionEvent e)
191 av.showAllHiddenColumns();
193 ap.paintAlignment(true, true);
199 pop.show(this, evt.getX(), evt.getY());
201 else if (av.getColumnSelection().contains(res))
203 JMenuItem item = new JMenuItem(
204 MessageManager.getString("label.hide_columns"));
205 item.addActionListener(new ActionListener()
208 public void actionPerformed(ActionEvent e)
210 av.hideColumns(res, res);
211 if (av.getSelectionGroup() != null && av.getSelectionGroup()
212 .getSize() == av.getAlignment().getHeight())
214 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();
246 // try to be as quick as possible
247 SequenceI[] iVec = av.getAlignment().getSequencesArray();
248 for (int i = 0; i < iVec.length; i++)
250 sg.addSequence(iVec[i], false);
257 if (evt.isShiftDown())
259 int min = Math.min(av.getColumnSelection().getMin(), res);
260 int max = Math.max(av.getColumnSelection().getMax(), res);
261 for (int i = min; i < max; i++)
263 av.getColumnSelection().addElement(i);
268 av.setSelectionGroup(sg);
269 ap.paintAlignment(false, false);
280 public void mouseReleased(MouseEvent evt)
282 mouseDragging = false;
284 int res = (evt.getX() / av.getCharWidth())
285 + av.getRanges().getStartRes();
287 if (av.hasHiddenColumns())
289 res = av.getAlignment().getHiddenColumns()
290 .visibleToAbsoluteColumn(res);
293 if (res >= av.getAlignment().getWidth())
295 res = av.getAlignment().getWidth() - 1;
298 if (!stretchingGroup)
300 if (evt.isPopupTrigger()) // Windows: mouseReleased
302 rightMouseButtonPressed(evt, res);
306 ap.paintAlignment(false, false);
311 SequenceGroup sg = av.getSelectionGroup();
315 if (res > sg.getStartRes())
319 else if (res < sg.getStartRes())
324 stretchingGroup = false;
325 ap.paintAlignment(false, false);
330 * Action on dragging the mouse in the scale panel is to expand or shrink the
331 * selection group range (including any hidden columns that it spans)
336 public void mouseDragged(MouseEvent evt)
338 mouseDragging = true;
339 ColumnSelection cs = av.getColumnSelection();
340 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
342 int res = (evt.getX() / av.getCharWidth())
343 + av.getRanges().getStartRes();
344 res = Math.max(0, res);
345 res = hidden.visibleToAbsoluteColumn(res);
346 res = Math.min(res, av.getAlignment().getWidth() - 1);
347 min = Math.min(res, min);
348 max = Math.max(res, max);
350 SequenceGroup sg = av.getSelectionGroup();
353 stretchingGroup = true;
354 cs.stretchGroup(res, sg, min, max);
355 ap.paintAlignment(false, false);
360 public void mouseEntered(MouseEvent evt)
364 ap.getSeqPanel().scrollCanvas(null);
369 public void mouseExited(MouseEvent evt)
373 ap.getSeqPanel().scrollCanvas(evt);
378 public void mouseClicked(MouseEvent evt)
383 * Creates a tooltip when the mouse is over a hidden columns marker
386 public void mouseMoved(MouseEvent evt)
388 this.setToolTipText(null);
390 if (!av.hasHiddenColumns())
395 int res = (evt.getX() / av.getCharWidth())
396 + av.getRanges().getStartRes();
398 reveal = av.getAlignment().getHiddenColumns()
399 .getRegionWithEdgeAtRes(res);
401 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
403 ToolTipManager.sharedInstance().registerComponent(this);
405 MessageManager.getString("label.reveal_hidden_columns"));
416 public void paintComponent(Graphics g)
418 super.paintComponent(g);
421 * shouldn't get called in wrapped mode as the scale above is
422 * drawn instead by SeqCanvas.drawNorthScale
424 if (!av.getWrapAlignment())
426 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
427 getWidth(), getHeight());
431 // scalewidth will normally be screenwidth,
432 public void drawScale(Graphics g, int startx, int endx, int width,
435 Graphics2D gg = (Graphics2D) g;
436 gg.setFont(av.getFont());
440 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
441 RenderingHints.VALUE_ANTIALIAS_ON);
444 // Fill in the background
445 gg.setColor(Color.white);
446 gg.fillRect(0, 0, width, height);
447 gg.setColor(Color.black);
449 // Fill the selected columns
450 ColumnSelection cs = av.getColumnSelection();
451 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
452 int avCharWidth = av.getCharWidth();
453 int avCharHeight = av.getCharHeight();
457 gg.setColor(new Color(220, 0, 0));
459 for (int sel : cs.getSelected())
461 // TODO: JAL-2001 - provide a fast method to list visible selected in a
464 if (av.hasHiddenColumns())
466 if (hidden.isVisible(sel))
468 sel = hidden.absoluteToVisibleColumn(sel);
476 if ((sel >= startx) && (sel <= endx))
478 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
484 int widthx = 1 + endx - startx;
486 FontMetrics fm = gg.getFontMetrics(av.getFont());
487 int y = avCharHeight;
488 int yOf = fm.getDescent();
490 if (av.hasHiddenColumns())
492 // draw any hidden column markers
493 gg.setColor(Color.blue);
496 if (av.getShowHiddenMarkers())
498 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
499 startx + widthx + 1);
502 res = it.next() - startx;
506 { -1 + res * avCharWidth - avCharHeight / 4,
507 -1 + res * avCharWidth + avCharHeight / 4,
508 -1 + res * avCharWidth }, new int[]
509 { y, y, y + 2 * yOf }, 3);
513 // Draw the scale numbers
514 gg.setColor(Color.black);
517 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
520 for (ScaleMark mark : marks)
522 boolean major = mark.major;
523 int mpos = mark.column; // (i - startx - 1)
524 String mstring = mark.text;
527 if (mpos * avCharWidth > maxX)
529 gg.drawString(mstring, mpos * avCharWidth, y);
530 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
535 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
536 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
540 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
541 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
547 public void propertyChange(PropertyChangeEvent evt)
549 // Respond to viewport change events (e.g. alignment panel was scrolled)
550 // Both scrolling and resizing change viewport ranges: scrolling changes
551 // both start and end points, but resize only changes end values.
552 // Here we only want to fastpaint on a scroll, with resize using a normal
553 // paint, so scroll events are identified as changes to the horizontal or
554 // vertical start value.
555 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
556 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
557 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
559 // scroll event, repaint panel
561 // Call repaint on alignment panel so that repaints from other alignment
562 // panel components can be aggregated. Otherwise performance of the overview
563 // window and others may be adversely affected.
564 av.getAlignPanel().repaint();