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.List;
47 import javax.swing.JMenuItem;
48 import javax.swing.JPanel;
49 import javax.swing.JPopupMenu;
50 import javax.swing.SwingUtilities;
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 implements MouseMotionListener,
58 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().adjustForHiddenColumns(x);
118 if (x >= av.getAlignment().getWidth())
120 res = av.getAlignment().getWidth() - 1;
130 if (evt.isPopupTrigger()) // Mac: mousePressed
132 rightMouseButtonPressed(evt, res);
134 else if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
137 * defer right-mouse click handling to mouse up on Windows
138 * (where isPopupTrigger() will answer true)
139 * but accept Cmd-click on Mac which passes isRightMouseButton
145 leftMouseButtonPressed(evt, res);
150 * Handles right mouse button press. If pressed in a selected column, opens
151 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
152 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
157 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
159 JPopupMenu pop = new JPopupMenu();
162 JMenuItem item = new JMenuItem(
163 MessageManager.getString("label.reveal"));
164 item.addActionListener(new ActionListener()
167 public void actionPerformed(ActionEvent e)
169 av.showColumn(reveal[0]);
171 ap.paintAlignment(true);
177 if (av.getAlignment().getHiddenColumns().hasHiddenColumns())
179 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
180 item.addActionListener(new ActionListener()
183 public void actionPerformed(ActionEvent e)
185 av.showAllHiddenColumns();
187 ap.paintAlignment(true);
193 pop.show(this, evt.getX(), evt.getY());
195 else if (av.getColumnSelection().contains(res))
197 JMenuItem item = new JMenuItem(
198 MessageManager.getString("label.hide_columns"));
199 item.addActionListener(new ActionListener()
202 public void actionPerformed(ActionEvent e)
204 av.hideColumns(res, res);
205 if (av.getSelectionGroup() != null
206 && av.getSelectionGroup().getSize() == av.getAlignment()
209 av.setSelectionGroup(null);
212 ap.paintAlignment(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);
275 public void mouseReleased(MouseEvent evt)
277 mouseDragging = false;
279 int res = (evt.getX() / av.getCharWidth())
280 + av.getRanges().getStartRes();
282 if (av.hasHiddenColumns())
284 res = av.getAlignment().getHiddenColumns()
285 .adjustForHiddenColumns(res);
288 if (res >= av.getAlignment().getWidth())
290 res = av.getAlignment().getWidth() - 1;
293 if (!stretchingGroup)
295 if (evt.isPopupTrigger()) // Windows: mouseReleased
297 rightMouseButtonPressed(evt, res);
301 ap.paintAlignment(false);
306 SequenceGroup sg = av.getSelectionGroup();
310 if (res > sg.getStartRes())
314 else if (res < sg.getStartRes())
319 stretchingGroup = false;
320 ap.paintAlignment(false);
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)
331 public void mouseDragged(MouseEvent evt)
333 mouseDragging = true;
334 ColumnSelection cs = av.getColumnSelection();
335 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
337 int res = (evt.getX() / av.getCharWidth())
338 + av.getRanges().getStartRes();
339 res = Math.max(0, res);
340 res = hidden.adjustForHiddenColumns(res);
341 res = Math.min(res, av.getAlignment().getWidth() - 1);
342 min = Math.min(res, min);
343 max = Math.max(res, max);
345 SequenceGroup sg = av.getSelectionGroup();
348 stretchingGroup = true;
349 cs.stretchGroup(res, sg, min, max);
350 ap.paintAlignment(false);
355 public void mouseEntered(MouseEvent evt)
359 ap.getSeqPanel().scrollCanvas(null);
364 public void mouseExited(MouseEvent evt)
368 ap.getSeqPanel().scrollCanvas(evt);
373 public void mouseClicked(MouseEvent evt)
378 * Creates a tooltip when the mouse is over a hidden columns marker
381 public void mouseMoved(MouseEvent evt)
383 this.setToolTipText(null);
385 if (!av.hasHiddenColumns())
390 int res = (evt.getX() / av.getCharWidth())
391 + av.getRanges().getStartRes();
393 reveal = av.getAlignment().getHiddenColumns()
394 .getRegionWithEdgeAtRes(res);
396 res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res);
398 ToolTipManager.sharedInstance().registerComponent(this);
400 MessageManager.getString("label.reveal_hidden_columns"));
411 public void paintComponent(Graphics g)
414 * shouldn't get called in wrapped mode as the scale above is
415 * drawn instead by SeqCanvas.drawNorthScale
417 if (!av.getWrapAlignment())
419 drawScale(g, av.getRanges().getStartRes(),
420 av.getRanges().getEndRes(), getWidth(), getHeight());
424 // scalewidth will normally be screenwidth,
425 public void drawScale(Graphics g, int startx, int endx, int width,
428 Graphics2D gg = (Graphics2D) g;
429 gg.setFont(av.getFont());
433 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
434 RenderingHints.VALUE_ANTIALIAS_ON);
437 // Fill in the background
438 gg.setColor(Color.white);
439 gg.fillRect(0, 0, width, height);
440 gg.setColor(Color.black);
442 // Fill the selected columns
443 ColumnSelection cs = av.getColumnSelection();
444 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
445 int avCharWidth = av.getCharWidth();
446 int avCharHeight = av.getCharHeight();
450 gg.setColor(new Color(220, 0, 0));
452 for (int sel : cs.getSelected())
454 // TODO: JAL-2001 - provide a fast method to list visible selected in a
457 if (av.hasHiddenColumns())
459 if (hidden.isVisible(sel))
461 sel = hidden.findColumnPosition(sel);
469 if ((sel >= startx) && (sel <= endx))
471 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
477 int widthx = 1 + endx - startx;
479 FontMetrics fm = gg.getFontMetrics(av.getFont());
480 int y = avCharHeight;
481 int yOf = fm.getDescent();
483 if (av.hasHiddenColumns())
485 // draw any hidden column markers
486 gg.setColor(Color.blue);
489 if (av.getShowHiddenMarkers())
491 List<Integer> positions = hidden.findHiddenRegionPositions();
492 for (int pos : positions)
496 if (res < 0 || res > widthx)
501 gg.fillPolygon(new int[] {
502 -1 + res * avCharWidth - avCharHeight / 4,
503 -1 + res * avCharWidth + avCharHeight / 4,
504 -1 + res * avCharWidth }, new int[] { y, y, y + 2 * yOf }, 3);
508 // Draw the scale numbers
509 gg.setColor(Color.black);
512 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
515 for (ScaleMark mark : marks)
517 boolean major = mark.major;
518 int mpos = mark.column; // (i - startx - 1)
519 String mstring = mark.text;
522 if (mpos * avCharWidth > maxX)
524 gg.drawString(mstring, mpos * avCharWidth, y);
525 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
530 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
531 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
535 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
536 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
542 public void propertyChange(PropertyChangeEvent evt)
544 // Respond to viewport change events (e.g. alignment panel was scrolled)
545 // Both scrolling and resizing change viewport ranges: scrolling changes
546 // both start and end points, but resize only changes end values.
547 // Here we only want to fastpaint on a scroll, with resize using a normal
548 // paint, so scroll events are identified as changes to the horizontal or
549 // vertical start value.
550 if (evt.getPropertyName().equals(ViewportRanges.STARTRES))
552 // scroll event, repaint panel