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;
39 import javax.swing.JMenuItem;
40 import javax.swing.JPanel;
41 import javax.swing.JPopupMenu;
42 import javax.swing.ToolTipManager;
50 public class ScalePanel extends JPanel implements MouseMotionListener,
53 protected int offy = 4;
58 protected AlignViewport av;
62 boolean stretchingGroup = false;
64 int min; // used by mouseDragged to see if user
66 int max; // used by mouseDragged to see if user
68 boolean mouseDragging = false;
70 // wants to delete columns
71 public ScalePanel(AlignViewport av, AlignmentPanel ap)
76 addMouseListener(this);
77 addMouseMotionListener(this);
87 public void mousePressed(MouseEvent evt)
89 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
92 if (av.hasHiddenColumns())
94 x = av.getColumnSelection().adjustForHiddenColumns(x);
97 if (x >= av.getAlignment().getWidth())
99 res = av.getAlignment().getWidth() - 1;
109 if (evt.isPopupTrigger())
111 rightMouseButtonPressed(evt, res);
115 leftMouseButtonPressed(evt, res);
120 * Handles right mouse button press. If pressed in a selected column, opens
121 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
122 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
127 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
129 JPopupMenu pop = new JPopupMenu();
132 JMenuItem item = new JMenuItem(
133 MessageManager.getString("label.reveal"));
134 item.addActionListener(new ActionListener()
137 public void actionPerformed(ActionEvent e)
139 av.showColumn(reveal[0]);
141 ap.paintAlignment(true);
142 if (ap.overviewPanel != null)
144 ap.overviewPanel.updateOverviewImage();
151 if (av.getColumnSelection().hasHiddenColumns())
153 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
154 item.addActionListener(new ActionListener()
157 public void actionPerformed(ActionEvent e)
159 av.showAllHiddenColumns();
161 ap.paintAlignment(true);
162 if (ap.overviewPanel != null)
164 ap.overviewPanel.updateOverviewImage();
171 pop.show(this, evt.getX(), evt.getY());
173 else if (av.getColumnSelection().contains(res))
175 JMenuItem item = new JMenuItem(
176 MessageManager.getString("label.hide_columns"));
177 item.addActionListener(new ActionListener()
180 public void actionPerformed(ActionEvent e)
182 av.hideColumns(res, res);
183 if (av.getSelectionGroup() != null
184 && av.getSelectionGroup().getSize() == av.getAlignment()
187 av.setSelectionGroup(null);
190 ap.paintAlignment(true);
191 if (ap.overviewPanel != null)
193 ap.overviewPanel.updateOverviewImage();
199 pop.show(this, evt.getX(), evt.getY());
204 * Handles left mouse button press
209 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
211 if (!evt.isControlDown() && !evt.isShiftDown())
213 av.getColumnSelection().clear();
216 av.getColumnSelection().addElement(res);
217 SequenceGroup sg = new SequenceGroup();
218 // try to be as quick as possible
219 SequenceI[] iVec = av.getAlignment().getSequencesArray();
220 for (int i = 0; i < iVec.length; i++)
222 sg.addSequence(iVec[i], false);
229 if (evt.isShiftDown())
231 int min = Math.min(av.getColumnSelection().getMin(), res);
232 int max = Math.max(av.getColumnSelection().getMax(), res);
233 for (int i = min; i < max; i++)
235 av.getColumnSelection().addElement(i);
240 av.setSelectionGroup(sg);
241 ap.paintAlignment(false);
252 public void mouseReleased(MouseEvent evt)
254 mouseDragging = false;
256 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
258 if (av.hasHiddenColumns())
260 res = av.getColumnSelection().adjustForHiddenColumns(res);
263 if (res >= av.getAlignment().getWidth())
265 res = av.getAlignment().getWidth() - 1;
268 if (!stretchingGroup)
270 ap.paintAlignment(false);
275 SequenceGroup sg = av.getSelectionGroup();
279 if (res > sg.getStartRes())
283 else if (res < sg.getStartRes())
288 stretchingGroup = false;
289 ap.paintAlignment(false);
300 public void mouseDragged(MouseEvent evt)
302 mouseDragging = true;
304 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
310 if (av.hasHiddenColumns())
312 res = av.getColumnSelection().adjustForHiddenColumns(res);
315 if (res >= av.getAlignment().getWidth())
317 res = av.getAlignment().getWidth() - 1;
330 SequenceGroup sg = av.getSelectionGroup();
334 stretchingGroup = true;
336 if (!av.getColumnSelection().contains(res))
338 av.getColumnSelection().addElement(res);
341 if (res > sg.getStartRes())
345 if (res < sg.getStartRes())
351 for (int i = min; i <= max; i++)
353 col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
355 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
357 av.getColumnSelection().removeElement(col);
361 av.getColumnSelection().addElement(col);
365 ap.paintAlignment(false);
370 public void mouseEntered(MouseEvent evt)
374 ap.getSeqPanel().scrollCanvas(null);
379 public void mouseExited(MouseEvent evt)
383 ap.getSeqPanel().scrollCanvas(evt);
388 public void mouseClicked(MouseEvent evt)
393 public void mouseMoved(MouseEvent evt)
395 if (!av.hasHiddenColumns())
400 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
402 res = av.getColumnSelection().adjustForHiddenColumns(res);
405 if (av.getColumnSelection().getHiddenColumns() != null)
407 for (int[] region : av.getColumnSelection().getHiddenColumns())
409 if (res + 1 == region[0] || res - 1 == region[1])
412 ToolTipManager.sharedInstance().registerComponent(this);
413 this.setToolTipText(MessageManager
414 .getString("label.reveal_hidden_columns"));
419 this.setToolTipText(null);
435 public void paintComponent(Graphics g)
437 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
440 // scalewidth will normally be screenwidth,
441 public void drawScale(Graphics g, int startx, int endx, int width,
444 Graphics2D gg = (Graphics2D) g;
445 gg.setFont(av.getFont());
449 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
450 RenderingHints.VALUE_ANTIALIAS_ON);
453 // Fill in the background
454 gg.setColor(Color.white);
455 gg.fillRect(0, 0, width, height);
456 gg.setColor(Color.black);
458 // Fill the selected columns
459 ColumnSelection cs = av.getColumnSelection();
460 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
465 gg.setColor(new Color(220, 0, 0));
467 for (int sel : cs.getSelected())
469 // TODO: JAL-2001 - provide a fast method to list visible selected in a
472 if (av.hasHiddenColumns())
474 if (cs.isVisible(sel))
476 sel = cs.findColumnPosition(sel);
484 if ((sel >= startx) && (sel <= endx))
486 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
491 // Draw the scale numbers
492 gg.setColor(Color.black);
494 int scalestartx = (startx / 10) * 10;
495 int widthx = 1 + endx - startx;
497 FontMetrics fm = gg.getFontMetrics(av.getFont());
498 int y = avCharHeight - fm.getDescent();
500 if ((scalestartx % 10) == 0)
506 boolean isrgap = false;
508 int maxX = 0, refN, iadj;
509 SequenceI refSeq = av.getAlignment().getSeqrep();
510 int refSp = -1, refEp = -1;
513 refSp = refSeq.findIndex(refSeq.getStart()) - 1;
514 refEp = refSeq.findIndex(refSeq.getEnd()) - 1;
516 // todo: add a 'reference origin column' to set column number relative to
517 for (int i = scalestartx; i < endx; i += 5)
521 iadj = av.getColumnSelection().adjustForHiddenColumns(i) - 1;
524 string = String.valueOf(iadj + 1);
528 refN = refSeq.findPosition(iadj);
529 // TODO show bounds if position is a gap
530 // - ie L--R -> "1L|2R" for
534 string = String.valueOf(iadj - refSp);
536 else if (iadj > refEp)
538 string = "+" + String.valueOf(iadj - refEp);
542 string = String.valueOf(refN) + refSeq.getCharAt(iadj);
545 if ((i - startx - 1) * avCharWidth > maxX)
547 gg.drawString(string, (i - startx - 1) * avCharWidth, y);
548 maxX = (i - startx + 1) * avCharWidth + fm.stringWidth(string);
551 gg.drawLine(((i - startx - 1) * avCharWidth) + (avCharWidth / 2),
553 ((i - startx - 1) * avCharWidth) + (avCharWidth / 2), y
554 + (fm.getDescent() * 2));
558 gg.drawLine(((i - startx - 1) * avCharWidth) + (avCharWidth / 2), y
559 + fm.getDescent(), ((i - startx - 1) * avCharWidth)
560 + (avCharWidth / 2), y + (fm.getDescent() * 2));
564 if (av.hasHiddenColumns())
566 gg.setColor(Color.blue);
568 if (av.getShowHiddenMarkers()
569 && av.getColumnSelection().getHiddenColumns() != null)
571 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
574 res = av.getColumnSelection().findHiddenRegionPosition(i)
577 if (res < 0 || res > widthx)
582 gg.fillPolygon(new int[] { res * avCharWidth - avCharHeight / 4,
583 res * avCharWidth + avCharHeight / 4, res * avCharWidth },
584 new int[] { y - avCharHeight / 2, y - avCharHeight / 2,
590 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
592 gg.drawString(MessageManager.getString("label.reveal_columns"),
593 reveal[0] * avCharWidth, 0);