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;
165 public void actionPerformed(ActionEvent evt)
167 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
169 if (evt.getActionCommand().equals(ADDNEW))
171 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
172 null, new Annotation[ap.av.alignment.getWidth()]);
174 if (!editLabelDescription(newAnnotation))
179 ap.av.alignment.addAnnotation(newAnnotation);
180 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
182 else if (evt.getActionCommand().equals(EDITNAME))
184 editLabelDescription(aa[selectedRow]);
187 else if (evt.getActionCommand().equals(HIDE))
189 aa[selectedRow].visible = false;
191 if (aa[selectedRow].label.equals("Quality"))
193 ap.av.quality = null;
196 else if (evt.getActionCommand().equals(DELETE))
198 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
200 else if (evt.getActionCommand().equals(SHOWALL))
202 for (int i = 0; i < aa.length; i++)
204 if (!aa[i].visible && aa[i].annotations != null)
206 aa[i].visible = true;
210 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
212 new AnnotationExporter().exportAnnotations(ap,
213 new AlignmentAnnotation[]
214 { aa[selectedRow] }, null, null);
216 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
218 SequenceI cons = null;
219 if (aa[selectedRow].groupRef != null)
221 cons = aa[selectedRow].groupRef.getConsensusSeq();
225 cons = av.getConsensusSeq();
229 copy_annotseqtoclipboard(cons);
233 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
235 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
238 ap.annotationPanel.adjustPanelHeight();
239 ap.annotationScroller.validate();
240 ap.paintAlignment(true);
249 boolean editLabelDescription(AlignmentAnnotation annotation)
251 EditNameDialog dialog = new EditNameDialog(annotation.label,
252 annotation.description, " Annotation Name ",
253 "Annotation Description ", "Edit Annotation Name/Description");
260 annotation.label = dialog.getName();
262 String text = dialog.getDescription();
263 if (text != null && text.length() == 0)
267 annotation.description = text;
278 public void mousePressed(MouseEvent evt)
280 getSelectedRow(evt.getY() - scrollOffset);
290 public void mouseReleased(MouseEvent evt)
292 int start = selectedRow;
293 getSelectedRow(evt.getY() - scrollOffset);
294 int end = selectedRow;
298 // Swap these annotations
299 AlignmentAnnotation startAA = ap.av.alignment
300 .getAlignmentAnnotation()[start];
301 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
303 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
304 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
310 ap.annotationPanel.repaint();
319 public void mouseEntered(MouseEvent evt)
334 public void mouseExited(MouseEvent evt)
336 if (dragEvent == null)
349 public void mouseDragged(MouseEvent evt)
355 Dimension d = ap.annotationScroller.getPreferredSize();
356 int dif = evt.getY() - oldY;
358 dif /= ap.av.charHeight;
359 dif *= ap.av.charHeight;
361 if ((d.height - dif) > 20)
363 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
365 d = ap.annotationSpaceFillerHolder.getPreferredSize();
366 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
367 d.width, d.height - dif));
368 ap.paintAlignment(true);
385 public void mouseMoved(MouseEvent evt)
387 resizePanel = evt.getY() < 10;
389 getSelectedRow(evt.getY() - scrollOffset);
392 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
394 AlignmentAnnotation aa = ap.av.alignment.getAlignmentAnnotation()[selectedRow];
396 StringBuffer desc = new StringBuffer("<html>");
398 if (aa.description != null
399 && !aa.description.equals("New description"))
401 desc.append(aa.getDescription(true));
407 desc.append("Score: " + aa.score);
410 if (desc.length() != 6)
412 desc.append("</html>");
413 this.setToolTipText(desc.toString());
416 this.setToolTipText(null);
427 public void mouseClicked(MouseEvent evt)
429 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
430 if (SwingUtilities.isLeftMouseButton(evt))
432 if (selectedRow < aa.length)
434 if (aa[selectedRow].groupRef != null)
436 if (evt.getClickCount() >= 2)
438 // todo: make the ap scroll to the selection
439 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
440 ap.av.setSelectionGroup(// new SequenceGroup(
441 aa[selectedRow].groupRef); // );
442 ap.paintAlignment(false);
446 ap.seqPanel.ap.idPanel
447 .highlightSearchResults(aa[selectedRow].groupRef
448 .getSequences(null));
452 else if (aa[selectedRow].sequenceRef != null)
454 Vector sr = new Vector();
455 sr.addElement(aa[selectedRow].sequenceRef);
456 if (evt.getClickCount() == 1)
458 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
460 else if (evt.getClickCount() >= 2)
462 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
463 SequenceGroup sg = new SequenceGroup();
464 sg.addSequence(aa[selectedRow].sequenceRef, false);
465 ap.av.setSelectionGroup(sg);
466 ap.paintAlignment(false);
467 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
473 if (!SwingUtilities.isRightMouseButton(evt))
478 JPopupMenu pop = new JPopupMenu("Annotations");
479 JMenuItem item = new JMenuItem(ADDNEW);
480 item.addActionListener(this);
482 if ((aa == null) || (aa.length == 0))
484 item = new JMenuItem(SHOWALL);
485 item.addActionListener(this);
487 pop.show(this, evt.getX(), evt.getY());
492 item = new JMenuItem(EDITNAME);
493 item.addActionListener(this);
495 item = new JMenuItem(HIDE);
496 item.addActionListener(this);
498 item = new JMenuItem(DELETE);
499 item.addActionListener(this);
501 item = new JMenuItem(SHOWALL);
502 item.addActionListener(this);
504 item = new JMenuItem(OUTPUT_TEXT);
505 item.addActionListener(this);
507 // TODO: annotation object should be typed for autocalculated/derived
509 if (selectedRow < aa.length)
511 if (!aa[selectedRow].autoCalculated)
513 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
515 // display formatting settings for this row.
517 // av and sequencegroup need to implement same interface for
518 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
519 aa[selectedRow].scaleColLabel);
520 item.addActionListener(this);
524 else if (aa[selectedRow].label.indexOf("Consensus") > -1)
527 // av and sequencegroup need to implement same interface for
528 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
529 "Ignore Gaps In Consensus",
530 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
531 .getIgnoreGapsConsensus()
532 : ap.av.getIgnoreGapsConsensus());
533 final AlignmentAnnotation aaa = aa[selectedRow];
534 cbmi.addActionListener(new ActionListener()
536 public void actionPerformed(ActionEvent e)
538 if (aaa.groupRef != null)
540 // TODO: pass on reference to ap so the view can be updated.
541 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
542 ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
546 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
551 // av and sequencegroup need to implement same interface for
552 if (aaa.groupRef != null)
554 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
555 "Show Group Histogram", aa[selectedRow].groupRef
556 .isShowConsensusHistogram());
557 chist.addActionListener(new ActionListener()
559 public void actionPerformed(ActionEvent e)
561 // TODO: pass on reference
567 aaa.groupRef.setShowConsensusHistogram(chist.getState());
569 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
573 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
574 "Show Group Logo", aa[selectedRow].groupRef
575 .isShowSequenceLogo());
576 cprof.addActionListener(new ActionListener()
578 public void actionPerformed(ActionEvent e)
580 // TODO: pass on reference
586 aaa.groupRef.setIncludeAllConsSymbols(cprof.getState());
588 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
595 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
596 "Show Histogram", av.isShowConsensusHistogram());
597 chist.addActionListener(new ActionListener()
599 public void actionPerformed(ActionEvent e)
601 // TODO: pass on reference
607 av.setShowConsensusHistogram(chist.getState());
609 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
613 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
614 "Show Logo", av.isShowSequenceLogo());
615 cprof.addActionListener(new ActionListener()
617 public void actionPerformed(ActionEvent e)
619 // TODO: pass on reference
625 av.setShowSequenceLogo(cprof.getState());
627 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
632 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
633 consclipbrd.addActionListener(this);
634 pop.add(consclipbrd);
637 pop.show(this, evt.getX(), evt.getY());
641 * do a single sequence copy to jalview and the system clipboard
644 * sequence to be copied to clipboard
646 protected void copy_annotseqtoclipboard(SequenceI sq)
648 SequenceI[] seqs = new SequenceI[]
650 String[] omitHidden = null;
651 SequenceI[] dseqs = new SequenceI[]
652 { sq.getDatasetSequence() };
653 if (dseqs[0] == null)
655 dseqs[0] = new Sequence(sq);
656 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
657 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
659 sq.setDatasetSequence(dseqs[0]);
661 Alignment ds = new Alignment(dseqs);
662 if (av.hasHiddenColumns)
664 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
665 sq.getLength(), seqs);
668 String output = new FormatAdapter().formatSequences("Fasta", seqs,
671 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
672 new StringSelection(output), Desktop.instance);
674 Vector hiddenColumns = null;
675 if (av.hasHiddenColumns)
677 hiddenColumns = new Vector();
678 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
680 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
683 hiddenColumns.addElement(new int[]
684 { region[0], region[1] });
688 Desktop.jalviewClipboard = new Object[]
689 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
690 // sequence as special.
700 public void paintComponent(Graphics g)
703 int width = getWidth();
706 width = ap.calculateIdWidth().width + 4;
709 Graphics2D g2 = (Graphics2D) g;
712 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
713 RenderingHints.VALUE_ANTIALIAS_ON);
716 drawComponent(g2, width);
726 public void drawComponent(Graphics g, int width)
728 if (av.getFont().getSize() < 10)
734 g.setFont(av.getFont());
737 FontMetrics fm = g.getFontMetrics(g.getFont());
738 g.setColor(Color.white);
739 g.fillRect(0, 0, getWidth(), getHeight());
741 g.translate(0, scrollOffset);
742 g.setColor(Color.black);
744 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
745 int fontHeight = g.getFont().getSize();
753 for (int i = 0; i < aa.length; i++)
755 g.setColor(Color.black);
764 offset = -aa[i].height / 2;
768 offset += fm.getHeight() / 2;
769 offset -= fm.getDescent();
772 offset += fm.getDescent();
774 x = width - fm.stringWidth(aa[i].label) - 3;
776 if (aa[i].graphGroup > -1)
779 for (int gg = 0; gg < aa.length; gg++)
781 if (aa[gg].graphGroup == aa[i].graphGroup)
787 if (groupSize * (fontHeight + 8) < aa[i].height)
789 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
792 for (int gg = 0; gg < aa.length; gg++)
794 if (aa[gg].graphGroup == aa[i].graphGroup)
796 x = width - fm.stringWidth(aa[gg].label) - 3;
797 g.drawString(aa[gg].label, x, y - graphExtras);
798 if (aa[gg].annotations[0] != null)
800 g.setColor(aa[gg].annotations[0].colour);
803 g.drawLine(x, y - graphExtras - 3, x
804 + fm.stringWidth(aa[gg].label), y - graphExtras - 3);
806 g.setColor(Color.black);
807 graphExtras += fontHeight + 8;
813 g.drawString(aa[i].label, x, y + offset);
820 g.drawImage(image, 2, 0 - scrollOffset, this);
822 else if (dragEvent != null && aa != null)
824 g.setColor(Color.lightGray);
825 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent
830 if ((aa == null) || (aa.length < 1))
832 g.drawString("Right click", 2, 8);
833 g.drawString("to add annotation", 2, 18);