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);
87 public void mousePressed(MouseEvent evt)
89 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
92 if (av.hasHiddenColumns())
94 x = av.getColumnSelection().adjustForHiddenColumns(x);
97 if (x >= av.getAlignment().getWidth())
99 res = av.getAlignment().getWidth() - 1;
109 if (SwingUtilities.isRightMouseButton(evt))
111 JPopupMenu pop = new JPopupMenu();
114 JMenuItem item = new JMenuItem(
115 MessageManager.getString("label.reveal"));
116 item.addActionListener(new ActionListener()
118 public void actionPerformed(ActionEvent e)
120 av.showColumn(reveal[0]);
122 ap.paintAlignment(true);
123 if (ap.overviewPanel != null)
125 ap.overviewPanel.updateOverviewImage();
131 if (av.getColumnSelection().hasHiddenColumns())
133 item = new JMenuItem(
134 MessageManager.getString("action.reveal_all"));
135 item.addActionListener(new ActionListener()
137 public void actionPerformed(ActionEvent e)
139 av.showAllHiddenColumns();
141 ap.paintAlignment(true);
142 if (ap.overviewPanel != null)
144 ap.overviewPanel.updateOverviewImage();
150 pop.show(this, evt.getX(), evt.getY());
152 else if (av.getColumnSelection().contains(res))
154 JMenuItem item = new JMenuItem(
155 MessageManager.getString("label.hide_columns"));
156 item.addActionListener(new ActionListener()
158 public void actionPerformed(ActionEvent e)
160 av.hideColumns(res, res);
161 if (av.getSelectionGroup() != null
162 && av.getSelectionGroup().getSize() == av
163 .getAlignment().getHeight())
165 av.setSelectionGroup(null);
168 ap.paintAlignment(true);
169 if (ap.overviewPanel != null)
171 ap.overviewPanel.updateOverviewImage();
176 pop.show(this, evt.getX(), evt.getY());
180 // LEFT MOUSE TO SELECT
182 if (!evt.isControlDown() && !evt.isShiftDown())
184 av.getColumnSelection().clear();
187 av.getColumnSelection().addElement(res);
188 SequenceGroup sg = new SequenceGroup();
189 // try to be as quick as possible
190 SequenceI[] iVec = av.getAlignment().getSequencesArray();
191 for (int i = 0; i < iVec.length; i++)
193 sg.addSequence(iVec[i], false);
200 if (evt.isShiftDown())
202 int min = Math.min(av.getColumnSelection().getMin(), res);
203 int max = Math.max(av.getColumnSelection().getMax(), res);
204 for (int i = min; i < max; i++)
206 av.getColumnSelection().addElement(i);
211 av.setSelectionGroup(sg);
214 ap.paintAlignment(false);
224 public void mouseReleased(MouseEvent evt)
226 mouseDragging = false;
228 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
230 if (av.hasHiddenColumns())
232 res = av.getColumnSelection().adjustForHiddenColumns(res);
235 if (res >= av.getAlignment().getWidth())
237 res = av.getAlignment().getWidth() - 1;
240 if (!stretchingGroup)
242 ap.paintAlignment(false);
247 SequenceGroup sg = av.getSelectionGroup();
251 if (res > sg.getStartRes())
255 else if (res < sg.getStartRes())
260 stretchingGroup = false;
261 ap.paintAlignment(false);
271 public void mouseDragged(MouseEvent evt)
273 mouseDragging = true;
275 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
281 if (av.hasHiddenColumns())
283 res = av.getColumnSelection().adjustForHiddenColumns(res);
286 if (res >= av.getAlignment().getWidth())
288 res = av.getAlignment().getWidth() - 1;
301 SequenceGroup sg = av.getSelectionGroup();
305 stretchingGroup = true;
307 if (!av.getColumnSelection().contains(res))
309 av.getColumnSelection().addElement(res);
312 if (res > sg.getStartRes())
316 if (res < sg.getStartRes())
322 for (int i = min; i <= max; i++)
324 col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
326 if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
328 av.getColumnSelection().removeElement(col);
332 av.getColumnSelection().addElement(col);
336 ap.paintAlignment(false);
340 public void mouseEntered(MouseEvent evt)
344 ap.getSeqPanel().scrollCanvas(null);
348 public void mouseExited(MouseEvent evt)
352 ap.getSeqPanel().scrollCanvas(evt);
356 public void mouseClicked(MouseEvent evt)
360 public void mouseMoved(MouseEvent evt)
362 if (!av.hasHiddenColumns())
367 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
369 res = av.getColumnSelection().adjustForHiddenColumns(res);
372 if (av.getColumnSelection().getHiddenColumns() != null)
374 for (int[] region : av.getColumnSelection().getHiddenColumns())
376 if (res + 1 == region[0] || res - 1 == region[1])
379 ToolTipManager.sharedInstance().registerComponent(this);
380 this.setToolTipText(MessageManager
381 .getString("label.reveal_hidden_columns"));
386 this.setToolTipText(null);
401 public void paintComponent(Graphics g)
403 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
406 // scalewidth will normally be screenwidth,
407 public void drawScale(Graphics g, int startx, int endx, int width,
410 Graphics2D gg = (Graphics2D) g;
411 gg.setFont(av.getFont());
415 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
416 RenderingHints.VALUE_ANTIALIAS_ON);
419 // Fill in the background
420 gg.setColor(Color.white);
421 gg.fillRect(0, 0, width, height);
422 gg.setColor(Color.black);
424 // Fill the selected columns
425 ColumnSelection cs = av.getColumnSelection();
426 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
431 gg.setColor(new Color(220, 0, 0));
433 for (int i = 0; i < cs.size(); i++)
435 int sel = cs.columnAt(i);
436 if (av.hasHiddenColumns())
438 if (cs.isVisible(sel))
440 sel = cs.findColumnPosition(sel);
448 if ((sel >= startx) && (sel <= endx))
450 gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
455 // Draw the scale numbers
456 gg.setColor(Color.black);
458 int scalestartx = (startx / 10) * 10;
460 FontMetrics fm = gg.getFontMetrics(av.getFont());
461 int y = avCharHeight - fm.getDescent();
463 if ((scalestartx % 10) == 0)
471 for (int i = scalestartx; i < endx; i += 5)
475 string = String.valueOf(av.getColumnSelection()
476 .adjustForHiddenColumns(i));
477 if ((i - startx - 1) * avCharWidth > maxX)
479 gg.drawString(string, (i - startx - 1) * avCharWidth, y);
480 maxX = (i - startx + 1) * avCharWidth + fm.stringWidth(string);
483 gg.drawLine(((i - startx - 1) * avCharWidth) + (avCharWidth / 2),
485 ((i - startx - 1) * avCharWidth) + (avCharWidth / 2),
486 y + (fm.getDescent() * 2));
490 gg.drawLine(((i - startx - 1) * avCharWidth) + (avCharWidth / 2),
491 y + fm.getDescent(), ((i - startx - 1) * avCharWidth)
492 + (avCharWidth / 2), y + (fm.getDescent() * 2));
496 if (av.hasHiddenColumns())
498 gg.setColor(Color.blue);
500 if (av.getShowHiddenMarkers()
501 && av.getColumnSelection().getHiddenColumns() != null)
503 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
507 res = av.getColumnSelection().findHiddenRegionPosition(i)
510 if (res < 0 || res > endx - scalestartx)
515 gg.fillPolygon(new int[]
516 { res * avCharWidth - avCharHeight / 4,
517 res * avCharWidth + avCharHeight / 4, res * avCharWidth },
519 { y - avCharHeight / 2, y - avCharHeight / 2, y + 8 },
525 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
527 gg.drawString(MessageManager.getString("label.reveal_columns"),
528 reveal[0] * avCharWidth, 0);