2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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
25 import java.awt.image.*;
\r
27 import javax.swing.*;
\r
34 * @version $Revision$
\r
36 public class AnnotationLabels extends JPanel implements MouseListener,
\r
37 MouseMotionListener, ActionListener
\r
39 static String ADDNEW = "Add New Row";
\r
40 static String HIDE = "Hide This Row";
\r
41 static String DELETE = "Delete This Row";
\r
42 static String SHOWALL = "Show All Hidden Rows";
\r
43 static String OUTPUT_TEXT = "Export Annotation";
\r
44 boolean resizePanel = false;
\r
48 boolean resizing = false;
\r
49 MouseEvent dragEvent;
\r
52 int scrollOffset = 0;
\r
53 Font font = new Font("Arial", Font.PLAIN, 11);
\r
57 * Creates a new AnnotationLabels object.
\r
59 * @param ap DOCUMENT ME!
\r
61 public AnnotationLabels(AlignmentPanel ap)
\r
66 java.net.URL url = getClass().getResource("/images/idwidth.gif");
\r
71 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
\r
76 MediaTracker mt = new MediaTracker(this);
\r
77 mt.addImage(temp, 0);
\r
80 catch (Exception ex)
\r
84 BufferedImage bi = new BufferedImage(temp.getHeight(this),
\r
85 temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
\r
86 Graphics2D g = (Graphics2D) bi.getGraphics();
\r
87 g.rotate(Math.toRadians(90));
\r
88 g.drawImage(temp, 0, -bi.getWidth(this), this);
\r
91 addMouseListener(this);
\r
92 addMouseMotionListener(this);
\r
95 public AnnotationLabels(AlignViewport av)
\r
103 * @param y DOCUMENT ME!
\r
105 public void setScrollOffset(int y)
\r
111 void getSelectedRow(int y)
\r
114 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
\r
116 for (int i = 0; i < aa.length; i++)
\r
118 if (!aa[i].visible)
\r
123 height += aa[i].height;
\r
137 * @param evt DOCUMENT ME!
\r
139 public void actionPerformed(ActionEvent evt)
\r
142 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
\r
144 if (evt.getActionCommand().equals(ADDNEW))
\r
146 String label = JOptionPane.showInputDialog(this,
\r
147 "Label for annotation");
\r
154 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(label,
\r
156 new Annotation[ap.av.alignment.getWidth()]);
\r
158 ap.av.alignment.addAnnotation(newAnnotation);
\r
159 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
\r
160 dif = aa[aa.length - 1].height;
\r
162 else if (evt.getActionCommand().equals(HIDE))
\r
164 aa[selectedRow].visible = false;
\r
166 if (aa[selectedRow].label.equals("Conservation"))
\r
168 ap.av.showConservation = false;
\r
171 if (aa[selectedRow].label.equals("Quality"))
\r
173 ap.av.showQuality = false;
\r
176 if (aa[selectedRow].label.equals("Consensus"))
\r
178 ap.av.showIdentity = false;
\r
181 dif = aa[selectedRow].height * -1;
\r
183 else if (evt.getActionCommand().equals(DELETE))
\r
185 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
\r
186 dif = aa[selectedRow].height * -1;
\r
188 else if (evt.getActionCommand().equals(SHOWALL))
\r
190 for (int i = 0; i < aa.length; i++)
\r
192 if (!aa[i].visible)
\r
194 dif += aa[i].height;
\r
195 aa[i].visible = true;
\r
199 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
\r
201 new AnnotationExporter().exportAnnotations(
\r
203 new AlignmentAnnotation[]
\r
209 ap.annotationPanel.adjustPanelHeight();
\r
210 ap.annotationScroller.validate();
\r
217 * @param evt DOCUMENT ME!
\r
219 public void mousePressed(MouseEvent evt)
\r
221 getSelectedRow(evt.getY() - scrollOffset);
\r
228 * @param evt DOCUMENT ME!
\r
230 public void mouseReleased(MouseEvent evt)
\r
232 int start = selectedRow;
\r
233 getSelectedRow(evt.getY() - scrollOffset);
\r
234 int end = selectedRow;
\r
238 //Swap these annotations
\r
239 AlignmentAnnotation startAA = ap.av.alignment.getAlignmentAnnotation()[start];
\r
240 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
\r
243 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
\r
244 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
\r
247 resizePanel = false;
\r
250 ap.annotationPanel.repaint();
\r
256 * @param evt DOCUMENT ME!
\r
258 public void mouseEntered(MouseEvent evt)
\r
262 resizePanel = true;
\r
270 * @param evt DOCUMENT ME!
\r
272 public void mouseExited(MouseEvent evt)
\r
278 * @param evt DOCUMENT ME!
\r
280 public void mouseDragged(MouseEvent evt)
\r
286 Dimension d = ap.annotationScroller.getPreferredSize();
\r
287 int dif = evt.getY() - oldY;
\r
289 dif /= ap.av.charHeight;
\r
290 dif *= ap.av.charHeight;
\r
292 if ( (d.height - dif) > 20)
\r
294 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
\r
296 d = ap.annotationSpaceFillerHolder.getPreferredSize();
\r
297 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
\r
298 d.width, d.height - dif));
\r
311 * @param evt DOCUMENT ME!
\r
313 public void mouseMoved(MouseEvent evt)
\r
315 resizePanel = evt.getY()<10;
\r
323 * @param evt DOCUMENT ME!
\r
325 public void mouseClicked(MouseEvent evt)
\r
327 if(!SwingUtilities.isRightMouseButton(evt))
\r
330 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
\r
332 if ((aa == null) || (aa.length == 0))
\r
334 JPopupMenu pop = new JPopupMenu("Annotations");
\r
335 JMenuItem item = new JMenuItem(ADDNEW);
\r
336 item.addActionListener(this);
\r
338 pop.show(this, evt.getX(), evt.getY());
\r
344 JPopupMenu pop = new JPopupMenu("Annotations");
\r
345 JMenuItem item = new JMenuItem(ADDNEW);
\r
346 item.addActionListener(this);
\r
348 item = new JMenuItem(HIDE);
\r
349 item.addActionListener(this);
\r
351 item = new JMenuItem(DELETE);
\r
352 item.addActionListener(this);
\r
354 item = new JMenuItem(SHOWALL);
\r
355 item.addActionListener(this);
\r
357 item = new JMenuItem(OUTPUT_TEXT);
\r
358 item.addActionListener(this);
\r
361 if (aa[selectedRow].label.equals("Consensus"))
\r
363 pop.addSeparator();
\r
364 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
\r
365 "Ignore Gaps In Consensus",
\r
366 ap.av.getIgnoreGapsConsensus());
\r
367 cbmi.addActionListener(new ActionListener()
\r
368 {public void actionPerformed(ActionEvent e)
\r
370 ap.av.setIgnoreGapsConsensus(cbmi.getState());
\r
378 pop.show(this, evt.getX(), evt.getY());
\r
384 * @param g1 DOCUMENT ME!
\r
386 public void paintComponent(Graphics g)
\r
389 int width = getWidth();
\r
391 width = ap.calculateIdWidth().width + 4;
\r
393 Graphics2D g2 = (Graphics2D) g;
\r
395 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
396 RenderingHints.VALUE_ANTIALIAS_ON);
\r
398 drawComponent(g2, width);
\r
405 * @param g DOCUMENT ME!
\r
407 public void drawComponent(Graphics g, int width)
\r
409 if(av.getFont().getSize()<10)
\r
410 g.setFont(new Font("Arial", Font.PLAIN, av.getFont().getSize()-1));
\r
414 FontMetrics fm = g.getFontMetrics(g.getFont());
\r
415 g.setColor(Color.white);
\r
416 g.fillRect(0, 0, getWidth(), getHeight());
\r
418 g.translate(0, scrollOffset);
\r
419 g.setColor(Color.black);
\r
421 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
422 int fontHeight = g.getFont().getSize();
\r
423 int y = fontHeight;
\r
425 int graphExtras = 0;
\r
431 for (int i = 0; i < aa.length; i++)
\r
433 if (!aa[i].visible)
\r
438 x = width - fm.stringWidth(aa[i].label) - 3;
\r
444 y += (aa[i].height / 3);
\r
446 if(aa[i].graphGroup<0)
\r
447 graphExtras = y + fontHeight;
\r
450 if(aa[i].graphGroup>-1)
\r
453 for (int gg = 0; gg < aa.length; gg++)
\r
455 if (aa[gg].graphGroup == aa[i].graphGroup)
\r
459 if(groupSize * (fontHeight+8) < aa[i].height)
\r
460 graphExtras += (aa[i].height -( groupSize * (fontHeight+8)) )/2;
\r
462 for(int gg=0; gg<aa.length; gg++)
\r
464 if(aa[gg].graphGroup==aa[i].graphGroup)
\r
466 x = width - fm.stringWidth(aa[gg].label) - 3;
\r
467 g.drawString(aa[gg].label, x, graphExtras );
\r
468 if(aa[gg].annotations[0]!=null)
\r
469 g.setColor(aa[gg].annotations[0].colour);
\r
471 g.drawLine( x, graphExtras+3,
\r
472 x+fm.stringWidth(aa[gg].label),
\r
475 g.setColor(Color.black);
\r
476 graphExtras += fontHeight+8;
\r
481 g.drawString(aa[i].label, x, y);
\r
485 /* if (aa[i].graphLines != null)
\r
487 for (int gl = 0; gl < aa[i].graphLines.size(); gl++)
\r
489 x = width - fm.stringWidth(aa[i].getGraphLine(gl).label) - 3;
\r
490 g.drawString(aa[i].getGraphLine(gl).label, x, graphExtras);
\r
491 g.setColor(aa[i].getGraphLine(gl).colour);
\r
492 Graphics2D g2 = (Graphics2D) g;
\r
493 g2.setStroke(new BasicStroke(1,
\r
494 BasicStroke.CAP_SQUARE,
\r
495 BasicStroke.JOIN_ROUND, 3f,
\r
501 g.drawLine(x, graphExtras,
\r
502 x+fm.stringWidth(aa[i].label),
\r
504 g2.setStroke(new BasicStroke());
\r
507 y += ((2 * aa[i].height) / 3);
\r
518 g.drawImage(image, 2, 0 - scrollOffset, this);
\r
520 else if (dragEvent != null)
\r
522 g.setColor(Color.lightGray);
\r
523 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent.getY() - scrollOffset);
\r
527 if ((aa == null) || (aa.length < 1))
\r
529 g.drawString("Right click", 2, 8);
\r
530 g.drawString("to add annotation", 2, 18);
\r