2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import jalview.datamodel.*;
24 import java.awt.event.*;
35 public class ScalePanel extends JPanel implements MouseMotionListener, MouseListener
37 protected int offy = 4;
41 protected AlignViewport av;
43 boolean stretchingGroup = false;
44 int min; //used by mouseDragged to see if user
45 int max; //used by mouseDragged to see if user
46 boolean mouseDragging = false;
48 // wants to delete columns
49 public ScalePanel(AlignViewport av, AlignmentPanel ap)
54 addMouseListener(this);
55 addMouseMotionListener(this);
61 * @param evt DOCUMENT ME!
63 public void mousePressed(MouseEvent evt)
65 int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
68 if(av.hasHiddenColumns)
69 res = av.getColumnSelection().adjustForHiddenColumns(x);
76 if (SwingUtilities.isRightMouseButton(evt))
78 JPopupMenu pop = new JPopupMenu();
81 JMenuItem item = new JMenuItem("Reveal");
82 item.addActionListener(new ActionListener()
84 public void actionPerformed(ActionEvent e)
86 av.showColumn(reveal[0]);
89 if (ap.overviewPanel != null)
90 ap.overviewPanel.updateOverviewImage();
95 if (av.getColumnSelection().getHiddenColumns().size() > 1)
97 item = new JMenuItem("Reveal All");
98 item.addActionListener(new ActionListener()
100 public void actionPerformed(ActionEvent e)
102 av.showAllHiddenColumns();
105 if (ap.overviewPanel != null)
106 ap.overviewPanel.updateOverviewImage();
111 pop.show(this, evt.getX(), evt.getY());
113 else if (av.getColumnSelection().contains(res))
115 JMenuItem item = new JMenuItem("Hide Columns");
116 item.addActionListener(new ActionListener()
118 public void actionPerformed(ActionEvent e)
120 av.hideColumns(res, res);
121 if(av.getSelectionGroup()!=null
122 && av.getSelectionGroup().getSize(false)==av.alignment.getHeight())
123 av.setSelectionGroup(null);
126 if (ap.overviewPanel != null)
127 ap.overviewPanel.updateOverviewImage();
131 pop.show(this, evt.getX(), evt.getY());
134 else // LEFT MOUSE TO SELECT
136 if (!evt.isControlDown() && !evt.isShiftDown())
138 av.getColumnSelection().clear();
141 av.getColumnSelection().addElement(res);
142 SequenceGroup sg = new SequenceGroup();
143 for (int i = 0; i < av.alignment.getSequences().size(); i++)
145 sg.addSequence(av.alignment.getSequenceAt(i), false);
150 av.setSelectionGroup(sg);
152 if(evt.isShiftDown())
154 int min = Math.min(av.getColumnSelection().getMin(), res);
155 int max = Math.max(av.getColumnSelection().getMax(), res);
156 for (int i = min; i<max; i++)
158 av.getColumnSelection().addElement(i);
174 * @param evt DOCUMENT ME!
176 public void mouseReleased(MouseEvent evt)
178 mouseDragging = false;
180 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
182 if(res> av.alignment.getWidth())
184 res = av.alignment.getWidth()-1;
187 if(av.hasHiddenColumns)
188 res = av.getColumnSelection().adjustForHiddenColumns(res);
190 if (!stretchingGroup)
197 SequenceGroup sg = av.getSelectionGroup();
199 if (res > sg.getStartRes())
203 else if (res < sg.getStartRes())
208 stretchingGroup = false;
215 * @param evt DOCUMENT ME!
217 public void mouseDragged(MouseEvent evt)
219 mouseDragging = true;
221 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
225 if(av.hasHiddenColumns)
226 res = av.getColumnSelection().adjustForHiddenColumns(res);
229 if(res> av.alignment.getWidth())
231 res = av.alignment.getWidth()-1;
246 SequenceGroup sg = av.getSelectionGroup();
250 stretchingGroup = true;
252 if (!av.getColumnSelection().contains(res))
254 av.getColumnSelection().addElement(res);
257 if (res > sg.getStartRes())
261 if (res < sg.getStartRes())
266 for (int i = min; i <= max; i++)
268 if ((i < sg.getStartRes()) || (i > sg.getEndRes()))
270 av.getColumnSelection().removeElement(i);
274 av.getColumnSelection().addElement(i);
282 public void mouseEntered(MouseEvent evt)
285 ap.seqPanel.scrollCanvas(null);
288 public void mouseExited(MouseEvent evt)
291 ap.seqPanel.scrollCanvas(evt);
294 public void mouseClicked(MouseEvent evt){}
296 public void mouseMoved(MouseEvent evt)
298 if(!av.hasHiddenColumns)
301 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
303 res = av.getColumnSelection().adjustForHiddenColumns(res);
306 for(int i=0; i<av.getColumnSelection().getHiddenColumns().size(); i++)
308 int [] region = (int[])av.getColumnSelection().getHiddenColumns().elementAt(i);
309 if(res+1==region[0] || res-1==region[1])
312 ToolTipManager.sharedInstance().registerComponent(this);
313 this.setToolTipText("Reveal Hidden Columns with Right Mouse Button");
317 this.setToolTipText(null);
329 * @param g DOCUMENT ME!
331 public void paintComponent(Graphics g)
333 drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
336 // scalewidth will normally be screenwidth,
337 public void drawScale(Graphics g, int startx, int endx, int width,
340 Graphics2D gg = (Graphics2D) g;
341 gg.setFont(av.getFont());
344 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
345 RenderingHints.VALUE_ANTIALIAS_ON);
347 //Fill in the background
348 gg.setColor(Color.white);
349 gg.fillRect(0, 0, width, height);
350 gg.setColor(Color.black);
352 //Fill the selected columns
353 ColumnSelection cs = av.getColumnSelection();
354 gg.setColor(new Color(220, 0, 0));
356 for (int i = 0; i < cs.size(); i++)
358 int sel = cs.columnAt(i);
359 if(av.hasHiddenColumns)
360 sel = av.getColumnSelection().findColumnPosition(sel);
362 if ((sel >= startx) && (sel <= endx))
364 gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
369 // Draw the scale numbers
370 gg.setColor(Color.black);
372 int scalestartx = (startx / 10) * 10;
374 FontMetrics fm = gg.getFontMetrics(av.getFont());
375 int y = av.charHeight - fm.getDescent();
377 if ((scalestartx % 10) == 0)
385 for (int i = scalestartx; i < endx; i += 5)
389 string = String.valueOf(av.getColumnSelection().adjustForHiddenColumns(i));
390 if ( (i - startx - 1) * av.charWidth > maxX)
392 gg.drawString(string,
393 (i - startx - 1) * av.charWidth, y);
394 maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
397 gg.drawLine( (int) ( ( (i - startx - 1) * av.charWidth) +
398 (av.charWidth / 2)), y + 2,
399 (int) ( ( (i - startx - 1) * av.charWidth) +
401 y + (fm.getDescent() * 2));
406 gg.drawLine((int) (((i - startx - 1) * av.charWidth) +
407 (av.charWidth / 2)), y + fm.getDescent(),
408 (int) (((i - startx - 1) * av.charWidth) +
409 (av.charWidth / 2)), y + (fm.getDescent() * 2));
413 if (av.hasHiddenColumns)
415 gg.setColor(Color.blue);
417 if(av.getShowHiddenMarkers())
419 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size();
423 res = av.getColumnSelection().findHiddenRegionPosition(i) -
426 if(res < 0 || res > endx-scalestartx)
429 gg.fillPolygon(new int[]
430 {res * av.charWidth - av.charHeight / 4,
431 res * av.charWidth + av.charHeight / 4,
435 y - av.charHeight / 2, y - av.charHeight / 2,
442 if (reveal != null && reveal[0] > startx && reveal[0] < endx)
444 gg.drawString("Reveal Columns", reveal[0] * av.charWidth, 0);