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;
32 import java.awt.Color;
33 import java.awt.FontMetrics;
34 import java.awt.Graphics;
35 import java.awt.Graphics2D;
36 import java.awt.RenderingHints;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.MouseEvent;
40 import java.awt.event.MouseListener;
41 import java.awt.event.MouseMotionListener;
42 import java.util.List;
44 import javax.swing.JMenuItem;
45 import javax.swing.JPanel;
46 import javax.swing.JPopupMenu;
47 import javax.swing.SwingUtilities;
48 import javax.swing.ToolTipManager;
51 * The panel containing the sequence ruler (when not in wrapped mode), and
52 * supports a range of mouse operations to select, hide or reveal columns.
54 public class ScalePanel extends JPanel implements MouseMotionListener,
57 protected int offy = 4;
61 protected AlignViewport av;
65 boolean stretchingGroup = false;
68 * min, max hold the extent of a mouse drag action
74 boolean mouseDragging = false;
77 * holds a hidden column range when the mouse is over an adjacent column
87 public ScalePanel(AlignViewport av, AlignmentPanel ap)
92 addMouseListener(this);
93 addMouseMotionListener(this);
103 public void mousePressed(MouseEvent evt)
105 int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes();
108 if (av.hasHiddenColumns())
110 x = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(x);
113 if (x >= av.getAlignment().getWidth())
115 res = av.getAlignment().getWidth() - 1;
125 if (evt.isPopupTrigger()) // Mac: mousePressed
127 rightMouseButtonPressed(evt, res);
129 else if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
132 * defer right-mouse click handling to mouse up on Windows
133 * (where isPopupTrigger() will answer true)
134 * but accept Cmd-click on Mac which passes isRightMouseButton
140 leftMouseButtonPressed(evt, res);
145 * Handles right mouse button press. If pressed in a selected column, opens
146 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
147 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
152 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
154 JPopupMenu pop = new JPopupMenu();
157 JMenuItem item = new JMenuItem(
158 MessageManager.getString("label.reveal"));
159 item.addActionListener(new ActionListener()
162 public void actionPerformed(ActionEvent e)
164 av.showColumn(reveal[0]);
166 ap.paintAlignment(true);
167 if (ap.overviewPanel != null)
169 ap.overviewPanel.updateOverviewImage();
176 if (av.getAlignment().getHiddenColumns().hasHiddenColumns())
178 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
179 item.addActionListener(new ActionListener()
182 public void actionPerformed(ActionEvent e)
184 av.showAllHiddenColumns();
186 ap.paintAlignment(true);
187 if (ap.overviewPanel != null)
189 ap.overviewPanel.updateOverviewImage();
196 pop.show(this, evt.getX(), evt.getY());
198 else if (av.getColumnSelection().contains(res))
200 JMenuItem item = new JMenuItem(
201 MessageManager.getString("label.hide_columns"));
202 item.addActionListener(new ActionListener()
205 public void actionPerformed(ActionEvent e)
207 av.hideColumns(res, res);
208 if (av.getSelectionGroup() != null
209 && av.getSelectionGroup().getSize() == av.getAlignment()
212 av.setSelectionGroup(null);
215 ap.paintAlignment(true);
216 if (ap.overviewPanel != null)
218 ap.overviewPanel.updateOverviewImage();
224 pop.show(this, evt.getX(), evt.getY());
229 * Handles left mouse button press
234 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
237 * Ctrl-click/Cmd-click adds to the selection
238 * Shift-click extends the selection
240 // TODO Problem: right-click on Windows not reported until mouseReleased?!?
241 if (!Platform.isControlDown(evt) && !evt.isShiftDown())
243 av.getColumnSelection().clear();
246 av.getColumnSelection().addElement(res);
247 SequenceGroup sg = new SequenceGroup();
248 // try to be as quick as possible
249 SequenceI[] iVec = av.getAlignment().getSequencesArray();
250 for (int i = 0; i < iVec.length; i++)
252 sg.addSequence(iVec[i], false);
259 if (evt.isShiftDown())
261 int min = Math.min(av.getColumnSelection().getMin(), res);
262 int max = Math.max(av.getColumnSelection().getMax(), res);
263 for (int i = min; i < max; i++)
265 av.getColumnSelection().addElement(i);
270 av.setSelectionGroup(sg);
271 ap.paintAlignment(false);
282 public void mouseReleased(MouseEvent evt)
284 mouseDragging = false;
286 int res = (evt.getX() / av.getCharWidth())
287 + av.getRanges().getStartRes();
289 if (av.hasHiddenColumns())
291 res = av.getAlignment().getHiddenColumns()
292 .adjustForHiddenColumns(res);
295 if (res >= av.getAlignment().getWidth())
297 res = av.getAlignment().getWidth() - 1;
300 if (!stretchingGroup)
302 if (evt.isPopupTrigger()) // Windows: mouseReleased
304 rightMouseButtonPressed(evt, res);
308 ap.paintAlignment(false);
313 SequenceGroup sg = av.getSelectionGroup();
317 if (res > sg.getStartRes())
321 else if (res < sg.getStartRes())
326 stretchingGroup = false;
327 ap.paintAlignment(false);
332 * Action on dragging the mouse in the scale panel is to expand or shrink the
333 * selection group range (including any hidden columns that it spans)
338 public void mouseDragged(MouseEvent evt)
340 mouseDragging = true;
341 ColumnSelection cs = av.getColumnSelection();
342 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
344 int res = (evt.getX() / av.getCharWidth())
345 + av.getRanges().getStartRes();
346 res = Math.max(0, res);
347 res = hidden.adjustForHiddenColumns(res);
348 res = Math.min(res, av.getAlignment().getWidth() - 1);
349 min = Math.min(res, min);
350 max = Math.max(res, max);
352 SequenceGroup sg = av.getSelectionGroup();
355 stretchingGroup = true;
356 cs.stretchGroup(res, sg, min, max);
357 ap.paintAlignment(false);
362 public void mouseEntered(MouseEvent evt)
366 ap.getSeqPanel().scrollCanvas(null);
371 public void mouseExited(MouseEvent evt)
375 ap.getSeqPanel().scrollCanvas(evt);
380 public void mouseClicked(MouseEvent evt)
385 * Creates a tooltip when the mouse is over a hidden columns marker
388 public void mouseMoved(MouseEvent evt)
390 this.setToolTipText(null);
392 if (!av.hasHiddenColumns())
397 int res = (evt.getX() / av.getCharWidth())
398 + av.getRanges().getStartRes();
400 res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res);
402 if (av.getAlignment().getHiddenColumns().getHiddenRegions() != null)
404 for (int[] region : av.getAlignment().getHiddenColumns()
407 if (res + 1 == region[0] || res - 1 == region[1])
410 ToolTipManager.sharedInstance().registerComponent(this);
411 this.setToolTipText(MessageManager
412 .getString("label.reveal_hidden_columns"));
427 public void paintComponent(Graphics g)
429 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
430 getWidth(), getHeight());
433 // scalewidth will normally be screenwidth,
434 public void drawScale(Graphics g, int startx, int endx, int width,
437 Graphics2D gg = (Graphics2D) g;
438 gg.setFont(av.getFont());
442 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
443 RenderingHints.VALUE_ANTIALIAS_ON);
446 // Fill in the background
447 gg.setColor(Color.white);
448 gg.fillRect(0, 0, width, height);
449 gg.setColor(Color.black);
451 // Fill the selected columns
452 ColumnSelection cs = av.getColumnSelection();
453 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
454 int avCharWidth = av.getCharWidth();
455 int avCharHeight = av.getCharHeight();
459 gg.setColor(new Color(220, 0, 0));
461 for (int sel : cs.getSelected())
463 // TODO: JAL-2001 - provide a fast method to list visible selected in a
466 if (av.hasHiddenColumns())
468 if (hidden.isVisible(sel))
470 sel = hidden.findColumnPosition(sel);
478 if ((sel >= startx) && (sel <= endx))
480 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
486 int widthx = 1 + endx - startx;
488 FontMetrics fm = gg.getFontMetrics(av.getFont());
489 int y = avCharHeight;
490 int yOf = fm.getDescent();
492 if (av.hasHiddenColumns())
494 // draw any hidden column markers
495 gg.setColor(Color.blue);
498 if (av.getShowHiddenMarkers() && hidden.getHiddenRegions() != null)
500 for (int i = 0; i < hidden.getHiddenRegions()
503 res = hidden.findHiddenRegionPosition(i)
506 if (res < 0 || res > widthx)
511 gg.fillPolygon(new int[] {
512 -1 + res * avCharWidth - avCharHeight / 4,
513 -1 + res * avCharWidth + avCharHeight / 4,
514 -1 + res * avCharWidth }, new int[] { y, y, y + 2 * yOf }, 3);
518 // Draw the scale numbers
519 gg.setColor(Color.black);
522 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
525 for (ScaleMark mark : marks)
527 boolean major = mark.major;
528 int mpos = mark.column; // (i - startx - 1)
529 String mstring = mark.text;
532 if (mpos * avCharWidth > maxX)
534 gg.drawString(mstring, mpos * avCharWidth, y);
535 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
540 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
541 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
545 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
546 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));