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.Iterator;
46 import java.util.List;
48 public class ScalePanel extends Panel
49 implements MouseMotionListener, MouseListener, ViewportListenerI
52 protected int offy = 4;
56 protected AlignViewport av;
60 boolean stretchingGroup = false;
62 int min; // used by mouseDragged to see if user
64 int max; // used by mouseDragged to see if user
66 boolean mouseDragging = false;
70 public ScalePanel(AlignViewport av, AlignmentPanel ap)
76 addMouseListener(this);
77 addMouseMotionListener(this);
79 av.getRanges().addPropertyChangeListener(this);
83 public void mousePressed(MouseEvent evt)
85 int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes();
88 if (av.hasHiddenColumns())
90 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(x);
99 if ((evt.getModifiers()
100 & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
102 rightMouseButtonPressed(evt, res);
106 leftMouseButtonPressed(evt, res);
111 * Handles left mouse button pressed (selection / clear selections)
116 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
118 if (!evt.isControlDown() && !evt.isShiftDown())
120 av.getColumnSelection().clear();
123 av.getColumnSelection().addElement(res);
124 SequenceGroup sg = new SequenceGroup();
125 for (int i = 0; i < av.getAlignment().getSequences().size(); i++)
127 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
132 av.setSelectionGroup(sg);
134 if (evt.isShiftDown())
136 int min = Math.min(av.getColumnSelection().getMin(), res);
137 int max = Math.max(av.getColumnSelection().getMax(), res);
138 for (int i = min; i < max; i++)
140 av.getColumnSelection().addElement(i);
145 ap.paintAlignment(false, false);
150 * Handles right mouse button press. If pressed in a selected column, opens
151 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
152 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
157 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
159 PopupMenu pop = new PopupMenu();
162 MenuItem item = new MenuItem(
163 MessageManager.getString("label.reveal"));
164 item.addActionListener(new ActionListener()
167 public void actionPerformed(ActionEvent e)
169 av.showColumn(reveal[0]);
171 ap.paintAlignment(true, true);
177 if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
179 item = new MenuItem(MessageManager.getString("action.reveal_all"));
180 item.addActionListener(new ActionListener()
183 public void actionPerformed(ActionEvent e)
185 av.showAllHiddenColumns();
187 ap.paintAlignment(true, true);
194 pop.show(this, evt.getX(), evt.getY());
196 else if (av.getColumnSelection().contains(res))
198 MenuItem item = new MenuItem(
199 MessageManager.getString("label.hide_columns"));
200 item.addActionListener(new ActionListener()
203 public void actionPerformed(ActionEvent e)
205 av.hideColumns(res, res);
206 if (av.getSelectionGroup() != null && av.getSelectionGroup()
207 .getSize() == av.getAlignment().getHeight())
209 av.setSelectionGroup(null);
212 ap.paintAlignment(true, true);
218 pop.show(this, evt.getX(), evt.getY());
223 public void mouseReleased(MouseEvent evt)
225 mouseDragging = false;
227 int res = (evt.getX() / av.getCharWidth())
228 + av.getRanges().getStartRes();
230 if (res > av.getAlignment().getWidth())
232 res = av.getAlignment().getWidth() - 1;
235 if (av.hasHiddenColumns())
237 res = av.getAlignment().getHiddenColumns()
238 .visibleToAbsoluteColumn(res);
241 if (!stretchingGroup)
243 ap.paintAlignment(false, false);
248 SequenceGroup sg = av.getSelectionGroup();
250 if (res > sg.getStartRes())
254 else if (res < sg.getStartRes())
259 stretchingGroup = false;
260 ap.paintAlignment(false, false);
265 * Action on dragging the mouse in the scale panel is to expand or shrink the
266 * selection group range (including any hidden columns that it spans)
271 public void mouseDragged(MouseEvent evt)
273 mouseDragging = true;
274 ColumnSelection cs = av.getColumnSelection();
276 int res = (evt.getX() / av.getCharWidth())
277 + av.getRanges().getStartRes();
278 res = Math.max(0, res);
279 res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
280 res = Math.min(res, av.getAlignment().getWidth() - 1);
281 min = Math.min(res, min);
282 max = Math.max(res, max);
284 SequenceGroup sg = av.getSelectionGroup();
287 stretchingGroup = true;
288 cs.stretchGroup(res, sg, min, max);
289 ap.paintAlignment(false, false);
294 public void mouseEntered(MouseEvent evt)
298 ap.seqPanel.scrollCanvas(null);
303 public void mouseExited(MouseEvent evt)
307 ap.seqPanel.scrollCanvas(evt);
312 public void mouseClicked(MouseEvent evt)
318 public void mouseMoved(MouseEvent evt)
320 if (!av.hasHiddenColumns())
325 int res = (evt.getX() / av.getCharWidth())
326 + av.getRanges().getStartRes();
328 reveal = av.getAlignment().getHiddenColumns()
329 .getRegionWithEdgeAtRes(res);
335 public void update(Graphics g)
341 public void paint(Graphics g)
344 * shouldn't get called in wrapped mode as the scale above is
345 * drawn instead by SeqCanvas.drawNorthScale
347 if (!av.getWrapAlignment())
349 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
350 getSize().width, getSize().height);
354 // scalewidth will normally be screenwidth,
355 public void drawScale(Graphics gg, int startx, int endx, int width,
358 gg.setFont(av.getFont());
359 // Fill in the background
360 gg.setColor(Color.white);
361 gg.fillRect(0, 0, width, height);
362 gg.setColor(Color.black);
364 // Fill the selected columns
365 ColumnSelection cs = av.getColumnSelection();
366 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
367 int avCharWidth = av.getCharWidth();
368 int avcharHeight = av.getCharHeight();
371 gg.setColor(new Color(220, 0, 0));
372 boolean hasHiddenColumns = hidden.hasHiddenColumns();
373 for (int sel : cs.getSelected())
375 // TODO: JAL-2001 - provide a fast method to list visible selected in a
377 if (hasHiddenColumns)
379 if (hidden.isVisible(sel))
381 sel = hidden.absoluteToVisibleColumn(sel);
389 if ((sel >= startx) && (sel <= endx))
391 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
397 // Draw the scale numbers
398 gg.setColor(Color.black);
401 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
404 FontMetrics fm = gg.getFontMetrics(av.getFont());
405 int y = avcharHeight;
406 int yOf = fm.getDescent();
408 for (ScaleMark mark : marks)
410 boolean major = mark.major;
411 int mpos = mark.column; // (i - startx - 1)
412 String mstring = mark.text;
415 if (mpos * avCharWidth > maxX)
417 gg.drawString(mstring, mpos * avCharWidth, y);
418 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
423 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
424 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
428 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
429 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
433 if (av.hasHiddenColumns())
435 gg.setColor(Color.blue);
437 if (av.getShowHiddenMarkers())
439 int widthx = 1 + endx - startx;
440 Iterator<Integer> it = hidden.getStartRegionIterator(startx,
441 startx + widthx + 1);
444 res = it.next() - startx;
448 { -1 + res * avCharWidth - avcharHeight / 4, -1 + res * avCharWidth + avcharHeight / 4,
449 -1 + res * avCharWidth }, new int[]
450 { y, y, y + 2 * yOf }, 3);
457 public void propertyChange(PropertyChangeEvent evt)
459 // Respond to viewport change events (e.g. alignment panel was scrolled)
460 // Both scrolling and resizing change viewport ranges: scrolling changes
461 // both start and end points, but resize only changes end values.
462 // Here we only want to fastpaint on a scroll, with resize using a normal
463 // paint, so scroll events are identified as changes to the horizontal or
464 // vertical start value.
465 if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
466 || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
467 || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
469 // scroll event, repaint panel