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.getRanges().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())
233 + av.getRanges().getStartRes();
235 if (res > av.getAlignment().getWidth())
237 res = av.getAlignment().getWidth() - 1;
240 if (av.hasHiddenColumns())
242 res = av.getColumnSelection().adjustForHiddenColumns(res);
245 if (!stretchingGroup)
247 ap.paintAlignment(false);
252 SequenceGroup sg = av.getSelectionGroup();
254 if (res > sg.getStartRes())
258 else if (res < sg.getStartRes())
263 stretchingGroup = false;
264 ap.paintAlignment(false);
269 * Action on dragging the mouse in the scale panel is to expand or shrink the
270 * selection group range (including any hidden columns that it spans)
275 public void mouseDragged(MouseEvent evt)
277 mouseDragging = true;
278 ColumnSelection cs = av.getColumnSelection();
280 int res = (evt.getX() / av.getCharWidth())
281 + av.getRanges().getStartRes();
282 res = Math.max(0, res);
283 res = cs.adjustForHiddenColumns(res);
284 res = Math.min(res, av.getAlignment().getWidth() - 1);
285 min = Math.min(res, min);
286 max = Math.max(res, max);
288 SequenceGroup sg = av.getSelectionGroup();
291 stretchingGroup = true;
292 cs.stretchGroup(res, sg, min, max);
293 ap.paintAlignment(false);
298 public void mouseEntered(MouseEvent evt)
302 ap.seqPanel.scrollCanvas(null);
307 public void mouseExited(MouseEvent evt)
311 ap.seqPanel.scrollCanvas(evt);
316 public void mouseClicked(MouseEvent evt)
322 public void mouseMoved(MouseEvent evt)
324 if (!av.hasHiddenColumns())
329 int res = (evt.getX() / av.getCharWidth())
330 + av.getRanges().getStartRes();
332 res = av.getColumnSelection().adjustForHiddenColumns(res);
335 for (int[] region : av.getColumnSelection().getHiddenColumns())
337 if (res + 1 == region[0] || res - 1 == region[1])
348 public void update(Graphics g)
354 public void paint(Graphics g)
356 drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
361 // scalewidth will normally be screenwidth,
362 public void drawScale(Graphics gg, int startx, int endx, int width,
365 gg.setFont(av.getFont());
366 // Fill in the background
367 gg.setColor(Color.white);
368 gg.fillRect(0, 0, width, height);
369 gg.setColor(Color.black);
371 // Fill the selected columns
372 ColumnSelection cs = av.getColumnSelection();
373 int avCharWidth = av.getCharWidth();
374 int avcharHeight = av.getCharHeight();
377 gg.setColor(new Color(220, 0, 0));
378 boolean hasHiddenColumns = cs.hasHiddenColumns();
379 for (int sel : cs.getSelected())
381 // TODO: JAL-2001 - provide a fast method to list visible selected in a
383 if (hasHiddenColumns)
385 if (cs.isVisible(sel))
387 sel = cs.findColumnPosition(sel);
395 if ((sel >= startx) && (sel <= endx))
397 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
403 // Draw the scale numbers
404 gg.setColor(Color.black);
407 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
410 FontMetrics fm = gg.getFontMetrics(av.getFont());
411 int y = avcharHeight;
412 int yOf = fm.getDescent();
414 for (ScaleMark mark : marks)
416 boolean major = mark.major;
417 int mpos = mark.column; // (i - startx - 1)
418 String mstring = mark.text;
421 if (mpos * avCharWidth > maxX)
423 gg.drawString(mstring, mpos * avCharWidth, y);
424 maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
429 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
430 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
434 gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
435 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
439 if (av.hasHiddenColumns())
441 gg.setColor(Color.blue);
443 if (av.getShowHiddenMarkers())
445 int widthx = 1 + endx - startx;
446 for (int i = 0; i < cs.getHiddenColumns().size(); i++)
449 res = cs.findHiddenRegionPosition(i) - startx;
451 if (res < 0 || res > widthx)
456 gg.fillPolygon(new int[] {
457 -1 + res * avCharWidth - avcharHeight / 4,
458 -1 + res * avCharWidth + avcharHeight / 4,
459 -1 + res * avCharWidth }, new int[] { y, y, y + 2 * yOf }, 3);