2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5.1)
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/>.
18 package jalview.appletgui;
21 import java.awt.event.*;
23 import jalview.datamodel.*;
25 public class ScalePanel extends Panel implements MouseMotionListener,
29 protected int offy = 4;
33 protected AlignViewport av;
37 boolean stretchingGroup = false;
39 int min; // used by mouseDragged to see if user
41 int max; // used by mouseDragged to see if user
43 boolean mouseDragging = false;
47 public ScalePanel(AlignViewport av, AlignmentPanel ap)
53 addMouseListener(this);
54 addMouseMotionListener(this);
58 public void mousePressed(MouseEvent evt)
60 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
63 if (av.hasHiddenColumns)
65 res = av.getColumnSelection().adjustForHiddenColumns(x);
74 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
76 PopupMenu pop = new PopupMenu();
79 MenuItem item = new MenuItem("Reveal");
80 item.addActionListener(new ActionListener()
82 public void actionPerformed(ActionEvent e)
84 av.showColumn(reveal[0]);
86 ap.paintAlignment(true);
87 if (ap.overviewPanel != null)
89 ap.overviewPanel.updateOverviewImage();
95 if (av.getColumnSelection().getHiddenColumns().size() > 1)
97 item = new MenuItem("Reveal All");
98 item.addActionListener(new ActionListener()
100 public void actionPerformed(ActionEvent e)
102 av.showAllHiddenColumns();
104 ap.paintAlignment(true);
105 if (ap.overviewPanel != null)
107 ap.overviewPanel.updateOverviewImage();
114 pop.show(this, evt.getX(), evt.getY());
116 else if (av.getColumnSelection().contains(res))
118 MenuItem item = new MenuItem("Hide Columns");
119 item.addActionListener(new ActionListener()
121 public void actionPerformed(ActionEvent e)
123 av.hideColumns(res, res);
124 if (av.getSelectionGroup() != null
125 && av.getSelectionGroup().getSize() == av.alignment
128 av.setSelectionGroup(null);
131 ap.paintAlignment(true);
132 if (ap.overviewPanel != null)
134 ap.overviewPanel.updateOverviewImage();
140 pop.show(this, evt.getX(), evt.getY());
144 // LEFT MOUSE TO SELECT
146 if (!evt.isControlDown() && !evt.isShiftDown())
148 av.getColumnSelection().clear();
151 av.getColumnSelection().addElement(res);
152 SequenceGroup sg = new SequenceGroup();
153 for (int i = 0; i < av.alignment.getSequences().size(); i++)
155 sg.addSequence(av.alignment.getSequenceAt(i), false);
160 av.setSelectionGroup(sg);
162 if (evt.isShiftDown())
164 int min = Math.min(av.getColumnSelection().getMin(), res);
165 int max = Math.max(av.getColumnSelection().getMax(), res);
166 for (int i = min; i < max; i++)
168 av.getColumnSelection().addElement(i);
175 ap.paintAlignment(true);
178 public void mouseReleased(MouseEvent evt)
180 mouseDragging = false;
182 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
184 if (res > av.alignment.getWidth())
186 res = av.alignment.getWidth() - 1;
189 if (av.hasHiddenColumns)
191 res = av.getColumnSelection().adjustForHiddenColumns(res);
194 if (!stretchingGroup)
196 ap.paintAlignment(false);
201 SequenceGroup sg = av.getSelectionGroup();
203 if (res > sg.getStartRes())
207 else if (res < sg.getStartRes())
212 stretchingGroup = false;
213 ap.paintAlignment(false);
216 public void mouseDragged(MouseEvent evt)
218 mouseDragging = true;
220 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
226 if (av.hasHiddenColumns)
228 res = av.getColumnSelection().adjustForHiddenColumns(res);
231 if (res > av.alignment.getWidth())
233 res = av.alignment.getWidth() - 1;
246 SequenceGroup sg = av.getSelectionGroup();
250 stretchingGroup = true;
252 if (!av.getColumnSelection().contains(res))
254 av.getColumnSelection().addElement(res);
257 if (res > sg.getStartRes())
261 if (res < sg.getStartRes())
267 for (int i = min; i <= max; i++)
269 col = av.getColumnSelection().adjustForHiddenColumns(i);
271 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
273 av.getColumnSelection().removeElement(col);
277 av.getColumnSelection().addElement(col);
281 ap.paintAlignment(false);
285 public void mouseEntered(MouseEvent evt)
289 ap.seqPanel.scrollCanvas(null);
293 public void mouseExited(MouseEvent evt)
297 ap.seqPanel.scrollCanvas(evt);
301 public void mouseClicked(MouseEvent evt)
306 public void mouseMoved(MouseEvent evt)
308 if (!av.hasHiddenColumns)
313 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
315 res = av.getColumnSelection().adjustForHiddenColumns(res);
318 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
320 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
322 if (res + 1 == region[0] || res - 1 == region[1])
332 public void update(Graphics g)
337 public void paint(Graphics g)
339 drawScale(g, av.getStartRes(), av.getEndRes(), getSize().width,
343 // scalewidth will normally be screenwidth,
344 public void drawScale(Graphics gg, int startx, int endx, int width,
347 gg.setFont(av.getFont());
349 // Fill in the background
350 gg.setColor(Color.white);
351 gg.fillRect(0, 0, width, height);
352 gg.setColor(Color.black);
354 // Fill the selected columns
355 ColumnSelection cs = av.getColumnSelection();
356 gg.setColor(new Color(220, 0, 0));
358 for (int i = 0; i < cs.size(); i++)
360 int sel = cs.columnAt(i);
361 if (av.hasHiddenColumns)
363 sel = av.getColumnSelection().findColumnPosition(sel);
366 if ((sel >= startx) && (sel <= endx))
368 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
373 // Draw the scale numbers
374 gg.setColor(Color.black);
376 int scalestartx = (startx / 10) * 10;
378 FontMetrics fm = gg.getFontMetrics(av.getFont());
379 int y = av.charHeight - fm.getDescent();
381 if ((scalestartx % 10) == 0)
389 for (int i = scalestartx; i < endx; i += 5)
393 string = String.valueOf(av.getColumnSelection()
394 .adjustForHiddenColumns(i));
395 if ((i - startx - 1) * av.charWidth > maxX)
397 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
398 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
403 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
405 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
406 y + (fm.getDescent() * 2));
413 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
415 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
416 y + (fm.getDescent() * 2));
420 if (av.hasHiddenColumns)
422 gg.setColor(Color.blue);
424 if (av.getShowHiddenMarkers())
426 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
430 res = av.getColumnSelection().findHiddenRegionPosition(i)
433 if (res < 0 || res > endx - scalestartx)
438 gg.fillPolygon(new int[]
439 { res * av.charWidth - av.charHeight / 4,
440 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
442 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
448 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
450 gg.drawString("Reveal Columns", reveal[0] * av.charWidth, 0);