2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
24 import java.awt.event.*;
26 import jalview.datamodel.*;
27 import jalview.util.MessageManager;
29 public class ScalePanel extends Panel implements MouseMotionListener,
33 protected int offy = 4;
37 protected AlignViewport av;
41 boolean stretchingGroup = false;
43 int min; // used by mouseDragged to see if user
45 int max; // used by mouseDragged to see if user
47 boolean mouseDragging = false;
51 public ScalePanel(AlignViewport av, AlignmentPanel ap)
57 addMouseListener(this);
58 addMouseMotionListener(this);
62 public void mousePressed(MouseEvent evt)
64 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
67 if (av.hasHiddenColumns())
69 res = av.getColumnSelection().adjustForHiddenColumns(x);
78 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
80 PopupMenu pop = new PopupMenu();
83 MenuItem item = new MenuItem(
84 MessageManager.getString("label.reveal"));
85 item.addActionListener(new ActionListener()
87 public void actionPerformed(ActionEvent e)
89 av.showColumn(reveal[0]);
91 ap.paintAlignment(true);
92 if (ap.overviewPanel != null)
94 ap.overviewPanel.updateOverviewImage();
100 if (av.getColumnSelection().getHiddenColumns().size() > 1)
102 item = new MenuItem(MessageManager.getString("action.reveal_all"));
103 item.addActionListener(new ActionListener()
105 public void actionPerformed(ActionEvent e)
107 av.showAllHiddenColumns();
109 ap.paintAlignment(true);
110 if (ap.overviewPanel != null)
112 ap.overviewPanel.updateOverviewImage();
119 pop.show(this, evt.getX(), evt.getY());
121 else if (av.getColumnSelection().contains(res))
123 MenuItem item = new MenuItem(
124 MessageManager.getString("label.hide_columns"));
125 item.addActionListener(new ActionListener()
127 public void actionPerformed(ActionEvent e)
129 av.hideColumns(res, res);
130 if (av.getSelectionGroup() != null
131 && av.getSelectionGroup().getSize() == av
132 .getAlignment().getHeight())
134 av.setSelectionGroup(null);
137 ap.paintAlignment(true);
138 if (ap.overviewPanel != null)
140 ap.overviewPanel.updateOverviewImage();
146 pop.show(this, evt.getX(), evt.getY());
150 // LEFT MOUSE TO SELECT
152 if (!evt.isControlDown() && !evt.isShiftDown())
154 av.getColumnSelection().clear();
157 av.getColumnSelection().addElement(res);
158 SequenceGroup sg = new SequenceGroup();
159 for (int i = 0; i < av.getAlignment().getSequences().size(); i++)
161 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
166 av.setSelectionGroup(sg);
168 if (evt.isShiftDown())
170 int min = Math.min(av.getColumnSelection().getMin(), res);
171 int max = Math.max(av.getColumnSelection().getMax(), res);
172 for (int i = min; i < max; i++)
174 av.getColumnSelection().addElement(i);
181 ap.paintAlignment(true);
185 public void mouseReleased(MouseEvent evt)
187 mouseDragging = false;
189 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
191 if (res > av.getAlignment().getWidth())
193 res = av.getAlignment().getWidth() - 1;
196 if (av.hasHiddenColumns())
198 res = av.getColumnSelection().adjustForHiddenColumns(res);
201 if (!stretchingGroup)
203 ap.paintAlignment(false);
208 SequenceGroup sg = av.getSelectionGroup();
210 if (res > sg.getStartRes())
214 else if (res < sg.getStartRes())
219 stretchingGroup = false;
220 ap.paintAlignment(false);
224 public void mouseDragged(MouseEvent evt)
226 mouseDragging = true;
228 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
234 if (av.hasHiddenColumns())
236 res = av.getColumnSelection().adjustForHiddenColumns(res);
239 if (res > av.getAlignment().getWidth())
241 res = av.getAlignment().getWidth() - 1;
254 SequenceGroup sg = av.getSelectionGroup();
258 stretchingGroup = true;
260 if (!av.getColumnSelection().contains(res))
262 av.getColumnSelection().addElement(res);
265 if (res > sg.getStartRes())
269 if (res < sg.getStartRes())
275 for (int i = min; i <= max; i++)
277 col = av.getColumnSelection().adjustForHiddenColumns(i);
279 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
281 av.getColumnSelection().removeElement(col);
285 av.getColumnSelection().addElement(col);
289 ap.paintAlignment(false);
293 public void mouseEntered(MouseEvent evt)
297 ap.seqPanel.scrollCanvas(null);
301 public void mouseExited(MouseEvent evt)
305 ap.seqPanel.scrollCanvas(evt);
309 public void mouseClicked(MouseEvent evt)
314 public void mouseMoved(MouseEvent evt)
316 if (!av.hasHiddenColumns())
321 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
323 res = av.getColumnSelection().adjustForHiddenColumns(res);
326 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
328 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
330 if (res + 1 == region[0] || res - 1 == region[1])
340 public void update(Graphics g)
345 public void paint(Graphics g)
347 drawScale(g, av.getStartRes(), av.getEndRes(), getSize().width,
351 // scalewidth will normally be screenwidth,
352 public void drawScale(Graphics gg, int startx, int endx, int width,
355 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 gg.setColor(new Color(220, 0, 0));
366 for (int i = 0; i < cs.size(); i++)
368 int sel = cs.columnAt(i);
369 if (av.hasHiddenColumns())
371 sel = av.getColumnSelection().findColumnPosition(sel);
374 if ((sel >= startx) && (sel <= endx))
376 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
381 // Draw the scale numbers
382 gg.setColor(Color.black);
384 int scalestartx = (startx / 10) * 10;
386 FontMetrics fm = gg.getFontMetrics(av.getFont());
387 int y = av.charHeight - fm.getDescent();
389 if ((scalestartx % 10) == 0)
397 for (int i = scalestartx; i < endx; i += 5)
401 string = String.valueOf(av.getColumnSelection()
402 .adjustForHiddenColumns(i));
403 if ((i - startx - 1) * av.charWidth > maxX)
405 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
406 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
410 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
412 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
413 y + (fm.getDescent() * 2));
419 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
421 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
422 y + (fm.getDescent() * 2));
426 if (av.hasHiddenColumns())
428 gg.setColor(Color.blue);
430 if (av.getShowHiddenMarkers())
432 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
436 res = av.getColumnSelection().findHiddenRegionPosition(i)
439 if (res < 0 || res > endx - scalestartx)
444 gg.fillPolygon(new int[]
445 { res * av.charWidth - av.charHeight / 4,
446 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
448 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
454 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
456 gg.drawString(MessageManager.getString("label.reveal_columns"),
457 reveal[0] * av.charWidth, 0);