2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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 extends JPanel implements MouseListener,
39 MouseMotionListener, ActionListener
41 static String TOGGLE_LABELSCALE = "Scale Label to Column";
43 static String ADDNEW = "Add New Row";
45 static String EDITNAME = "Edit Label/Description";
47 static String HIDE = "Hide This Row";
49 static String DELETE = "Delete This Row";
51 static String SHOWALL = "Show All Hidden Rows";
53 static String OUTPUT_TEXT = "Export Annotation";
55 static String COPYCONS_SEQ = "Copy Consensus Sequence";
57 boolean resizePanel = false;
65 boolean resizing = false;
75 Font font = new Font("Arial", Font.PLAIN, 11);
78 * Creates a new AnnotationLabels object.
83 public AnnotationLabels(AlignmentPanel ap)
87 ToolTipManager.sharedInstance().registerComponent(this);
89 java.net.URL url = getClass().getResource("/images/idwidth.gif");
94 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
99 MediaTracker mt = new MediaTracker(this);
100 mt.addImage(temp, 0);
102 } catch (Exception ex)
106 BufferedImage bi = new BufferedImage(temp.getHeight(this), temp
107 .getWidth(this), BufferedImage.TYPE_INT_RGB);
108 Graphics2D g = (Graphics2D) bi.getGraphics();
109 g.rotate(Math.toRadians(90));
110 g.drawImage(temp, 0, -bi.getWidth(this), this);
113 addMouseListener(this);
114 addMouseMotionListener(this);
117 public AnnotationLabels(AlignViewport av)
128 public void setScrollOffset(int y)
134 void getSelectedRow(int y)
137 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
141 for (int i = 0; i < aa.length; i++)
148 height += aa[i].height;
166 public void actionPerformed(ActionEvent evt)
168 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
170 if (evt.getActionCommand().equals(ADDNEW))
172 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
173 null, new Annotation[ap.av.alignment.getWidth()]);
175 if (!editLabelDescription(newAnnotation))
180 ap.av.alignment.addAnnotation(newAnnotation);
181 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
183 else if (evt.getActionCommand().equals(EDITNAME))
185 editLabelDescription(aa[selectedRow]);
188 else if (evt.getActionCommand().equals(HIDE))
190 aa[selectedRow].visible = false;
192 if (aa[selectedRow].label.equals("Quality"))
194 ap.av.quality = null;
197 else if (evt.getActionCommand().equals(DELETE))
199 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
201 else if (evt.getActionCommand().equals(SHOWALL))
203 for (int i = 0; i < aa.length; i++)
205 if (!aa[i].visible && aa[i].annotations != null)
207 aa[i].visible = true;
211 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
213 new AnnotationExporter().exportAnnotations(ap,
214 new AlignmentAnnotation[]
215 { aa[selectedRow] }, null, null);
217 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
219 SequenceI cons = null;
220 if (aa[selectedRow].groupRef!=null)
222 cons = aa[selectedRow].groupRef.getConsensusSeq();
224 cons = av.getConsensusSeq();
228 copy_annotseqtoclipboard(cons);
232 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
234 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
237 ap.annotationPanel.adjustPanelHeight();
238 ap.annotationScroller.validate();
239 ap.paintAlignment(true);
248 boolean editLabelDescription(AlignmentAnnotation annotation)
250 EditNameDialog dialog = new EditNameDialog(annotation.label,
251 annotation.description, " Annotation Name ",
252 "Annotation Description ", "Edit Annotation Name/Description");
259 annotation.label = dialog.getName();
261 String text = dialog.getDescription();
262 if (text != null && text.length() == 0)
266 annotation.description = text;
277 public void mousePressed(MouseEvent evt)
279 getSelectedRow(evt.getY() - scrollOffset);
289 public void mouseReleased(MouseEvent evt)
291 int start = selectedRow;
292 getSelectedRow(evt.getY() - scrollOffset);
293 int end = selectedRow;
297 // Swap these annotations
298 AlignmentAnnotation startAA = ap.av.alignment
299 .getAlignmentAnnotation()[start];
300 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
302 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
303 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
309 ap.annotationPanel.repaint();
318 public void mouseEntered(MouseEvent evt)
333 public void mouseExited(MouseEvent evt)
335 if (dragEvent == null)
348 public void mouseDragged(MouseEvent evt)
354 Dimension d = ap.annotationScroller.getPreferredSize();
355 int dif = evt.getY() - oldY;
357 dif /= ap.av.charHeight;
358 dif *= ap.av.charHeight;
360 if ((d.height - dif) > 20)
362 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
364 d = ap.annotationSpaceFillerHolder.getPreferredSize();
365 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
366 d.width, d.height - dif));
367 ap.paintAlignment(true);
384 public void mouseMoved(MouseEvent evt)
386 resizePanel = evt.getY() < 10;
388 getSelectedRow(evt.getY() - scrollOffset);
391 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
393 AlignmentAnnotation aa = ap.av.alignment.getAlignmentAnnotation()[selectedRow];
395 StringBuffer desc = new StringBuffer("<html>");
397 if (aa.description != null
398 && !aa.description.equals("New description"))
400 desc.append(aa.getDescription(true));
406 desc.append("Score: " + aa.score);
409 if (desc.length() != 6)
411 desc.append("</html>");
412 this.setToolTipText(desc.toString());
415 this.setToolTipText(null);
426 public void mouseClicked(MouseEvent evt)
428 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
429 if (SwingUtilities.isLeftMouseButton(evt))
431 if (selectedRow < aa.length)
433 if (aa[selectedRow].groupRef!=null)
435 if (evt.getClickCount()>=2)
437 // todo: make the ap scroll to the selection
438 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
439 ap.av.setSelectionGroup(//new SequenceGroup(
440 aa[selectedRow].groupRef); // );
441 ap.paintAlignment(false);
443 ap.seqPanel.ap.idPanel.highlightSearchResults(aa[selectedRow].groupRef.getSequences(null));
447 if (aa[selectedRow].sequenceRef!=null){
448 Vector sr = new Vector();
449 sr.addElement(aa[selectedRow].sequenceRef);
450 if (evt.getClickCount()==1)
452 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
454 if (evt.getClickCount()>=2)
456 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
457 SequenceGroup sg = new SequenceGroup();
458 sg.addSequence(aa[selectedRow].sequenceRef,false);
459 ap.av.setSelectionGroup(sg);
460 ap.paintAlignment(false);
461 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
467 if (!SwingUtilities.isRightMouseButton(evt))
473 JPopupMenu pop = new JPopupMenu("Annotations");
474 JMenuItem item = new JMenuItem(ADDNEW);
475 item.addActionListener(this);
477 if ((aa == null) || (aa.length == 0))
479 item = new JMenuItem(SHOWALL);
480 item.addActionListener(this);
482 pop.show(this, evt.getX(), evt.getY());
487 item = new JMenuItem(EDITNAME);
488 item.addActionListener(this);
490 item = new JMenuItem(HIDE);
491 item.addActionListener(this);
493 item = new JMenuItem(DELETE);
494 item.addActionListener(this);
496 item = new JMenuItem(SHOWALL);
497 item.addActionListener(this);
499 item = new JMenuItem(OUTPUT_TEXT);
500 item.addActionListener(this);
502 // TODO: annotation object should be typed for autocalculated/derived property methods
503 if (selectedRow < aa.length)
505 if (!aa[selectedRow].autoCalculated) {
506 if (aa[selectedRow].graph==AlignmentAnnotation.NO_GRAPH)
508 // display formatting settings for this row.
510 // av and sequencegroup need to implement same interface for
511 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
512 aa[selectedRow].scaleColLabel);
513 item.addActionListener(this);
517 else if (aa[selectedRow].label.indexOf("Consensus") > -1)
520 // av and sequencegroup need to implement same interface for
521 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
522 "Ignore Gaps In Consensus",
523 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
524 .getIgnoreGapsConsensus()
525 : ap.av.getIgnoreGapsConsensus());
526 final AlignmentAnnotation aaa = aa[selectedRow];
527 cbmi.addActionListener(new ActionListener()
529 public void actionPerformed(ActionEvent e)
531 if (aaa.groupRef != null)
533 // TODO: pass on reference to ap so the view can be updated.
534 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
535 ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
539 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
544 // av and sequencegroup need to implement same interface for
545 if (aaa.groupRef != null)
547 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
548 "Show Group Histogram", aa[selectedRow].groupRef
549 .isShowConsensusHistogram());
550 chist.addActionListener(new ActionListener()
552 public void actionPerformed(ActionEvent e)
554 // TODO: pass on reference
560 aaa.groupRef.setShowConsensusHistogram(chist.getState());
562 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
566 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
567 "Show Group Logo", aa[selectedRow].groupRef
568 .isIncludeAllConsSymbols());
569 cprof.addActionListener(new ActionListener()
571 public void actionPerformed(ActionEvent e)
573 // TODO: pass on reference
579 aaa.groupRef.setIncludeAllConsSymbols(cprof.getState());
581 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
586 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
587 "Show Histogram", av.isShowConsensusHistogram());
588 chist.addActionListener(new ActionListener()
590 public void actionPerformed(ActionEvent e)
592 // TODO: pass on reference
598 av.setShowConsensusHistogram(chist.getState());
600 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
604 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
605 "Show Logo", av.isShowConsensusProfile());
606 cprof.addActionListener(new ActionListener()
608 public void actionPerformed(ActionEvent e)
610 // TODO: pass on reference
616 av.setShowConsensusProfile(cprof.getState());
618 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
623 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
624 consclipbrd.addActionListener(this);
625 pop.add(consclipbrd);
628 pop.show(this, evt.getX(), evt.getY());
632 * do a single sequence copy to jalview and the system clipboard
635 * sequence to be copied to clipboard
637 protected void copy_annotseqtoclipboard(SequenceI sq)
639 SequenceI[] seqs = new SequenceI[]
641 String[] omitHidden = null;
642 SequenceI[] dseqs = new SequenceI[]
643 { sq.getDatasetSequence() };
644 if (dseqs[0] == null)
646 dseqs[0] = new Sequence(sq);
647 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
648 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
650 sq.setDatasetSequence(dseqs[0]);
652 Alignment ds = new Alignment(dseqs);
653 if (av.hasHiddenColumns)
655 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
656 sq.getLength(), seqs);
659 String output = new FormatAdapter().formatSequences("Fasta", seqs,
662 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
663 new StringSelection(output), Desktop.instance);
665 Vector hiddenColumns = null;
666 if (av.hasHiddenColumns)
668 hiddenColumns = new Vector();
669 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
671 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
674 hiddenColumns.addElement(new int[]
675 { region[0], region[1] });
679 Desktop.jalviewClipboard = new Object[]
680 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
681 // sequence as special.
691 public void paintComponent(Graphics g)
694 int width = getWidth();
697 width = ap.calculateIdWidth().width + 4;
700 Graphics2D g2 = (Graphics2D) g;
703 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
704 RenderingHints.VALUE_ANTIALIAS_ON);
707 drawComponent(g2, width);
717 public void drawComponent(Graphics g, int width)
719 if (av.getFont().getSize() < 10)
725 g.setFont(av.getFont());
728 FontMetrics fm = g.getFontMetrics(g.getFont());
729 g.setColor(Color.white);
730 g.fillRect(0, 0, getWidth(), getHeight());
732 g.translate(0, scrollOffset);
733 g.setColor(Color.black);
735 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
736 int fontHeight = g.getFont().getSize();
744 for (int i = 0; i < aa.length; i++)
746 g.setColor(Color.black);
755 offset = -aa[i].height / 2;
759 offset += fm.getHeight() / 2;
760 offset -= fm.getDescent();
763 offset += fm.getDescent();
765 x = width - fm.stringWidth(aa[i].label) - 3;
767 if (aa[i].graphGroup > -1)
770 for (int gg = 0; gg < aa.length; gg++)
772 if (aa[gg].graphGroup == aa[i].graphGroup)
778 if (groupSize * (fontHeight + 8) < aa[i].height)
780 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
783 for (int gg = 0; gg < aa.length; gg++)
785 if (aa[gg].graphGroup == aa[i].graphGroup)
787 x = width - fm.stringWidth(aa[gg].label) - 3;
788 g.drawString(aa[gg].label, x, y - graphExtras);
789 if (aa[gg].annotations[0] != null)
791 g.setColor(aa[gg].annotations[0].colour);
794 g.drawLine(x, y - graphExtras - 3, x
795 + fm.stringWidth(aa[gg].label), y - graphExtras - 3);
797 g.setColor(Color.black);
798 graphExtras += fontHeight + 8;
804 g.drawString(aa[i].label, x, y + offset);
811 g.drawImage(image, 2, 0 - scrollOffset, this);
813 else if (dragEvent != null && aa != null)
815 g.setColor(Color.lightGray);
816 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent
821 if ((aa == null) || (aa.length < 1))
823 g.drawString("Right click", 2, 8);
824 g.drawString("to add annotation", 2, 18);