2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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
24 import java.awt.datatransfer.*;
25 import java.awt.event.*;
26 import java.awt.image.*;
29 import jalview.datamodel.*;
38 public class AnnotationLabels
39 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);
61 * Creates a new AnnotationLabels object.
63 * @param ap DOCUMENT ME!
65 public AnnotationLabels(AlignmentPanel ap)
69 ToolTipManager.sharedInstance().registerComponent(this);
71 java.net.URL url = getClass().getResource("/images/idwidth.gif");
76 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
81 MediaTracker mt = new MediaTracker(this);
89 BufferedImage bi = new BufferedImage(temp.getHeight(this),
91 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))
164 ap.av.alignment.addAnnotation(newAnnotation);
165 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
168 dif = aa[aa.length - 1].height;
171 else if (evt.getActionCommand().equals(EDITNAME))
173 editLabelDescription(aa[selectedRow]);
176 else if (evt.getActionCommand().equals(HIDE))
178 aa[selectedRow].visible = false;
180 if (aa[selectedRow].label.equals("Quality"))
182 ap.av.quality = null;
185 dif = aa[selectedRow].height * -1;
187 else if (evt.getActionCommand().equals(DELETE))
189 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
190 dif = aa[selectedRow].height * -1;
192 else if (evt.getActionCommand().equals(SHOWALL))
194 for (int i = 0; i < aa.length; i++)
196 if (!aa[i].visible && aa[i].annotations!=null)
199 aa[i].visible = true;
203 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
205 new AnnotationExporter().exportAnnotations(
207 new AlignmentAnnotation[]
212 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
214 SequenceI cons = av.getConsensusSeq();
217 copy_annotseqtoclipboard(cons);
222 ap.annotationPanel.adjustPanelHeight();
223 ap.annotationScroller.validate();
224 ap.paintAlignment(true);
230 * @param e DOCUMENT ME!
232 boolean editLabelDescription(AlignmentAnnotation annotation)
234 EditNameDialog dialog = new EditNameDialog(annotation.label,
235 annotation.description,
237 "Annotation Description ",
238 "Edit Annotation Name/Description");
245 annotation.label = dialog.getName();
247 String text = dialog.getDescription();
248 if (text!=null && text.length() == 0)
252 annotation.description = text;
260 * @param evt DOCUMENT ME!
262 public void mousePressed(MouseEvent evt)
264 getSelectedRow(evt.getY() - scrollOffset);
271 * @param evt DOCUMENT ME!
273 public void mouseReleased(MouseEvent evt)
275 int start = selectedRow;
276 getSelectedRow(evt.getY() - scrollOffset);
277 int end = selectedRow;
281 //Swap these annotations
282 AlignmentAnnotation startAA = ap.av.alignment.getAlignmentAnnotation()[
284 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
286 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
287 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
293 ap.annotationPanel.repaint();
299 * @param evt DOCUMENT ME!
301 public void mouseEntered(MouseEvent evt)
313 * @param evt DOCUMENT ME!
315 public void mouseExited(MouseEvent evt)
317 if (dragEvent == null)
327 * @param evt DOCUMENT ME!
329 public void mouseDragged(MouseEvent evt)
335 Dimension d = ap.annotationScroller.getPreferredSize();
336 int dif = evt.getY() - oldY;
338 dif /= ap.av.charHeight;
339 dif *= ap.av.charHeight;
341 if ( (d.height - dif) > 20)
343 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
345 d = ap.annotationSpaceFillerHolder.getPreferredSize();
346 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
347 d.width, d.height - dif));
348 ap.paintAlignment(true);
362 * @param evt DOCUMENT ME!
364 public void mouseMoved(MouseEvent evt)
366 resizePanel = evt.getY() < 10;
368 getSelectedRow(evt.getY() - scrollOffset);
372 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
374 AlignmentAnnotation aa = ap.av.alignment.
375 getAlignmentAnnotation()[selectedRow];
377 StringBuffer desc = new StringBuffer("<html>");
379 if (aa.description != null && !aa.description.equals("New description"))
381 desc.append(aa.description+"<br>");
385 desc.append("Score: "+aa.score);
390 desc.append("</html>");
391 this.setToolTipText(desc.toString());
394 this.setToolTipText(null);
402 * @param evt DOCUMENT ME!
404 public void mouseClicked(MouseEvent evt)
406 if (!SwingUtilities.isRightMouseButton(evt))
411 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
413 if ( (aa == null) || (aa.length == 0))
415 JPopupMenu pop = new JPopupMenu("Annotations");
416 JMenuItem item = new JMenuItem(ADDNEW);
417 item.addActionListener(this);
419 pop.show(this, evt.getX(), evt.getY());
424 JPopupMenu pop = new JPopupMenu("Annotations");
425 JMenuItem item = new JMenuItem(ADDNEW);
426 item.addActionListener(this);
428 item = new JMenuItem(EDITNAME);
429 item.addActionListener(this);
431 item = new JMenuItem(HIDE);
432 item.addActionListener(this);
434 item = new JMenuItem(DELETE);
435 item.addActionListener(this);
437 item = new JMenuItem(SHOWALL);
438 item.addActionListener(this);
440 item = new JMenuItem(OUTPUT_TEXT);
441 item.addActionListener(this);
443 // annotation object should be typed
444 if (aa[selectedRow] == ap.av.consensus)
447 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
448 "Ignore Gaps In Consensus",
449 ap.av.getIgnoreGapsConsensus());
450 cbmi.addActionListener(new ActionListener()
452 public void actionPerformed(ActionEvent e)
454 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
458 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
459 consclipbrd.addActionListener(this);
460 pop.add(consclipbrd);
463 pop.show(this, evt.getX(), evt.getY());
467 * do a single sequence copy to jalview and the system clipboard
469 * @param sq sequence to be copied to clipboard
471 protected void copy_annotseqtoclipboard(SequenceI sq)
473 SequenceI[] seqs = new SequenceI[]
476 String[] omitHidden = null;
477 SequenceI[] dseqs = new SequenceI[]
479 sq.getDatasetSequence()};
480 if (dseqs[0] == null)
482 dseqs[0] = new Sequence(sq);
483 dseqs[0].setSequence(
484 jalview.analysis.AlignSeq.extractGaps(
485 jalview.util.Comparison.GapChars,
486 sq.getSequenceAsString()));
488 sq.setDatasetSequence(dseqs[0]);
490 Alignment ds = new Alignment(dseqs);
491 if (av.hasHiddenColumns)
493 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
494 sq.getLength(), seqs);
497 String output = new FormatAdapter().formatSequences(
502 Toolkit.getDefaultToolkit().getSystemClipboard()
503 .setContents(new StringSelection(output), Desktop.instance);
505 Vector hiddenColumns = null;
506 if (av.hasHiddenColumns)
508 hiddenColumns = new Vector();
509 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
511 int[] region = (int[])
512 av.getColumnSelection().getHiddenColumns().elementAt(i);
514 hiddenColumns.addElement(new int[]
520 Desktop.jalviewClipboard = new Object[]
523 ds, // what is the dataset of a consensus sequence ? need to flag sequence as special.
530 * @param g1 DOCUMENT ME!
532 public void paintComponent(Graphics g)
535 int width = getWidth();
538 width = ap.calculateIdWidth().width + 4;
541 Graphics2D g2 = (Graphics2D) g;
544 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
545 RenderingHints.VALUE_ANTIALIAS_ON);
548 drawComponent(g2, width);
555 * @param g DOCUMENT ME!
557 public void drawComponent(Graphics g, int width)
559 if (av.getFont().getSize() < 10)
565 g.setFont(av.getFont());
568 FontMetrics fm = g.getFontMetrics(g.getFont());
569 g.setColor(Color.white);
570 g.fillRect(0, 0, getWidth(), getHeight());
572 g.translate(0, scrollOffset);
573 g.setColor(Color.black);
575 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
576 int fontHeight = g.getFont().getSize();
584 for (int i = 0; i < aa.length; i++)
586 g.setColor(Color.black);
596 offset = -aa[i].height/2;
600 offset += fm.getHeight()/2;
601 offset -= fm.getDescent();
604 offset += fm.getDescent();
606 x = width - fm.stringWidth(aa[i].label) - 3;
608 if (aa[i].graphGroup > -1)
611 for (int gg = 0; gg < aa.length; gg++)
613 if (aa[gg].graphGroup == aa[i].graphGroup)
619 if (groupSize * (fontHeight + 8) < aa[i].height)
621 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
624 for (int gg = 0; gg < aa.length; gg++)
626 if (aa[gg].graphGroup == aa[i].graphGroup)
628 x = width - fm.stringWidth(aa[gg].label) - 3;
629 g.drawString(aa[gg].label, x,y - graphExtras);
630 if (aa[gg].annotations[0] != null)
632 g.setColor(aa[gg].annotations[0].colour);
635 g.drawLine(x, y - graphExtras - 3,
636 x + fm.stringWidth(aa[gg].label),
637 y - graphExtras - 3);
639 g.setColor(Color.black);
640 graphExtras += fontHeight + 8;
646 g.drawString(aa[i].label, x, y +offset);
653 g.drawImage(image, 2, 0 - scrollOffset, this);
655 else if (dragEvent != null && aa != null)
657 g.setColor(Color.lightGray);
658 g.drawString(aa[selectedRow].label, dragEvent.getX(),
659 dragEvent.getY() - scrollOffset);
662 if ( (aa == null) || (aa.length < 1))
664 g.drawString("Right click", 2, 8);
665 g.drawString("to add annotation", 2, 18);