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.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.renderer.ScaleRenderer;
27 import jalview.renderer.ScaleRenderer.ScaleMark;
28 import jalview.util.MessageManager;
29 import jalview.util.Platform;
31 import java.awt.Color;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
34 import java.awt.Graphics2D;
35 import java.awt.RenderingHints;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.MouseEvent;
39 import java.awt.event.MouseListener;
40 import java.awt.event.MouseMotionListener;
41 import java.util.List;
43 import javax.swing.JMenuItem;
44 import javax.swing.JPanel;
45 import javax.swing.JPopupMenu;
46 import javax.swing.SwingUtilities;
47 import javax.swing.ToolTipManager;
50 * The panel containing the sequence ruler (when not in wrapped mode), and
51 * supports a range of mouse operations to select, hide or reveal columns.
53 public class ScalePanel extends JPanel implements MouseMotionListener,
56 protected int offy = 4;
60 protected AlignViewport av;
64 boolean stretchingGroup = false;
67 * min, max hold the extent of a mouse drag action
73 boolean mouseDragging = false;
76 * holds a hidden column range when the mouse is over an adjacent column
86 public ScalePanel(AlignViewport av, AlignmentPanel ap)
91 addMouseListener(this);
92 addMouseMotionListener(this);
102 public void mousePressed(MouseEvent evt)
104 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
107 if (av.hasHiddenColumns())
109 x = av.getColumnSelection().adjustForHiddenColumns(x);
112 if (x >= av.getAlignment().getWidth())
114 res = av.getAlignment().getWidth() - 1;
124 if (evt.isPopupTrigger()) // Mac: mousePressed
126 rightMouseButtonPressed(evt, res);
128 else if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
131 * defer right-mouse click handling to mouse up on Windows
132 * (where isPopupTrigger() will answer true)
133 * but accept Cmd-click on Mac which passes isRightMouseButton
139 leftMouseButtonPressed(evt, res);
144 * Handles right mouse button press. If pressed in a selected column, opens
145 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
146 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
151 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
153 JPopupMenu pop = new JPopupMenu();
156 JMenuItem item = new JMenuItem(
157 MessageManager.getString("label.reveal"));
158 item.addActionListener(new ActionListener()
161 public void actionPerformed(ActionEvent e)
163 av.showColumn(reveal[0]);
165 ap.paintAlignment(true);
166 if (ap.overviewPanel != null)
168 ap.overviewPanel.updateOverviewImage();
175 if (av.getColumnSelection().hasHiddenColumns())
177 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
178 item.addActionListener(new ActionListener()
181 public void actionPerformed(ActionEvent e)
183 av.showAllHiddenColumns();
185 ap.paintAlignment(true);
186 if (ap.overviewPanel != null)
188 ap.overviewPanel.updateOverviewImage();
195 pop.show(this, evt.getX(), evt.getY());
197 else if (av.getColumnSelection().contains(res))
199 JMenuItem item = new JMenuItem(
200 MessageManager.getString("label.hide_columns"));
201 item.addActionListener(new ActionListener()
204 public void actionPerformed(ActionEvent e)
206 av.hideColumns(res, res);
207 if (av.getSelectionGroup() != null
208 && av.getSelectionGroup().getSize() == av.getAlignment()
211 av.setSelectionGroup(null);
214 ap.paintAlignment(true);
215 if (ap.overviewPanel != null)
217 ap.overviewPanel.updateOverviewImage();
223 pop.show(this, evt.getX(), evt.getY());
228 * Handles left mouse button press
233 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
236 * Ctrl-click/Cmd-click adds to the selection
237 * Shift-click extends the selection
239 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
240 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
242 av.getColumnSelection().clear();
245 av.getColumnSelection().addElement(res);
246 SequenceGroup sg = new SequenceGroup();
247 // try to be as quick as possible
248 SequenceI[] iVec = av.getAlignment().getSequencesArray();
249 for (int i = 0; i < iVec.length; i++)
251 sg.addSequence(iVec[i], false);
258 if (evt.isShiftDown())
260 int min = Math.min(av.getColumnSelection().getMin(), res);
261 int max = Math.max(av.getColumnSelection().getMax(), res);
262 for (int i = min; i < max; i++)
264 av.getColumnSelection().addElement(i);
269 av.setSelectionGroup(sg);
270 ap.paintAlignment(false);
281 public void mouseReleased(MouseEvent evt)
283 mouseDragging = false;
285 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
287 if (av.hasHiddenColumns())
289 res = av.getColumnSelection().adjustForHiddenColumns(res);
292 if (res >= av.getAlignment().getWidth())
294 res = av.getAlignment().getWidth() - 1;
297 if (!stretchingGroup)
299 if (evt.isPopupTrigger()) // Windows: mouseReleased
301 rightMouseButtonPressed(evt, res);
305 ap.paintAlignment(false);
310 SequenceGroup sg = av.getSelectionGroup();
314 if (res > sg.getStartRes())
318 else if (res < sg.getStartRes())
323 stretchingGroup = false;
324 ap.paintAlignment(false);
329 * Action on dragging the mouse in the scale panel is to expand or shrink the
330 * selection group range (including any hidden columns that it spans)
335 public void mouseDragged(MouseEvent evt)
337 mouseDragging = true;
338 ColumnSelection cs = av.getColumnSelection();
340 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
341 res = Math.max(0, res);
342 res = cs.adjustForHiddenColumns(res);
343 res = Math.min(res, av.getAlignment().getWidth() - 1);
344 min = Math.min(res, min);
345 max = Math.max(res, max);
347 SequenceGroup sg = av.getSelectionGroup();
350 stretchingGroup = true;
351 cs.stretchGroup(res, sg, min, max);
352 ap.paintAlignment(false);
357 public void mouseEntered(MouseEvent evt)
361 ap.getSeqPanel().scrollCanvas(null);
366 public void mouseExited(MouseEvent evt)
370 ap.getSeqPanel().scrollCanvas(evt);
375 public void mouseClicked(MouseEvent evt)
380 * Creates a tooltip when the mouse is over a hidden columns marker
383 public void mouseMoved(MouseEvent evt)
385 this.setToolTipText(null);
387 if (!av.hasHiddenColumns())
392 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
394 res = av.getColumnSelection().adjustForHiddenColumns(res);
396 if (av.getColumnSelection().getHiddenColumns() != null)
398 for (int[] region : av.getColumnSelection().getHiddenColumns())
400 if (res + 1 == region[0] || res - 1 == region[1])
403 ToolTipManager.sharedInstance().registerComponent(this);
404 this.setToolTipText(MessageManager
405 .getString("label.reveal_hidden_columns"));
420 public void paintComponent(Graphics g)
422 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
425 // scalewidth will normally be screenwidth,
426 public void drawScale(Graphics g, int startx, int endx, int width,
429 Graphics2D gg = (Graphics2D) g;
430 gg.setFont(av.getFont());
434 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
435 RenderingHints.VALUE_ANTIALIAS_ON);
438 // Fill in the background
439 gg.setColor(Color.white);
440 gg.fillRect(0, 0, width, height);
441 gg.setColor(Color.black);
443 // Fill the selected columns
444 ColumnSelection cs = av.getColumnSelection();
445 int avCharWidth = av.getCharWidth(), 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 (cs.isVisible(sel))
460 sel = cs.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);
487 if (av.getShowHiddenMarkers()
488 && av.getColumnSelection().getHiddenColumns() != null)
490 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
493 res = av.getColumnSelection().findHiddenRegionPosition(i)
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));