2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
22 import java.awt.event.*;
24 import jalview.datamodel.*;
25 import jalview.util.MessageManager;
27 public class ScalePanel extends Panel implements MouseMotionListener,
31 protected int offy = 4;
35 protected AlignViewport av;
39 boolean stretchingGroup = false;
41 int min; // used by mouseDragged to see if user
43 int max; // used by mouseDragged to see if user
45 boolean mouseDragging = false;
49 public ScalePanel(AlignViewport av, AlignmentPanel ap)
55 addMouseListener(this);
56 addMouseMotionListener(this);
60 public void mousePressed(MouseEvent evt)
62 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
65 if (av.hasHiddenColumns())
67 res = av.getColumnSelection().adjustForHiddenColumns(x);
76 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
78 PopupMenu pop = new PopupMenu();
81 MenuItem item = new MenuItem(MessageManager.getString("label.reveal"));
82 item.addActionListener(new ActionListener()
84 public void actionPerformed(ActionEvent e)
86 av.showColumn(reveal[0]);
88 ap.paintAlignment(true);
89 if (ap.overviewPanel != null)
91 ap.overviewPanel.updateOverviewImage();
97 if (av.getColumnSelection().getHiddenColumns().size() > 1)
99 item = new MenuItem(MessageManager.getString("action.reveal_all"));
100 item.addActionListener(new ActionListener()
102 public void actionPerformed(ActionEvent e)
104 av.showAllHiddenColumns();
106 ap.paintAlignment(true);
107 if (ap.overviewPanel != null)
109 ap.overviewPanel.updateOverviewImage();
116 pop.show(this, evt.getX(), evt.getY());
118 else if (av.getColumnSelection().contains(res))
120 MenuItem item = new MenuItem(MessageManager.getString("label.hide_columns"));
121 item.addActionListener(new ActionListener()
123 public void actionPerformed(ActionEvent e)
125 av.hideColumns(res, res);
126 if (av.getSelectionGroup() != null
127 && av.getSelectionGroup().getSize() == av
128 .getAlignment().getHeight())
130 av.setSelectionGroup(null);
133 ap.paintAlignment(true);
134 if (ap.overviewPanel != null)
136 ap.overviewPanel.updateOverviewImage();
142 pop.show(this, evt.getX(), evt.getY());
146 // LEFT MOUSE TO SELECT
148 if (!evt.isControlDown() && !evt.isShiftDown())
150 av.getColumnSelection().clear();
153 av.getColumnSelection().addElement(res);
154 SequenceGroup sg = new SequenceGroup();
155 for (int i = 0; i < av.getAlignment().getSequences().size(); i++)
157 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
162 av.setSelectionGroup(sg);
164 if (evt.isShiftDown())
166 int min = Math.min(av.getColumnSelection().getMin(), res);
167 int max = Math.max(av.getColumnSelection().getMax(), res);
168 for (int i = min; i < max; i++)
170 av.getColumnSelection().addElement(i);
177 ap.paintAlignment(true);
181 public void mouseReleased(MouseEvent evt)
183 mouseDragging = false;
185 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
187 if (res > av.getAlignment().getWidth())
189 res = av.getAlignment().getWidth() - 1;
192 if (av.hasHiddenColumns())
194 res = av.getColumnSelection().adjustForHiddenColumns(res);
197 if (!stretchingGroup)
199 ap.paintAlignment(false);
204 SequenceGroup sg = av.getSelectionGroup();
206 if (res > sg.getStartRes())
210 else if (res < sg.getStartRes())
215 stretchingGroup = false;
216 ap.paintAlignment(false);
220 public void mouseDragged(MouseEvent evt)
222 mouseDragging = true;
224 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
230 if (av.hasHiddenColumns())
232 res = av.getColumnSelection().adjustForHiddenColumns(res);
235 if (res > av.getAlignment().getWidth())
237 res = av.getAlignment().getWidth() - 1;
250 SequenceGroup sg = av.getSelectionGroup();
254 stretchingGroup = true;
256 if (!av.getColumnSelection().contains(res))
258 av.getColumnSelection().addElement(res);
261 if (res > sg.getStartRes())
265 if (res < sg.getStartRes())
271 for (int i = min; i <= max; i++)
273 col = av.getColumnSelection().adjustForHiddenColumns(i);
275 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
277 av.getColumnSelection().removeElement(col);
281 av.getColumnSelection().addElement(col);
285 ap.paintAlignment(false);
289 public void mouseEntered(MouseEvent evt)
293 ap.seqPanel.scrollCanvas(null);
297 public void mouseExited(MouseEvent evt)
301 ap.seqPanel.scrollCanvas(evt);
305 public void mouseClicked(MouseEvent evt)
310 public void mouseMoved(MouseEvent evt)
312 if (!av.hasHiddenColumns())
317 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
319 res = av.getColumnSelection().adjustForHiddenColumns(res);
322 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
324 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
326 if (res + 1 == region[0] || res - 1 == region[1])
336 public void update(Graphics g)
341 public void paint(Graphics g)
343 drawScale(g, av.getStartRes(), av.getEndRes(), getSize().width,
347 // scalewidth will normally be screenwidth,
348 public void drawScale(Graphics gg, int startx, int endx, int width,
351 gg.setFont(av.getFont());
353 // Fill in the background
354 gg.setColor(Color.white);
355 gg.fillRect(0, 0, width, height);
356 gg.setColor(Color.black);
358 // Fill the selected columns
359 ColumnSelection cs = av.getColumnSelection();
360 gg.setColor(new Color(220, 0, 0));
362 for (int i = 0; i < cs.size(); i++)
364 int sel = cs.columnAt(i);
365 if (av.hasHiddenColumns())
367 sel = av.getColumnSelection().findColumnPosition(sel);
370 if ((sel >= startx) && (sel <= endx))
372 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
377 // Draw the scale numbers
378 gg.setColor(Color.black);
380 int scalestartx = (startx / 10) * 10;
382 FontMetrics fm = gg.getFontMetrics(av.getFont());
383 int y = av.charHeight - fm.getDescent();
385 if ((scalestartx % 10) == 0)
393 for (int i = scalestartx; i < endx; i += 5)
397 string = String.valueOf(av.getColumnSelection()
398 .adjustForHiddenColumns(i));
399 if ((i - startx - 1) * av.charWidth > maxX)
401 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
402 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
406 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
408 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
409 y + (fm.getDescent() * 2));
415 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
417 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
418 y + (fm.getDescent() * 2));
422 if (av.hasHiddenColumns())
424 gg.setColor(Color.blue);
426 if (av.getShowHiddenMarkers())
428 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
432 res = av.getColumnSelection().findHiddenRegionPosition(i)
435 if (res < 0 || res > endx - scalestartx)
440 gg.fillPolygon(new int[]
441 { res * av.charWidth - av.charHeight / 4,
442 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
444 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
450 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
452 gg.drawString(MessageManager.getString("label.reveal_columns"), reveal[0] * av.charWidth, 0);