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.*;
22 import jalview.io.FormatAdapter;
25 import java.awt.datatransfer.StringSelection;
26 import java.awt.event.*;
27 import java.awt.image.*;
28 import java.util.Vector;
39 public class AnnotationLabels extends JPanel implements MouseListener,
40 MouseMotionListener, ActionListener
42 static String ADDNEW = "Add New Row";
43 static String EDITNAME="Edit Label/Description";
44 static String HIDE = "Hide This Row";
45 static String DELETE = "Delete This Row";
46 static String SHOWALL = "Show All Hidden Rows";
47 static String OUTPUT_TEXT = "Export Annotation";
48 static String COPYCONS_SEQ = "Copy Consensus Sequence";
49 boolean resizePanel = false;
53 boolean resizing = false;
58 Font font = new Font("Arial", Font.PLAIN, 11);
62 * Creates a new AnnotationLabels object.
64 * @param ap DOCUMENT ME!
66 public AnnotationLabels(AlignmentPanel ap)
70 ToolTipManager.sharedInstance().registerComponent(this);
72 java.net.URL url = getClass().getResource("/images/idwidth.gif");
77 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
82 MediaTracker mt = new MediaTracker(this);
90 BufferedImage bi = new BufferedImage(temp.getHeight(this),
91 temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
92 Graphics2D g = (Graphics2D) bi.getGraphics();
93 g.rotate(Math.toRadians(90));
94 g.drawImage(temp, 0, -bi.getWidth(this), this);
97 addMouseListener(this);
98 addMouseMotionListener(this);
101 public AnnotationLabels(AlignViewport av)
109 * @param y DOCUMENT ME!
111 public void setScrollOffset(int y)
117 void getSelectedRow(int y)
120 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
124 for (int i = 0; i < aa.length; i++)
131 height += aa[i].height;
146 * @param evt DOCUMENT ME!
148 public void actionPerformed(ActionEvent evt)
151 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
153 if (evt.getActionCommand().equals(ADDNEW))
155 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
157 new Annotation[ap.av.alignment.getWidth()]);
159 if(!editLabelDescription(newAnnotation))
162 ap.av.alignment.addAnnotation(newAnnotation);
163 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
165 dif = aa[aa.length - 1].height;
167 else if(evt.getActionCommand().equals(EDITNAME))
169 editLabelDescription(aa[selectedRow]);
172 else if (evt.getActionCommand().equals(HIDE))
174 aa[selectedRow].visible = false;
176 if (aa[selectedRow].label.equals("Quality"))
178 ap.av.quality = null;
181 dif = aa[selectedRow].height * -1;
183 else if (evt.getActionCommand().equals(DELETE))
185 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
186 dif = aa[selectedRow].height * -1;
188 else if (evt.getActionCommand().equals(SHOWALL))
190 for (int i = 0; i < aa.length; i++)
195 aa[i].visible = true;
199 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
201 new AnnotationExporter().exportAnnotations(
203 new AlignmentAnnotation[]
208 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
210 SequenceI cons=av.getConsensusSeq();
212 copy_annotseqtoclipboard(cons);
216 ap.annotationPanel.adjustPanelHeight();
217 ap.annotationScroller.validate();
225 * @param e DOCUMENT ME!
227 boolean editLabelDescription(AlignmentAnnotation annotation)
229 EditNameDialog dialog = new EditNameDialog(annotation.label,
230 annotation.description,
232 "Annotation Description ",
233 "Edit Annotation Name/Description");
240 annotation.label = dialog.getName();
242 String text = dialog.getDescription();
245 annotation.description = text;
253 * @param evt DOCUMENT ME!
255 public void mousePressed(MouseEvent evt)
257 getSelectedRow(evt.getY() - scrollOffset);
264 * @param evt DOCUMENT ME!
266 public void mouseReleased(MouseEvent evt)
268 int start = selectedRow;
269 getSelectedRow(evt.getY() - scrollOffset);
270 int end = selectedRow;
274 //Swap these annotations
275 AlignmentAnnotation startAA = ap.av.alignment.getAlignmentAnnotation()[start];
276 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
279 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
280 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
286 ap.annotationPanel.repaint();
292 * @param evt DOCUMENT ME!
294 public void mouseEntered(MouseEvent evt)
306 * @param evt DOCUMENT ME!
308 public void mouseExited(MouseEvent evt)
310 if(dragEvent == null)
320 * @param evt DOCUMENT ME!
322 public void mouseDragged(MouseEvent evt)
328 Dimension d = ap.annotationScroller.getPreferredSize();
329 int dif = evt.getY() - oldY;
331 dif /= ap.av.charHeight;
332 dif *= ap.av.charHeight;
334 if ( (d.height - dif) > 20)
336 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
338 d = ap.annotationSpaceFillerHolder.getPreferredSize();
339 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
340 d.width, d.height - dif));
353 * @param evt DOCUMENT ME!
355 public void mouseMoved(MouseEvent evt)
357 resizePanel = evt.getY()<10;
359 getSelectedRow(evt.getY() - scrollOffset);
363 String desc = ap.av.alignment.
364 getAlignmentAnnotation()[selectedRow].description;
366 if (desc != null && !desc.equals("New Description"))
367 setToolTipText(ap.av.alignment.
368 getAlignmentAnnotation()[selectedRow].description);
377 * @param evt DOCUMENT ME!
379 public void mouseClicked(MouseEvent evt)
381 if(!SwingUtilities.isRightMouseButton(evt))
384 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
386 if ((aa == null) || (aa.length == 0))
388 JPopupMenu pop = new JPopupMenu("Annotations");
389 JMenuItem item = new JMenuItem(ADDNEW);
390 item.addActionListener(this);
392 pop.show(this, evt.getX(), evt.getY());
398 JPopupMenu pop = new JPopupMenu("Annotations");
399 JMenuItem item = new JMenuItem(ADDNEW);
400 item.addActionListener(this);
402 item = new JMenuItem(EDITNAME);
403 item.addActionListener(this);
405 item = new JMenuItem(HIDE);
406 item.addActionListener(this);
408 item = new JMenuItem(DELETE);
409 item.addActionListener(this);
411 item = new JMenuItem(SHOWALL);
412 item.addActionListener(this);
414 item = new JMenuItem(OUTPUT_TEXT);
415 item.addActionListener(this);
417 // annotation object should be typed
418 if (aa[selectedRow]==ap.av.consensus)
421 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
422 "Ignore Gaps In Consensus",
423 ap.av.getIgnoreGapsConsensus());
424 cbmi.addActionListener(new ActionListener()
425 {public void actionPerformed(ActionEvent e)
427 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
431 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
432 consclipbrd.addActionListener(this);
433 pop.add(consclipbrd);
436 pop.show(this, evt.getX(), evt.getY());
439 * do a single sequence copy to jalview and the system clipboard
441 * @param sq sequence to be copied to clipboard
443 protected void copy_annotseqtoclipboard(SequenceI sq)
445 SequenceI [] seqs = new SequenceI[] { sq };
446 String[] omitHidden = null;
447 SequenceI [] dseqs=new SequenceI[] { sq.getDatasetSequence()};
448 if (dseqs[0]==null) {
449 dseqs[0] = new Sequence(sq);
450 dseqs[0].setSequence(
451 jalview.analysis.AlignSeq.extractGaps(
452 jalview.util.Comparison.GapChars,
453 sq.getSequenceAsString()));
455 sq.setDatasetSequence(dseqs[0]);
457 Alignment ds=new Alignment(dseqs);
458 if (av.hasHiddenColumns)
460 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0, sq.getLength(), seqs);
463 String output = new FormatAdapter().formatSequences(
469 Toolkit.getDefaultToolkit().getSystemClipboard()
470 .setContents(new StringSelection(output), Desktop.instance);
472 Vector hiddenColumns = null;
473 if(av.hasHiddenColumns)
475 hiddenColumns =new Vector();
476 for(int i=0; i<av.getColumnSelection().getHiddenColumns().size(); i++)
478 int[] region = (int[])
479 av.getColumnSelection().getHiddenColumns().elementAt(i);
481 hiddenColumns.addElement(new int[]{region[0],
486 Desktop.jalviewClipboard = new Object[]{ seqs,
487 ds, // what is the dataset of a consensus sequence ? need to flag sequence as special.
493 * @param g1 DOCUMENT ME!
495 public void paintComponent(Graphics g)
498 int width = getWidth();
500 width = ap.calculateIdWidth().width + 4;
502 Graphics2D g2 = (Graphics2D) g;
504 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
505 RenderingHints.VALUE_ANTIALIAS_ON);
507 drawComponent(g2, width);
514 * @param g DOCUMENT ME!
516 public void drawComponent(Graphics g, int width)
518 if(av.getFont().getSize()<10)
521 g.setFont(av.getFont());
523 FontMetrics fm = g.getFontMetrics(g.getFont());
524 g.setColor(Color.white);
525 g.fillRect(0, 0, getWidth(), getHeight());
527 g.translate(0, scrollOffset);
528 g.setColor(Color.black);
530 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
531 int fontHeight = g.getFont().getSize();
540 for (int i = 0; i < aa.length; i++)
547 x = width - fm.stringWidth(aa[i].label) - 3;
549 if (aa[i].graph>0 && aa[i].graphHeight>0)
553 y += (aa[i].height / 3);
555 if(aa[i].graphGroup<0)
556 graphExtras = y + fontHeight;
559 if(aa[i].graphGroup>-1)
562 for (int gg = 0; gg < aa.length; gg++)
564 if (aa[gg].graphGroup == aa[i].graphGroup)
568 if(groupSize * (fontHeight+8) < aa[i].height)
569 graphExtras += (aa[i].height -( groupSize * (fontHeight+8)) )/2;
571 for(int gg=0; gg<aa.length; gg++)
573 if(aa[gg].graphGroup==aa[i].graphGroup)
575 x = width - fm.stringWidth(aa[gg].label) - 3;
576 g.drawString(aa[gg].label, x, graphExtras );
577 if(aa[gg].annotations[0]!=null)
578 g.setColor(aa[gg].annotations[0].colour);
580 g.drawLine( x, graphExtras+3,
581 x+fm.stringWidth(aa[gg].label),
584 g.setColor(Color.black);
585 graphExtras += fontHeight+8;
590 g.drawString(aa[i].label, x, y);
594 /* if (aa[i].graphLines != null)
596 for (int gl = 0; gl < aa[i].graphLines.size(); gl++)
598 x = width - fm.stringWidth(aa[i].getGraphLine(gl).label) - 3;
599 g.drawString(aa[i].getGraphLine(gl).label, x, graphExtras);
600 g.setColor(aa[i].getGraphLine(gl).colour);
601 Graphics2D g2 = (Graphics2D) g;
602 g2.setStroke(new BasicStroke(1,
603 BasicStroke.CAP_SQUARE,
604 BasicStroke.JOIN_ROUND, 3f,
610 g.drawLine(x, graphExtras,
611 x+fm.stringWidth(aa[i].label),
613 g2.setStroke(new BasicStroke());
616 y += ((2 * aa[i].height) / 3);
627 g.drawImage(image, 2, 0 - scrollOffset, this);
629 else if (dragEvent != null && aa!=null)
631 g.setColor(Color.lightGray);
632 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent.getY() - scrollOffset);
636 if ((aa == null) || (aa.length < 1))
638 g.drawString("Right click", 2, 8);
639 g.drawString("to add annotation", 2, 18);