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 java.awt.Color;
24 import java.awt.Cursor;
25 import java.awt.Dimension;
27 import java.awt.FontMetrics;
28 import java.awt.Graphics;
29 import java.awt.Graphics2D;
30 import java.awt.RenderingHints;
31 import java.awt.Toolkit;
32 import java.awt.datatransfer.StringSelection;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseMotionListener;
38 import java.awt.geom.AffineTransform;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.Iterator;
42 import java.util.Locale;
44 import javax.swing.JCheckBoxMenuItem;
45 import javax.swing.JMenuItem;
46 import javax.swing.JPanel;
47 import javax.swing.JPopupMenu;
48 import javax.swing.SwingUtilities;
49 import javax.swing.ToolTipManager;
51 import jalview.analysis.AlignSeq;
52 import jalview.analysis.AlignmentUtils;
53 import jalview.datamodel.Alignment;
54 import jalview.datamodel.AlignmentAnnotation;
55 import jalview.datamodel.Annotation;
56 import jalview.datamodel.ContactMatrixI;
57 import jalview.datamodel.GroupSet;
58 import jalview.datamodel.HiddenColumns;
59 import jalview.datamodel.Sequence;
60 import jalview.datamodel.SequenceGroup;
61 import jalview.datamodel.SequenceI;
62 import jalview.io.FileFormat;
63 import jalview.io.FormatAdapter;
64 import jalview.util.Comparison;
65 import jalview.util.MessageManager;
66 import jalview.util.Platform;
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 private static final String HTML_END_TAG = "</html>";
78 private static final String HTML_START_TAG = "<html>";
81 * width in pixels within which height adjuster arrows are shown and active
83 private static final int HEIGHT_ADJUSTER_WIDTH = 50;
86 * height in pixels for allowing height adjuster to be active
88 private static int HEIGHT_ADJUSTER_HEIGHT = 10;
90 private static final Font font = new Font("Arial", Font.PLAIN, 11);
92 private static final String TOGGLE_LABELSCALE = MessageManager
93 .getString("label.scale_label_to_column");
95 private static final String ADDNEW = MessageManager
96 .getString("label.add_new_row");
98 private static final String EDITNAME = MessageManager
99 .getString("label.edit_label_description");
101 private static final String HIDE = MessageManager
102 .getString("label.hide_row");
104 private static final String DELETE = MessageManager
105 .getString("label.delete_row");
107 private static final String SHOWALL = MessageManager
108 .getString("label.show_all_hidden_rows");
110 private static final String OUTPUT_TEXT = MessageManager
111 .getString("label.export_annotation");
113 private static final String COPYCONS_SEQ = MessageManager
114 .getString("label.copy_consensus_sequence");
116 private final boolean debugRedraw = false;
118 private AlignmentPanel ap;
122 private MouseEvent dragEvent;
126 private int selectedRow;
128 private int scrollOffset = 0;
130 private boolean hasHiddenRows;
132 private boolean resizePanel = false;
135 * Creates a new AnnotationLabels object
139 public AnnotationLabels(AlignmentPanel ap)
144 ToolTipManager.sharedInstance().registerComponent(this);
146 addMouseListener(this);
147 addMouseMotionListener(this);
148 addMouseWheelListener(ap.getAnnotationPanel());
151 public AnnotationLabels(AlignViewport av)
162 public void setScrollOffset(int y)
169 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
173 * coordinate position to search for a row
175 void getSelectedRow(int y)
178 AlignmentAnnotation[] aa = ap.av.getAlignment()
179 .getAlignmentAnnotation();
183 for (int i = 0; i < aa.length; i++)
191 height += aa[i].height;
210 public void actionPerformed(ActionEvent evt)
212 AlignmentAnnotation[] aa = ap.av.getAlignment()
213 .getAlignmentAnnotation();
215 String action = evt.getActionCommand();
216 if (ADDNEW.equals(action))
219 * non-returning dialog
221 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
222 null, new Annotation[ap.av.getAlignment().getWidth()]);
223 editLabelDescription(newAnnotation, true);
225 else if (EDITNAME.equals(action))
228 * non-returning dialog
230 editLabelDescription(aa[selectedRow], false);
232 else if (HIDE.equals(action))
234 aa[selectedRow].visible = false;
236 else if (DELETE.equals(action))
238 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
239 ap.av.getCalcManager().removeWorkerForAnnotation(aa[selectedRow]);
241 else if (SHOWALL.equals(action))
243 for (int i = 0; i < aa.length; i++)
245 if (!aa[i].visible && aa[i].annotations != null)
247 aa[i].visible = true;
251 else if (OUTPUT_TEXT.equals(action))
253 new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]);
255 else if (COPYCONS_SEQ.equals(action))
257 SequenceI cons = null;
258 if (aa[selectedRow].groupRef != null)
260 cons = aa[selectedRow].groupRef.getConsensusSeq();
264 cons = av.getConsensusSeq();
268 copy_annotseqtoclipboard(cons);
271 else if (TOGGLE_LABELSCALE.equals(action))
273 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
280 * Shows a dialog where the annotation name and description may be edited. If
281 * parameter addNew is true, then on confirmation, a new AlignmentAnnotation
282 * is added, else an existing annotation is updated.
287 void editLabelDescription(AlignmentAnnotation annotation, boolean addNew)
289 String name = MessageManager.getString("label.annotation_name");
290 String description = MessageManager
291 .getString("label.annotation_description");
292 String title = MessageManager
293 .getString("label.edit_annotation_name_description");
294 EditNameDialog dialog = new EditNameDialog(annotation.label,
295 annotation.description, name, description);
297 dialog.showDialog(ap.alignFrame, title, () -> {
298 annotation.label = dialog.getName();
299 String text = dialog.getDescription();
300 if (text != null && text.length() == 0)
304 annotation.description = text;
307 ap.av.getAlignment().addAnnotation(annotation);
308 ap.av.getAlignment().setAnnotationIndex(annotation, 0);
315 public void mousePressed(MouseEvent evt)
317 getSelectedRow(evt.getY() - getScrollOffset());
319 if (evt.isPopupTrigger())
326 * Build and show the Pop-up menu at the right-click mouse position
330 void showPopupMenu(MouseEvent evt)
333 final AlignmentAnnotation[] aa = ap.av.getAlignment()
334 .getAlignmentAnnotation();
336 JPopupMenu pop = new JPopupMenu(
337 MessageManager.getString("label.annotations"));
338 JMenuItem item = new JMenuItem(ADDNEW);
339 item.addActionListener(this);
344 { // let the user make everything visible again
345 item = new JMenuItem(SHOWALL);
346 item.addActionListener(this);
349 pop.show(this, evt.getX(), evt.getY());
352 item = new JMenuItem(EDITNAME);
353 item.addActionListener(this);
355 item = new JMenuItem(HIDE);
356 item.addActionListener(this);
358 // JAL-1264 hide all sequence-specific annotations of this type
359 if (selectedRow < aa.length)
361 if (aa[selectedRow].sequenceRef != null)
363 final String label = aa[selectedRow].label;
364 JMenuItem hideType = new JMenuItem();
365 String text = MessageManager.getString("label.hide_all") + " "
367 hideType.setText(text);
368 hideType.addActionListener(new ActionListener()
371 public void actionPerformed(ActionEvent e)
373 AlignmentUtils.showOrHideSequenceAnnotations(
374 ap.av.getAlignment(), Collections.singleton(label),
382 item = new JMenuItem(DELETE);
383 item.addActionListener(this);
387 item = new JMenuItem(SHOWALL);
388 item.addActionListener(this);
391 item = new JMenuItem(OUTPUT_TEXT);
392 item.addActionListener(this);
394 // TODO: annotation object should be typed for autocalculated/derived
396 if (selectedRow < aa.length)
398 final String label = aa[selectedRow].label;
399 if (!aa[selectedRow].autoCalculated)
401 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
403 // display formatting settings for this row.
405 // av and sequencegroup need to implement same interface for
406 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
407 aa[selectedRow].scaleColLabel);
408 item.addActionListener(this);
412 else if (label.indexOf("Consensus") > -1)
414 addConsensusMenuOptions(ap, aa[selectedRow], pop);
416 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
417 consclipbrd.addActionListener(this);
418 pop.add(consclipbrd);
421 addColourOrFilterByOptions(ap,aa[selectedRow],pop);
423 if (aa[selectedRow].graph == AlignmentAnnotation.CONTACT_MAP)
425 addContactMatrixOptions(ap,aa[selectedRow],pop);
426 // Set/adjust threshold for grouping ?
427 // colour alignment by this [type]
428 // select/hide columns by this row
433 pop.show(this, evt.getX(), evt.getY());
436 static void addColourOrFilterByOptions(final AlignmentPanel ap,
437 final AlignmentAnnotation alignmentAnnotation, final JPopupMenu pop)
440 item = new JMenuItem(MessageManager.getString("label.colour_by_annotation"));
441 item.addActionListener(new ActionListener()
445 public void actionPerformed(ActionEvent e)
447 AnnotationColourChooser.displayFor(ap.av, ap,alignmentAnnotation,false);
451 if (alignmentAnnotation.sequenceRef!=null)
453 item = new JMenuItem(MessageManager.getString("label.colour_by_annotation")+" ("+MessageManager.getString("label.per_seq")+")");
454 item.addActionListener(new ActionListener()
457 public void actionPerformed(ActionEvent e)
459 AnnotationColourChooser.displayFor(ap.av, ap,alignmentAnnotation,true);
464 item = new JMenuItem(MessageManager.getString("action.select_by_annotation"));
465 item.addActionListener(new ActionListener()
469 public void actionPerformed(ActionEvent e)
471 AnnotationColumnChooser.displayFor(ap.av,ap,alignmentAnnotation);
476 static void addContactMatrixOptions(final AlignmentPanel ap,
477 final AlignmentAnnotation alignmentAnnotation, final JPopupMenu pop)
480 final ContactMatrixI cm = ap.av.getContactMatrix(alignmentAnnotation);
488 JCheckBoxMenuItem chitem = new JCheckBoxMenuItem("Show Groups on Matrix");
489 boolean showGroups = alignmentAnnotation.isShowGroupsForContactMatrix();
490 final AlignmentAnnotation sel_row=alignmentAnnotation;
491 chitem.setState(showGroups);
492 chitem.addActionListener(new ActionListener()
496 public void actionPerformed(ActionEvent e)
498 sel_row.setShowGroupsForContactMatrix(chitem.getState());
499 ap.getAnnotationPanel()
500 .paint(ap.getAnnotationPanel().getGraphics());
507 item = new JMenuItem("Show Tree for Matrix");
508 item.addActionListener(new ActionListener()
512 public void actionPerformed(ActionEvent e)
515 ap.alignFrame.showContactMapTree(alignmentAnnotation, cm);
523 item = new JMenuItem("Calculate Tree for Matrix");
524 item.addActionListener(new ActionListener()
526 // TODO - refactor to analysis background thread
528 public void actionPerformed(ActionEvent e)
530 new Thread(new Runnable()
535 AlignmentAnnotation alan = alignmentAnnotation;
536 cm.setGroupSet(GroupSet.makeGroups(cm, 5f, true));
537 ap.alignFrame.showContactMapTree(alan, cm);
548 * A helper method that adds menu options for calculation and visualisation of
549 * group and/or alignment consensus annotation to a popup menu. This is
550 * designed to be reusable for either unwrapped mode (popup menu is shown on
551 * component AnnotationLabels), or wrapped mode (popup menu is shown on
552 * IdPanel when the mouse is over an annotation label).
558 static void addConsensusMenuOptions(AlignmentPanel ap,
559 AlignmentAnnotation ann, JPopupMenu pop)
563 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
564 MessageManager.getString("label.ignore_gaps_consensus"),
565 (ann.groupRef != null) ? ann.groupRef.getIgnoreGapsConsensus()
566 : ap.av.isIgnoreGapsConsensus());
567 final AlignmentAnnotation aaa = ann;
568 cbmi.addActionListener(new ActionListener()
571 public void actionPerformed(ActionEvent e)
573 if (aaa.groupRef != null)
575 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
576 ap.getAnnotationPanel()
577 .paint(ap.getAnnotationPanel().getGraphics());
581 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
583 ap.alignmentChanged();
588 if (aaa.groupRef != null)
591 * group consensus options
593 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
594 MessageManager.getString("label.show_group_histogram"),
595 ann.groupRef.isShowConsensusHistogram());
596 chist.addActionListener(new ActionListener()
599 public void actionPerformed(ActionEvent e)
601 aaa.groupRef.setShowConsensusHistogram(chist.getState());
606 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
607 MessageManager.getString("label.show_group_logo"),
608 ann.groupRef.isShowSequenceLogo());
609 cprofl.addActionListener(new ActionListener()
612 public void actionPerformed(ActionEvent e)
614 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
619 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
620 MessageManager.getString("label.normalise_group_logo"),
621 ann.groupRef.isNormaliseSequenceLogo());
622 cproflnorm.addActionListener(new ActionListener()
625 public void actionPerformed(ActionEvent e)
627 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
628 // automatically enable logo display if we're clicked
629 aaa.groupRef.setshowSequenceLogo(true);
638 * alignment consensus options
640 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
641 MessageManager.getString("label.show_histogram"),
642 ap.av.isShowConsensusHistogram());
643 chist.addActionListener(new ActionListener()
646 public void actionPerformed(ActionEvent e)
648 ap.av.setShowConsensusHistogram(chist.getState());
649 ap.alignFrame.setMenusForViewport();
654 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
655 MessageManager.getString("label.show_logo"),
656 ap.av.isShowSequenceLogo());
657 cprof.addActionListener(new ActionListener()
660 public void actionPerformed(ActionEvent e)
662 ap.av.setShowSequenceLogo(cprof.getState());
663 ap.alignFrame.setMenusForViewport();
668 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
669 MessageManager.getString("label.normalise_logo"),
670 ap.av.isNormaliseSequenceLogo());
671 cprofnorm.addActionListener(new ActionListener()
674 public void actionPerformed(ActionEvent e)
676 ap.av.setShowSequenceLogo(true);
677 ap.av.setNormaliseSequenceLogo(cprofnorm.getState());
678 ap.alignFrame.setMenusForViewport();
687 * Reorders annotation rows after a drag of a label
692 public void mouseReleased(MouseEvent evt)
694 if (evt.isPopupTrigger())
700 int start = selectedRow;
701 getSelectedRow(evt.getY() - getScrollOffset());
702 int end = selectedRow;
705 * if dragging to resize instead, start == end
709 // Swap these annotations
710 AlignmentAnnotation startAA = ap.av.getAlignment()
711 .getAlignmentAnnotation()[start];
714 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
716 AlignmentAnnotation endAA = ap.av.getAlignment()
717 .getAlignmentAnnotation()[end];
719 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
720 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
726 ap.getAnnotationPanel().repaint();
730 * Removes the height adjuster image on leaving the panel, unless currently
734 public void mouseExited(MouseEvent evt)
736 if (resizePanel && dragEvent == null)
744 * A mouse drag may be either an adjustment of the panel height (if flag
745 * resizePanel is set on), or a reordering of the annotation rows. The former
746 * is dealt with by this method, the latter in mouseReleased.
751 public void mouseDragged(MouseEvent evt)
757 Dimension d = ap.annotationScroller.getPreferredSize();
758 int dif = evt.getY() - oldY;
760 dif /= ap.av.getCharHeight();
761 dif *= ap.av.getCharHeight();
763 if ((d.height - dif) > 20)
765 ap.annotationScroller
766 .setPreferredSize(new Dimension(d.width, d.height - dif));
767 d = ap.annotationSpaceFillerHolder.getPreferredSize();
768 ap.annotationSpaceFillerHolder
769 .setPreferredSize(new Dimension(d.width, d.height - dif));
770 ap.paintAlignment(true, false);
782 * Updates the tooltip as the mouse moves over the labels
787 public void mouseMoved(MouseEvent evt)
789 showOrHideAdjuster(evt);
791 getSelectedRow(evt.getY() - getScrollOffset());
793 if (selectedRow > -1 && ap.av.getAlignment()
794 .getAlignmentAnnotation().length > selectedRow)
796 AlignmentAnnotation[] anns = ap.av.getAlignment()
797 .getAlignmentAnnotation();
798 AlignmentAnnotation aa = anns[selectedRow];
800 String desc = getTooltip(aa);
801 this.setToolTipText(desc);
802 String msg = getStatusMessage(aa, anns);
803 ap.alignFrame.setStatus(msg);
808 * Constructs suitable text to show in the status bar when over an annotation
809 * label, containing the associated sequence name (if any), and the annotation
810 * labels (or all labels for a graph group annotation)
816 static String getStatusMessage(AlignmentAnnotation aa,
817 AlignmentAnnotation[] anns)
824 StringBuilder msg = new StringBuilder(32);
825 if (aa.sequenceRef != null)
827 msg.append(aa.sequenceRef.getName()).append(" : ");
830 if (aa.graphGroup == -1)
832 msg.append(aa.label);
834 else if (anns != null)
836 boolean first = true;
837 for (int i = anns.length - 1; i >= 0; i--)
839 if (anns[i].graphGroup == aa.graphGroup)
845 msg.append(anns[i].label);
851 return msg.toString();
855 * Answers a tooltip, formatted as html, containing the annotation description
856 * (prefixed by associated sequence id if applicable), and the annotation
857 * (non-positional) score if it has one. Answers null if neither description
858 * nor score is found.
863 static String getTooltip(AlignmentAnnotation aa)
869 StringBuilder tooltip = new StringBuilder();
870 if (aa.description != null && !aa.description.equals("New description"))
872 // TODO: we could refactor and merge this code with the code in
873 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
875 String desc = aa.getDescription(true).trim();
876 if (!desc.toLowerCase(Locale.ROOT).startsWith(HTML_START_TAG))
878 tooltip.append(HTML_START_TAG);
879 desc = desc.replace("<", "<");
881 else if (desc.toLowerCase(Locale.ROOT).endsWith(HTML_END_TAG))
883 desc = desc.substring(0, desc.length() - HTML_END_TAG.length());
885 tooltip.append(desc);
889 // begin the tooltip's html fragment
890 tooltip.append(HTML_START_TAG);
894 if (tooltip.length() > HTML_START_TAG.length())
896 tooltip.append("<br/>");
898 // TODO: limit precision of score to avoid noise from imprecise
900 // (64.7 becomes 64.7+/some tiny value).
901 tooltip.append(" Score: ").append(String.valueOf(aa.score));
904 if (tooltip.length() > HTML_START_TAG.length())
906 return tooltip.append(HTML_END_TAG).toString();
910 * nothing in the tooltip (except "<html>")
916 * Shows the height adjuster image if the mouse moves into the top left
917 * region, or hides it if the mouse leaves the regio
921 protected void showOrHideAdjuster(MouseEvent evt)
923 boolean was = resizePanel;
924 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
925 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
927 if (resizePanel != was)
930 .getPredefinedCursor(resizePanel ? Cursor.S_RESIZE_CURSOR
931 : Cursor.DEFAULT_CURSOR));
937 public void mouseClicked(MouseEvent evt)
939 final AlignmentAnnotation[] aa = ap.av.getAlignment()
940 .getAlignmentAnnotation();
941 if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
943 if (selectedRow > -1 && selectedRow < aa.length)
945 if (aa[selectedRow].groupRef != null)
947 if (evt.getClickCount() >= 2)
949 // todo: make the ap scroll to the selection - not necessary, first
950 // click highlights/scrolls, second selects
951 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
953 SequenceGroup sg = ap.av.getSelectionGroup();
954 if (sg == null || sg == aa[selectedRow].groupRef
955 || !(Platform.isControlDown(evt) || evt.isShiftDown()))
957 if (Platform.isControlDown(evt) || evt.isShiftDown())
959 // clone a new selection group from the associated group
960 ap.av.setSelectionGroup(
961 new SequenceGroup(aa[selectedRow].groupRef));
965 // set selection to the associated group so it can be edited
966 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
971 // modify current selection with associated group
972 int remainToAdd = aa[selectedRow].groupRef.getSize();
973 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
975 if (jalview.util.Platform.isControlDown(evt))
977 sg.addOrRemove(sgs, --remainToAdd == 0);
981 // notionally, we should also add intermediate sequences from
982 // last added sequence ?
983 sg.addSequence(sgs, --remainToAdd == 0);
988 ap.paintAlignment(false, false);
989 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
990 ap.av.sendSelection();
994 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
995 aa[selectedRow].groupRef.getSequences(null));
999 else if (aa[selectedRow].sequenceRef != null)
1001 if (evt.getClickCount() == 1)
1003 ap.getSeqPanel().ap.getIdPanel()
1004 .highlightSearchResults(Arrays.asList(new SequenceI[]
1005 { aa[selectedRow].sequenceRef }));
1007 else if (evt.getClickCount() >= 2)
1009 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
1010 SequenceGroup sg = ap.av.getSelectionGroup();
1013 // we make a copy rather than edit the current selection if no
1014 // modifiers pressed
1015 // see Enhancement JAL-1557
1016 if (!(Platform.isControlDown(evt) || evt.isShiftDown()))
1018 sg = new SequenceGroup(sg);
1020 sg.addSequence(aa[selectedRow].sequenceRef, false);
1024 if (Platform.isControlDown(evt))
1026 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
1030 // notionally, we should also add intermediate sequences from
1031 // last added sequence ?
1032 sg.addSequence(aa[selectedRow].sequenceRef, true);
1038 sg = new SequenceGroup();
1040 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
1041 sg.addSequence(aa[selectedRow].sequenceRef, false);
1043 ap.av.setSelectionGroup(sg);
1044 ap.paintAlignment(false, false);
1045 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
1046 ap.av.sendSelection();
1056 * do a single sequence copy to jalview and the system clipboard
1059 * sequence to be copied to clipboard
1061 protected void copy_annotseqtoclipboard(SequenceI sq)
1063 SequenceI[] seqs = new SequenceI[] { sq };
1064 String[] omitHidden = null;
1065 SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
1066 if (dseqs[0] == null)
1068 dseqs[0] = new Sequence(sq);
1069 dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars,
1070 sq.getSequenceAsString()));
1072 sq.setDatasetSequence(dseqs[0]);
1074 Alignment ds = new Alignment(dseqs);
1075 if (av.hasHiddenColumns())
1077 Iterator<int[]> it = av.getAlignment().getHiddenColumns()
1078 .getVisContigsIterator(0, sq.getLength(), false);
1079 omitHidden = new String[] { sq.getSequenceStringFromIterator(it) };
1082 int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
1083 if (av.hasHiddenColumns())
1085 alignmentStartEnd = av.getAlignment().getHiddenColumns()
1086 .getVisibleStartAndEndIndex(av.getAlignment().getWidth());
1089 String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
1090 seqs, omitHidden, alignmentStartEnd);
1092 Toolkit.getDefaultToolkit().getSystemClipboard()
1093 .setContents(new StringSelection(output), Desktop.instance);
1095 HiddenColumns hiddenColumns = null;
1097 if (av.hasHiddenColumns())
1099 hiddenColumns = new HiddenColumns(
1100 av.getAlignment().getHiddenColumns());
1103 Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
1105 // sequence ? need to
1107 // sequence as special.
1118 public void paintComponent(Graphics g)
1121 int width = getWidth();
1124 width = ap.calculateIdWidth().width;
1127 Graphics2D g2 = (Graphics2D) g;
1130 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1131 RenderingHints.VALUE_ANTIALIAS_ON);
1134 drawComponent(g2, true, width);
1139 * Draw the full set of annotation Labels for the alignment at the given
1143 * Graphics2D instance (needed for font scaling)
1145 * Width for scaling labels
1148 public void drawComponent(Graphics g, int width)
1150 drawComponent(g, false, width);
1154 * Draw the full set of annotation Labels for the alignment at the given
1158 * Graphics2D instance (needed for font scaling)
1160 * - true indicates that only current visible area needs to be
1163 * Width for scaling labels
1165 public void drawComponent(Graphics g, boolean clip, int width)
1167 if (av.getFont().getSize() < 10)
1173 g.setFont(av.getFont());
1176 FontMetrics fm = g.getFontMetrics(g.getFont());
1177 g.setColor(Color.white);
1178 g.fillRect(0, 0, getWidth(), getHeight());
1180 g.translate(0, getScrollOffset());
1181 g.setColor(Color.black);
1182 SequenceI lastSeqRef = null;
1183 String lastLabel = null;
1184 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1185 int fontHeight = g.getFont().getSize();
1188 int graphExtras = 0;
1190 Font baseFont = g.getFont();
1191 FontMetrics baseMetrics = fm;
1192 int ofontH = fontHeight;
1195 int[] visr = (ap != null && ap.getAnnotationPanel() != null)
1196 ? ap.getAnnotationPanel().getVisibleVRange()
1198 if (clip && visr != null)
1201 visHeight = visr[1];
1203 boolean visible = true, before = false, after = false;
1206 hasHiddenRows = false;
1209 for (int i = 0; i < aa.length; i++)
1214 hasHiddenRows = true;
1218 // look ahead to next annotation
1219 for (nexAA = i + 1; nexAA < aa.length
1220 && !aa[nexAA].visible; nexAA++)
1231 System.out.println("before vis: " + i);
1235 // don't draw what isn't visible
1238 if (olY > visHeight)
1246 "Scroll offset: " + sOffset + " after vis: " + i);
1250 // don't draw what isn't visible
1254 g.setColor(Color.black);
1256 offset = -aa[i].height / 2;
1260 offset += fm.getHeight() / 2;
1261 offset -= fm.getDescent();
1265 offset += fm.getDescent();
1267 String label = aa[i].label;
1268 boolean vertBar = false;
1269 if ((lastLabel != null && lastLabel.equals(label)))
1271 label = aa[i].description;
1275 if (nexAA < aa.length && label.equals(aa[nexAA].label)) // &&
1276 // aa[nexY].sequenceRef==aa[i].sequenceRef)
1279 // next label is the same as this label
1280 label = aa[i].description;
1287 if (aa[i].sequenceRef != null)
1289 if (aa[i].sequenceRef != lastSeqRef)
1291 label = aa[i].sequenceRef.getName() + " " + label;
1292 // TODO record relationship between sequence and this annotation and
1300 x = width - fm.stringWidth(label) - 3;
1302 if (aa[i].graphGroup > -1)
1305 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1306 // computed efficiently for all visible labels
1307 for (int gg = 0; gg < aa.length; gg++)
1309 if (aa[gg].graphGroup == aa[i].graphGroup)
1314 if (groupSize * (fontHeight + 8) < aa[i].height)
1316 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1321 // scale font to fit
1322 float h = aa[i].height / (float) groupSize, s;
1329 fontHeight = -8 + (int) h;
1330 s = ((float) fontHeight) / (float) ofontH;
1332 .deriveFont(AffineTransform.getScaleInstance(s, s));
1334 fm = g.getFontMetrics();
1335 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1341 for (int gg = 0; gg < aa.length; gg++)
1343 if (aa[gg].graphGroup == aa[i].graphGroup)
1345 x = width - fm.stringWidth(aa[gg].label) - 3;
1346 g.drawString(aa[gg].label, x, y - graphExtras);
1348 if (aa[gg]._linecolour != null)
1351 g.setColor(aa[gg]._linecolour);
1352 g.drawLine(x, y - graphExtras + 3,
1353 x + fm.stringWidth(aa[gg].label),
1354 y - graphExtras + 3);
1357 g.setColor(Color.black);
1358 graphExtras += fontHeight + 8;
1362 g.setFont(baseFont);
1364 fontHeight = ofontH;
1370 g.drawLine(width - 3, y + offset - fontHeight, width - 3,
1371 (int) (y - 1.5 * aa[i].height - offset - fontHeight));
1372 // g.drawLine(20, y + offset, x - 20, y + offset);
1375 g.drawString(label, x, y + offset);
1377 lastSeqRef = aa[i].sequenceRef;
1381 if (!resizePanel && dragEvent != null && aa != null)
1383 g.setColor(Color.lightGray);
1385 (aa[selectedRow].sequenceRef == null ? ""
1386 : aa[selectedRow].sequenceRef.getName())
1387 + aa[selectedRow].label,
1388 dragEvent.getX(), dragEvent.getY() - getScrollOffset());
1391 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1393 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1394 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1399 public int getScrollOffset()
1401 return scrollOffset;
1405 public void mouseEntered(MouseEvent e)