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
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().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, 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, 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 && av.getSelectionGroup()
206 .getSize() == av.getAlignment().getHeight())
208 av.setSelectionGroup(null);
211 ap.paintAlignment(true, true);
216 pop.show(this, evt.getX(), evt.getY());
221 * Handles left mouse button press
226 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
229 * Ctrl-click/Cmd-click adds to the selection
230 * Shift-click extends the selection
232 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
233 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
235 av.getColumnSelection().clear();
238 av.getColumnSelection().addElement(res);
239 SequenceGroup sg = new SequenceGroup();
240 // try to be as quick as possible
241 SequenceI[] iVec = av.getAlignment().getSequencesArray();
242 for (int i = 0; i < iVec.length; i++)
244 sg.addSequence(iVec[i], false);
251 if (evt.isShiftDown())
253 int min = Math.min(av.getColumnSelection().getMin(), res);
254 int max = Math.max(av.getColumnSelection().getMax(), res);
255 for (int i = min; i < max; i++)
257 av.getColumnSelection().addElement(i);
262 av.setSelectionGroup(sg);
263 ap.paintAlignment(false, false);
274 public void mouseReleased(MouseEvent evt)
276 mouseDragging = false;
278 int res = (evt.getX() / av.getCharWidth())
279 + av.getRanges().getStartRes();
281 if (av.hasHiddenColumns())
283 res = av.getAlignment().getHiddenColumns()
284 .adjustForHiddenColumns(res);
287 if (res >= av.getAlignment().getWidth())
289 res = av.getAlignment().getWidth() - 1;
292 if (!stretchingGroup)
294 if (evt.isPopupTrigger()) // Windows: mouseReleased
296 rightMouseButtonPressed(evt, res);
300 ap.paintAlignment(false, false);
305 SequenceGroup sg = av.getSelectionGroup();
309 if (res > sg.getStartRes())
313 else if (res < sg.getStartRes())
318 stretchingGroup = false;
319 ap.paintAlignment(false, false);
324 * Action on dragging the mouse in the scale panel is to expand or shrink the
325 * selection group range (including any hidden columns that it spans)
330 public void mouseDragged(MouseEvent evt)
332 mouseDragging = true;
333 ColumnSelection cs = av.getColumnSelection();
334 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
336 int res = (evt.getX() / av.getCharWidth())
337 + av.getRanges().getStartRes();
338 res = Math.max(0, res);
339 res = hidden.adjustForHiddenColumns(res);
340 res = Math.min(res, av.getAlignment().getWidth() - 1);
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);
354 public void mouseEntered(MouseEvent evt)
358 ap.getSeqPanel().scrollCanvas(null);
363 public void mouseExited(MouseEvent evt)
367 ap.getSeqPanel().scrollCanvas(evt);
372 public void mouseClicked(MouseEvent evt)
377 * Creates a tooltip when the mouse is over a hidden columns marker
380 public void mouseMoved(MouseEvent evt)
382 this.setToolTipText(null);
384 if (!av.hasHiddenColumns())
389 int res = (evt.getX() / av.getCharWidth())
390 + av.getRanges().getStartRes();
392 reveal = av.getAlignment().getHiddenColumns()
393 .getRegionWithEdgeAtRes(res);
395 res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res);
397 ToolTipManager.sharedInstance().registerComponent(this);
399 MessageManager.getString("label.reveal_hidden_columns"));
410 public void paintComponent(Graphics g)
413 * shouldn't get called in wrapped mode as the scale above is
414 * drawn instead by SeqCanvas.drawNorthScale
416 if (!av.getWrapAlignment())
418 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
419 getWidth(), getHeight());
423 // scalewidth will normally be screenwidth,
424 public void drawScale(Graphics g, int startx, int endx, int width,
427 Graphics2D gg = (Graphics2D) g;
428 gg.setFont(av.getFont());
432 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
433 RenderingHints.VALUE_ANTIALIAS_ON);
436 // Fill in the background
437 gg.setColor(Color.white);
438 gg.fillRect(0, 0, width, height);
439 gg.setColor(Color.black);
441 // Fill the selected columns
442 ColumnSelection cs = av.getColumnSelection();
443 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
444 int avCharWidth = av.getCharWidth();
445 int avCharHeight = av.getCharHeight();
449 gg.setColor(new Color(220, 0, 0));
451 for (int sel : cs.getSelected())
453 // TODO: JAL-2001 - provide a fast method to list visible selected in a
456 if (av.hasHiddenColumns())
458 if (hidden.isVisible(sel))
460 sel = hidden.findColumnPosition(sel);
468 if ((sel >= startx) && (sel <= endx))
470 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
476 int widthx = 1 + endx - startx;
478 FontMetrics fm = gg.getFontMetrics(av.getFont());
479 int y = avCharHeight;
480 int yOf = fm.getDescent();
482 if (av.hasHiddenColumns())
484 // draw any hidden column markers
485 gg.setColor(Color.blue);
488 if (av.getShowHiddenMarkers())
490 List<Integer> positions = hidden.findHiddenRegionPositions();
491 for (int pos : positions)
495 if (res < 0 || res > widthx)
502 { -1 + res * avCharWidth - avCharHeight / 4,
503 -1 + res * avCharWidth + avCharHeight / 4,
504 -1 + res * avCharWidth },
506 { y, y, y + 2 * yOf }, 3);
510 // Draw the scale numbers
511 gg.setColor(Color.black);
514 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
517 for (ScaleMark mark : marks)
519 boolean major = mark.major;
520 int mpos = mark.column; // (i - startx - 1)
521 String mstring = mark.text;
524 if (mpos * avCharWidth > maxX)
526 gg.drawString(mstring, mpos * avCharWidth, y);
527 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
532 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
533 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
537 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
538 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
544 public void propertyChange(PropertyChangeEvent evt)
546 // Respond to viewport change events (e.g. alignment panel was scrolled)
547 // Both scrolling and resizing change viewport ranges: scrolling changes
548 // both start and end points, but resize only changes end values.
549 // Here we only want to fastpaint on a scroll, with resize using a normal
550 // paint, so scroll events are identified as changes to the horizontal or
551 // vertical start value.
552 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
553 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
554 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
556 // scroll event, repaint panel