2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import java.util.regex.Pattern;
27 import java.awt.datatransfer.*;
28 import java.awt.event.*;
29 import java.awt.geom.AffineTransform;
30 import java.awt.image.*;
33 import jalview.datamodel.*;
35 import jalview.util.MessageManager;
43 public class AnnotationLabels extends JPanel implements MouseListener,
44 MouseMotionListener, ActionListener
46 String TOGGLE_LABELSCALE = MessageManager.getString("label.scale_label_to_column");
48 String ADDNEW = MessageManager.getString("label.add_new_row");
50 String EDITNAME = MessageManager.getString("label.edit_label_description");
52 String HIDE = MessageManager.getString("label.hide_row");
54 String DELETE = MessageManager.getString("label.delete_row");
56 String SHOWALL = MessageManager.getString("label.show_all_hidden_rows");
58 String OUTPUT_TEXT = MessageManager.getString("label.export_annotation");
60 String COPYCONS_SEQ = MessageManager.getString("label.copy_consensus_sequence");
62 boolean resizePanel = false;
70 boolean resizing = false;
80 Font font = new Font("Arial", Font.PLAIN, 11);
82 private boolean hasHiddenRows;
85 * Creates a new AnnotationLabels object.
90 public AnnotationLabels(AlignmentPanel ap)
94 ToolTipManager.sharedInstance().registerComponent(this);
96 java.net.URL url = getClass().getResource("/images/idwidth.gif");
101 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
106 MediaTracker mt = new MediaTracker(this);
107 mt.addImage(temp, 0);
109 } catch (Exception ex)
113 BufferedImage bi = new BufferedImage(temp.getHeight(this),
114 temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
115 Graphics2D g = (Graphics2D) bi.getGraphics();
116 g.rotate(Math.toRadians(90));
117 g.drawImage(temp, 0, -bi.getWidth(this), this);
120 addMouseListener(this);
121 addMouseMotionListener(this);
122 addMouseWheelListener(ap.annotationPanel);
125 public AnnotationLabels(AlignViewport av)
136 public void setScrollOffset(int y)
143 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
147 * coordinate position to search for a row
149 void getSelectedRow(int y)
152 AlignmentAnnotation[] aa = ap.av.getAlignment()
153 .getAlignmentAnnotation();
157 for (int i = 0; i < aa.length; i++)
165 height += aa[i].height;
183 public void actionPerformed(ActionEvent evt)
185 AlignmentAnnotation[] aa = ap.av.getAlignment()
186 .getAlignmentAnnotation();
188 if (evt.getActionCommand().equals(ADDNEW))
190 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
191 null, new Annotation[ap.av.getAlignment().getWidth()]);
193 if (!editLabelDescription(newAnnotation))
198 ap.av.getAlignment().addAnnotation(newAnnotation);
199 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
201 else if (evt.getActionCommand().equals(EDITNAME))
203 editLabelDescription(aa[selectedRow]);
206 else if (evt.getActionCommand().equals(HIDE))
208 aa[selectedRow].visible = false;
210 else if (evt.getActionCommand().equals(DELETE))
212 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
214 else if (evt.getActionCommand().equals(SHOWALL))
216 for (int i = 0; i < aa.length; i++)
218 if (!aa[i].visible && aa[i].annotations != null)
220 aa[i].visible = true;
224 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
226 new AnnotationExporter().exportAnnotations(ap,
227 new AlignmentAnnotation[]
228 { aa[selectedRow] }, null, null);
230 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
232 SequenceI cons = null;
233 if (aa[selectedRow].groupRef != null)
235 cons = aa[selectedRow].groupRef.getConsensusSeq();
239 cons = av.getConsensusSeq();
243 copy_annotseqtoclipboard(cons);
247 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
249 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
252 ap.validateAnnotationDimensions(false);
256 // ap.paintAlignment(true);
265 boolean editLabelDescription(AlignmentAnnotation annotation)
267 EditNameDialog dialog = new EditNameDialog(annotation.label,
268 annotation.description, " Annotation Name ",
269 "Annotation Description ", "Edit Annotation Name/Description",
277 annotation.label = dialog.getName();
279 String text = dialog.getDescription();
280 if (text != null && text.length() == 0)
284 annotation.description = text;
295 public void mousePressed(MouseEvent evt)
297 getSelectedRow(evt.getY() - scrollOffset);
307 public void mouseReleased(MouseEvent evt)
309 int start = selectedRow;
310 getSelectedRow(evt.getY() - scrollOffset);
311 int end = selectedRow;
315 // Swap these annotations
316 AlignmentAnnotation startAA = ap.av.getAlignment()
317 .getAlignmentAnnotation()[start];
320 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
322 AlignmentAnnotation endAA = ap.av.getAlignment()
323 .getAlignmentAnnotation()[end];
325 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
326 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
332 ap.annotationPanel.repaint();
341 public void mouseEntered(MouseEvent evt)
356 public void mouseExited(MouseEvent evt)
358 if (dragEvent == null)
371 public void mouseDragged(MouseEvent evt)
377 Dimension d = ap.annotationScroller.getPreferredSize();
378 int dif = evt.getY() - oldY;
380 dif /= ap.av.charHeight;
381 dif *= ap.av.charHeight;
383 if ((d.height - dif) > 20)
385 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
387 d = ap.annotationSpaceFillerHolder.getPreferredSize();
388 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
389 d.width, d.height - dif));
390 ap.paintAlignment(true);
407 public void mouseMoved(MouseEvent evt)
409 resizePanel = evt.getY() < 10;
411 getSelectedRow(evt.getY() - scrollOffset);
414 && ap.av.getAlignment().getAlignmentAnnotation().length > selectedRow)
416 AlignmentAnnotation aa = ap.av.getAlignment()
417 .getAlignmentAnnotation()[selectedRow];
419 StringBuffer desc = new StringBuffer();
420 if (aa.description != null
421 && !aa.description.equals("New description"))
423 // TODO: we could refactor and merge this code with the code in
424 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
426 desc.append(aa.getDescription(true).trim());
427 // check to see if the description is an html fragment.
428 if (desc.length() < 6
429 || (desc.substring(0, 6).toLowerCase().indexOf("<html>") < 0))
431 // clean the description ready for embedding in html
432 desc = new StringBuffer(Pattern.compile("<").matcher(desc)
433 .replaceAll("<"));
434 desc.insert(0, "<html>");
438 // remove terminating html if any
439 int i = desc.substring(desc.length() - 7).toLowerCase()
440 .lastIndexOf("</html>");
443 desc.setLength(desc.length() - 7 + i);
448 desc.append("<br/>");
454 // begin the tooltip's html fragment
455 desc.append("<html>");
459 // TODO: limit precision of score to avoid noise from imprecise doubles
460 // (64.7 becomes 64.7+/some tiny value).
461 desc.append(" Score: " + aa.score);
464 if (desc.length() > 6)
466 desc.append("</html>");
467 this.setToolTipText(desc.toString());
470 this.setToolTipText(null);
481 public void mouseClicked(MouseEvent evt)
483 AlignmentAnnotation[] aa = ap.av.getAlignment()
484 .getAlignmentAnnotation();
485 if (SwingUtilities.isLeftMouseButton(evt))
487 if (selectedRow > -1 && selectedRow < aa.length)
489 if (aa[selectedRow].groupRef != null)
491 if (evt.getClickCount() >= 2)
493 // todo: make the ap scroll to the selection - not necessary, first
494 // click highlights/scrolls, second selects
495 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
496 ap.av.setSelectionGroup(// new SequenceGroup(
497 aa[selectedRow].groupRef); // );
498 ap.paintAlignment(false);
499 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
500 ap.av.sendSelection();
504 ap.seqPanel.ap.idPanel
505 .highlightSearchResults(aa[selectedRow].groupRef
506 .getSequences(null));
510 else if (aa[selectedRow].sequenceRef != null)
512 if (evt.getClickCount() == 1)
514 ap.seqPanel.ap.idPanel.highlightSearchResults(Arrays
515 .asList(new SequenceI[]
516 { aa[selectedRow].sequenceRef }));
518 else if (evt.getClickCount() >= 2)
520 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
521 SequenceGroup sg = ap.av.getSelectionGroup();
524 sg = new SequenceGroup(sg);
526 sg.addSequence(aa[selectedRow].sequenceRef, false);
528 sg = new SequenceGroup();
530 sg.setEndRes(ap.av.getAlignment().getWidth()-1);
531 sg.addSequence(aa[selectedRow].sequenceRef, false);
533 ap.av.setSelectionGroup(sg);
534 ap.av.sendSelection();
535 ap.paintAlignment(false);
536 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
542 if (!SwingUtilities.isRightMouseButton(evt))
547 JPopupMenu pop = new JPopupMenu(
548 MessageManager.getString("label.annotations"));
549 JMenuItem item = new JMenuItem(ADDNEW);
550 item.addActionListener(this);
555 { // let the user make everything visible again
556 item = new JMenuItem(SHOWALL);
557 item.addActionListener(this);
560 pop.show(this, evt.getX(), evt.getY());
563 item = new JMenuItem(EDITNAME);
564 item.addActionListener(this);
566 item = new JMenuItem(HIDE);
567 item.addActionListener(this);
569 item = new JMenuItem(DELETE);
570 item.addActionListener(this);
574 item = new JMenuItem(SHOWALL);
575 item.addActionListener(this);
578 item = new JMenuItem(OUTPUT_TEXT);
579 item.addActionListener(this);
581 // TODO: annotation object should be typed for autocalculated/derived
583 if (selectedRow < aa.length)
585 if (!aa[selectedRow].autoCalculated)
587 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
589 // display formatting settings for this row.
591 // av and sequencegroup need to implement same interface for
592 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
593 aa[selectedRow].scaleColLabel);
594 item.addActionListener(this);
598 else if (aa[selectedRow].label.indexOf("Consensus") > -1)
601 // av and sequencegroup need to implement same interface for
602 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
603 "Ignore Gaps In Consensus",
604 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
605 .getIgnoreGapsConsensus() : ap.av
606 .getIgnoreGapsConsensus());
607 final AlignmentAnnotation aaa = aa[selectedRow];
608 cbmi.addActionListener(new ActionListener()
610 public void actionPerformed(ActionEvent e)
612 if (aaa.groupRef != null)
614 // TODO: pass on reference to ap so the view can be updated.
615 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
616 ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
620 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
625 // av and sequencegroup need to implement same interface for
626 if (aaa.groupRef != null)
628 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
629 "Show Group Histogram",
630 aa[selectedRow].groupRef.isShowConsensusHistogram());
631 chist.addActionListener(new ActionListener()
633 public void actionPerformed(ActionEvent e)
635 // TODO: pass on reference
641 aaa.groupRef.setShowConsensusHistogram(chist.getState());
643 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
647 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
649 aa[selectedRow].groupRef.isShowSequenceLogo());
650 cprofl.addActionListener(new ActionListener()
652 public void actionPerformed(ActionEvent e)
654 // TODO: pass on reference
660 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
662 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
666 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
667 "Normalise Group Logo",
668 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
669 cproflnorm.addActionListener(new ActionListener()
671 public void actionPerformed(ActionEvent e)
674 // TODO: pass on reference
680 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
681 // automatically enable logo display if we're clicked
682 aaa.groupRef.setshowSequenceLogo(true);
684 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
691 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
692 "Show Histogram", av.isShowConsensusHistogram());
693 chist.addActionListener(new ActionListener()
695 public void actionPerformed(ActionEvent e)
697 // TODO: pass on reference
703 av.setShowConsensusHistogram(chist.getState());
704 ap.alignFrame.setMenusForViewport();
706 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
710 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
711 "Show Logo", av.isShowSequenceLogo());
712 cprof.addActionListener(new ActionListener()
714 public void actionPerformed(ActionEvent e)
716 // TODO: pass on reference
722 av.setShowSequenceLogo(cprof.getState());
723 ap.alignFrame.setMenusForViewport();
725 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
729 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
730 "Normalise Logo", av.isNormaliseSequenceLogo());
731 cprofnorm.addActionListener(new ActionListener()
733 public void actionPerformed(ActionEvent e)
735 // TODO: pass on reference
741 av.setShowSequenceLogo(true);
742 av.setNormaliseSequenceLogo(cprofnorm.getState());
743 ap.alignFrame.setMenusForViewport();
745 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
750 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
751 consclipbrd.addActionListener(this);
752 pop.add(consclipbrd);
755 pop.show(this, evt.getX(), evt.getY());
759 * do a single sequence copy to jalview and the system clipboard
762 * sequence to be copied to clipboard
764 protected void copy_annotseqtoclipboard(SequenceI sq)
766 SequenceI[] seqs = new SequenceI[]
768 String[] omitHidden = null;
769 SequenceI[] dseqs = new SequenceI[]
770 { sq.getDatasetSequence() };
771 if (dseqs[0] == null)
773 dseqs[0] = new Sequence(sq);
774 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
775 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
777 sq.setDatasetSequence(dseqs[0]);
779 Alignment ds = new Alignment(dseqs);
780 if (av.hasHiddenColumns())
782 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
783 sq.getLength(), seqs);
786 String output = new FormatAdapter().formatSequences("Fasta", seqs,
789 Toolkit.getDefaultToolkit().getSystemClipboard()
790 .setContents(new StringSelection(output), Desktop.instance);
792 Vector hiddenColumns = null;
793 if (av.hasHiddenColumns())
795 hiddenColumns = new Vector();
796 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
798 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
801 hiddenColumns.addElement(new int[]
802 { region[0], region[1] });
806 Desktop.jalviewClipboard = new Object[]
807 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
808 // sequence as special.
818 public void paintComponent(Graphics g)
821 int width = getWidth();
824 width = ap.calculateIdWidth().width + 4;
827 Graphics2D g2 = (Graphics2D) g;
830 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
831 RenderingHints.VALUE_ANTIALIAS_ON);
834 drawComponent(g2, true, width);
839 * Draw the full set of annotation Labels for the alignment at the given
843 * Graphics2D instance (needed for font scaling)
845 * Width for scaling labels
848 public void drawComponent(Graphics g, int width)
850 drawComponent(g, false, width);
853 private final boolean debugRedraw = false;
856 * Draw the full set of annotation Labels for the alignment at the given
860 * Graphics2D instance (needed for font scaling)
862 * - true indicates that only current visible area needs to be
865 * Width for scaling labels
867 public void drawComponent(Graphics g, boolean clip, int width)
869 if (av.getFont().getSize() < 10)
875 g.setFont(av.getFont());
878 FontMetrics fm = g.getFontMetrics(g.getFont());
879 g.setColor(Color.white);
880 g.fillRect(0, 0, getWidth(), getHeight());
882 g.translate(0, scrollOffset);
883 g.setColor(Color.black);
885 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
886 int fontHeight = g.getFont().getSize();
891 Font baseFont = g.getFont();
892 FontMetrics baseMetrics = fm;
893 int ofontH = fontHeight;
896 int[] visr = (ap != null && ap.annotationPanel != null) ? ap.annotationPanel
897 .getVisibleVRange() : null;
898 if (clip && visr != null)
903 boolean visible = true, before = false, after = false;
906 hasHiddenRows = false;
908 for (int i = 0; i < aa.length; i++)
913 hasHiddenRows = true;
926 System.out.println("before vis: " + i);
930 // don't draw what isn't visible
940 System.out.println("Scroll offset: " + sOffset
941 + " after vis: " + i);
945 // don't draw what isn't visible
949 g.setColor(Color.black);
951 offset = -aa[i].height / 2;
955 offset += fm.getHeight() / 2;
956 offset -= fm.getDescent();
959 offset += fm.getDescent();
961 x = width - fm.stringWidth(aa[i].label) - 3;
963 if (aa[i].graphGroup > -1)
966 // TODO: JAL-1291 revise rendering model so the graphGroup map is
967 // computed efficiently for all visible labels
968 for (int gg = 0; gg < aa.length; gg++)
970 if (aa[gg].graphGroup == aa[i].graphGroup)
975 if (groupSize * (fontHeight + 8) < aa[i].height)
977 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
982 float h = aa[i].height / (float) groupSize, s;
989 fontHeight = -8 + (int) h;
990 s = ((float) fontHeight) / (float) ofontH;
991 Font f = baseFont.deriveFont(AffineTransform
992 .getScaleInstance(s, s));
994 fm = g.getFontMetrics();
995 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
1000 for (int gg = 0; gg < aa.length; gg++)
1002 if (aa[gg].graphGroup == aa[i].graphGroup)
1004 x = width - fm.stringWidth(aa[gg].label) - 3;
1005 g.drawString(aa[gg].label, x, y - graphExtras);
1007 if (aa[gg]._linecolour != null)
1010 g.setColor(aa[gg]._linecolour);
1011 g.drawLine(x, y - graphExtras + 3,
1012 x + fm.stringWidth(aa[gg].label), y - graphExtras
1016 g.setColor(Color.black);
1017 graphExtras += fontHeight + 8;
1021 g.setFont(baseFont);
1023 fontHeight = ofontH;
1027 g.drawString(aa[i].label, x, y + offset);
1034 g.drawImage(image, 2, 0 - scrollOffset, this);
1036 else if (dragEvent != null && aa != null)
1038 g.setColor(Color.lightGray);
1039 g.drawString(aa[selectedRow].label, dragEvent.getX(),
1040 dragEvent.getY() - scrollOffset);
1043 if (!av.wrapAlignment && ((aa == null) || (aa.length < 1)))
1045 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1046 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,