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.SequenceGroup;
25 import jalview.renderer.ScaleRenderer;
26 import jalview.renderer.ScaleRenderer.ScaleMark;
27 import jalview.util.MessageManager;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
32 import java.awt.MenuItem;
33 import java.awt.Panel;
34 import java.awt.PopupMenu;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.InputEvent;
38 import java.awt.event.MouseEvent;
39 import java.awt.event.MouseListener;
40 import java.awt.event.MouseMotionListener;
41 import java.util.List;
43 public class ScalePanel extends Panel implements MouseMotionListener,
47 protected int offy = 4;
51 protected AlignViewport av;
55 boolean stretchingGroup = false;
57 int min; // used by mouseDragged to see if user
59 int max; // used by mouseDragged to see if user
61 boolean mouseDragging = false;
65 public ScalePanel(AlignViewport av, AlignmentPanel ap)
71 addMouseListener(this);
72 addMouseMotionListener(this);
77 public void mousePressed(MouseEvent evt)
79 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
82 if (av.hasHiddenColumns())
84 res = av.getColumnSelection().adjustForHiddenColumns(x);
93 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
95 rightMouseButtonPressed(evt, res);
99 leftMouseButtonPressed(evt, res);
104 * Handles left mouse button pressed (selection / clear selections)
109 protected void leftMouseButtonPressed(MouseEvent evt, final int res)
111 if (!evt.isControlDown() && !evt.isShiftDown())
113 av.getColumnSelection().clear();
116 av.getColumnSelection().addElement(res);
117 SequenceGroup sg = new SequenceGroup();
118 for (int i = 0; i < av.getAlignment().getSequences().size(); i++)
120 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
125 av.setSelectionGroup(sg);
127 if (evt.isShiftDown())
129 int min = Math.min(av.getColumnSelection().getMin(), res);
130 int max = Math.max(av.getColumnSelection().getMax(), res);
131 for (int i = min; i < max; i++)
133 av.getColumnSelection().addElement(i);
138 ap.paintAlignment(true);
143 * Handles right mouse button press. If pressed in a selected column, opens
144 * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
145 * opens context menu for 'Reveal / Reveal All'. Else does nothing.
150 protected void rightMouseButtonPressed(MouseEvent evt, final int res)
152 PopupMenu pop = new PopupMenu();
155 MenuItem item = new MenuItem(MessageManager.getString("label.reveal"));
156 item.addActionListener(new ActionListener()
159 public void actionPerformed(ActionEvent e)
161 av.showColumn(reveal[0]);
163 ap.paintAlignment(true);
164 if (ap.overviewPanel != null)
166 ap.overviewPanel.updateOverviewImage();
173 if (av.getColumnSelection().hasManyHiddenColumns())
175 item = new MenuItem(MessageManager.getString("action.reveal_all"));
176 item.addActionListener(new ActionListener()
179 public void actionPerformed(ActionEvent e)
181 av.showAllHiddenColumns();
183 ap.paintAlignment(true);
184 if (ap.overviewPanel != null)
186 ap.overviewPanel.updateOverviewImage();
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
207 && av.getSelectionGroup().getSize() == av.getAlignment()
210 av.setSelectionGroup(null);
213 ap.paintAlignment(true);
214 if (ap.overviewPanel != null)
216 ap.overviewPanel.updateOverviewImage();
223 pop.show(this, evt.getX(), evt.getY());
228 public void mouseReleased(MouseEvent evt)
230 mouseDragging = false;
232 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
234 if (res > av.getAlignment().getWidth())
236 res = av.getAlignment().getWidth() - 1;
239 if (av.hasHiddenColumns())
241 res = av.getColumnSelection().adjustForHiddenColumns(res);
244 if (!stretchingGroup)
246 ap.paintAlignment(false);
251 SequenceGroup sg = av.getSelectionGroup();
253 if (res > sg.getStartRes())
257 else if (res < sg.getStartRes())
262 stretchingGroup = false;
263 ap.paintAlignment(false);
268 * Action on dragging the mouse in the scale panel is to expand or shrink the
269 * selection group range (including any hidden columns that it spans)
274 public void mouseDragged(MouseEvent evt)
276 mouseDragging = true;
277 ColumnSelection cs = av.getColumnSelection();
279 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
280 res = Math.max(0, res);
281 res = cs.adjustForHiddenColumns(res);
282 res = Math.min(res, av.getAlignment().getWidth() - 1);
283 min = Math.min(res, min);
284 max = Math.max(res, max);
286 SequenceGroup sg = av.getSelectionGroup();
289 stretchingGroup = true;
290 cs.stretchGroup(res, sg, min, max);
291 ap.paintAlignment(false);
296 public void mouseEntered(MouseEvent evt)
300 ap.seqPanel.scrollCanvas(null);
305 public void mouseExited(MouseEvent evt)
309 ap.seqPanel.scrollCanvas(evt);
314 public void mouseClicked(MouseEvent evt)
320 public void mouseMoved(MouseEvent evt)
322 if (!av.hasHiddenColumns())
327 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
329 res = av.getColumnSelection().adjustForHiddenColumns(res);
332 for (int[] region : av.getColumnSelection().getHiddenColumns())
334 if (res + 1 == region[0] || res - 1 == region[1])
345 public void update(Graphics g)
351 public void paint(Graphics g)
353 drawScale(g, av.getStartRes(), av.getEndRes(), getSize().width,
357 // scalewidth will normally be screenwidth,
358 public void drawScale(Graphics gg, int startx, int endx, int width,
361 gg.setFont(av.getFont());
362 // Fill in the background
363 gg.setColor(Color.white);
364 gg.fillRect(0, 0, width, height);
365 gg.setColor(Color.black);
367 // Fill the selected columns
368 ColumnSelection cs = av.getColumnSelection();
369 int avCharWidth = av.getCharWidth();
370 int avcharHeight = av.getCharHeight();
373 gg.setColor(new Color(220, 0, 0));
374 boolean hasHiddenColumns = cs.hasHiddenColumns();
375 for (int sel : cs.getSelected())
377 // TODO: JAL-2001 - provide a fast method to list visible selected in a
379 if (hasHiddenColumns)
381 if (cs.isVisible(sel))
383 sel = cs.findColumnPosition(sel);
391 if ((sel >= startx) && (sel <= endx))
393 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
399 // Draw the scale numbers
400 gg.setColor(Color.black);
403 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
406 FontMetrics fm = gg.getFontMetrics(av.getFont());
407 int y = avcharHeight;
408 int yOf = fm.getDescent();
410 for (ScaleMark mark : marks)
412 boolean major = mark.major;
413 int mpos = mark.column; // (i - startx - 1)
414 String mstring = mark.text;
417 if (mpos * avCharWidth > maxX)
419 gg.drawString(mstring, mpos * avCharWidth, y);
420 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
425 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
426 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
430 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
431 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
435 if (av.hasHiddenColumns())
437 gg.setColor(Color.blue);
439 if (av.getShowHiddenMarkers())
441 int widthx = 1 + endx - startx;
442 for (int i = 0; i < cs.getHiddenColumns().size(); i++)
445 res = cs.findHiddenRegionPosition(i) - startx;
447 if (res < 0 || res > widthx)
452 gg.fillPolygon(new int[] {
453 -1 + res * avCharWidth - avcharHeight / 4,
454 -1 + res * avCharWidth + avcharHeight / 4,
455 -1 + res * avCharWidth }, new int[] { y, y, y + 2 * yOf }, 3);