2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.datamodel.ColumnSelection;
24 import jalview.datamodel.HiddenColumns;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.renderer.ScaleRenderer;
27 import jalview.renderer.ScaleRenderer.ScaleMark;
28 import jalview.util.MessageManager;
29 import jalview.viewmodel.ViewportListenerI;
30 import jalview.viewmodel.ViewportRanges;
32 import java.awt.Color;
33 import java.awt.FontMetrics;
34 import java.awt.Graphics;
35 import java.awt.MenuItem;
36 import java.awt.Panel;
37 import java.awt.PopupMenu;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.InputEvent;
41 import java.awt.event.MouseEvent;
42 import java.awt.event.MouseListener;
43 import java.awt.event.MouseMotionListener;
44 import java.beans.PropertyChangeEvent;
45 import java.util.List;
47 public class ScalePanel extends Panel implements MouseMotionListener,
48 MouseListener, ViewportListenerI
51 protected int offy = 4;
55 protected AlignViewport av;
59 boolean stretchingGroup = false;
61 int min; // used by mouseDragged to see if user
63 int max; // used by mouseDragged to see if user
65 boolean mouseDragging = false;
69 public ScalePanel(AlignViewport av, AlignmentPanel ap)
75 addMouseListener(this);
76 addMouseMotionListener(this);
78 av.getRanges().addPropertyChangeListener(this);
82 public void mousePressed(MouseEvent evt)
84 int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes();
87 if (av.hasHiddenColumns())
89 res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(x);
98 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
100 rightMouseButtonPressed(evt, res);
104 leftMouseButtonPressed(evt, res);
109 * Handles left mouse button pressed (selection / clear selections)
114 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
116 if (!evt.isControlDown() && !evt.isShiftDown())
118 av.getColumnSelection().clear();
121 av.getColumnSelection().addElement(res);
122 SequenceGroup sg = new SequenceGroup();
123 for (int i = 0; i < av.getAlignment().getSequences().size(); i++)
125 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
130 av.setSelectionGroup(sg);
132 if (evt.isShiftDown())
134 int min = Math.min(av.getColumnSelection().getMin(), res);
135 int max = Math.max(av.getColumnSelection().getMax(), res);
136 for (int i = min; i < max; i++)
138 av.getColumnSelection().addElement(i);
143 ap.paintAlignment(false);
148 * Handles right mouse button press. If pressed in a selected column, opens
149 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
150 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
155 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
157 PopupMenu pop = new PopupMenu();
160 MenuItem item = new MenuItem(MessageManager.getString("label.reveal"));
161 item.addActionListener(new ActionListener()
164 public void actionPerformed(ActionEvent e)
166 av.showColumn(reveal[0]);
168 ap.paintAlignment(true);
174 if (av.getAlignment().getHiddenColumns().hasManyHiddenColumns())
176 item = new MenuItem(MessageManager.getString("action.reveal_all"));
177 item.addActionListener(new ActionListener()
180 public void actionPerformed(ActionEvent e)
182 av.showAllHiddenColumns();
184 ap.paintAlignment(true);
191 pop.show(this, evt.getX(), evt.getY());
193 else if (av.getColumnSelection().contains(res))
195 MenuItem item = new MenuItem(
196 MessageManager.getString("label.hide_columns"));
197 item.addActionListener(new ActionListener()
200 public void actionPerformed(ActionEvent e)
202 av.hideColumns(res, res);
203 if (av.getSelectionGroup() != null
204 && av.getSelectionGroup().getSize() == av.getAlignment()
207 av.setSelectionGroup(null);
210 ap.paintAlignment(true);
216 pop.show(this, evt.getX(), evt.getY());
221 public void mouseReleased(MouseEvent evt)
223 mouseDragging = false;
225 int res = (evt.getX() / av.getCharWidth())
226 + av.getRanges().getStartRes();
228 if (res > av.getAlignment().getWidth())
230 res = av.getAlignment().getWidth() - 1;
233 if (av.hasHiddenColumns())
235 res = av.getAlignment().getHiddenColumns()
236 .adjustForHiddenColumns(res);
239 if (!stretchingGroup)
241 ap.paintAlignment(false);
246 SequenceGroup sg = av.getSelectionGroup();
248 if (res > sg.getStartRes())
252 else if (res < sg.getStartRes())
257 stretchingGroup = false;
258 ap.paintAlignment(false);
263 * Action on dragging the mouse in the scale panel is to expand or shrink the
264 * selection group range (including any hidden columns that it spans)
269 public void mouseDragged(MouseEvent evt)
271 mouseDragging = true;
272 ColumnSelection cs = av.getColumnSelection();
274 int res = (evt.getX() / av.getCharWidth())
275 + av.getRanges().getStartRes();
276 res = Math.max(0, res);
277 res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res);
278 res = Math.min(res, av.getAlignment().getWidth() - 1);
279 min = Math.min(res, min);
280 max = Math.max(res, max);
282 SequenceGroup sg = av.getSelectionGroup();
285 stretchingGroup = true;
286 cs.stretchGroup(res, sg, min, max);
287 ap.paintAlignment(false);
292 public void mouseEntered(MouseEvent evt)
296 ap.seqPanel.scrollCanvas(null);
301 public void mouseExited(MouseEvent evt)
305 ap.seqPanel.scrollCanvas(evt);
310 public void mouseClicked(MouseEvent evt)
316 public void mouseMoved(MouseEvent evt)
318 if (!av.hasHiddenColumns())
323 int res = (evt.getX() / av.getCharWidth())
324 + av.getRanges().getStartRes();
326 reveal = av.getAlignment().getHiddenColumns()
327 .getRegionWithEdgeAtRes(res);
333 public void update(Graphics g)
339 public void paint(Graphics g)
342 * shouldn't get called in wrapped mode as the scale above is
343 * drawn instead by SeqCanvas.drawNorthScale
345 if (!av.getWrapAlignment())
347 drawScale(g, av.getRanges().getStartRes(),
348 av.getRanges().getEndRes(), getSize().width, getSize().height);
352 // scalewidth will normally be screenwidth,
353 public void drawScale(Graphics gg, int startx, int endx, int width,
356 gg.setFont(av.getFont());
357 // Fill in the background
358 gg.setColor(Color.white);
359 gg.fillRect(0, 0, width, height);
360 gg.setColor(Color.black);
362 // Fill the selected columns
363 ColumnSelection cs = av.getColumnSelection();
364 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
365 int avCharWidth = av.getCharWidth();
366 int avcharHeight = av.getCharHeight();
369 gg.setColor(new Color(220, 0, 0));
370 boolean hasHiddenColumns = hidden.hasHiddenColumns();
371 for (int sel : cs.getSelected())
373 // TODO: JAL-2001 - provide a fast method to list visible selected in a
375 if (hasHiddenColumns)
377 if (hidden.isVisible(sel))
379 sel = hidden.findColumnPosition(sel);
387 if ((sel >= startx) && (sel <= endx))
389 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
395 // Draw the scale numbers
396 gg.setColor(Color.black);
399 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
402 FontMetrics fm = gg.getFontMetrics(av.getFont());
403 int y = avcharHeight;
404 int yOf = fm.getDescent();
406 for (ScaleMark mark : marks)
408 boolean major = mark.major;
409 int mpos = mark.column; // (i - startx - 1)
410 String mstring = mark.text;
413 if (mpos * avCharWidth > maxX)
415 gg.drawString(mstring, mpos * avCharWidth, y);
416 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
421 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
422 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
426 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
427 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
431 if (av.hasHiddenColumns())
433 gg.setColor(Color.blue);
435 if (av.getShowHiddenMarkers())
437 int widthx = 1 + endx - startx;
438 List<Integer> positions = hidden.findHiddenRegionPositions();
439 for (int pos : positions)
444 if (res < 0 || res > widthx)
449 gg.fillPolygon(new int[] {
450 -1 + res * avCharWidth - avcharHeight / 4,
451 -1 + res * avCharWidth + avcharHeight / 4,
452 -1 + res * avCharWidth }, new int[] { y, y, y + 2 * yOf }, 3);
459 public void propertyChange(PropertyChangeEvent evt)
461 // Respond to viewport change events (e.g. alignment panel was scrolled)
462 // Both scrolling and resizing change viewport ranges: scrolling changes
463 // both start and end points, but resize only changes end values.
464 // Here we only want to fastpaint on a scroll, with resize using a normal
465 // paint, so scroll events are identified as changes to the horizontal or
466 // vertical start value.
467 if (evt.getPropertyName().equals(ViewportRanges.STARTRES))
469 // scroll event, repaint panel