2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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/>.
18 package jalview.appletgui;
21 import java.awt.event.*;
23 import jalview.datamodel.*;
25 public class ScalePanel extends Panel implements MouseMotionListener,
29 protected int offy = 4;
33 protected AlignViewport av;
37 boolean stretchingGroup = false;
39 int min; // used by mouseDragged to see if user
41 int max; // used by mouseDragged to see if user
43 boolean mouseDragging = false;
47 public ScalePanel(AlignViewport av, AlignmentPanel ap)
53 addMouseListener(this);
54 addMouseMotionListener(this);
58 public void mousePressed(MouseEvent evt)
60 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
63 if (av.hasHiddenColumns())
65 res = av.getColumnSelection().adjustForHiddenColumns(x);
74 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
76 PopupMenu pop = new PopupMenu();
79 MenuItem item = new MenuItem("Reveal");
80 item.addActionListener(new ActionListener()
82 public void actionPerformed(ActionEvent e)
84 av.showColumn(reveal[0]);
86 ap.paintAlignment(true);
87 if (ap.overviewPanel != null)
89 ap.overviewPanel.updateOverviewImage();
95 if (av.getColumnSelection().getHiddenColumns().size() > 1)
97 item = new MenuItem("Reveal All");
98 item.addActionListener(new ActionListener()
100 public void actionPerformed(ActionEvent e)
102 av.showAllHiddenColumns();
104 ap.paintAlignment(true);
105 if (ap.overviewPanel != null)
107 ap.overviewPanel.updateOverviewImage();
114 pop.show(this, evt.getX(), evt.getY());
116 else if (av.getColumnSelection().contains(res))
118 MenuItem item = new MenuItem("Hide Columns");
119 item.addActionListener(new ActionListener()
121 public void actionPerformed(ActionEvent e)
123 av.hideColumns(res, res);
124 if (av.getSelectionGroup() != null
125 && av.getSelectionGroup().getSize() == av.getAlignment()
128 av.setSelectionGroup(null);
131 ap.paintAlignment(true);
132 if (ap.overviewPanel != null)
134 ap.overviewPanel.updateOverviewImage();
140 pop.show(this, evt.getX(), evt.getY());
144 // LEFT MOUSE TO SELECT
146 if (!evt.isControlDown() && !evt.isShiftDown())
148 av.getColumnSelection().clear();
151 av.getColumnSelection().addElement(res);
152 SequenceGroup sg = new SequenceGroup();
153 for (int i = 0; i < av.getAlignment().getSequences().size(); i++)
155 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
160 av.setSelectionGroup(sg);
162 if (evt.isShiftDown())
164 int min = Math.min(av.getColumnSelection().getMin(), res);
165 int max = Math.max(av.getColumnSelection().getMax(), res);
166 for (int i = min; i < max; i++)
168 av.getColumnSelection().addElement(i);
175 ap.paintAlignment(true);
179 public void mouseReleased(MouseEvent evt)
181 mouseDragging = false;
183 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
185 if (res > av.getAlignment().getWidth())
187 res = av.getAlignment().getWidth() - 1;
190 if (av.hasHiddenColumns())
192 res = av.getColumnSelection().adjustForHiddenColumns(res);
195 if (!stretchingGroup)
197 ap.paintAlignment(false);
202 SequenceGroup sg = av.getSelectionGroup();
204 if (res > sg.getStartRes())
208 else if (res < sg.getStartRes())
213 stretchingGroup = false;
214 ap.paintAlignment(false);
218 public void mouseDragged(MouseEvent evt)
220 mouseDragging = true;
222 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
228 if (av.hasHiddenColumns())
230 res = av.getColumnSelection().adjustForHiddenColumns(res);
233 if (res > av.getAlignment().getWidth())
235 res = av.getAlignment().getWidth() - 1;
248 SequenceGroup sg = av.getSelectionGroup();
252 stretchingGroup = true;
254 if (!av.getColumnSelection().contains(res))
256 av.getColumnSelection().addElement(res);
259 if (res > sg.getStartRes())
263 if (res < sg.getStartRes())
269 for (int i = min; i <= max; i++)
271 col = av.getColumnSelection().adjustForHiddenColumns(i);
273 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
275 av.getColumnSelection().removeElement(col);
279 av.getColumnSelection().addElement(col);
283 ap.paintAlignment(false);
287 public void mouseEntered(MouseEvent evt)
291 ap.seqPanel.scrollCanvas(null);
295 public void mouseExited(MouseEvent evt)
299 ap.seqPanel.scrollCanvas(evt);
303 public void mouseClicked(MouseEvent evt)
308 public void mouseMoved(MouseEvent evt)
310 if (!av.hasHiddenColumns())
315 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
317 res = av.getColumnSelection().adjustForHiddenColumns(res);
320 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
322 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
324 if (res + 1 == region[0] || res - 1 == region[1])
334 public void update(Graphics g)
339 public void paint(Graphics g)
341 drawScale(g, av.getStartRes(), av.getEndRes(), getSize().width,
345 // scalewidth will normally be screenwidth,
346 public void drawScale(Graphics gg, int startx, int endx, int width,
349 gg.setFont(av.getFont());
351 // Fill in the background
352 gg.setColor(Color.white);
353 gg.fillRect(0, 0, width, height);
354 gg.setColor(Color.black);
356 // Fill the selected columns
357 ColumnSelection cs = av.getColumnSelection();
358 gg.setColor(new Color(220, 0, 0));
360 for (int i = 0; i < cs.size(); i++)
362 int sel = cs.columnAt(i);
363 if (av.hasHiddenColumns())
365 sel = av.getColumnSelection().findColumnPosition(sel);
368 if ((sel >= startx) && (sel <= endx))
370 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
375 // Draw the scale numbers
376 gg.setColor(Color.black);
378 int scalestartx = (startx / 10) * 10;
380 FontMetrics fm = gg.getFontMetrics(av.getFont());
381 int y = av.charHeight - fm.getDescent();
383 if ((scalestartx % 10) == 0)
391 for (int i = scalestartx; i < endx; i += 5)
395 string = String.valueOf(av.getColumnSelection()
396 .adjustForHiddenColumns(i));
397 if ((i - startx - 1) * av.charWidth > maxX)
399 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
400 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
404 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
406 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
407 y + (fm.getDescent() * 2));
413 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
415 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
416 y + (fm.getDescent() * 2));
420 if (av.hasHiddenColumns())
422 gg.setColor(Color.blue);
424 if (av.getShowHiddenMarkers())
426 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
430 res = av.getColumnSelection().findHiddenRegionPosition(i)
433 if (res < 0 || res > endx - scalestartx)
438 gg.fillPolygon(new int[]
439 { res * av.charWidth - av.charHeight / 4,
440 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
442 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
448 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
450 gg.drawString("Reveal Columns", reveal[0] * av.charWidth, 0);