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 res = av.getColumnSelection().adjustForHiddenColumns(x);
85 if (SwingUtilities.isRightMouseButton(evt))
87 JPopupMenu pop = new JPopupMenu();
90 JMenuItem item = new JMenuItem("Reveal");
91 item.addActionListener(new ActionListener()
93 public void actionPerformed(ActionEvent e)
95 av.showColumn(reveal[0]);
97 ap.paintAlignment(true);
98 if (ap.overviewPanel != null)
100 ap.overviewPanel.updateOverviewImage();
106 if (av.getColumnSelection().getHiddenColumns().size() > 1)
108 item = new JMenuItem("Reveal All");
109 item.addActionListener(new ActionListener()
111 public void actionPerformed(ActionEvent e)
113 av.showAllHiddenColumns();
115 ap.paintAlignment(true);
116 if (ap.overviewPanel != null)
118 ap.overviewPanel.updateOverviewImage();
124 pop.show(this, evt.getX(), evt.getY());
126 else if (av.getColumnSelection().contains(res))
128 JMenuItem item = new JMenuItem("Hide Columns");
129 item.addActionListener(new ActionListener()
131 public void actionPerformed(ActionEvent e)
133 av.hideColumns(res, res);
134 if (av.getSelectionGroup() != null
135 && av.getSelectionGroup().getSize() == av.alignment
138 av.setSelectionGroup(null);
141 ap.paintAlignment(true);
142 if (ap.overviewPanel != null)
144 ap.overviewPanel.updateOverviewImage();
149 pop.show(this, evt.getX(), evt.getY());
153 // LEFT MOUSE TO SELECT
155 if (!evt.isControlDown() && !evt.isShiftDown())
157 av.getColumnSelection().clear();
160 av.getColumnSelection().addElement(res);
161 SequenceGroup sg = new SequenceGroup();
162 // try to be as quick as possible
163 SequenceI[] iVec = av.alignment.getSequencesArray();
164 for (int i = 0; i < iVec.length; i++)
166 sg.addSequence(iVec[i], false);
172 av.setSelectionGroup(sg);
174 if (evt.isShiftDown())
176 int min = Math.min(av.getColumnSelection().getMin(), res);
177 int max = Math.max(av.getColumnSelection().getMax(), res);
178 for (int i = min; i < max; i++)
180 av.getColumnSelection().addElement(i);
188 ap.paintAlignment(false);
198 public void mouseReleased(MouseEvent evt)
200 mouseDragging = false;
202 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
204 if (res > av.alignment.getWidth())
206 res = av.alignment.getWidth() - 1;
209 if (av.hasHiddenColumns)
211 res = av.getColumnSelection().adjustForHiddenColumns(res);
214 if (!stretchingGroup)
216 ap.paintAlignment(false);
221 SequenceGroup sg = av.getSelectionGroup();
225 if (res > sg.getStartRes())
229 else if (res < sg.getStartRes())
234 stretchingGroup = false;
235 ap.paintAlignment(false);
245 public void mouseDragged(MouseEvent evt)
247 mouseDragging = true;
249 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
255 if (av.hasHiddenColumns)
257 res = av.getColumnSelection().adjustForHiddenColumns(res);
260 if (res > av.alignment.getWidth())
262 res = av.alignment.getWidth() - 1;
275 SequenceGroup sg = av.getSelectionGroup();
279 stretchingGroup = true;
281 if (!av.getColumnSelection().contains(res))
283 av.getColumnSelection().addElement(res);
286 if (res > sg.getStartRes())
290 if (res < sg.getStartRes())
296 for (int i = min; i <= max; i++)
298 col = av.getColumnSelection().adjustForHiddenColumns(i);
300 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
302 av.getColumnSelection().removeElement(col);
306 av.getColumnSelection().addElement(col);
310 ap.paintAlignment(false);
314 public void mouseEntered(MouseEvent evt)
318 ap.seqPanel.scrollCanvas(null);
322 public void mouseExited(MouseEvent evt)
326 ap.seqPanel.scrollCanvas(evt);
330 public void mouseClicked(MouseEvent evt)
334 public void mouseMoved(MouseEvent evt)
336 if (!av.hasHiddenColumns)
341 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
343 res = av.getColumnSelection().adjustForHiddenColumns(res);
346 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
348 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
350 if (res + 1 == region[0] || res - 1 == region[1])
353 ToolTipManager.sharedInstance().registerComponent(this);
355 .setToolTipText("Reveal Hidden Columns with Right Mouse Button");
360 this.setToolTipText(null);
376 public void paintComponent(Graphics g)
378 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
381 // scalewidth will normally be screenwidth,
382 public void drawScale(Graphics g, int startx, int endx, int width,
385 Graphics2D gg = (Graphics2D) g;
386 gg.setFont(av.getFont());
390 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
391 RenderingHints.VALUE_ANTIALIAS_ON);
394 // Fill in the background
395 gg.setColor(Color.white);
396 gg.fillRect(0, 0, width, height);
397 gg.setColor(Color.black);
399 // Fill the selected columns
400 ColumnSelection cs = av.getColumnSelection();
403 gg.setColor(new Color(220, 0, 0));
405 for (int i = 0; i < cs.size(); i++)
407 int sel = cs.columnAt(i);
408 if (av.hasHiddenColumns)
410 sel = cs.findColumnPosition(sel);
413 if ((sel >= startx) && (sel <= endx))
415 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
420 // Draw the scale numbers
421 gg.setColor(Color.black);
423 int scalestartx = (startx / 10) * 10;
425 FontMetrics fm = gg.getFontMetrics(av.getFont());
426 int y = av.charHeight - fm.getDescent();
428 if ((scalestartx % 10) == 0)
436 for (int i = scalestartx; i < endx; i += 5)
440 string = String.valueOf(av.getColumnSelection()
441 .adjustForHiddenColumns(i));
442 if ((i - startx - 1) * av.charWidth > maxX)
444 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
445 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
450 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
452 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
453 y + (fm.getDescent() * 2));
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));
467 if (av.hasHiddenColumns)
469 gg.setColor(Color.blue);
471 if (av.getShowHiddenMarkers())
473 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
477 res = av.getColumnSelection().findHiddenRegionPosition(i)
480 if (res < 0 || res > endx - scalestartx)
485 gg.fillPolygon(new int[]
486 { res * av.charWidth - av.charHeight / 4,
487 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
489 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
495 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
497 gg.drawString("Reveal Columns", reveal[0] * av.charWidth, 0);