2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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.alignment.getWidth())
80 res = av.alignment.getWidth() - 1;
88 if (SwingUtilities.isRightMouseButton(evt))
90 JPopupMenu pop = new JPopupMenu();
93 JMenuItem item = new JMenuItem("Reveal");
94 item.addActionListener(new ActionListener()
96 public void actionPerformed(ActionEvent e)
98 av.showColumn(reveal[0]);
100 ap.paintAlignment(true);
101 if (ap.overviewPanel != null)
103 ap.overviewPanel.updateOverviewImage();
109 if (av.getColumnSelection().getHiddenColumns().size() > 1)
111 item = new JMenuItem("Reveal All");
112 item.addActionListener(new ActionListener()
114 public void actionPerformed(ActionEvent e)
116 av.showAllHiddenColumns();
118 ap.paintAlignment(true);
119 if (ap.overviewPanel != null)
121 ap.overviewPanel.updateOverviewImage();
127 pop.show(this, evt.getX(), evt.getY());
129 else if (av.getColumnSelection().contains(res))
131 JMenuItem item = new JMenuItem("Hide Columns");
132 item.addActionListener(new ActionListener()
134 public void actionPerformed(ActionEvent e)
136 av.hideColumns(res, res);
137 if (av.getSelectionGroup() != null
138 && av.getSelectionGroup().getSize() == av.alignment
141 av.setSelectionGroup(null);
144 ap.paintAlignment(true);
145 if (ap.overviewPanel != null)
147 ap.overviewPanel.updateOverviewImage();
152 pop.show(this, evt.getX(), evt.getY());
156 // LEFT MOUSE TO SELECT
158 if (!evt.isControlDown() && !evt.isShiftDown())
160 av.getColumnSelection().clear();
163 av.getColumnSelection().addElement(res);
164 SequenceGroup sg = new SequenceGroup();
165 // try to be as quick as possible
166 SequenceI[] iVec = av.alignment.getSequencesArray();
167 for (int i = 0; i < iVec.length; i++)
169 sg.addSequence(iVec[i], false);
176 if (evt.isShiftDown())
178 int min = Math.min(av.getColumnSelection().getMin(), res);
179 int max = Math.max(av.getColumnSelection().getMax(), res);
180 for (int i = min; i < max; i++)
182 av.getColumnSelection().addElement(i);
187 av.setSelectionGroup(sg);
190 ap.paintAlignment(false);
200 public void mouseReleased(MouseEvent evt)
202 mouseDragging = false;
204 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
206 if (av.hasHiddenColumns)
208 res = av.getColumnSelection().adjustForHiddenColumns(res);
211 if (res >= av.alignment.getWidth())
213 res = av.alignment.getWidth() - 1;
216 if (!stretchingGroup)
218 ap.paintAlignment(false);
223 SequenceGroup sg = av.getSelectionGroup();
227 if (res > sg.getStartRes())
231 else if (res < sg.getStartRes())
236 stretchingGroup = false;
237 ap.paintAlignment(false);
247 public void mouseDragged(MouseEvent evt)
249 mouseDragging = true;
251 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
257 if (av.hasHiddenColumns)
259 res = av.getColumnSelection().adjustForHiddenColumns(res);
262 if (res >= av.alignment.getWidth())
264 res = av.alignment.getWidth() - 1;
277 SequenceGroup sg = av.getSelectionGroup();
281 stretchingGroup = true;
283 if (!av.getColumnSelection().contains(res))
285 av.getColumnSelection().addElement(res);
288 if (res > sg.getStartRes())
292 if (res < sg.getStartRes())
298 for (int i = min; i <= max; i++)
300 col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
302 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
304 av.getColumnSelection().removeElement(col);
308 av.getColumnSelection().addElement(col);
312 ap.paintAlignment(false);
316 public void mouseEntered(MouseEvent evt)
320 ap.seqPanel.scrollCanvas(null);
324 public void mouseExited(MouseEvent evt)
328 ap.seqPanel.scrollCanvas(evt);
332 public void mouseClicked(MouseEvent evt)
336 public void mouseMoved(MouseEvent evt)
338 if (!av.hasHiddenColumns)
343 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
345 res = av.getColumnSelection().adjustForHiddenColumns(res);
348 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
350 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
352 if (res + 1 == region[0] || res - 1 == region[1])
355 ToolTipManager.sharedInstance().registerComponent(this);
357 .setToolTipText("Reveal Hidden Columns with Right Mouse Button");
362 this.setToolTipText(null);
378 public void paintComponent(Graphics g)
380 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
383 // scalewidth will normally be screenwidth,
384 public void drawScale(Graphics g, int startx, int endx, int width,
387 Graphics2D gg = (Graphics2D) g;
388 gg.setFont(av.getFont());
392 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
393 RenderingHints.VALUE_ANTIALIAS_ON);
396 // Fill in the background
397 gg.setColor(Color.white);
398 gg.fillRect(0, 0, width, height);
399 gg.setColor(Color.black);
401 // Fill the selected columns
402 ColumnSelection cs = av.getColumnSelection();
406 gg.setColor(new Color(220, 0, 0));
408 for (int i = 0; i < cs.size(); i++)
410 int sel = cs.columnAt(i);
411 if (av.hasHiddenColumns)
413 if (cs.isVisible(sel))
415 sel = cs.findColumnPosition(sel);
423 if ((sel >= startx) && (sel <= endx))
425 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
430 // Draw the scale numbers
431 gg.setColor(Color.black);
433 int scalestartx = (startx / 10) * 10;
435 FontMetrics fm = gg.getFontMetrics(av.getFont());
436 int y = av.charHeight - fm.getDescent();
438 if ((scalestartx % 10) == 0)
446 for (int i = scalestartx; i < endx; i += 5)
450 string = String.valueOf(av.getColumnSelection()
451 .adjustForHiddenColumns(i));
452 if ((i - startx - 1) * av.charWidth > maxX)
454 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
455 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));
470 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
472 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
473 y + (fm.getDescent() * 2));
477 if (av.hasHiddenColumns)
479 gg.setColor(Color.blue);
481 if (av.getShowHiddenMarkers())
483 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
487 res = av.getColumnSelection().findHiddenRegionPosition(i)
490 if (res < 0 || res > endx - scalestartx)
495 gg.fillPolygon(new int[]
496 { res * av.charWidth - av.charHeight / 4,
497 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
499 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
505 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
507 gg.drawString("Reveal Columns", reveal[0] * av.charWidth, 0);