2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 import java.awt.event.*;
25 import jalview.datamodel.*;
33 public class ScalePanel
34 extends JPanel implements MouseMotionListener, MouseListener
36 protected int offy = 4;
40 protected AlignViewport av;
42 boolean stretchingGroup = false;
43 int min; //used by mouseDragged to see if user
44 int max; //used by mouseDragged to see if user
45 boolean mouseDragging = false;
47 // wants to delete columns
48 public ScalePanel(AlignViewport av, AlignmentPanel ap)
53 addMouseListener(this);
54 addMouseMotionListener(this);
60 * @param evt DOCUMENT ME!
62 public void mousePressed(MouseEvent evt)
64 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
67 if (av.hasHiddenColumns)
69 res = av.getColumnSelection().adjustForHiddenColumns(x);
79 if (SwingUtilities.isRightMouseButton(evt))
81 JPopupMenu pop = new JPopupMenu();
84 JMenuItem item = new JMenuItem("Reveal");
85 item.addActionListener(new ActionListener()
87 public void actionPerformed(ActionEvent e)
89 av.showColumn(reveal[0]);
91 ap.paintAlignment(true);
92 if (ap.overviewPanel != null)
94 ap.overviewPanel.updateOverviewImage();
100 if (av.getColumnSelection().getHiddenColumns().size() > 1)
102 item = new JMenuItem("Reveal All");
103 item.addActionListener(new ActionListener()
105 public void actionPerformed(ActionEvent e)
107 av.showAllHiddenColumns();
109 ap.paintAlignment(true);
110 if (ap.overviewPanel != null)
112 ap.overviewPanel.updateOverviewImage();
118 pop.show(this, evt.getX(), evt.getY());
120 else if (av.getColumnSelection().contains(res))
122 JMenuItem item = new JMenuItem("Hide Columns");
123 item.addActionListener(new ActionListener()
125 public void actionPerformed(ActionEvent e)
127 av.hideColumns(res, res);
128 if (av.getSelectionGroup() != null
129 && av.getSelectionGroup().getSize() == av.alignment.getHeight())
131 av.setSelectionGroup(null);
134 ap.paintAlignment(true);
135 if (ap.overviewPanel != null)
137 ap.overviewPanel.updateOverviewImage();
142 pop.show(this, evt.getX(), evt.getY());
145 else // LEFT MOUSE TO SELECT
147 if (!evt.isControlDown() && !evt.isShiftDown())
149 av.getColumnSelection().clear();
152 av.getColumnSelection().addElement(res);
153 SequenceGroup sg = new SequenceGroup();
154 // try to be as quick as possible
155 SequenceI[] iVec = av.alignment.getSequencesArray();
156 for (int i = 0; i < iVec.length; i++)
158 sg.addSequence(iVec[i], false);
164 av.setSelectionGroup(sg);
166 if (evt.isShiftDown())
168 int min = Math.min(av.getColumnSelection().getMin(), res);
169 int max = Math.max(av.getColumnSelection().getMax(), res);
170 for (int i = min; i < max; i++)
172 av.getColumnSelection().addElement(i);
180 ap.paintAlignment(false);
186 * @param evt DOCUMENT ME!
188 public void mouseReleased(MouseEvent evt)
190 mouseDragging = false;
192 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
194 if (res > av.alignment.getWidth())
196 res = av.alignment.getWidth() - 1;
199 if (av.hasHiddenColumns)
201 res = av.getColumnSelection().adjustForHiddenColumns(res);
204 if (!stretchingGroup)
206 ap.paintAlignment(false);
211 SequenceGroup sg = av.getSelectionGroup();
215 if (res > sg.getStartRes())
219 else if (res < sg.getStartRes())
224 stretchingGroup = false;
225 ap.paintAlignment(false);
231 * @param evt DOCUMENT ME!
233 public void mouseDragged(MouseEvent evt)
235 mouseDragging = true;
237 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
243 if (av.hasHiddenColumns)
245 res = av.getColumnSelection().adjustForHiddenColumns(res);
248 if (res > av.alignment.getWidth())
250 res = av.alignment.getWidth() - 1;
263 SequenceGroup sg = av.getSelectionGroup();
267 stretchingGroup = true;
269 if (!av.getColumnSelection().contains(res))
271 av.getColumnSelection().addElement(res);
274 if (res > sg.getStartRes())
278 if (res < sg.getStartRes())
284 for (int i = min; i <= max; i++)
286 col = av.getColumnSelection().adjustForHiddenColumns(i);
288 if ( (col < sg.getStartRes()) || (col > sg.getEndRes()))
290 av.getColumnSelection().removeElement(col);
294 av.getColumnSelection().addElement(col);
298 ap.paintAlignment(false);
302 public void mouseEntered(MouseEvent evt)
306 ap.seqPanel.scrollCanvas(null);
310 public void mouseExited(MouseEvent evt)
314 ap.seqPanel.scrollCanvas(evt);
318 public void mouseClicked(MouseEvent evt)
321 public void mouseMoved(MouseEvent evt)
323 if (!av.hasHiddenColumns)
328 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
330 res = av.getColumnSelection().adjustForHiddenColumns(res);
333 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
335 int[] region = (int[]) av.getColumnSelection().getHiddenColumns().
337 if (res + 1 == region[0] || res - 1 == region[1])
340 ToolTipManager.sharedInstance().registerComponent(this);
341 this.setToolTipText("Reveal Hidden Columns with Right Mouse Button");
346 this.setToolTipText(null);
359 * @param g DOCUMENT ME!
361 public void paintComponent(Graphics g)
363 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
366 // scalewidth will normally be screenwidth,
367 public void drawScale(Graphics g, int startx, int endx, int width,
370 Graphics2D gg = (Graphics2D) g;
371 gg.setFont(av.getFont());
375 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
376 RenderingHints.VALUE_ANTIALIAS_ON);
379 //Fill in the background
380 gg.setColor(Color.white);
381 gg.fillRect(0, 0, width, height);
382 gg.setColor(Color.black);
384 //Fill the selected columns
385 ColumnSelection cs = av.getColumnSelection();
386 gg.setColor(new Color(220, 0, 0));
388 for (int i = 0; i < cs.size(); i++)
390 int sel = cs.columnAt(i);
391 if (av.hasHiddenColumns)
393 sel = av.getColumnSelection().findColumnPosition(sel);
396 if ( (sel >= startx) && (sel <= endx))
398 gg.fillRect( (sel - startx) * av.charWidth, 0, av.charWidth,
403 // Draw the scale numbers
404 gg.setColor(Color.black);
406 int scalestartx = (startx / 10) * 10;
408 FontMetrics fm = gg.getFontMetrics(av.getFont());
409 int y = av.charHeight - fm.getDescent();
411 if ( (scalestartx % 10) == 0)
419 for (int i = scalestartx; i < endx; i += 5)
423 string = String.valueOf(av.getColumnSelection().adjustForHiddenColumns(
425 if ( (i - startx - 1) * av.charWidth > maxX)
427 gg.drawString(string,
428 (i - startx - 1) * av.charWidth, y);
429 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
432 gg.drawLine( (int) ( ( (i - startx - 1) * av.charWidth) +
433 (av.charWidth / 2)), y + 2,
434 (int) ( ( (i - startx - 1) * av.charWidth) +
436 y + (fm.getDescent() * 2));
441 gg.drawLine( (int) ( ( (i - startx - 1) * av.charWidth) +
442 (av.charWidth / 2)), y + fm.getDescent(),
443 (int) ( ( (i - startx - 1) * av.charWidth) +
444 (av.charWidth / 2)), y + (fm.getDescent() * 2));
448 if (av.hasHiddenColumns)
450 gg.setColor(Color.blue);
452 if (av.getShowHiddenMarkers())
454 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size();
458 res = av.getColumnSelection().findHiddenRegionPosition(i) -
461 if (res < 0 || res > endx - scalestartx)
466 gg.fillPolygon(new int[]
467 {res * av.charWidth - av.charHeight / 4,
468 res * av.charWidth + av.charHeight / 4,
472 y - av.charHeight / 2, y - av.charHeight / 2,
479 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
481 gg.drawString("Reveal Columns", reveal[0] * av.charWidth, 0);