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);
172 if (ap.overviewPanel != null)
174 ap.overviewPanel.updateOverviewImage();
181 if (av.getAlignment().getHiddenColumns().hasHiddenColumns())
183 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
184 item.addActionListener(new ActionListener()
187 public void actionPerformed(ActionEvent e)
189 av.showAllHiddenColumns();
191 ap.paintAlignment(true);
192 if (ap.overviewPanel != null)
194 ap.overviewPanel.updateOverviewImage();
201 pop.show(this, evt.getX(), evt.getY());
203 else 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
214 && av.getSelectionGroup().getSize() == av.getAlignment()
217 av.setSelectionGroup(null);
220 ap.paintAlignment(true);
221 if (ap.overviewPanel != null)
223 ap.overviewPanel.updateOverviewImage();
229 pop.show(this, evt.getX(), evt.getY());
234 * Handles left mouse button press
239 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
242 * Ctrl-click/Cmd-click adds to the selection
243 * Shift-click extends the selection
245 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
246 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
248 av.getColumnSelection().clear();
251 av.getColumnSelection().addElement(res);
252 SequenceGroup sg = new SequenceGroup();
253 // try to be as quick as possible
254 SequenceI[] iVec = av.getAlignment().getSequencesArray();
255 for (int i = 0; i < iVec.length; i++)
257 sg.addSequence(iVec[i], false);
264 if (evt.isShiftDown())
266 int min = Math.min(av.getColumnSelection().getMin(), res);
267 int max = Math.max(av.getColumnSelection().getMax(), res);
268 for (int i = min; i < max; i++)
270 av.getColumnSelection().addElement(i);
275 av.setSelectionGroup(sg);
276 ap.paintAlignment(false);
287 public void mouseReleased(MouseEvent evt)
289 mouseDragging = false;
291 int res = (evt.getX() / av.getCharWidth())
292 + av.getRanges().getStartRes();
294 if (av.hasHiddenColumns())
296 res = av.getAlignment().getHiddenColumns()
297 .adjustForHiddenColumns(res);
300 if (res >= av.getAlignment().getWidth())
302 res = av.getAlignment().getWidth() - 1;
305 if (!stretchingGroup)
307 if (evt.isPopupTrigger()) // Windows: mouseReleased
309 rightMouseButtonPressed(evt, res);
313 ap.paintAlignment(false);
318 SequenceGroup sg = av.getSelectionGroup();
322 if (res > sg.getStartRes())
326 else if (res < sg.getStartRes())
331 stretchingGroup = false;
332 ap.paintAlignment(false);
337 * Action on dragging the mouse in the scale panel is to expand or shrink the
338 * selection group range (including any hidden columns that it spans)
343 public void mouseDragged(MouseEvent evt)
345 mouseDragging = true;
346 ColumnSelection cs = av.getColumnSelection();
347 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
349 int res = (evt.getX() / av.getCharWidth())
350 + av.getRanges().getStartRes();
351 res = Math.max(0, res);
352 res = hidden.adjustForHiddenColumns(res);
353 res = Math.min(res, av.getAlignment().getWidth() - 1);
354 min = Math.min(res, min);
355 max = Math.max(res, max);
357 SequenceGroup sg = av.getSelectionGroup();
360 stretchingGroup = true;
361 cs.stretchGroup(res, sg, min, max);
362 ap.paintAlignment(false);
367 public void mouseEntered(MouseEvent evt)
371 ap.getSeqPanel().scrollCanvas(null);
376 public void mouseExited(MouseEvent evt)
380 ap.getSeqPanel().scrollCanvas(evt);
385 public void mouseClicked(MouseEvent evt)
390 * Creates a tooltip when the mouse is over a hidden columns marker
393 public void mouseMoved(MouseEvent evt)
395 this.setToolTipText(null);
397 if (!av.hasHiddenColumns())
402 int res = (evt.getX() / av.getCharWidth())
403 + av.getRanges().getStartRes();
405 reveal = av.getAlignment().getHiddenColumns()
406 .getRegionWithEdgeAtRes(res);
408 res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res);
410 ToolTipManager.sharedInstance().registerComponent(this);
412 MessageManager.getString("label.reveal_hidden_columns"));
423 public void paintComponent(Graphics g)
425 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
426 getWidth(), getHeight());
429 // scalewidth will normally be screenwidth,
430 public void drawScale(Graphics g, int startx, int endx, int width,
433 Graphics2D gg = (Graphics2D) g;
434 gg.setFont(av.getFont());
438 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
439 RenderingHints.VALUE_ANTIALIAS_ON);
442 // Fill in the background
443 gg.setColor(Color.white);
444 gg.fillRect(0, 0, width, height);
445 gg.setColor(Color.black);
447 // Fill the selected columns
448 ColumnSelection cs = av.getColumnSelection();
449 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
450 int avCharWidth = av.getCharWidth();
451 int avCharHeight = av.getCharHeight();
455 gg.setColor(new Color(220, 0, 0));
457 for (int sel : cs.getSelected())
459 // TODO: JAL-2001 - provide a fast method to list visible selected in a
462 if (av.hasHiddenColumns())
464 if (hidden.isVisible(sel))
466 sel = hidden.findColumnPosition(sel);
474 if ((sel >= startx) && (sel <= endx))
476 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
482 int widthx = 1 + endx - startx;
484 FontMetrics fm = gg.getFontMetrics(av.getFont());
485 int y = avCharHeight;
486 int yOf = fm.getDescent();
488 if (av.hasHiddenColumns())
490 // draw any hidden column markers
491 gg.setColor(Color.blue);
494 if (av.getShowHiddenMarkers())
496 List<Integer> positions = hidden.findHiddenRegionPositions();
497 for (int pos : positions)
501 if (res < 0 || res > widthx)
506 gg.fillPolygon(new int[] {
507 -1 + res * avCharWidth - avCharHeight / 4,
508 -1 + res * avCharWidth + avCharHeight / 4,
509 -1 + res * avCharWidth }, new int[] { 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))
557 // scroll event, repaint panel