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 extends JPanel implements MouseMotionListener,
36 protected int offy = 4;
41 protected AlignViewport av;
45 boolean stretchingGroup = false;
47 int min; // used by mouseDragged to see if user
49 int max; // used by mouseDragged to see if user
51 boolean mouseDragging = false;
53 // wants to delete columns
54 public ScalePanel(AlignViewport av, AlignmentPanel ap)
59 addMouseListener(this);
60 addMouseMotionListener(this);
69 public void mousePressed(MouseEvent evt)
71 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
74 if (av.hasHiddenColumns)
76 res = av.getColumnSelection().adjustForHiddenColumns(x);
86 if (SwingUtilities.isRightMouseButton(evt))
88 JPopupMenu pop = new JPopupMenu();
91 JMenuItem item = new JMenuItem("Reveal");
92 item.addActionListener(new ActionListener()
94 public void actionPerformed(ActionEvent e)
96 av.showColumn(reveal[0]);
98 ap.paintAlignment(true);
99 if (ap.overviewPanel != null)
101 ap.overviewPanel.updateOverviewImage();
107 if (av.getColumnSelection().getHiddenColumns().size() > 1)
109 item = new JMenuItem("Reveal All");
110 item.addActionListener(new ActionListener()
112 public void actionPerformed(ActionEvent e)
114 av.showAllHiddenColumns();
116 ap.paintAlignment(true);
117 if (ap.overviewPanel != null)
119 ap.overviewPanel.updateOverviewImage();
125 pop.show(this, evt.getX(), evt.getY());
127 else if (av.getColumnSelection().contains(res))
129 JMenuItem item = new JMenuItem("Hide Columns");
130 item.addActionListener(new ActionListener()
132 public void actionPerformed(ActionEvent e)
134 av.hideColumns(res, res);
135 if (av.getSelectionGroup() != null
136 && av.getSelectionGroup().getSize() == av.alignment
139 av.setSelectionGroup(null);
142 ap.paintAlignment(true);
143 if (ap.overviewPanel != null)
145 ap.overviewPanel.updateOverviewImage();
150 pop.show(this, evt.getX(), evt.getY());
154 // LEFT MOUSE TO SELECT
156 if (!evt.isControlDown() && !evt.isShiftDown())
158 av.getColumnSelection().clear();
161 av.getColumnSelection().addElement(res);
162 SequenceGroup sg = new SequenceGroup();
163 // try to be as quick as possible
164 SequenceI[] iVec = av.alignment.getSequencesArray();
165 for (int i = 0; i < iVec.length; i++)
167 sg.addSequence(iVec[i], false);
173 av.setSelectionGroup(sg);
175 if (evt.isShiftDown())
177 int min = Math.min(av.getColumnSelection().getMin(), res);
178 int max = Math.max(av.getColumnSelection().getMax(), res);
179 for (int i = min; i < max; i++)
181 av.getColumnSelection().addElement(i);
189 ap.paintAlignment(false);
199 public void mouseReleased(MouseEvent evt)
201 mouseDragging = false;
203 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
205 if (res > av.alignment.getWidth())
207 res = av.alignment.getWidth() - 1;
210 if (av.hasHiddenColumns)
212 res = av.getColumnSelection().adjustForHiddenColumns(res);
215 if (!stretchingGroup)
217 ap.paintAlignment(false);
222 SequenceGroup sg = av.getSelectionGroup();
226 if (res > sg.getStartRes())
230 else if (res < sg.getStartRes())
235 stretchingGroup = false;
236 ap.paintAlignment(false);
246 public void mouseDragged(MouseEvent evt)
248 mouseDragging = true;
250 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
256 if (av.hasHiddenColumns)
258 res = av.getColumnSelection().adjustForHiddenColumns(res);
261 if (res > av.alignment.getWidth())
263 res = av.alignment.getWidth() - 1;
276 SequenceGroup sg = av.getSelectionGroup();
280 stretchingGroup = true;
282 if (!av.getColumnSelection().contains(res))
284 av.getColumnSelection().addElement(res);
287 if (res > sg.getStartRes())
291 if (res < sg.getStartRes())
297 for (int i = min; i <= max; i++)
299 col = av.getColumnSelection().adjustForHiddenColumns(i);
301 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
303 av.getColumnSelection().removeElement(col);
307 av.getColumnSelection().addElement(col);
311 ap.paintAlignment(false);
315 public void mouseEntered(MouseEvent evt)
319 ap.seqPanel.scrollCanvas(null);
323 public void mouseExited(MouseEvent evt)
327 ap.seqPanel.scrollCanvas(evt);
331 public void mouseClicked(MouseEvent evt)
335 public void mouseMoved(MouseEvent evt)
337 if (!av.hasHiddenColumns)
342 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
344 res = av.getColumnSelection().adjustForHiddenColumns(res);
347 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
349 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
351 if (res + 1 == region[0] || res - 1 == region[1])
354 ToolTipManager.sharedInstance().registerComponent(this);
356 .setToolTipText("Reveal Hidden Columns with Right Mouse Button");
361 this.setToolTipText(null);
377 public void paintComponent(Graphics g)
379 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
382 // scalewidth will normally be screenwidth,
383 public void drawScale(Graphics g, int startx, int endx, int width,
386 Graphics2D gg = (Graphics2D) g;
387 gg.setFont(av.getFont());
391 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
392 RenderingHints.VALUE_ANTIALIAS_ON);
395 // Fill in the background
396 gg.setColor(Color.white);
397 gg.fillRect(0, 0, width, height);
398 gg.setColor(Color.black);
400 // Fill the selected columns
401 ColumnSelection cs = av.getColumnSelection();
402 gg.setColor(new Color(220, 0, 0));
404 for (int i = 0; i < cs.size(); i++)
406 int sel = cs.columnAt(i);
407 if (av.hasHiddenColumns)
409 sel = av.getColumnSelection().findColumnPosition(sel);
412 if ((sel >= startx) && (sel <= endx))
414 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
419 // Draw the scale numbers
420 gg.setColor(Color.black);
422 int scalestartx = (startx / 10) * 10;
424 FontMetrics fm = gg.getFontMetrics(av.getFont());
425 int y = av.charHeight - fm.getDescent();
427 if ((scalestartx % 10) == 0)
435 for (int i = scalestartx; i < endx; i += 5)
439 string = String.valueOf(av.getColumnSelection()
440 .adjustForHiddenColumns(i));
441 if ((i - startx - 1) * av.charWidth > maxX)
443 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
444 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
449 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
451 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
452 y + (fm.getDescent() * 2));
459 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
461 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
462 y + (fm.getDescent() * 2));
466 if (av.hasHiddenColumns)
468 gg.setColor(Color.blue);
470 if (av.getShowHiddenMarkers())
472 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
476 res = av.getColumnSelection().findHiddenRegionPosition(i)
479 if (res < 0 || res > endx - scalestartx)
484 gg.fillPolygon(new int[]
485 { res * av.charWidth - av.charHeight / 4,
486 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
488 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
494 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
496 gg.drawString("Reveal Columns", reveal[0] * av.charWidth, 0);