2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.datatransfer.*;
24 import java.awt.event.*;
25 import java.awt.image.*;
28 import jalview.datamodel.*;
37 public class AnnotationLabels extends JPanel implements MouseListener,
38 MouseMotionListener, ActionListener
40 static String TOGGLE_LABELSCALE = "Scale Label to Column";
42 static String ADDNEW = "Add New Row";
44 static String EDITNAME = "Edit Label/Description";
46 static String HIDE = "Hide This Row";
48 static String DELETE = "Delete This Row";
50 static String SHOWALL = "Show All Hidden Rows";
52 static String OUTPUT_TEXT = "Export Annotation";
54 static String COPYCONS_SEQ = "Copy Consensus Sequence";
56 boolean resizePanel = false;
64 boolean resizing = false;
74 Font font = new Font("Arial", Font.PLAIN, 11);
77 * Creates a new AnnotationLabels object.
82 public AnnotationLabels(AlignmentPanel ap)
86 ToolTipManager.sharedInstance().registerComponent(this);
88 java.net.URL url = getClass().getResource("/images/idwidth.gif");
93 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
98 MediaTracker mt = new MediaTracker(this);
101 } catch (Exception ex)
105 BufferedImage bi = new BufferedImage(temp.getHeight(this), temp
106 .getWidth(this), BufferedImage.TYPE_INT_RGB);
107 Graphics2D g = (Graphics2D) bi.getGraphics();
108 g.rotate(Math.toRadians(90));
109 g.drawImage(temp, 0, -bi.getWidth(this), this);
112 addMouseListener(this);
113 addMouseMotionListener(this);
116 public AnnotationLabels(AlignViewport av)
127 public void setScrollOffset(int y)
133 void getSelectedRow(int y)
136 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
140 for (int i = 0; i < aa.length; i++)
147 height += aa[i].height;
167 public void actionPerformed(ActionEvent evt)
169 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
171 if (evt.getActionCommand().equals(ADDNEW))
173 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
174 null, new Annotation[ap.av.alignment.getWidth()]);
176 if (!editLabelDescription(newAnnotation))
181 ap.av.alignment.addAnnotation(newAnnotation);
182 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
184 else if (evt.getActionCommand().equals(EDITNAME))
186 editLabelDescription(aa[selectedRow]);
189 else if (evt.getActionCommand().equals(HIDE))
191 aa[selectedRow].visible = false;
193 if (aa[selectedRow].label.equals("Quality"))
195 ap.av.quality = null;
198 else if (evt.getActionCommand().equals(DELETE))
200 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
202 else if (evt.getActionCommand().equals(SHOWALL))
204 for (int i = 0; i < aa.length; i++)
206 if (!aa[i].visible && aa[i].annotations != null)
208 aa[i].visible = true;
212 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
214 new AnnotationExporter().exportAnnotations(ap,
215 new AlignmentAnnotation[]
216 { aa[selectedRow] }, null, null);
218 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
220 SequenceI cons = null;
221 if (aa[selectedRow].groupRef != null)
223 cons = aa[selectedRow].groupRef.getConsensusSeq();
227 cons = av.getConsensusSeq();
231 copy_annotseqtoclipboard(cons);
235 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
237 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
240 ap.annotationPanel.adjustPanelHeight();
241 ap.annotationScroller.validate();
242 ap.paintAlignment(true);
251 boolean editLabelDescription(AlignmentAnnotation annotation)
253 EditNameDialog dialog = new EditNameDialog(annotation.label,
254 annotation.description, " Annotation Name ",
255 "Annotation Description ", "Edit Annotation Name/Description");
262 annotation.label = dialog.getName();
264 String text = dialog.getDescription();
265 if (text != null && text.length() == 0)
269 annotation.description = text;
280 public void mousePressed(MouseEvent evt)
282 getSelectedRow(evt.getY() - scrollOffset);
292 public void mouseReleased(MouseEvent evt)
294 int start = selectedRow;
295 getSelectedRow(evt.getY() - scrollOffset);
296 int end = selectedRow;
300 // Swap these annotations
301 AlignmentAnnotation startAA = ap.av.alignment
302 .getAlignmentAnnotation()[start];
303 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
305 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
306 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
312 ap.annotationPanel.repaint();
321 public void mouseEntered(MouseEvent evt)
336 public void mouseExited(MouseEvent evt)
338 if (dragEvent == null)
351 public void mouseDragged(MouseEvent evt)
357 Dimension d = ap.annotationScroller.getPreferredSize();
358 int dif = evt.getY() - oldY;
360 dif /= ap.av.charHeight;
361 dif *= ap.av.charHeight;
363 if ((d.height - dif) > 20)
365 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
367 d = ap.annotationSpaceFillerHolder.getPreferredSize();
368 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
369 d.width, d.height - dif));
370 ap.paintAlignment(true);
387 public void mouseMoved(MouseEvent evt)
389 resizePanel = evt.getY() < 10;
391 getSelectedRow(evt.getY() - scrollOffset);
394 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
396 AlignmentAnnotation aa = ap.av.alignment.getAlignmentAnnotation()[selectedRow];
398 StringBuffer desc = new StringBuffer("<html>");
400 if (aa.description != null
401 && !aa.description.equals("New description"))
403 desc.append(aa.getDescription(true));
409 desc.append("Score: " + aa.score);
412 if (desc.length() != 6)
414 desc.append("</html>");
415 this.setToolTipText(desc.toString());
418 this.setToolTipText(null);
429 public void mouseClicked(MouseEvent evt)
431 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
432 if (SwingUtilities.isLeftMouseButton(evt))
434 if (aa!=null && selectedRow < aa.length)
436 if (aa[selectedRow].groupRef != null)
438 if (evt.getClickCount() >= 2)
440 // todo: make the ap scroll to the selection
441 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
442 ap.av.setSelectionGroup(// new SequenceGroup(
443 aa[selectedRow].groupRef); // );
444 ap.paintAlignment(false);
448 ap.seqPanel.ap.idPanel
449 .highlightSearchResults(aa[selectedRow].groupRef
450 .getSequences(null));
454 else if (aa[selectedRow].sequenceRef != null)
456 Vector sr = new Vector();
457 sr.addElement(aa[selectedRow].sequenceRef);
458 if (evt.getClickCount() == 1)
460 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
462 else if (evt.getClickCount() >= 2)
464 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
465 SequenceGroup sg = new SequenceGroup();
466 sg.addSequence(aa[selectedRow].sequenceRef, false);
467 ap.av.setSelectionGroup(sg);
468 ap.paintAlignment(false);
469 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
475 if (!SwingUtilities.isRightMouseButton(evt))
480 JPopupMenu pop = new JPopupMenu("Annotations");
481 JMenuItem item = new JMenuItem(ADDNEW);
482 item.addActionListener(this);
484 if ((aa == null) || (aa.length == 0))
486 item = new JMenuItem(SHOWALL);
487 item.addActionListener(this);
489 pop.show(this, evt.getX(), evt.getY());
494 item = new JMenuItem(EDITNAME);
495 item.addActionListener(this);
497 item = new JMenuItem(HIDE);
498 item.addActionListener(this);
500 item = new JMenuItem(DELETE);
501 item.addActionListener(this);
503 item = new JMenuItem(SHOWALL);
504 item.addActionListener(this);
506 item = new JMenuItem(OUTPUT_TEXT);
507 item.addActionListener(this);
509 // TODO: annotation object should be typed for autocalculated/derived
511 if (selectedRow < aa.length)
513 if (!aa[selectedRow].autoCalculated)
515 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
517 // display formatting settings for this row.
519 // av and sequencegroup need to implement same interface for
520 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
521 aa[selectedRow].scaleColLabel);
522 item.addActionListener(this);
526 else if (aa[selectedRow].label.indexOf("Consensus") > -1)
529 // av and sequencegroup need to implement same interface for
530 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
531 "Ignore Gaps In Consensus",
532 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
533 .getIgnoreGapsConsensus()
534 : ap.av.getIgnoreGapsConsensus());
535 final AlignmentAnnotation aaa = aa[selectedRow];
536 cbmi.addActionListener(new ActionListener()
538 public void actionPerformed(ActionEvent e)
540 if (aaa.groupRef != null)
542 // TODO: pass on reference to ap so the view can be updated.
543 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
544 ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
548 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
553 // av and sequencegroup need to implement same interface for
554 if (aaa.groupRef != null)
556 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
557 "Show Group Histogram", aa[selectedRow].groupRef
558 .isShowConsensusHistogram());
559 chist.addActionListener(new ActionListener()
561 public void actionPerformed(ActionEvent e)
563 // TODO: pass on reference
569 aaa.groupRef.setShowConsensusHistogram(chist.getState());
571 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
575 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
576 "Show Group Logo", aa[selectedRow].groupRef
577 .isShowSequenceLogo());
578 cprof.addActionListener(new ActionListener()
580 public void actionPerformed(ActionEvent e)
582 // TODO: pass on reference
588 aaa.groupRef.setIncludeAllConsSymbols(cprof.getState());
590 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
597 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
598 "Show Histogram", av.isShowConsensusHistogram());
599 chist.addActionListener(new ActionListener()
601 public void actionPerformed(ActionEvent e)
603 // TODO: pass on reference
609 av.setShowConsensusHistogram(chist.getState());
611 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
615 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
616 "Show Logo", av.isShowSequenceLogo());
617 cprof.addActionListener(new ActionListener()
619 public void actionPerformed(ActionEvent e)
621 // TODO: pass on reference
627 av.setShowSequenceLogo(cprof.getState());
629 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
634 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
635 consclipbrd.addActionListener(this);
636 pop.add(consclipbrd);
639 pop.show(this, evt.getX(), evt.getY());
643 * do a single sequence copy to jalview and the system clipboard
646 * sequence to be copied to clipboard
648 protected void copy_annotseqtoclipboard(SequenceI sq)
650 SequenceI[] seqs = new SequenceI[]
652 String[] omitHidden = null;
653 SequenceI[] dseqs = new SequenceI[]
654 { sq.getDatasetSequence() };
655 if (dseqs[0] == null)
657 dseqs[0] = new Sequence(sq);
658 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
659 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
661 sq.setDatasetSequence(dseqs[0]);
663 Alignment ds = new Alignment(dseqs);
664 if (av.hasHiddenColumns)
666 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
667 sq.getLength(), seqs);
670 String output = new FormatAdapter().formatSequences("Fasta", seqs,
673 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
674 new StringSelection(output), Desktop.instance);
676 Vector hiddenColumns = null;
677 if (av.hasHiddenColumns)
679 hiddenColumns = new Vector();
680 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
682 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
685 hiddenColumns.addElement(new int[]
686 { region[0], region[1] });
690 Desktop.jalviewClipboard = new Object[]
691 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
692 // sequence as special.
702 public void paintComponent(Graphics g)
705 int width = getWidth();
708 width = ap.calculateIdWidth().width + 4;
711 Graphics2D g2 = (Graphics2D) g;
714 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
715 RenderingHints.VALUE_ANTIALIAS_ON);
718 drawComponent(g2, width);
728 public void drawComponent(Graphics g, int width)
730 if (av.getFont().getSize() < 10)
736 g.setFont(av.getFont());
739 FontMetrics fm = g.getFontMetrics(g.getFont());
740 g.setColor(Color.white);
741 g.fillRect(0, 0, getWidth(), getHeight());
743 g.translate(0, scrollOffset);
744 g.setColor(Color.black);
746 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
747 int fontHeight = g.getFont().getSize();
755 for (int i = 0; i < aa.length; i++)
757 g.setColor(Color.black);
766 offset = -aa[i].height / 2;
770 offset += fm.getHeight() / 2;
771 offset -= fm.getDescent();
774 offset += fm.getDescent();
776 x = width - fm.stringWidth(aa[i].label) - 3;
778 if (aa[i].graphGroup > -1)
781 for (int gg = 0; gg < aa.length; gg++)
783 if (aa[gg].graphGroup == aa[i].graphGroup)
789 if (groupSize * (fontHeight + 8) < aa[i].height)
791 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
794 for (int gg = 0; gg < aa.length; gg++)
796 if (aa[gg].graphGroup == aa[i].graphGroup)
798 x = width - fm.stringWidth(aa[gg].label) - 3;
799 g.drawString(aa[gg].label, x, y - graphExtras);
800 if (aa[gg].annotations[0] != null)
802 g.setColor(aa[gg].annotations[0].colour);
805 g.drawLine(x, y - graphExtras - 3, x
806 + fm.stringWidth(aa[gg].label), y - graphExtras - 3);
808 g.setColor(Color.black);
809 graphExtras += fontHeight + 8;
815 g.drawString(aa[i].label, x, y + offset);
822 g.drawImage(image, 2, 0 - scrollOffset, this);
824 else if (dragEvent != null && aa != null)
826 g.setColor(Color.lightGray);
827 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent
832 if ((aa == null) || (aa.length < 1))
834 g.drawString("Right click", 2, 8);
835 g.drawString("to add annotation", 2, 18);