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.util.MessageManager;
27 import java.awt.Color;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics;
30 import java.awt.MenuItem;
31 import java.awt.Panel;
32 import java.awt.PopupMenu;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.InputEvent;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38 import java.awt.event.MouseMotionListener;
40 public class ScalePanel extends Panel implements MouseMotionListener,
44 protected int offy = 4;
48 protected AlignViewport av;
52 boolean stretchingGroup = false;
54 int min; // used by mouseDragged to see if user
56 int max; // used by mouseDragged to see if user
58 boolean mouseDragging = false;
62 public ScalePanel(AlignViewport av, AlignmentPanel ap)
68 addMouseListener(this);
69 addMouseMotionListener(this);
73 public void mousePressed(MouseEvent evt)
75 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
78 if (av.hasHiddenColumns())
80 res = av.getColumnSelection().adjustForHiddenColumns(x);
89 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
91 PopupMenu pop = new PopupMenu();
94 MenuItem item = new MenuItem(
95 MessageManager.getString("label.reveal"));
96 item.addActionListener(new ActionListener()
98 public void actionPerformed(ActionEvent e)
100 av.showColumn(reveal[0]);
102 ap.paintAlignment(true);
103 if (ap.overviewPanel != null)
105 ap.overviewPanel.updateOverviewImage();
111 if (av.getColumnSelection().hasManyHiddenColumns())
113 item = new MenuItem(MessageManager.getString("action.reveal_all"));
114 item.addActionListener(new ActionListener()
116 public void actionPerformed(ActionEvent e)
118 av.showAllHiddenColumns();
120 ap.paintAlignment(true);
121 if (ap.overviewPanel != null)
123 ap.overviewPanel.updateOverviewImage();
130 pop.show(this, evt.getX(), evt.getY());
132 else if (av.getColumnSelection().contains(res))
134 MenuItem item = new MenuItem(
135 MessageManager.getString("label.hide_columns"));
136 item.addActionListener(new ActionListener()
138 public void actionPerformed(ActionEvent e)
140 av.hideColumns(res, res);
141 if (av.getSelectionGroup() != null
142 && av.getSelectionGroup().getSize() == av
143 .getAlignment().getHeight())
145 av.setSelectionGroup(null);
148 ap.paintAlignment(true);
149 if (ap.overviewPanel != null)
151 ap.overviewPanel.updateOverviewImage();
157 pop.show(this, evt.getX(), evt.getY());
161 // LEFT MOUSE TO SELECT
163 if (!evt.isControlDown() && !evt.isShiftDown())
165 av.getColumnSelection().clear();
168 av.getColumnSelection().addElement(res);
169 SequenceGroup sg = new SequenceGroup();
170 for (int i = 0; i < av.getAlignment().getSequences().size(); i++)
172 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
177 av.setSelectionGroup(sg);
179 if (evt.isShiftDown())
181 int min = Math.min(av.getColumnSelection().getMin(), res);
182 int max = Math.max(av.getColumnSelection().getMax(), res);
183 for (int i = min; i < max; i++)
185 av.getColumnSelection().addElement(i);
192 ap.paintAlignment(true);
196 public void mouseReleased(MouseEvent evt)
198 mouseDragging = false;
200 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
202 if (res > av.getAlignment().getWidth())
204 res = av.getAlignment().getWidth() - 1;
207 if (av.hasHiddenColumns())
209 res = av.getColumnSelection().adjustForHiddenColumns(res);
212 if (!stretchingGroup)
214 ap.paintAlignment(false);
219 SequenceGroup sg = av.getSelectionGroup();
221 if (res > sg.getStartRes())
225 else if (res < sg.getStartRes())
230 stretchingGroup = false;
231 ap.paintAlignment(false);
235 public void mouseDragged(MouseEvent evt)
237 mouseDragging = true;
239 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
245 if (av.hasHiddenColumns())
247 res = av.getColumnSelection().adjustForHiddenColumns(res);
250 if (res > av.getAlignment().getWidth())
252 res = av.getAlignment().getWidth() - 1;
265 SequenceGroup sg = av.getSelectionGroup();
269 stretchingGroup = true;
271 if (!av.getColumnSelection().contains(res))
273 av.getColumnSelection().addElement(res);
276 if (res > sg.getStartRes())
280 if (res < sg.getStartRes())
286 for (int i = min; i <= max; i++)
288 col = av.getColumnSelection().adjustForHiddenColumns(i);
290 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
292 av.getColumnSelection().removeElement(col);
296 av.getColumnSelection().addElement(col);
300 ap.paintAlignment(false);
304 public void mouseEntered(MouseEvent evt)
308 ap.seqPanel.scrollCanvas(null);
312 public void mouseExited(MouseEvent evt)
316 ap.seqPanel.scrollCanvas(evt);
320 public void mouseClicked(MouseEvent evt)
325 public void mouseMoved(MouseEvent evt)
327 if (!av.hasHiddenColumns())
332 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
334 res = av.getColumnSelection().adjustForHiddenColumns(res);
337 for (int[] region : av.getColumnSelection().getHiddenColumns())
339 if (res + 1 == region[0] || res - 1 == region[1])
349 public void update(Graphics g)
354 public void paint(Graphics g)
356 drawScale(g, av.getStartRes(), av.getEndRes(), getSize().width,
360 // scalewidth will normally be screenwidth,
361 public void drawScale(Graphics gg, int startx, int endx, int width,
364 gg.setFont(av.getFont());
365 // Fill in the background
366 gg.setColor(Color.white);
367 gg.fillRect(0, 0, width, height);
368 gg.setColor(Color.black);
370 // Fill the selected columns
371 ColumnSelection cs = av.getColumnSelection();
372 gg.setColor(new Color(220, 0, 0));
373 int avcharWidth = av.getCharWidth(), avcharHeight = av.getCharHeight();
374 for (int i = 0; i < cs.size(); i++)
376 int sel = cs.columnAt(i);
377 if (av.hasHiddenColumns())
379 sel = av.getColumnSelection().findColumnPosition(sel);
382 if ((sel >= startx) && (sel <= endx))
384 gg.fillRect((sel - startx) * avcharWidth, 0, avcharWidth,
389 // Draw the scale numbers
390 gg.setColor(Color.black);
392 int scalestartx = (startx / 10) * 10;
394 FontMetrics fm = gg.getFontMetrics(av.getFont());
395 int y = avcharHeight - fm.getDescent();
397 if ((scalestartx % 10) == 0)
405 for (int i = scalestartx; i < endx; i += 5)
409 string = String.valueOf(av.getColumnSelection()
410 .adjustForHiddenColumns(i));
411 if ((i - startx - 1) * avcharWidth > maxX)
413 gg.drawString(string, (i - startx - 1) * avcharWidth, y);
414 maxX = (i - startx + 1) * avcharWidth + fm.stringWidth(string);
418 ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
420 ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
421 y + (fm.getDescent() * 2));
427 ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
429 ((i - startx - 1) * avcharWidth)
431 y + (fm.getDescent() * 2));
435 if (av.hasHiddenColumns())
437 gg.setColor(Color.blue);
439 if (av.getShowHiddenMarkers())
441 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
445 res = av.getColumnSelection().findHiddenRegionPosition(i)
448 if (res < 0 || res > endx - scalestartx)
453 gg.fillPolygon(new int[]
454 { res * avcharWidth - avcharHeight / 4,
455 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
457 { y - avcharHeight / 2, y - avcharHeight / 2, y + 8 },
463 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
465 gg.drawString(MessageManager.getString("label.reveal_columns"),
466 reveal[0] * avcharWidth, 0);