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.
23 import jalview.datamodel.ColumnSelection;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.MessageManager;
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.RenderingHints;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseMotionListener;
39 import javax.swing.JMenuItem;
40 import javax.swing.JPanel;
41 import javax.swing.JPopupMenu;
42 import javax.swing.SwingUtilities;
43 import javax.swing.ToolTipManager;
51 public class ScalePanel extends JPanel implements MouseMotionListener,
54 protected int offy = 4;
59 protected AlignViewport av;
63 boolean stretchingGroup = false;
65 int min; // used by mouseDragged to see if user
67 int max; // used by mouseDragged to see if user
69 boolean mouseDragging = false;
71 // wants to delete columns
72 public ScalePanel(AlignViewport av, AlignmentPanel ap)
77 addMouseListener(this);
78 addMouseMotionListener(this);
88 public void mousePressed(MouseEvent evt)
90 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
93 if (av.hasHiddenColumns())
95 x = av.getColumnSelection().adjustForHiddenColumns(x);
98 if (x >= av.getAlignment().getWidth())
100 res = av.getAlignment().getWidth() - 1;
110 if (SwingUtilities.isRightMouseButton(evt))
112 JPopupMenu pop = new JPopupMenu();
115 JMenuItem item = new JMenuItem(
116 MessageManager.getString("label.reveal"));
117 item.addActionListener(new ActionListener()
120 public void actionPerformed(ActionEvent e)
122 av.showColumn(reveal[0]);
124 ap.paintAlignment(true);
125 if (ap.overviewPanel != null)
127 ap.overviewPanel.updateOverviewImage();
133 if (av.getColumnSelection().hasHiddenColumns())
135 item = new JMenuItem(
136 MessageManager.getString("action.reveal_all"));
137 item.addActionListener(new ActionListener()
140 public void actionPerformed(ActionEvent e)
142 av.showAllHiddenColumns();
144 ap.paintAlignment(true);
145 if (ap.overviewPanel != null)
147 ap.overviewPanel.updateOverviewImage();
153 pop.show(this, evt.getX(), evt.getY());
155 else if (av.getColumnSelection().contains(res))
157 JMenuItem item = new JMenuItem(
158 MessageManager.getString("label.hide_columns"));
159 item.addActionListener(new ActionListener()
162 public void actionPerformed(ActionEvent e)
164 av.hideColumns(res, res);
165 if (av.getSelectionGroup() != null
166 && av.getSelectionGroup().getSize() == av
167 .getAlignment().getHeight())
169 av.setSelectionGroup(null);
172 ap.paintAlignment(true);
173 if (ap.overviewPanel != null)
175 ap.overviewPanel.updateOverviewImage();
181 pop.show(this, evt.getX(), evt.getY());
185 // LEFT MOUSE TO SELECT
187 if (!evt.isControlDown() && !evt.isShiftDown())
189 av.getColumnSelection().clear();
192 av.getColumnSelection().addElement(res);
193 SequenceGroup sg = new SequenceGroup();
194 // try to be as quick as possible
195 SequenceI[] iVec = av.getAlignment().getSequencesArray();
196 for (int i = 0; i < iVec.length; i++)
198 sg.addSequence(iVec[i], false);
205 if (evt.isShiftDown())
207 int min = Math.min(av.getColumnSelection().getMin(), res);
208 int max = Math.max(av.getColumnSelection().getMax(), res);
209 for (int i = min; i < max; i++)
211 av.getColumnSelection().addElement(i);
216 av.setSelectionGroup(sg);
219 ap.paintAlignment(false);
230 public void mouseReleased(MouseEvent evt)
232 mouseDragging = false;
234 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
236 if (av.hasHiddenColumns())
238 res = av.getColumnSelection().adjustForHiddenColumns(res);
241 if (res >= av.getAlignment().getWidth())
243 res = av.getAlignment().getWidth() - 1;
246 if (!stretchingGroup)
248 ap.paintAlignment(false);
253 SequenceGroup sg = av.getSelectionGroup();
257 if (res > sg.getStartRes())
261 else if (res < sg.getStartRes())
266 stretchingGroup = false;
267 ap.paintAlignment(false);
278 public void mouseDragged(MouseEvent evt)
280 mouseDragging = true;
282 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
288 if (av.hasHiddenColumns())
290 res = av.getColumnSelection().adjustForHiddenColumns(res);
293 if (res >= av.getAlignment().getWidth())
295 res = av.getAlignment().getWidth() - 1;
308 SequenceGroup sg = av.getSelectionGroup();
312 stretchingGroup = true;
314 if (!av.getColumnSelection().contains(res))
316 av.getColumnSelection().addElement(res);
319 if (res > sg.getStartRes())
323 if (res < sg.getStartRes())
329 for (int i = min; i <= max; i++)
331 col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
333 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
335 av.getColumnSelection().removeElement(col);
339 av.getColumnSelection().addElement(col);
343 ap.paintAlignment(false);
348 public void mouseEntered(MouseEvent evt)
352 ap.getSeqPanel().scrollCanvas(null);
357 public void mouseExited(MouseEvent evt)
361 ap.getSeqPanel().scrollCanvas(evt);
366 public void mouseClicked(MouseEvent evt)
371 public void mouseMoved(MouseEvent evt)
373 if (!av.hasHiddenColumns())
378 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
380 res = av.getColumnSelection().adjustForHiddenColumns(res);
383 if (av.getColumnSelection().getHiddenColumns() != null)
385 for (int[] region : av.getColumnSelection().getHiddenColumns())
387 if (res + 1 == region[0] || res - 1 == region[1])
390 ToolTipManager.sharedInstance().registerComponent(this);
391 this.setToolTipText(MessageManager
392 .getString("label.reveal_hidden_columns"));
397 this.setToolTipText(null);
413 public void paintComponent(Graphics g)
415 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
418 // scalewidth will normally be screenwidth,
419 public void drawScale(Graphics g, int startx, int endx, int width,
422 Graphics2D gg = (Graphics2D) g;
423 gg.setFont(av.getFont());
427 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
428 RenderingHints.VALUE_ANTIALIAS_ON);
431 // Fill in the background
432 gg.setColor(Color.white);
433 gg.fillRect(0, 0, width, height);
434 gg.setColor(Color.black);
436 // Fill the selected columns
437 ColumnSelection cs = av.getColumnSelection();
438 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
443 gg.setColor(new Color(220, 0, 0));
445 for (int sel : cs.getSelected())
447 if (av.hasHiddenColumns())
449 if (cs.isVisible(sel))
451 sel = cs.findColumnPosition(sel);
459 if ((sel >= startx) && (sel <= endx))
461 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
466 // Draw the scale numbers
467 gg.setColor(Color.black);
469 int scalestartx = (startx / 10) * 10;
471 FontMetrics fm = gg.getFontMetrics(av.getFont());
472 int y = avCharHeight - fm.getDescent();
474 if ((scalestartx % 10) == 0)
482 for (int i = scalestartx; i < endx; i += 5)
486 string = String.valueOf(av.getColumnSelection()
487 .adjustForHiddenColumns(i));
488 if ((i - startx - 1) * avCharWidth > maxX)
490 gg.drawString(string, (i - startx - 1) * avCharWidth, y);
491 maxX = (i - startx + 1) * avCharWidth + fm.stringWidth(string);
494 gg.drawLine(((i - startx - 1) * avCharWidth) + (avCharWidth / 2),
496 ((i - startx - 1) * avCharWidth) + (avCharWidth / 2), y
497 + (fm.getDescent() * 2));
501 gg.drawLine(((i - startx - 1) * avCharWidth) + (avCharWidth / 2), y
502 + fm.getDescent(), ((i - startx - 1) * avCharWidth)
503 + (avCharWidth / 2), y + (fm.getDescent() * 2));
507 if (av.hasHiddenColumns())
509 gg.setColor(Color.blue);
511 if (av.getShowHiddenMarkers()
512 && av.getColumnSelection().getHiddenColumns() != null)
514 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
518 res = av.getColumnSelection().findHiddenRegionPosition(i)
521 if (res < 0 || res > endx - scalestartx)
526 gg.fillPolygon(new int[] { res * avCharWidth - avCharHeight / 4,
527 res * avCharWidth + avCharHeight / 4, res * avCharWidth },
528 new int[] { y - avCharHeight / 2, y - avCharHeight / 2,
534 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
536 gg.drawString(MessageManager.getString("label.reveal_columns"),
537 reveal[0] * avCharWidth, 0);