2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 import java.awt.event.*;
24 import jalview.datamodel.*;
32 public class ScalePanel extends JPanel implements MouseMotionListener,
35 protected int offy = 4;
40 protected AlignViewport av;
44 boolean stretchingGroup = false;
46 int min; // used by mouseDragged to see if user
48 int max; // used by mouseDragged to see if user
50 boolean mouseDragging = false;
52 // wants to delete columns
53 public ScalePanel(AlignViewport av, AlignmentPanel ap)
58 addMouseListener(this);
59 addMouseMotionListener(this);
68 public void mousePressed(MouseEvent evt)
70 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
73 if (av.hasHiddenColumns())
75 x = av.getColumnSelection().adjustForHiddenColumns(x);
78 if (x >= av.getAlignment().getWidth())
80 res = av.getAlignment().getWidth() - 1;
90 if (SwingUtilities.isRightMouseButton(evt))
92 JPopupMenu pop = new JPopupMenu();
95 JMenuItem item = new JMenuItem("Reveal");
96 item.addActionListener(new ActionListener()
98 public void actionPerformed(ActionEvent e)
100 av.showColumn(reveal[0]);
102 ap.paintAlignment(true);
103 if (ap.overviewPanel != null)
105 ap.overviewPanel.updateOverviewImage();
111 if (av.getColumnSelection().getHiddenColumns().size() > 1)
113 item = new JMenuItem("Reveal All");
114 item.addActionListener(new ActionListener()
116 public void actionPerformed(ActionEvent e)
118 av.showAllHiddenColumns();
120 ap.paintAlignment(true);
121 if (ap.overviewPanel != null)
123 ap.overviewPanel.updateOverviewImage();
129 pop.show(this, evt.getX(), evt.getY());
131 else if (av.getColumnSelection().contains(res))
133 JMenuItem item = new JMenuItem("Hide Columns");
134 item.addActionListener(new ActionListener()
136 public void actionPerformed(ActionEvent e)
138 av.hideColumns(res, res);
139 if (av.getSelectionGroup() != null
140 && av.getSelectionGroup().getSize() == av.getAlignment()
143 av.setSelectionGroup(null);
146 ap.paintAlignment(true);
147 if (ap.overviewPanel != null)
149 ap.overviewPanel.updateOverviewImage();
154 pop.show(this, evt.getX(), evt.getY());
158 // LEFT MOUSE TO SELECT
160 if (!evt.isControlDown() && !evt.isShiftDown())
162 av.getColumnSelection().clear();
165 av.getColumnSelection().addElement(res);
166 SequenceGroup sg = new SequenceGroup();
167 // try to be as quick as possible
168 SequenceI[] iVec = av.getAlignment().getSequencesArray();
169 for (int i = 0; i < iVec.length; i++)
171 sg.addSequence(iVec[i], false);
178 if (evt.isShiftDown())
180 int min = Math.min(av.getColumnSelection().getMin(), res);
181 int max = Math.max(av.getColumnSelection().getMax(), res);
182 for (int i = min; i < max; i++)
184 av.getColumnSelection().addElement(i);
189 av.setSelectionGroup(sg);
192 ap.paintAlignment(false);
202 public void mouseReleased(MouseEvent evt)
204 mouseDragging = false;
206 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
208 if (av.hasHiddenColumns())
210 res = av.getColumnSelection().adjustForHiddenColumns(res);
213 if (res >= av.getAlignment().getWidth())
215 res = av.getAlignment().getWidth() - 1;
218 if (!stretchingGroup)
220 ap.paintAlignment(false);
225 SequenceGroup sg = av.getSelectionGroup();
229 if (res > sg.getStartRes())
233 else if (res < sg.getStartRes())
238 stretchingGroup = false;
239 ap.paintAlignment(false);
249 public void mouseDragged(MouseEvent evt)
251 mouseDragging = true;
253 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
259 if (av.hasHiddenColumns())
261 res = av.getColumnSelection().adjustForHiddenColumns(res);
264 if (res >= av.getAlignment().getWidth())
266 res = av.getAlignment().getWidth() - 1;
279 SequenceGroup sg = av.getSelectionGroup();
283 stretchingGroup = true;
285 if (!av.getColumnSelection().contains(res))
287 av.getColumnSelection().addElement(res);
290 if (res > sg.getStartRes())
294 if (res < sg.getStartRes())
300 for (int i = min; i <= max; i++)
302 col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
304 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
306 av.getColumnSelection().removeElement(col);
310 av.getColumnSelection().addElement(col);
314 ap.paintAlignment(false);
318 public void mouseEntered(MouseEvent evt)
322 ap.seqPanel.scrollCanvas(null);
326 public void mouseExited(MouseEvent evt)
330 ap.seqPanel.scrollCanvas(evt);
334 public void mouseClicked(MouseEvent evt)
338 public void mouseMoved(MouseEvent evt)
340 if (!av.hasHiddenColumns())
345 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
347 res = av.getColumnSelection().adjustForHiddenColumns(res);
350 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
352 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
354 if (res + 1 == region[0] || res - 1 == region[1])
357 ToolTipManager.sharedInstance().registerComponent(this);
358 this.setToolTipText("Reveal Hidden Columns with Right Mouse Button");
363 this.setToolTipText(null);
379 public void paintComponent(Graphics g)
381 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
384 // scalewidth will normally be screenwidth,
385 public void drawScale(Graphics g, int startx, int endx, int width,
388 Graphics2D gg = (Graphics2D) g;
389 gg.setFont(av.getFont());
393 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
394 RenderingHints.VALUE_ANTIALIAS_ON);
397 // Fill in the background
398 gg.setColor(Color.white);
399 gg.fillRect(0, 0, width, height);
400 gg.setColor(Color.black);
402 // Fill the selected columns
403 ColumnSelection cs = av.getColumnSelection();
407 gg.setColor(new Color(220, 0, 0));
409 for (int i = 0; i < cs.size(); i++)
411 int sel = cs.columnAt(i);
412 if (av.hasHiddenColumns())
414 if (cs.isVisible(sel))
416 sel = cs.findColumnPosition(sel);
424 if ((sel >= startx) && (sel <= endx))
426 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
431 // Draw the scale numbers
432 gg.setColor(Color.black);
434 int scalestartx = (startx / 10) * 10;
436 FontMetrics fm = gg.getFontMetrics(av.getFont());
437 int y = av.charHeight - fm.getDescent();
439 if ((scalestartx % 10) == 0)
447 for (int i = scalestartx; i < endx; i += 5)
451 string = String.valueOf(av.getColumnSelection()
452 .adjustForHiddenColumns(i));
453 if ((i - startx - 1) * av.charWidth > maxX)
455 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
456 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
460 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
462 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
463 y + (fm.getDescent() * 2));
469 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
471 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
472 y + (fm.getDescent() * 2));
476 if (av.hasHiddenColumns())
478 gg.setColor(Color.blue);
480 if (av.getShowHiddenMarkers())
482 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
486 res = av.getColumnSelection().findHiddenRegionPosition(i)
489 if (res < 0 || res > endx - scalestartx)
494 gg.fillPolygon(new int[]
495 { res * av.charWidth - av.charHeight / 4,
496 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
498 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
504 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
506 gg.drawString("Reveal Columns", reveal[0] * av.charWidth, 0);