2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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.
22 import java.awt.event.*;
26 import jalview.datamodel.*;
27 import jalview.util.MessageManager;
35 public class ScalePanel extends JPanel implements MouseMotionListener,
38 protected int offy = 4;
43 protected AlignViewport av;
47 boolean stretchingGroup = false;
49 int min; // used by mouseDragged to see if user
51 int max; // used by mouseDragged to see if user
53 boolean mouseDragging = false;
55 // wants to delete columns
56 public ScalePanel(AlignViewport av, AlignmentPanel ap)
61 addMouseListener(this);
62 addMouseMotionListener(this);
71 public void mousePressed(MouseEvent evt)
73 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
76 if (av.hasHiddenColumns())
78 x = av.getColumnSelection().adjustForHiddenColumns(x);
81 if (x >= av.getAlignment().getWidth())
83 res = av.getAlignment().getWidth() - 1;
93 if (SwingUtilities.isRightMouseButton(evt))
95 JPopupMenu pop = new JPopupMenu();
98 JMenuItem item = new JMenuItem(MessageManager.getString("label.reveal"));
99 item.addActionListener(new ActionListener()
101 public void actionPerformed(ActionEvent e)
103 av.showColumn(reveal[0]);
105 ap.paintAlignment(true);
106 if (ap.overviewPanel != null)
108 ap.overviewPanel.updateOverviewImage();
114 if (av.getColumnSelection().getHiddenColumns().size() > 1)
116 item = new JMenuItem(MessageManager.getString("action.reveal_all"));
117 item.addActionListener(new ActionListener()
119 public void actionPerformed(ActionEvent e)
121 av.showAllHiddenColumns();
123 ap.paintAlignment(true);
124 if (ap.overviewPanel != null)
126 ap.overviewPanel.updateOverviewImage();
132 pop.show(this, evt.getX(), evt.getY());
134 else if (av.getColumnSelection().contains(res))
136 JMenuItem item = new JMenuItem(MessageManager.getString("label.hide_columns"));
137 item.addActionListener(new ActionListener()
139 public void actionPerformed(ActionEvent e)
141 av.hideColumns(res, res);
142 if (av.getSelectionGroup() != null
143 && av.getSelectionGroup().getSize() == av
144 .getAlignment().getHeight())
146 av.setSelectionGroup(null);
149 ap.paintAlignment(true);
150 if (ap.overviewPanel != null)
152 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 // try to be as quick as possible
171 SequenceI[] iVec = av.getAlignment().getSequencesArray();
172 for (int i = 0; i < iVec.length; i++)
174 sg.addSequence(iVec[i], false);
181 if (evt.isShiftDown())
183 int min = Math.min(av.getColumnSelection().getMin(), res);
184 int max = Math.max(av.getColumnSelection().getMax(), res);
185 for (int i = min; i < max; i++)
187 av.getColumnSelection().addElement(i);
192 av.setSelectionGroup(sg);
195 ap.paintAlignment(false);
205 public void mouseReleased(MouseEvent evt)
207 mouseDragging = false;
209 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
211 if (av.hasHiddenColumns())
213 res = av.getColumnSelection().adjustForHiddenColumns(res);
216 if (res >= av.getAlignment().getWidth())
218 res = av.getAlignment().getWidth() - 1;
221 if (!stretchingGroup)
223 ap.paintAlignment(false);
228 SequenceGroup sg = av.getSelectionGroup();
232 if (res > sg.getStartRes())
236 else if (res < sg.getStartRes())
241 stretchingGroup = false;
242 ap.paintAlignment(false);
252 public void mouseDragged(MouseEvent evt)
254 mouseDragging = true;
256 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
262 if (av.hasHiddenColumns())
264 res = av.getColumnSelection().adjustForHiddenColumns(res);
267 if (res >= av.getAlignment().getWidth())
269 res = av.getAlignment().getWidth() - 1;
282 SequenceGroup sg = av.getSelectionGroup();
286 stretchingGroup = true;
288 if (!av.getColumnSelection().contains(res))
290 av.getColumnSelection().addElement(res);
293 if (res > sg.getStartRes())
297 if (res < sg.getStartRes())
303 for (int i = min; i <= max; i++)
305 col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
307 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
309 av.getColumnSelection().removeElement(col);
313 av.getColumnSelection().addElement(col);
317 ap.paintAlignment(false);
321 public void mouseEntered(MouseEvent evt)
325 ap.seqPanel.scrollCanvas(null);
329 public void mouseExited(MouseEvent evt)
333 ap.seqPanel.scrollCanvas(evt);
337 public void mouseClicked(MouseEvent evt)
341 public void mouseMoved(MouseEvent evt)
343 if (!av.hasHiddenColumns())
348 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
350 res = av.getColumnSelection().adjustForHiddenColumns(res);
353 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
355 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
357 if (res + 1 == region[0] || res - 1 == region[1])
360 ToolTipManager.sharedInstance().registerComponent(this);
361 this.setToolTipText(MessageManager.getString("label.reveal_hidden_columns"));
366 this.setToolTipText(null);
382 public void paintComponent(Graphics g)
384 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
387 // scalewidth will normally be screenwidth,
388 public void drawScale(Graphics g, int startx, int endx, int width,
391 Graphics2D gg = (Graphics2D) g;
392 gg.setFont(av.getFont());
396 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
397 RenderingHints.VALUE_ANTIALIAS_ON);
400 // Fill in the background
401 gg.setColor(Color.white);
402 gg.fillRect(0, 0, width, height);
403 gg.setColor(Color.black);
405 // Fill the selected columns
406 ColumnSelection cs = av.getColumnSelection();
410 gg.setColor(new Color(220, 0, 0));
412 for (int i = 0; i < cs.size(); i++)
414 int sel = cs.columnAt(i);
415 if (av.hasHiddenColumns())
417 if (cs.isVisible(sel))
419 sel = cs.findColumnPosition(sel);
427 if ((sel >= startx) && (sel <= endx))
429 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
434 // Draw the scale numbers
435 gg.setColor(Color.black);
437 int scalestartx = (startx / 10) * 10;
439 FontMetrics fm = gg.getFontMetrics(av.getFont());
440 int y = av.charHeight - fm.getDescent();
442 if ((scalestartx % 10) == 0)
450 for (int i = scalestartx; i < endx; i += 5)
454 string = String.valueOf(av.getColumnSelection()
455 .adjustForHiddenColumns(i));
456 if ((i - startx - 1) * av.charWidth > maxX)
458 gg.drawString(string, (i - startx - 1) * av.charWidth, y);
459 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
463 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
465 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
466 y + (fm.getDescent() * 2));
472 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
474 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
475 y + (fm.getDescent() * 2));
479 if (av.hasHiddenColumns())
481 gg.setColor(Color.blue);
483 if (av.getShowHiddenMarkers())
485 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
489 res = av.getColumnSelection().findHiddenRegionPosition(i)
492 if (res < 0 || res > endx - scalestartx)
497 gg.fillPolygon(new int[]
498 { res * av.charWidth - av.charHeight / 4,
499 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
501 { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
507 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
509 gg.drawString(MessageManager.getString("label.reveal_columns"), reveal[0] * av.charWidth, 0);