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.
24 import java.awt.event.*;
28 import jalview.datamodel.*;
29 import jalview.util.MessageManager;
37 public class ScalePanel extends JPanel implements MouseMotionListener,
40 protected int offy = 4;
45 protected AlignViewport av;
49 boolean stretchingGroup = false;
51 int min; // used by mouseDragged to see if user
53 int max; // used by mouseDragged to see if user
55 boolean mouseDragging = false;
57 // wants to delete columns
58 public ScalePanel(AlignViewport av, AlignmentPanel ap)
63 addMouseListener(this);
64 addMouseMotionListener(this);
73 public void mousePressed(MouseEvent evt)
75 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
78 if (av.hasHiddenColumns())
80 x = av.getColumnSelection().adjustForHiddenColumns(x);
83 if (x >= av.getAlignment().getWidth())
85 res = av.getAlignment().getWidth() - 1;
95 if (SwingUtilities.isRightMouseButton(evt))
97 JPopupMenu pop = new JPopupMenu();
100 JMenuItem item = new JMenuItem(
101 MessageManager.getString("label.reveal"));
102 item.addActionListener(new ActionListener()
104 public void actionPerformed(ActionEvent e)
106 av.showColumn(reveal[0]);
108 ap.paintAlignment(true);
109 if (ap.overviewPanel != null)
111 ap.overviewPanel.updateOverviewImage();
117 if (av.getColumnSelection().getHiddenColumns().size() > 1)
119 item = new JMenuItem(
120 MessageManager.getString("action.reveal_all"));
121 item.addActionListener(new ActionListener()
123 public void actionPerformed(ActionEvent e)
125 av.showAllHiddenColumns();
127 ap.paintAlignment(true);
128 if (ap.overviewPanel != null)
130 ap.overviewPanel.updateOverviewImage();
136 pop.show(this, evt.getX(), evt.getY());
138 else if (av.getColumnSelection().contains(res))
140 JMenuItem item = new JMenuItem(
141 MessageManager.getString("label.hide_columns"));
142 item.addActionListener(new ActionListener()
144 public void actionPerformed(ActionEvent e)
146 av.hideColumns(res, res);
147 if (av.getSelectionGroup() != null
148 && av.getSelectionGroup().getSize() == av
149 .getAlignment().getHeight())
151 av.setSelectionGroup(null);
154 ap.paintAlignment(true);
155 if (ap.overviewPanel != null)
157 ap.overviewPanel.updateOverviewImage();
162 pop.show(this, evt.getX(), evt.getY());
166 // LEFT MOUSE TO SELECT
168 if (!evt.isControlDown() && !evt.isShiftDown())
170 av.getColumnSelection().clear();
173 av.getColumnSelection().addElement(res);
174 SequenceGroup sg = new SequenceGroup();
175 // try to be as quick as possible
176 SequenceI[] iVec = av.getAlignment().getSequencesArray();
177 for (int i = 0; i < iVec.length; i++)
179 sg.addSequence(iVec[i], false);
186 if (evt.isShiftDown())
188 int min = Math.min(av.getColumnSelection().getMin(), res);
189 int max = Math.max(av.getColumnSelection().getMax(), res);
190 for (int i = min; i < max; i++)
192 av.getColumnSelection().addElement(i);
197 av.setSelectionGroup(sg);
200 ap.paintAlignment(false);
210 public void mouseReleased(MouseEvent evt)
212 mouseDragging = false;
214 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
216 if (av.hasHiddenColumns())
218 res = av.getColumnSelection().adjustForHiddenColumns(res);
221 if (res >= av.getAlignment().getWidth())
223 res = av.getAlignment().getWidth() - 1;
226 if (!stretchingGroup)
228 ap.paintAlignment(false);
233 SequenceGroup sg = av.getSelectionGroup();
237 if (res > sg.getStartRes())
241 else if (res < sg.getStartRes())
246 stretchingGroup = false;
247 ap.paintAlignment(false);
257 public void mouseDragged(MouseEvent evt)
259 mouseDragging = true;
261 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
267 if (av.hasHiddenColumns())
269 res = av.getColumnSelection().adjustForHiddenColumns(res);
272 if (res >= av.getAlignment().getWidth())
274 res = av.getAlignment().getWidth() - 1;
287 SequenceGroup sg = av.getSelectionGroup();
291 stretchingGroup = true;
293 if (!av.getColumnSelection().contains(res))
295 av.getColumnSelection().addElement(res);
298 if (res > sg.getStartRes())
302 if (res < sg.getStartRes())
308 for (int i = min; i <= max; i++)
310 col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
312 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
314 av.getColumnSelection().removeElement(col);
318 av.getColumnSelection().addElement(col);
322 ap.paintAlignment(false);
326 public void mouseEntered(MouseEvent evt)
330 ap.seqPanel.scrollCanvas(null);
334 public void mouseExited(MouseEvent evt)
338 ap.seqPanel.scrollCanvas(evt);
342 public void mouseClicked(MouseEvent evt)
346 public void mouseMoved(MouseEvent evt)
348 if (!av.hasHiddenColumns())
353 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
355 res = av.getColumnSelection().adjustForHiddenColumns(res);
358 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
360 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
362 if (res + 1 == region[0] || res - 1 == region[1])
365 ToolTipManager.sharedInstance().registerComponent(this);
366 this.setToolTipText(MessageManager
367 .getString("label.reveal_hidden_columns"));
372 this.setToolTipText(null);
388 public void paintComponent(Graphics g)
390 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
393 // scalewidth will normally be screenwidth,
394 public void drawScale(Graphics g, int startx, int endx, int width,
397 Graphics2D gg = (Graphics2D) g;
398 gg.setFont(av.getFont());
402 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
403 RenderingHints.VALUE_ANTIALIAS_ON);
406 // Fill in the background
407 gg.setColor(Color.white);
408 gg.fillRect(0, 0, width, height);
409 gg.setColor(Color.black);
411 // Fill the selected columns
412 ColumnSelection cs = av.getColumnSelection();
416 gg.setColor(new Color(220, 0, 0));
418 for (int i = 0; i < cs.size(); i++)
420 int sel = cs.columnAt(i);
421 if (av.hasHiddenColumns())
423 if (cs.isVisible(sel))
425 sel = cs.findColumnPosition(sel);
433 if ((sel >= startx) && (sel <= endx))
435 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
440 // Draw the scale numbers
441 gg.setColor(Color.black);
443 int scalestartx = (startx / 10) * 10;
445 FontMetrics fm = gg.getFontMetrics(av.getFont());
446 int y = av.charHeight - fm.getDescent();
448 if ((scalestartx % 10) == 0)
456 for (int i = scalestartx; i < endx; i += 5)
460 string = String.valueOf(av.getColumnSelection()
461 .adjustForHiddenColumns(i));
462 if ((i - startx - 1) * av.charWidth > maxX)
464 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
465 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
469 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
471 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
472 y + (fm.getDescent() * 2));
478 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
480 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
481 y + (fm.getDescent() * 2));
485 if (av.hasHiddenColumns())
487 gg.setColor(Color.blue);
489 if (av.getShowHiddenMarkers())
491 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
495 res = av.getColumnSelection().findHiddenRegionPosition(i)
498 if (res < 0 || res > endx - scalestartx)
503 gg.fillPolygon(new int[]
504 { res * av.charWidth - av.charHeight / 4,
505 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
507 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
513 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
515 gg.drawString(MessageManager.getString("label.reveal_columns"),
516 reveal[0] * av.charWidth, 0);