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