2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.appletgui;
22 import java.awt.event.*;
24 import jalview.datamodel.*;
26 public class ScalePanel extends Panel implements MouseMotionListener,
30 protected int offy = 4;
34 protected AlignViewport av;
38 boolean stretchingGroup = false;
40 int min; // used by mouseDragged to see if user
42 int max; // used by mouseDragged to see if user
44 boolean mouseDragging = false;
48 public ScalePanel(AlignViewport av, AlignmentPanel ap)
54 addMouseListener(this);
55 addMouseMotionListener(this);
59 public void mousePressed(MouseEvent evt)
61 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
64 if (av.hasHiddenColumns)
66 res = av.getColumnSelection().adjustForHiddenColumns(x);
75 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
77 PopupMenu pop = new PopupMenu();
80 MenuItem item = new MenuItem("Reveal");
81 item.addActionListener(new ActionListener()
83 public void actionPerformed(ActionEvent e)
85 av.showColumn(reveal[0]);
87 ap.paintAlignment(true);
88 if (ap.overviewPanel != null)
90 ap.overviewPanel.updateOverviewImage();
96 if (av.getColumnSelection().getHiddenColumns().size() > 1)
98 item = new MenuItem("Reveal All");
99 item.addActionListener(new ActionListener()
101 public void actionPerformed(ActionEvent e)
103 av.showAllHiddenColumns();
105 ap.paintAlignment(true);
106 if (ap.overviewPanel != null)
108 ap.overviewPanel.updateOverviewImage();
115 pop.show(this, evt.getX(), evt.getY());
117 else if (av.getColumnSelection().contains(res))
119 MenuItem item = new MenuItem("Hide Columns");
120 item.addActionListener(new ActionListener()
122 public void actionPerformed(ActionEvent e)
124 av.hideColumns(res, res);
125 if (av.getSelectionGroup() != null
126 && av.getSelectionGroup().getSize() == av.alignment
129 av.setSelectionGroup(null);
132 ap.paintAlignment(true);
133 if (ap.overviewPanel != null)
135 ap.overviewPanel.updateOverviewImage();
141 pop.show(this, evt.getX(), evt.getY());
145 // LEFT MOUSE TO SELECT
147 if (!evt.isControlDown() && !evt.isShiftDown())
149 av.getColumnSelection().clear();
152 av.getColumnSelection().addElement(res);
153 SequenceGroup sg = new SequenceGroup();
154 for (int i = 0; i < av.alignment.getSequences().size(); i++)
156 sg.addSequence(av.alignment.getSequenceAt(i), false);
161 av.setSelectionGroup(sg);
163 if (evt.isShiftDown())
165 int min = Math.min(av.getColumnSelection().getMin(), res);
166 int max = Math.max(av.getColumnSelection().getMax(), res);
167 for (int i = min; i < max; i++)
169 av.getColumnSelection().addElement(i);
176 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.alignment.getWidth())
187 res = av.alignment.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);
217 public void mouseDragged(MouseEvent evt)
219 mouseDragging = true;
221 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
227 if (av.hasHiddenColumns)
229 res = av.getColumnSelection().adjustForHiddenColumns(res);
232 if (res > av.alignment.getWidth())
234 res = av.alignment.getWidth() - 1;
247 SequenceGroup sg = av.getSelectionGroup();
251 stretchingGroup = true;
253 if (!av.getColumnSelection().contains(res))
255 av.getColumnSelection().addElement(res);
258 if (res > sg.getStartRes())
262 if (res < sg.getStartRes())
268 for (int i = min; i <= max; i++)
270 col = av.getColumnSelection().adjustForHiddenColumns(i);
272 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
274 av.getColumnSelection().removeElement(col);
278 av.getColumnSelection().addElement(col);
282 ap.paintAlignment(false);
286 public void mouseEntered(MouseEvent evt)
290 ap.seqPanel.scrollCanvas(null);
294 public void mouseExited(MouseEvent evt)
298 ap.seqPanel.scrollCanvas(evt);
302 public void mouseClicked(MouseEvent evt)
307 public void mouseMoved(MouseEvent evt)
309 if (!av.hasHiddenColumns)
314 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
316 res = av.getColumnSelection().adjustForHiddenColumns(res);
319 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
321 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
323 if (res + 1 == region[0] || res - 1 == region[1])
333 public void update(Graphics g)
338 public void paint(Graphics g)
340 drawScale(g, av.getStartRes(), av.getEndRes(), getSize().width,
344 // scalewidth will normally be screenwidth,
345 public void drawScale(Graphics gg, int startx, int endx, int width,
348 gg.setFont(av.getFont());
350 // Fill in the background
351 gg.setColor(Color.white);
352 gg.fillRect(0, 0, width, height);
353 gg.setColor(Color.black);
355 // Fill the selected columns
356 ColumnSelection cs = av.getColumnSelection();
357 gg.setColor(new Color(220, 0, 0));
359 for (int i = 0; i < cs.size(); i++)
361 int sel = cs.columnAt(i);
362 if (av.hasHiddenColumns)
364 sel = av.getColumnSelection().findColumnPosition(sel);
367 if ((sel >= startx) && (sel <= endx))
369 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
374 // Draw the scale numbers
375 gg.setColor(Color.black);
377 int scalestartx = (startx / 10) * 10;
379 FontMetrics fm = gg.getFontMetrics(av.getFont());
380 int y = av.charHeight - fm.getDescent();
382 if ((scalestartx % 10) == 0)
390 for (int i = scalestartx; i < endx; i += 5)
394 string = String.valueOf(av.getColumnSelection()
395 .adjustForHiddenColumns(i));
396 if ((i - startx - 1) * av.charWidth > maxX)
398 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
399 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));
414 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
416 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
417 y + (fm.getDescent() * 2));
421 if (av.hasHiddenColumns)
423 gg.setColor(Color.blue);
425 if (av.getShowHiddenMarkers())
427 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
431 res = av.getColumnSelection().findHiddenRegionPosition(i)
434 if (res < 0 || res > endx - scalestartx)
439 gg.fillPolygon(new int[]
440 { res * av.charWidth - av.charHeight / 4,
441 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
443 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
449 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
451 gg.drawString("Reveal Columns", reveal[0] * av.charWidth, 0);