2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.
23 import jalview.analysis.AlignSeq;
24 import jalview.analysis.AlignmentUtils;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.FileFormat;
32 import jalview.io.FormatAdapter;
33 import jalview.util.Comparison;
34 import jalview.util.MessageManager;
35 import jalview.util.Platform;
37 import java.awt.Color;
38 import java.awt.Cursor;
39 import java.awt.Dimension;
41 import java.awt.FontMetrics;
42 import java.awt.Graphics;
43 import java.awt.Graphics2D;
44 import java.awt.Image;
45 import java.awt.MediaTracker;
46 import java.awt.RenderingHints;
47 import java.awt.Toolkit;
48 import java.awt.datatransfer.StringSelection;
49 import java.awt.event.ActionEvent;
50 import java.awt.event.ActionListener;
51 import java.awt.event.MouseEvent;
52 import java.awt.event.MouseListener;
53 import java.awt.event.MouseMotionListener;
54 import java.awt.geom.AffineTransform;
55 import java.awt.image.BufferedImage;
56 import java.util.ArrayList;
57 import java.util.Arrays;
58 import java.util.Collections;
59 import java.util.regex.Pattern;
61 import javax.swing.JCheckBoxMenuItem;
62 import javax.swing.JMenuItem;
63 import javax.swing.JPanel;
64 import javax.swing.JPopupMenu;
65 import javax.swing.SwingUtilities;
66 import javax.swing.ToolTipManager;
69 * The panel that holds the labels for alignment annotations, providing
70 * tooltips, context menus, drag to reorder rows, and drag to adjust panel
73 public class AnnotationLabels extends JPanel
74 implements MouseListener, MouseMotionListener, ActionListener
76 // width in pixels within which height adjuster arrows are shown and active
77 private static final int HEIGHT_ADJUSTER_WIDTH = 50;
79 private static final Pattern LEFT_ANGLE_BRACKET_PATTERN = Pattern
82 private static final Font font = new Font("Arial", Font.PLAIN, 11);
84 private static final String TOGGLE_LABELSCALE = MessageManager
85 .getString("label.scale_label_to_column");
87 private static final String ADDNEW = MessageManager
88 .getString("label.add_new_row");
90 private static final String EDITNAME = MessageManager
91 .getString("label.edit_label_description");
93 private static final String HIDE = MessageManager
94 .getString("label.hide_row");
96 private static final String DELETE = MessageManager
97 .getString("label.delete_row");
99 private static final String SHOWALL = MessageManager
100 .getString("label.show_all_hidden_rows");
102 private static final String OUTPUT_TEXT = MessageManager
103 .getString("label.export_annotation");
105 private static final String COPYCONS_SEQ = MessageManager
106 .getString("label.copy_consensus_sequence");
108 private static Image adjusterImage;
110 private static int adjusterImageHeight;
112 private final boolean debugRedraw = false;
114 private AlignmentPanel ap;
118 private MouseEvent dragEvent;
122 private int selectedRow;
124 private int scrollOffset = 0;
126 private boolean hasHiddenRows;
128 private boolean resizePanel = false;
131 * Creates a new AnnotationLabels object
135 public AnnotationLabels(AlignmentPanel ap)
139 ToolTipManager.sharedInstance().registerComponent(this);
141 if (adjusterImage == null)
146 addMouseListener(this);
147 addMouseMotionListener(this);
148 addMouseWheelListener(ap.getAnnotationPanel());
151 public AnnotationLabels(AlignViewport av)
157 * Loads the gif for the panel height adjustment
159 protected void loadAdjusterImage()
161 java.net.URL url = getClass().getResource("/images/idwidth.gif");
166 temp = Toolkit.getDefaultToolkit().createImage(url);
171 MediaTracker mt = new MediaTracker(this);
172 mt.addImage(temp, 0);
174 } catch (Exception ex)
178 BufferedImage bi = new BufferedImage(temp.getHeight(this),
179 temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
180 Graphics2D g = (Graphics2D) bi.getGraphics();
181 g.rotate(Math.toRadians(90));
182 g.drawImage(temp, 0, -bi.getWidth(this), this);
184 adjusterImageHeight = bi.getHeight();
193 public void setScrollOffset(int y)
200 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
204 * coordinate position to search for a row
206 void getSelectedRow(int y)
209 AlignmentAnnotation[] aa = ap.av.getAlignment()
210 .getAlignmentAnnotation();
214 for (int i = 0; i < aa.length; i++)
222 height += aa[i].height;
241 public void actionPerformed(ActionEvent evt)
243 AlignmentAnnotation[] aa = ap.av.getAlignment()
244 .getAlignmentAnnotation();
246 boolean fullRepaint = false;
247 if (evt.getActionCommand().equals(ADDNEW))
249 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
250 null, new Annotation[ap.av.getAlignment().getWidth()]);
252 if (!editLabelDescription(newAnnotation))
257 ap.av.getAlignment().addAnnotation(newAnnotation);
258 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
261 else if (evt.getActionCommand().equals(EDITNAME))
263 String name = aa[selectedRow].label;
264 editLabelDescription(aa[selectedRow]);
265 if (!name.equalsIgnoreCase(aa[selectedRow].label))
270 else if (evt.getActionCommand().equals(HIDE))
272 aa[selectedRow].visible = false;
274 else if (evt.getActionCommand().equals(DELETE))
276 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
277 ap.av.getCalcManager().removeWorkerForAnnotation(aa[selectedRow]);
280 else if (evt.getActionCommand().equals(SHOWALL))
282 for (int i = 0; i < aa.length; i++)
284 if (!aa[i].visible && aa[i].annotations != null)
286 aa[i].visible = true;
291 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
293 new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]);
295 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
297 SequenceI cons = null;
298 if (aa[selectedRow].groupRef != null)
300 cons = aa[selectedRow].groupRef.getConsensusSeq();
304 cons = av.getConsensusSeq();
308 copy_annotseqtoclipboard(cons);
312 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
314 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
317 ap.refresh(fullRepaint);
327 boolean editLabelDescription(AlignmentAnnotation annotation)
330 EditNameDialog dialog = new EditNameDialog(annotation.label,
331 annotation.description, " Annotation Name ",
332 "Annotation Description ", "Edit Annotation Name/Description",
340 annotation.label = dialog.getName();
342 String text = dialog.getDescription();
343 if (text != null && text.length() == 0)
347 annotation.description = text;
353 public void mousePressed(MouseEvent evt)
355 getSelectedRow(evt.getY() - getScrollOffset());
357 if (evt.isPopupTrigger())
364 * Build and show the Pop-up menu at the right-click mouse position
368 void showPopupMenu(MouseEvent evt)
371 final AlignmentAnnotation[] aa = ap.av.getAlignment()
372 .getAlignmentAnnotation();
374 JPopupMenu pop = new JPopupMenu(
375 MessageManager.getString("label.annotations"));
376 JMenuItem item = new JMenuItem(ADDNEW);
377 item.addActionListener(this);
382 { // let the user make everything visible again
383 item = new JMenuItem(SHOWALL);
384 item.addActionListener(this);
387 pop.show(this, evt.getX(), evt.getY());
390 item = new JMenuItem(EDITNAME);
391 item.addActionListener(this);
393 item = new JMenuItem(HIDE);
394 item.addActionListener(this);
396 // JAL-1264 hide all sequence-specific annotations of this type
397 if (selectedRow < aa.length)
399 if (aa[selectedRow].sequenceRef != null)
401 final String label = aa[selectedRow].label;
402 JMenuItem hideType = new JMenuItem();
403 String text = MessageManager.getString("label.hide_all") + " "
405 hideType.setText(text);
406 hideType.addActionListener(new ActionListener()
409 public void actionPerformed(ActionEvent e)
411 AlignmentUtils.showOrHideSequenceAnnotations(
412 ap.av.getAlignment(), Collections.singleton(label),
414 // for (AlignmentAnnotation ann : ap.av.getAlignment()
415 // .getAlignmentAnnotation())
417 // if (ann.sequenceRef != null && ann.label != null
418 // && ann.label.equals(label))
420 // ann.visible = false;
429 item = new JMenuItem(DELETE);
430 item.addActionListener(this);
434 item = new JMenuItem(SHOWALL);
435 item.addActionListener(this);
438 item = new JMenuItem(OUTPUT_TEXT);
439 item.addActionListener(this);
441 // TODO: annotation object should be typed for autocalculated/derived
443 if (selectedRow < aa.length)
445 final String label = aa[selectedRow].label;
446 if (!(aa[selectedRow].autoCalculated)
447 && !("HMM".equals(aa[selectedRow].getCalcId())))
449 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
451 // display formatting settings for this row.
453 // av and sequencegroup need to implement same interface for
454 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
455 aa[selectedRow].scaleColLabel);
456 item.addActionListener(this);
460 else if (label.indexOf("Consensus") > -1)
465 // av and sequencegroup need to implement same interface for
467 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
468 MessageManager.getString("label.ignore_gaps_consensus"),
469 (aa[selectedRow].groupRef != null)
470 ? aa[selectedRow].groupRef.getIgnoreGapsConsensus()
471 : ap.av.isIgnoreGapsConsensus());
472 final AlignmentAnnotation aaa = aa[selectedRow];
473 cbmi.addActionListener(new ActionListener()
476 public void actionPerformed(ActionEvent e)
478 if (aaa.groupRef != null)
480 // TODO: pass on reference to ap so the view can be updated.
481 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
482 ap.getAnnotationPanel()
483 .paint(ap.getAnnotationPanel().getGraphics());
487 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
489 ap.alignmentChanged();
493 // av and sequencegroup need to implement same interface for
494 if (aaa.groupRef != null)
496 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
497 MessageManager.getString("label.show_group_histogram"),
498 aa[selectedRow].groupRef.isShowConsensusHistogram());
499 chist.addActionListener(new ActionListener()
502 public void actionPerformed(ActionEvent e)
504 // TODO: pass on reference
510 aaa.groupRef.setShowConsensusHistogram(chist.getState());
512 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
516 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
517 MessageManager.getString("label.show_group_logo"),
518 aa[selectedRow].groupRef.isShowSequenceLogo());
519 cprofl.addActionListener(new ActionListener()
522 public void actionPerformed(ActionEvent e)
524 // TODO: pass on reference
530 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
532 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
536 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
537 MessageManager.getString("label.normalise_group_logo"),
538 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
539 cproflnorm.addActionListener(new ActionListener()
542 public void actionPerformed(ActionEvent e)
545 // TODO: pass on reference
551 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
552 // automatically enable logo display if we're clicked
553 aaa.groupRef.setshowSequenceLogo(true);
555 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
562 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
563 MessageManager.getString("label.show_histogram"),
564 av.isShowConsensusHistogram());
565 chist.addActionListener(new ActionListener()
568 public void actionPerformed(ActionEvent e)
570 // TODO: pass on reference
576 av.setShowConsensusHistogram(chist.getState());
578 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
582 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
583 MessageManager.getString("label.show_logo"),
584 av.isShowSequenceLogo());
585 cprof.addActionListener(new ActionListener()
588 public void actionPerformed(ActionEvent e)
590 // TODO: pass on reference
596 av.setShowSequenceLogo(cprof.getState());
598 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
602 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
603 MessageManager.getString("label.normalise_logo"),
604 av.isNormaliseSequenceLogo());
605 cprofnorm.addActionListener(new ActionListener()
608 public void actionPerformed(ActionEvent e)
610 // TODO: pass on reference
616 av.setShowSequenceLogo(true);
617 av.setNormaliseSequenceLogo(cprofnorm.getState());
619 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
624 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
625 consclipbrd.addActionListener(this);
626 pop.add(consclipbrd);
628 else if ("HMM".equals(aa[selectedRow].getCalcId())) // TODO create labels
629 // in message resource
633 final AlignmentAnnotation aaa = aa[selectedRow];
635 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
636 MessageManager.getString(
637 "label.ignore_below_background_frequency"),
638 (aa[selectedRow].groupRef != null)
639 ? aa[selectedRow].groupRef
640 .getIgnoreBelowBackground()
641 : ap.av.isIgnoreBelowBackground());
643 cbmi.addActionListener(new ActionListener()
646 public void actionPerformed(ActionEvent e)
649 if (aaa.groupRef != null)
651 // TODO: pass on reference to ap so the view can be updated.
652 if (aaa.groupRef.getInfoLetterHeight() == false)
654 aaa.groupRef.setIgnoreBelowBackground(cbmi.getState());
655 ap.getAnnotationPanel()
656 .paint(ap.getAnnotationPanel().getGraphics());
659 else if (ap.av.isInfoLetterHeight() == false)
661 ap.av.setIgnoreBelowBackground(cbmi.getState(), ap);
663 ap.alignmentChanged();
667 final JCheckBoxMenuItem letteHeight = new JCheckBoxMenuItem(
668 MessageManager.getString("label.use_info_for_height"),
669 (aa[selectedRow].groupRef != null)
670 ? aa[selectedRow].groupRef.getInfoLetterHeight()
671 : ap.av.isInfoLetterHeight());
673 letteHeight.addActionListener(new ActionListener()
676 public void actionPerformed(ActionEvent e)
678 if (aaa.groupRef != null)
680 // TODO: pass on reference to ap so the view can be updated.
681 aaa.groupRef.setInfoLetterHeight((letteHeight.getState()));
682 if (aaa.groupRef.getIgnoreBelowBackground() == false)
684 aaa.groupRef.setIgnoreBelowBackground(true);
686 ap.getAnnotationPanel()
687 .paint(ap.getAnnotationPanel().getGraphics());
691 ap.av.setInfoLetterHeight(letteHeight.getState(), ap);
692 if (ap.av.isIgnoreBelowBackground() == false)
694 ap.av.setIgnoreBelowBackground(true, ap);
697 ap.alignmentChanged();
700 pop.add(letteHeight);
701 if (aaa.groupRef != null)
703 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
704 MessageManager.getString("label.show_group_histogram"),
705 aa[selectedRow].groupRef.isShowInformationHistogram());
706 chist.addActionListener(new ActionListener()
709 public void actionPerformed(ActionEvent e)
711 // TODO: pass on reference
717 aaa.groupRef.setShowInformationHistogram(chist.getState());
719 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
723 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
724 MessageManager.getString("label.show_group_logo"),
725 aa[selectedRow].groupRef.isShowHMMSequenceLogo());
726 cprofl.addActionListener(new ActionListener()
729 public void actionPerformed(ActionEvent e)
731 // TODO: pass on reference
737 aaa.groupRef.setshowHMMSequenceLogo(cprofl.getState());
739 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
743 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
744 MessageManager.getString("label.normalise_group_logo"),
745 aa[selectedRow].groupRef.isNormaliseHMMSequenceLogo());
746 cproflnorm.addActionListener(new ActionListener()
749 public void actionPerformed(ActionEvent e)
752 // TODO: pass on reference
759 .setNormaliseHMMSequenceLogo(cproflnorm.getState());
760 // automatically enable logo display if we're clicked
761 aaa.groupRef.setshowHMMSequenceLogo(true);
763 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
770 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
771 MessageManager.getString("label.show_histogram"),
772 av.isShowInformationHistogram());
773 chist.addActionListener(new ActionListener()
776 public void actionPerformed(ActionEvent e)
778 // TODO: pass on reference
784 av.setShowInformationHistogram(chist.getState());
786 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
790 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
791 MessageManager.getString("label.show_logo"),
792 av.isShowHMMSequenceLogo());
793 cprof.addActionListener(new ActionListener()
796 public void actionPerformed(ActionEvent e)
798 // TODO: pass on reference
804 av.updateInformation(ap);
805 av.setShowHMMSequenceLogo(cprof.getState());
807 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
811 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
812 MessageManager.getString("label.normalise_logo"),
813 av.isNormaliseHMMSequenceLogo());
814 cprofnorm.addActionListener(new ActionListener()
817 public void actionPerformed(ActionEvent e)
819 // TODO: pass on reference
825 av.setShowHMMSequenceLogo(true);
826 av.setNormaliseHMMSequenceLogo(cprofnorm.getState());
828 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
835 pop.show(this, evt.getX(), evt.getY());
839 * Reorders annotation rows after a drag of a label
844 public void mouseReleased(MouseEvent evt)
846 if (evt.isPopupTrigger())
852 int start = selectedRow;
853 getSelectedRow(evt.getY() - getScrollOffset());
854 int end = selectedRow;
857 * if dragging to resize instead, start == end
861 // Swap these annotations
862 AlignmentAnnotation startAA = ap.av.getAlignment()
863 .getAlignmentAnnotation()[start];
866 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
868 AlignmentAnnotation endAA = ap.av.getAlignment()
869 .getAlignmentAnnotation()[end];
871 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
872 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
878 ap.getAnnotationPanel().repaint();
882 * Removes the height adjuster image on leaving the panel, unless currently
886 public void mouseExited(MouseEvent evt)
888 if (resizePanel && dragEvent == null)
896 * A mouse drag may be either an adjustment of the panel height (if flag
897 * resizePanel is set on), or a reordering of the annotation rows. The former
898 * is dealt with by this method, the latter in mouseReleased.
903 public void mouseDragged(MouseEvent evt)
909 Dimension d = ap.annotationScroller.getPreferredSize();
910 int dif = evt.getY() - oldY;
912 dif /= ap.av.getCharHeight();
913 dif *= ap.av.getCharHeight();
915 if ((d.height - dif) > 20)
917 ap.annotationScroller
918 .setPreferredSize(new Dimension(d.width, d.height - dif));
919 d = ap.annotationSpaceFillerHolder.getPreferredSize();
920 ap.annotationSpaceFillerHolder
921 .setPreferredSize(new Dimension(d.width, d.height - dif));
922 ap.paintAlignment(true, false);
934 * Updates the tooltip as the mouse moves over the labels
939 public void mouseMoved(MouseEvent evt)
941 showOrHideAdjuster(evt);
943 getSelectedRow(evt.getY() - getScrollOffset());
945 if (selectedRow > -1 && ap.av.getAlignment()
946 .getAlignmentAnnotation().length > selectedRow)
948 AlignmentAnnotation aa = ap.av.getAlignment()
949 .getAlignmentAnnotation()[selectedRow];
951 StringBuffer desc = new StringBuffer();
952 if (aa.description != null
953 && !aa.description.equals("New description"))
955 // TODO: we could refactor and merge this code with the code in
956 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
958 desc.append(aa.getDescription(true).trim());
959 // check to see if the description is an html fragment.
960 if (desc.length() < 6 || (desc.substring(0, 6).toLowerCase()
961 .indexOf("<html>") < 0))
963 // clean the description ready for embedding in html
964 desc = new StringBuffer(LEFT_ANGLE_BRACKET_PATTERN.matcher(desc)
965 .replaceAll("<"));
966 desc.insert(0, "<html>");
970 // remove terminating html if any
971 int i = desc.substring(desc.length() - 7).toLowerCase()
972 .lastIndexOf("</html>");
975 desc.setLength(desc.length() - 7 + i);
980 desc.append("<br/>");
982 // if (aa.hasProperties())
984 // desc.append("<table>");
985 // for (String prop : aa.getProperties())
987 // desc.append("<tr><td>" + prop + "</td><td>"
988 // + aa.getProperty(prop) + "</td><tr>");
990 // desc.append("</table>");
995 // begin the tooltip's html fragment
996 desc.append("<html>");
999 // TODO: limit precision of score to avoid noise from imprecise
1001 // (64.7 becomes 64.7+/some tiny value).
1002 desc.append(" Score: " + aa.score);
1005 if (desc.length() > 6)
1007 desc.append("</html>");
1008 this.setToolTipText(desc.toString());
1012 this.setToolTipText(null);
1018 * Shows the height adjuster image if the mouse moves into the top left
1019 * region, or hides it if the mouse leaves the regio
1023 protected void showOrHideAdjuster(MouseEvent evt)
1025 boolean was = resizePanel;
1026 resizePanel = evt.getY() < adjusterImageHeight && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
1028 if (resizePanel != was)
1030 setCursor(Cursor.getPredefinedCursor(
1031 resizePanel ? Cursor.S_RESIZE_CURSOR
1032 : Cursor.DEFAULT_CURSOR));
1038 public void mouseClicked(MouseEvent evt)
1040 final AlignmentAnnotation[] aa = ap.av.getAlignment()
1041 .getAlignmentAnnotation();
1042 if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
1044 if (selectedRow > -1 && selectedRow < aa.length)
1046 if (aa[selectedRow].groupRef != null)
1048 if (evt.getClickCount() >= 2)
1050 // todo: make the ap scroll to the selection - not necessary, first
1051 // click highlights/scrolls, second selects
1052 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
1053 // process modifiers
1054 SequenceGroup sg = ap.av.getSelectionGroup();
1055 if (sg == null || sg == aa[selectedRow].groupRef
1056 || !(Platform.isControlDown(evt) || evt.isShiftDown()))
1058 if (Platform.isControlDown(evt) || evt.isShiftDown())
1060 // clone a new selection group from the associated group
1061 ap.av.setSelectionGroup(
1062 new SequenceGroup(aa[selectedRow].groupRef));
1066 // set selection to the associated group so it can be edited
1067 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
1072 // modify current selection with associated group
1073 int remainToAdd = aa[selectedRow].groupRef.getSize();
1074 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
1076 if (jalview.util.Platform.isControlDown(evt))
1078 sg.addOrRemove(sgs, --remainToAdd == 0);
1082 // notionally, we should also add intermediate sequences from
1083 // last added sequence ?
1084 sg.addSequence(sgs, --remainToAdd == 0);
1089 ap.paintAlignment(false, false);
1090 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
1091 ap.av.sendSelection();
1095 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
1096 aa[selectedRow].groupRef.getSequences(null));
1100 else if (aa[selectedRow].sequenceRef != null)
1102 if (evt.getClickCount() == 1)
1104 ap.getSeqPanel().ap.getIdPanel()
1105 .highlightSearchResults(Arrays.asList(new SequenceI[]
1106 { aa[selectedRow].sequenceRef }));
1108 else if (evt.getClickCount() >= 2)
1110 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
1111 SequenceGroup sg = ap.av.getSelectionGroup();
1114 // we make a copy rather than edit the current selection if no
1115 // modifiers pressed
1116 // see Enhancement JAL-1557
1117 if (!(Platform.isControlDown(evt) || evt.isShiftDown()))
1119 sg = new SequenceGroup(sg);
1121 sg.addSequence(aa[selectedRow].sequenceRef, false);
1125 if (Platform.isControlDown(evt))
1127 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
1131 // notionally, we should also add intermediate sequences from
1132 // last added sequence ?
1133 sg.addSequence(aa[selectedRow].sequenceRef, true);
1139 sg = new SequenceGroup();
1141 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
1142 sg.addSequence(aa[selectedRow].sequenceRef, false);
1144 ap.av.setSelectionGroup(sg);
1145 ap.paintAlignment(false, false);
1146 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
1147 ap.av.sendSelection();
1157 * do a single sequence copy to jalview and the system clipboard
1160 * sequence to be copied to clipboard
1162 protected void copy_annotseqtoclipboard(SequenceI sq)
1164 SequenceI[] seqs = new SequenceI[] { sq };
1165 String[] omitHidden = null;
1166 SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
1167 if (dseqs[0] == null)
1169 dseqs[0] = new Sequence(sq);
1170 dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars,
1171 sq.getSequenceAsString()));
1173 sq.setDatasetSequence(dseqs[0]);
1175 Alignment ds = new Alignment(dseqs);
1176 if (av.hasHiddenColumns())
1178 omitHidden = av.getAlignment().getHiddenColumns()
1179 .getVisibleSequenceStrings(0, sq.getLength(), seqs);
1182 int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
1183 if (av.hasHiddenColumns())
1185 alignmentStartEnd = av.getAlignment().getHiddenColumns()
1186 .getVisibleStartAndEndIndex(av.getAlignment().getWidth());
1189 String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
1190 seqs, omitHidden, alignmentStartEnd);
1192 Toolkit.getDefaultToolkit().getSystemClipboard()
1193 .setContents(new StringSelection(output), Desktop.instance);
1195 ArrayList<int[]> hiddenColumns = null;
1197 if (av.hasHiddenColumns())
1200 hiddenColumns = av.getAlignment().getHiddenColumns()
1201 .getHiddenColumnsCopy();
1205 Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
1207 // sequence ? need to
1209 // sequence as special.
1220 public void paintComponent(Graphics g)
1223 int width = getWidth();
1226 width = ap.calculateIdWidth().width + 4;
1229 Graphics2D g2 = (Graphics2D) g;
1232 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1233 RenderingHints.VALUE_ANTIALIAS_ON);
1236 drawComponent(g2, true, width);
1241 * Draw the full set of annotation Labels for the alignment at the given
1245 * Graphics2D instance (needed for font scaling)
1247 * Width for scaling labels
1250 public void drawComponent(Graphics g, int width)
1252 drawComponent(g, false, width);
1256 * Draw the full set of annotation Labels for the alignment at the given
1260 * Graphics2D instance (needed for font scaling)
1262 * - true indicates that only current visible area needs to be
1265 * Width for scaling labels
1267 public void drawComponent(Graphics g, boolean clip, int width)
1269 if (av.getFont().getSize() < 10)
1275 g.setFont(av.getFont());
1278 FontMetrics fm = g.getFontMetrics(g.getFont());
1279 g.setColor(Color.white);
1280 g.fillRect(0, 0, getWidth(), getHeight());
1282 g.translate(0, getScrollOffset());
1283 g.setColor(Color.black);
1285 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1286 int fontHeight = g.getFont().getSize();
1289 int graphExtras = 0;
1291 Font baseFont = g.getFont();
1292 FontMetrics baseMetrics = fm;
1293 int ofontH = fontHeight;
1296 int[] visr = (ap != null && ap.getAnnotationPanel() != null)
1297 ? ap.getAnnotationPanel().getVisibleVRange()
1299 if (clip && visr != null)
1302 visHeight = visr[1];
1304 boolean visible = true, before = false, after = false;
1307 hasHiddenRows = false;
1309 for (int i = 0; i < aa.length; i++)
1314 hasHiddenRows = true;
1327 System.out.println("before vis: " + i);
1331 // don't draw what isn't visible
1334 if (olY > visHeight)
1342 "Scroll offset: " + sOffset + " after vis: " + i);
1346 // don't draw what isn't visible
1350 g.setColor(Color.black);
1352 offset = -aa[i].height / 2;
1356 offset += fm.getHeight() / 2;
1357 offset -= fm.getDescent();
1361 offset += fm.getDescent();
1364 x = width - fm.stringWidth(aa[i].label) - 3;
1366 if (aa[i].graphGroup > -1)
1369 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1370 // computed efficiently for all visible labels
1371 for (int gg = 0; gg < aa.length; gg++)
1373 if (aa[gg].graphGroup == aa[i].graphGroup)
1378 if (groupSize * (fontHeight + 8) < aa[i].height)
1380 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1385 // scale font to fit
1386 float h = aa[i].height / (float) groupSize, s;
1393 fontHeight = -8 + (int) h;
1394 s = ((float) fontHeight) / (float) ofontH;
1396 .deriveFont(AffineTransform.getScaleInstance(s, s));
1398 fm = g.getFontMetrics();
1399 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1405 for (int gg = 0; gg < aa.length; gg++)
1407 if (aa[gg].graphGroup == aa[i].graphGroup)
1409 x = width - fm.stringWidth(aa[gg].label) - 3;
1410 g.drawString(aa[gg].label, x, y - graphExtras);
1412 if (aa[gg]._linecolour != null)
1415 g.setColor(aa[gg]._linecolour);
1416 g.drawLine(x, y - graphExtras + 3,
1417 x + fm.stringWidth(aa[gg].label),
1418 y - graphExtras + 3);
1421 g.setColor(Color.black);
1422 graphExtras += fontHeight + 8;
1426 g.setFont(baseFont);
1428 fontHeight = ofontH;
1432 g.drawString(aa[i].label, x, y + offset);
1439 // g.drawImage(adjusterImage, 2, 0 - getScrollOffset(), this);
1441 else if (dragEvent != null && aa != null)
1443 g.setColor(Color.lightGray);
1444 g.drawString(aa[selectedRow].label, dragEvent.getX(),
1445 dragEvent.getY() - getScrollOffset());
1448 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1450 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1451 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1456 public int getScrollOffset()
1458 return scrollOffset;
1462 public void mouseEntered(MouseEvent e)