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);
76 private boolean hasHiddenRows;
79 * Creates a new AnnotationLabels object.
84 public AnnotationLabels(AlignmentPanel ap)
88 ToolTipManager.sharedInstance().registerComponent(this);
90 java.net.URL url = getClass().getResource("/images/idwidth.gif");
95 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
100 MediaTracker mt = new MediaTracker(this);
101 mt.addImage(temp, 0);
103 } catch (Exception ex)
107 BufferedImage bi = new BufferedImage(temp.getHeight(this), temp
108 .getWidth(this), BufferedImage.TYPE_INT_RGB);
109 Graphics2D g = (Graphics2D) bi.getGraphics();
110 g.rotate(Math.toRadians(90));
111 g.drawImage(temp, 0, -bi.getWidth(this), this);
114 addMouseListener(this);
115 addMouseMotionListener(this);
118 public AnnotationLabels(AlignViewport av)
129 public void setScrollOffset(int y)
136 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
140 * coordinate position to search for a row
142 void getSelectedRow(int y)
145 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
149 for (int i = 0; i < aa.length; i++)
157 height += aa[i].height;
175 public void actionPerformed(ActionEvent evt)
177 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
179 if (evt.getActionCommand().equals(ADDNEW))
181 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
182 null, new Annotation[ap.av.alignment.getWidth()]);
184 if (!editLabelDescription(newAnnotation))
189 ap.av.alignment.addAnnotation(newAnnotation);
190 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
192 else if (evt.getActionCommand().equals(EDITNAME))
194 editLabelDescription(aa[selectedRow]);
197 else if (evt.getActionCommand().equals(HIDE))
199 aa[selectedRow].visible = false;
201 if (aa[selectedRow].label.equals("Quality"))
203 ap.av.quality = null;
206 else if (evt.getActionCommand().equals(DELETE))
208 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
210 else if (evt.getActionCommand().equals(SHOWALL))
212 for (int i = 0; i < aa.length; i++)
214 if (!aa[i].visible && aa[i].annotations != null)
216 aa[i].visible = true;
220 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
222 new AnnotationExporter().exportAnnotations(ap,
223 new AlignmentAnnotation[]
224 { aa[selectedRow] }, null, null);
226 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
228 SequenceI cons = null;
229 if (aa[selectedRow].groupRef != null)
231 cons = aa[selectedRow].groupRef.getConsensusSeq();
235 cons = av.getConsensusSeq();
239 copy_annotseqtoclipboard(cons);
243 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
245 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
248 ap.annotationPanel.adjustPanelHeight();
249 ap.annotationScroller.validate();
250 ap.paintAlignment(true);
259 boolean editLabelDescription(AlignmentAnnotation annotation)
261 EditNameDialog dialog = new EditNameDialog(annotation.label,
262 annotation.description, " Annotation Name ",
263 "Annotation Description ", "Edit Annotation Name/Description",
271 annotation.label = dialog.getName();
273 String text = dialog.getDescription();
274 if (text != null && text.length() == 0)
278 annotation.description = text;
289 public void mousePressed(MouseEvent evt)
291 getSelectedRow(evt.getY() - scrollOffset);
301 public void mouseReleased(MouseEvent evt)
303 int start = selectedRow;
304 getSelectedRow(evt.getY() - scrollOffset);
305 int end = selectedRow;
309 // Swap these annotations
310 AlignmentAnnotation startAA = ap.av.alignment
311 .getAlignmentAnnotation()[start];
314 end = ap.av.alignment.getAlignmentAnnotation().length - 1;
316 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
318 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
319 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
325 ap.annotationPanel.repaint();
334 public void mouseEntered(MouseEvent evt)
349 public void mouseExited(MouseEvent evt)
351 if (dragEvent == null)
364 public void mouseDragged(MouseEvent evt)
370 Dimension d = ap.annotationScroller.getPreferredSize();
371 int dif = evt.getY() - oldY;
373 dif /= ap.av.charHeight;
374 dif *= ap.av.charHeight;
376 if ((d.height - dif) > 20)
378 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
380 d = ap.annotationSpaceFillerHolder.getPreferredSize();
381 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
382 d.width, d.height - dif));
383 ap.paintAlignment(true);
400 public void mouseMoved(MouseEvent evt)
402 resizePanel = evt.getY() < 10;
404 getSelectedRow(evt.getY() - scrollOffset);
407 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
409 AlignmentAnnotation aa = ap.av.alignment.getAlignmentAnnotation()[selectedRow];
411 StringBuffer desc = new StringBuffer("<html>");
413 if (aa.description != null
414 && !aa.description.equals("New description"))
416 desc.append(aa.getDescription(true));
422 desc.append("Score: " + aa.score);
425 if (desc.length() != 6)
427 desc.append("</html>");
428 this.setToolTipText(desc.toString());
431 this.setToolTipText(null);
442 public void mouseClicked(MouseEvent evt)
444 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
445 if (SwingUtilities.isLeftMouseButton(evt))
447 if (selectedRow > -1 && selectedRow < aa.length)
449 if (aa[selectedRow].groupRef != null)
451 if (evt.getClickCount() >= 2)
453 // todo: make the ap scroll to the selection
454 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
455 ap.av.setSelectionGroup(// new SequenceGroup(
456 aa[selectedRow].groupRef); // );
457 ap.av.sendSelection();
458 ap.paintAlignment(false);
459 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
463 ap.seqPanel.ap.idPanel
464 .highlightSearchResults(aa[selectedRow].groupRef
465 .getSequences(null));
469 else if (aa[selectedRow].sequenceRef != null)
471 Vector sr = new Vector();
472 sr.addElement(aa[selectedRow].sequenceRef);
473 if (evt.getClickCount() == 1)
475 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
477 else if (evt.getClickCount() >= 2)
479 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
480 SequenceGroup sg = new SequenceGroup();
481 sg.addSequence(aa[selectedRow].sequenceRef, false);
482 ap.av.setSelectionGroup(sg);
483 ap.av.sendSelection();
484 ap.paintAlignment(false);
485 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
491 if (!SwingUtilities.isRightMouseButton(evt))
496 JPopupMenu pop = new JPopupMenu("Annotations");
497 JMenuItem item = new JMenuItem(ADDNEW);
498 item.addActionListener(this);
503 { // let the user make everything visible again
504 item = new JMenuItem(SHOWALL);
505 item.addActionListener(this);
508 pop.show(this, evt.getX(), evt.getY());
511 item = new JMenuItem(EDITNAME);
512 item.addActionListener(this);
514 item = new JMenuItem(HIDE);
515 item.addActionListener(this);
517 item = new JMenuItem(DELETE);
518 item.addActionListener(this);
522 item = new JMenuItem(SHOWALL);
523 item.addActionListener(this);
526 item = new JMenuItem(OUTPUT_TEXT);
527 item.addActionListener(this);
529 // TODO: annotation object should be typed for autocalculated/derived
531 if (selectedRow < aa.length)
533 if (!aa[selectedRow].autoCalculated)
535 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
537 // display formatting settings for this row.
539 // av and sequencegroup need to implement same interface for
540 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
541 aa[selectedRow].scaleColLabel);
542 item.addActionListener(this);
546 else if (aa[selectedRow].label.indexOf("Consensus") > -1)
549 // av and sequencegroup need to implement same interface for
550 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
551 "Ignore Gaps In Consensus",
552 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
553 .getIgnoreGapsConsensus()
554 : ap.av.getIgnoreGapsConsensus());
555 final AlignmentAnnotation aaa = aa[selectedRow];
556 cbmi.addActionListener(new ActionListener()
558 public void actionPerformed(ActionEvent e)
560 if (aaa.groupRef != null)
562 // TODO: pass on reference to ap so the view can be updated.
563 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
564 ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
568 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
573 // av and sequencegroup need to implement same interface for
574 if (aaa.groupRef != null)
576 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
577 "Show Group Histogram", aa[selectedRow].groupRef
578 .isShowConsensusHistogram());
579 chist.addActionListener(new ActionListener()
581 public void actionPerformed(ActionEvent e)
583 // TODO: pass on reference
589 aaa.groupRef.setShowConsensusHistogram(chist.getState());
591 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
595 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
596 "Show Group Logo", aa[selectedRow].groupRef
597 .isShowSequenceLogo());
598 cprofl.addActionListener(new ActionListener()
600 public void actionPerformed(ActionEvent e)
602 // TODO: pass on reference
608 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
610 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
617 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
618 "Show Histogram", av.isShowConsensusHistogram());
619 chist.addActionListener(new ActionListener()
621 public void actionPerformed(ActionEvent e)
623 // TODO: pass on reference
629 av.setShowConsensusHistogram(chist.getState());
631 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
635 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
636 "Show Logo", av.isShowSequenceLogo());
637 cprof.addActionListener(new ActionListener()
639 public void actionPerformed(ActionEvent e)
641 // TODO: pass on reference
647 av.setShowSequenceLogo(cprof.getState());
649 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
654 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
655 consclipbrd.addActionListener(this);
656 pop.add(consclipbrd);
659 pop.show(this, evt.getX(), evt.getY());
663 * do a single sequence copy to jalview and the system clipboard
666 * sequence to be copied to clipboard
668 protected void copy_annotseqtoclipboard(SequenceI sq)
670 SequenceI[] seqs = new SequenceI[]
672 String[] omitHidden = null;
673 SequenceI[] dseqs = new SequenceI[]
674 { sq.getDatasetSequence() };
675 if (dseqs[0] == null)
677 dseqs[0] = new Sequence(sq);
678 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
679 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
681 sq.setDatasetSequence(dseqs[0]);
683 Alignment ds = new Alignment(dseqs);
684 if (av.hasHiddenColumns)
686 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
687 sq.getLength(), seqs);
690 String output = new FormatAdapter().formatSequences("Fasta", seqs,
693 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
694 new StringSelection(output), Desktop.instance);
696 Vector hiddenColumns = null;
697 if (av.hasHiddenColumns)
699 hiddenColumns = new Vector();
700 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
702 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
705 hiddenColumns.addElement(new int[]
706 { region[0], region[1] });
710 Desktop.jalviewClipboard = new Object[]
711 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
712 // sequence as special.
722 public void paintComponent(Graphics g)
725 int width = getWidth();
728 width = ap.calculateIdWidth().width + 4;
731 Graphics2D g2 = (Graphics2D) g;
734 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
735 RenderingHints.VALUE_ANTIALIAS_ON);
738 drawComponent(g2, width);
748 public void drawComponent(Graphics g, int width)
750 if (av.getFont().getSize() < 10)
756 g.setFont(av.getFont());
759 FontMetrics fm = g.getFontMetrics(g.getFont());
760 g.setColor(Color.white);
761 g.fillRect(0, 0, getWidth(), getHeight());
763 g.translate(0, scrollOffset);
764 g.setColor(Color.black);
766 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
767 int fontHeight = g.getFont().getSize();
775 hasHiddenRows = false;
776 for (int i = 0; i < aa.length; i++)
778 g.setColor(Color.black);
782 hasHiddenRows = true;
788 offset = -aa[i].height / 2;
792 offset += fm.getHeight() / 2;
793 offset -= fm.getDescent();
796 offset += fm.getDescent();
798 x = width - fm.stringWidth(aa[i].label) - 3;
800 if (aa[i].graphGroup > -1)
803 for (int gg = 0; gg < aa.length; gg++)
805 if (aa[gg].graphGroup == aa[i].graphGroup)
811 if (groupSize * (fontHeight + 8) < aa[i].height)
813 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
816 for (int gg = 0; gg < aa.length; gg++)
818 if (aa[gg].graphGroup == aa[i].graphGroup)
820 x = width - fm.stringWidth(aa[gg].label) - 3;
821 g.drawString(aa[gg].label, x, y - graphExtras);
822 if (aa[gg].annotations[0] != null)
824 g.setColor(aa[gg].annotations[0].colour);
827 g.drawLine(x, y - graphExtras - 3, x
828 + fm.stringWidth(aa[gg].label), y - graphExtras - 3);
830 g.setColor(Color.black);
831 graphExtras += fontHeight + 8;
837 g.drawString(aa[i].label, x, y + offset);
844 g.drawImage(image, 2, 0 - scrollOffset, this);
846 else if (dragEvent != null && aa != null)
848 g.setColor(Color.lightGray);
849 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent
854 if ((aa == null) || (aa.length < 1))
856 g.drawString("Right click", 2, 8);
857 g.drawString("to add annotation", 2, 18);