2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.SwingUtilities;
43 import javax.swing.ToolTipManager;
51 public class ScalePanel extends JPanel implements MouseMotionListener,
54 protected int offy = 4;
59 protected AlignViewport av;
63 boolean stretchingGroup = false;
65 int min; // used by mouseDragged to see if user
67 int max; // used by mouseDragged to see if user
69 boolean mouseDragging = false;
71 // wants to delete columns
72 public ScalePanel(AlignViewport av, AlignmentPanel ap)
77 addMouseListener(this);
78 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 (SwingUtilities.isRightMouseButton(evt))
111 JPopupMenu pop = new JPopupMenu();
114 JMenuItem item = new JMenuItem(
115 MessageManager.getString("label.reveal"));
116 item.addActionListener(new ActionListener()
118 public void actionPerformed(ActionEvent e)
120 av.showColumn(reveal[0]);
122 ap.paintAlignment(true);
123 if (ap.overviewPanel != null)
125 ap.overviewPanel.updateOverviewImage();
131 if (av.getColumnSelection().getHiddenColumns().size() > 1)
133 item = new JMenuItem(
134 MessageManager.getString("action.reveal_all"));
135 item.addActionListener(new ActionListener()
137 public void actionPerformed(ActionEvent e)
139 av.showAllHiddenColumns();
141 ap.paintAlignment(true);
142 if (ap.overviewPanel != null)
144 ap.overviewPanel.updateOverviewImage();
150 pop.show(this, evt.getX(), evt.getY());
152 else if (av.getColumnSelection().contains(res))
154 JMenuItem item = new JMenuItem(
155 MessageManager.getString("label.hide_columns"));
156 item.addActionListener(new ActionListener()
158 public void actionPerformed(ActionEvent e)
160 av.hideColumns(res, res);
161 if (av.getSelectionGroup() != null
162 && av.getSelectionGroup().getSize() == av
163 .getAlignment().getHeight())
165 av.setSelectionGroup(null);
168 ap.paintAlignment(true);
169 if (ap.overviewPanel != null)
171 ap.overviewPanel.updateOverviewImage();
176 pop.show(this, evt.getX(), evt.getY());
180 // LEFT MOUSE TO SELECT
182 if (!evt.isControlDown() && !evt.isShiftDown())
184 av.getColumnSelection().clear();
187 av.getColumnSelection().addElement(res);
188 SequenceGroup sg = new SequenceGroup();
189 // try to be as quick as possible
190 SequenceI[] iVec = av.getAlignment().getSequencesArray();
191 for (int i = 0; i < iVec.length; i++)
193 sg.addSequence(iVec[i], false);
200 if (evt.isShiftDown())
202 int min = Math.min(av.getColumnSelection().getMin(), res);
203 int max = Math.max(av.getColumnSelection().getMax(), res);
204 for (int i = min; i < max; i++)
206 av.getColumnSelection().addElement(i);
211 av.setSelectionGroup(sg);
214 ap.paintAlignment(false);
224 public void mouseReleased(MouseEvent evt)
226 mouseDragging = false;
228 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
230 if (av.hasHiddenColumns())
232 res = av.getColumnSelection().adjustForHiddenColumns(res);
235 if (res >= av.getAlignment().getWidth())
237 res = av.getAlignment().getWidth() - 1;
240 if (!stretchingGroup)
242 ap.paintAlignment(false);
247 SequenceGroup sg = av.getSelectionGroup();
251 if (res > sg.getStartRes())
255 else if (res < sg.getStartRes())
260 stretchingGroup = false;
261 ap.paintAlignment(false);
271 public void mouseDragged(MouseEvent evt)
273 mouseDragging = true;
275 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
281 if (av.hasHiddenColumns())
283 res = av.getColumnSelection().adjustForHiddenColumns(res);
286 if (res >= av.getAlignment().getWidth())
288 res = av.getAlignment().getWidth() - 1;
301 SequenceGroup sg = av.getSelectionGroup();
305 stretchingGroup = true;
307 if (!av.getColumnSelection().contains(res))
309 av.getColumnSelection().addElement(res);
312 if (res > sg.getStartRes())
316 if (res < sg.getStartRes())
322 for (int i = min; i <= max; i++)
324 col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
326 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
328 av.getColumnSelection().removeElement(col);
332 av.getColumnSelection().addElement(col);
336 ap.paintAlignment(false);
340 public void mouseEntered(MouseEvent evt)
344 ap.getSeqPanel().scrollCanvas(null);
348 public void mouseExited(MouseEvent evt)
352 ap.getSeqPanel().scrollCanvas(evt);
356 public void mouseClicked(MouseEvent evt)
360 public void mouseMoved(MouseEvent evt)
362 if (!av.hasHiddenColumns())
367 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
369 res = av.getColumnSelection().adjustForHiddenColumns(res);
372 if (av.getColumnSelection().getHiddenColumns() != null)
374 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
376 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
378 if (res + 1 == region[0] || res - 1 == region[1])
381 ToolTipManager.sharedInstance().registerComponent(this);
382 this.setToolTipText(MessageManager
383 .getString("label.reveal_hidden_columns"));
388 this.setToolTipText(null);
404 public void paintComponent(Graphics g)
406 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
409 // scalewidth will normally be screenwidth,
410 public void drawScale(Graphics g, int startx, int endx, int width,
413 Graphics2D gg = (Graphics2D) g;
414 gg.setFont(av.getFont());
418 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
419 RenderingHints.VALUE_ANTIALIAS_ON);
422 // Fill in the background
423 gg.setColor(Color.white);
424 gg.fillRect(0, 0, width, height);
425 gg.setColor(Color.black);
427 // Fill the selected columns
428 ColumnSelection cs = av.getColumnSelection();
432 gg.setColor(new Color(220, 0, 0));
434 for (int i = 0; i < cs.size(); i++)
436 int sel = cs.columnAt(i);
437 if (av.hasHiddenColumns())
439 if (cs.isVisible(sel))
441 sel = cs.findColumnPosition(sel);
449 if ((sel >= startx) && (sel <= endx))
451 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
456 // Draw the scale numbers
457 gg.setColor(Color.black);
459 int scalestartx = (startx / 10) * 10;
461 FontMetrics fm = gg.getFontMetrics(av.getFont());
462 int y = av.charHeight - fm.getDescent();
464 if ((scalestartx % 10) == 0)
472 for (int i = scalestartx; i < endx; i += 5)
476 string = String.valueOf(av.getColumnSelection()
477 .adjustForHiddenColumns(i));
478 if ((i - startx - 1) * av.charWidth > maxX)
480 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
481 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
485 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
487 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
488 y + (fm.getDescent() * 2));
494 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
496 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
497 y + (fm.getDescent() * 2));
501 if (av.hasHiddenColumns())
503 gg.setColor(Color.blue);
505 if (av.getShowHiddenMarkers()
506 && av.getColumnSelection().getHiddenColumns() != null)
508 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
512 res = av.getColumnSelection().findHiddenRegionPosition(i)
515 if (res < 0 || res > endx - scalestartx)
520 gg.fillPolygon(new int[]
521 { res * av.charWidth - av.charHeight / 4,
522 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
524 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
530 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
532 gg.drawString(MessageManager.getString("label.reveal_columns"),
533 reveal[0] * av.charWidth, 0);