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.util.MessageManager;
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.RenderingHints;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseMotionListener;
38 import java.util.List;
40 import javax.swing.JMenuItem;
41 import javax.swing.JPanel;
42 import javax.swing.JPopupMenu;
43 import javax.swing.ToolTipManager;
46 * The panel containing the sequence ruler (when not in wrapped mode), and
47 * supports a range of mouse operations to select, hide or reveal columns.
49 public class ScalePanel extends JPanel implements MouseMotionListener,
52 protected int offy = 4;
56 protected AlignViewport av;
60 boolean stretchingGroup = false;
63 * min, max hold the extent of a mouse drag action
69 boolean mouseDragging = false;
72 * holds a hidden column range when the mouse is over an adjacent column
82 public ScalePanel(AlignViewport av, AlignmentPanel ap)
87 addMouseListener(this);
88 addMouseMotionListener(this);
98 public void mousePressed(MouseEvent evt)
100 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
103 if (av.hasHiddenColumns())
105 x = av.getColumnSelection().adjustForHiddenColumns(x);
108 if (x >= av.getAlignment().getWidth())
110 res = av.getAlignment().getWidth() - 1;
120 if (evt.isPopupTrigger())
122 rightMouseButtonPressed(evt, res);
126 leftMouseButtonPressed(evt, res);
131 * Handles right mouse button press. If pressed in a selected column, opens
132 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
133 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
138 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
140 JPopupMenu pop = new JPopupMenu();
143 JMenuItem item = new JMenuItem(
144 MessageManager.getString("label.reveal"));
145 item.addActionListener(new ActionListener()
148 public void actionPerformed(ActionEvent e)
150 av.showColumn(reveal[0]);
152 ap.paintAlignment(true);
153 if (ap.overviewPanel != null)
155 ap.overviewPanel.updateOverviewImage();
162 if (av.getColumnSelection().hasHiddenColumns())
164 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
165 item.addActionListener(new ActionListener()
168 public void actionPerformed(ActionEvent e)
170 av.showAllHiddenColumns();
172 ap.paintAlignment(true);
173 if (ap.overviewPanel != null)
175 ap.overviewPanel.updateOverviewImage();
182 pop.show(this, evt.getX(), evt.getY());
184 else if (av.getColumnSelection().contains(res))
186 JMenuItem item = new JMenuItem(
187 MessageManager.getString("label.hide_columns"));
188 item.addActionListener(new ActionListener()
191 public void actionPerformed(ActionEvent e)
193 av.hideColumns(res, res);
194 if (av.getSelectionGroup() != null
195 && av.getSelectionGroup().getSize() == av.getAlignment()
198 av.setSelectionGroup(null);
201 ap.paintAlignment(true);
202 if (ap.overviewPanel != null)
204 ap.overviewPanel.updateOverviewImage();
210 pop.show(this, evt.getX(), evt.getY());
215 * Handles left mouse button press
220 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
222 if (!evt.isControlDown() && !evt.isShiftDown())
224 av.getColumnSelection().clear();
227 av.getColumnSelection().addElement(res);
228 SequenceGroup sg = new SequenceGroup();
229 // try to be as quick as possible
230 SequenceI[] iVec = av.getAlignment().getSequencesArray();
231 for (int i = 0; i < iVec.length; i++)
233 sg.addSequence(iVec[i], false);
240 if (evt.isShiftDown())
242 int min = Math.min(av.getColumnSelection().getMin(), res);
243 int max = Math.max(av.getColumnSelection().getMax(), res);
244 for (int i = min; i < max; i++)
246 av.getColumnSelection().addElement(i);
251 av.setSelectionGroup(sg);
252 ap.paintAlignment(false);
263 public void mouseReleased(MouseEvent evt)
265 mouseDragging = false;
267 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
269 if (av.hasHiddenColumns())
271 res = av.getColumnSelection().adjustForHiddenColumns(res);
274 if (res >= av.getAlignment().getWidth())
276 res = av.getAlignment().getWidth() - 1;
279 if (!stretchingGroup)
281 ap.paintAlignment(false);
286 SequenceGroup sg = av.getSelectionGroup();
290 if (res > sg.getStartRes())
294 else if (res < sg.getStartRes())
299 stretchingGroup = false;
300 ap.paintAlignment(false);
311 public void mouseDragged(MouseEvent evt)
313 mouseDragging = true;
315 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
321 if (av.hasHiddenColumns())
323 res = av.getColumnSelection().adjustForHiddenColumns(res);
326 if (res >= av.getAlignment().getWidth())
328 res = av.getAlignment().getWidth() - 1;
341 SequenceGroup sg = av.getSelectionGroup();
345 stretchingGroup = true;
347 if (!av.getColumnSelection().contains(res))
349 av.getColumnSelection().addElement(res);
352 if (res > sg.getStartRes())
356 if (res < sg.getStartRes())
362 for (int i = min; i <= max; i++)
364 col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
366 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
368 av.getColumnSelection().removeElement(col);
372 av.getColumnSelection().addElement(col);
376 ap.paintAlignment(false);
381 public void mouseEntered(MouseEvent evt)
385 ap.getSeqPanel().scrollCanvas(null);
390 public void mouseExited(MouseEvent evt)
394 ap.getSeqPanel().scrollCanvas(evt);
399 public void mouseClicked(MouseEvent evt)
404 public void mouseMoved(MouseEvent evt)
406 this.setToolTipText(null);
408 if (!av.hasHiddenColumns())
413 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
415 res = av.getColumnSelection().adjustForHiddenColumns(res);
417 if (av.getColumnSelection().getHiddenColumns() != null)
419 for (int[] region : av.getColumnSelection().getHiddenColumns())
421 if (res + 1 == region[0] || res - 1 == region[1])
424 ToolTipManager.sharedInstance().registerComponent(this);
425 this.setToolTipText(MessageManager
426 .getString("label.reveal_hidden_columns"));
441 public void paintComponent(Graphics g)
443 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
446 // scalewidth will normally be screenwidth,
447 public void drawScale(Graphics g, int startx, int endx, int width,
450 Graphics2D gg = (Graphics2D) g;
451 gg.setFont(av.getFont());
455 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
456 RenderingHints.VALUE_ANTIALIAS_ON);
459 // Fill in the background
460 gg.setColor(Color.white);
461 gg.fillRect(0, 0, width, height);
462 gg.setColor(Color.black);
464 // Fill the selected columns
465 ColumnSelection cs = av.getColumnSelection();
466 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
470 gg.setColor(new Color(220, 0, 0));
472 for (int sel : cs.getSelected())
474 // TODO: JAL-2001 - provide a fast method to list visible selected in a
477 if (av.hasHiddenColumns())
479 if (cs.isVisible(sel))
481 sel = cs.findColumnPosition(sel);
489 if ((sel >= startx) && (sel <= endx))
491 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
497 int widthx = 1 + endx - startx;
499 FontMetrics fm = gg.getFontMetrics(av.getFont());
500 int y = avCharHeight, yOf = fm.getDescent();
502 if (av.hasHiddenColumns())
504 // draw any hidden column markers
505 gg.setColor(Color.blue);
507 if (av.getShowHiddenMarkers()
508 && av.getColumnSelection().getHiddenColumns() != null)
510 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
513 res = av.getColumnSelection().findHiddenRegionPosition(i)
516 if (res < 0 || res > widthx)
521 gg.fillPolygon(new int[] {
522 -1 + res * avCharWidth - avCharHeight / 4,
523 -1 + res * avCharWidth + avCharHeight / 4,
524 -1 + res * avCharWidth }, new int[] { y, y, y + 2 * yOf }, 3);
529 // Draw the scale numbers
530 gg.setColor(Color.black);
533 List<Object[]> marks = jalview.renderer.ScaleRenderer.calculateMarks(
536 for (Object[] mark : marks)
538 boolean major = Boolean.valueOf((Boolean) mark[0]);
539 int mpos = ((Integer) mark[1]).intValue(); // (i - startx - 1)
540 String mstring = (String) mark[2];
543 if (mpos * avCharWidth > maxX)
545 gg.drawString(mstring, mpos * avCharWidth, y);
546 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
551 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
552 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
556 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
557 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
560 if (av.hasHiddenColumns())
562 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
564 gg.drawString(MessageManager.getString("label.reveal_columns"),
565 reveal[0] * avCharWidth, 0);