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.Canvas;
24 import java.awt.Color;
25 import java.awt.Cursor;
26 import java.awt.Dimension;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.RenderingHints;
32 import java.awt.Toolkit;
33 import java.awt.datatransfer.StringSelection;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38 import java.awt.event.MouseMotionListener;
39 import java.awt.geom.AffineTransform;
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.Iterator;
43 import java.util.Locale;
45 import javax.swing.JCheckBoxMenuItem;
46 import javax.swing.JMenuItem;
47 import javax.swing.JPanel;
48 import javax.swing.JPopupMenu;
49 import javax.swing.SwingUtilities;
50 import javax.swing.ToolTipManager;
52 import jalview.analysis.AlignSeq;
53 import jalview.analysis.AlignmentUtils;
54 import jalview.bin.Cache;
55 import jalview.bin.Jalview;
56 import jalview.datamodel.Alignment;
57 import jalview.datamodel.AlignmentAnnotation;
58 import jalview.datamodel.Annotation;
59 import jalview.datamodel.ContactMatrixI;
60 import jalview.datamodel.GroupSet;
61 import jalview.datamodel.HiddenColumns;
62 import jalview.datamodel.Sequence;
63 import jalview.datamodel.SequenceGroup;
64 import jalview.datamodel.SequenceI;
65 import jalview.io.FileFormat;
66 import jalview.io.FormatAdapter;
67 import jalview.util.Comparison;
68 import jalview.util.IdUtils;
69 import jalview.util.IdUtils.IdType;
70 import jalview.util.MessageManager;
71 import jalview.util.Platform;
74 * The panel that holds the labels for alignment annotations, providing
75 * tooltips, context menus, drag to reorder rows, and drag to adjust panel
78 public class AnnotationLabels extends JPanel
79 implements MouseListener, MouseMotionListener, ActionListener
81 private static final String HTML_END_TAG = "</html>";
83 private static final String HTML_START_TAG = "<html>";
86 * width in pixels within which height adjuster arrows are shown and active
88 private static final int HEIGHT_ADJUSTER_WIDTH = 50;
91 * height in pixels for allowing height adjuster to be active
93 public static int HEIGHT_ADJUSTER_HEIGHT = 10;
95 private static final Font font = new Font("Arial", Font.PLAIN, 11);
97 private static final String TOGGLE_LABELSCALE = MessageManager
98 .getString("label.scale_label_to_column");
100 private static final String ADDNEW = MessageManager
101 .getString("label.add_new_row");
103 private static final String EDITNAME = MessageManager
104 .getString("label.edit_label_description");
106 private static final String HIDE = MessageManager
107 .getString("label.hide_row");
109 private static final String DELETE = MessageManager
110 .getString("label.delete_row");
112 private static final String SHOWALL = MessageManager
113 .getString("label.show_all_hidden_rows");
115 private static final String OUTPUT_TEXT = MessageManager
116 .getString("label.export_annotation");
118 private static final String COPYCONS_SEQ = MessageManager
119 .getString("label.copy_consensus_sequence");
121 private static final String ADJUST_ANNOTATION_LABELS_WIDTH_PREF = "ADJUST_ANNOTATION_LABELS_WIDTH";
123 private final boolean debugRedraw = false;
125 private AlignmentPanel ap;
129 private MouseEvent dragEvent;
133 private int selectedRow;
135 private int scrollOffset = 0;
137 private boolean hasHiddenRows;
139 private boolean resizePanel = false;
141 private int annotationIdWidth = -1;
143 public static final String RESIZE_MARGINS_MARK_PREF = "RESIZE_MARGINS_MARK";
146 * Creates a new AnnotationLabels object
150 public AnnotationLabels(AlignmentPanel ap)
154 ToolTipManager.sharedInstance().registerComponent(this);
156 addMouseListener(this);
157 addMouseMotionListener(this);
158 addMouseWheelListener(ap.getAnnotationPanel());
161 public AnnotationLabels(AlignViewport av)
172 public void setScrollOffset(int y)
179 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
183 * coordinate position to search for a row
185 void getSelectedRow(int y)
188 AlignmentAnnotation[] aa = ap.av.getAlignment()
189 .getAlignmentAnnotation();
193 for (int i = 0; i < aa.length; i++)
201 height += aa[i].height;
220 public void actionPerformed(ActionEvent evt)
222 AlignmentAnnotation[] aa = ap.av.getAlignment()
223 .getAlignmentAnnotation();
225 String action = evt.getActionCommand();
226 if (ADDNEW.equals(action))
229 * non-returning dialog
231 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
232 null, new Annotation[ap.av.getAlignment().getWidth()]);
233 editLabelDescription(newAnnotation, true);
235 else if (EDITNAME.equals(action))
238 * non-returning dialog
240 editLabelDescription(aa[selectedRow], false);
242 else if (HIDE.equals(action))
244 aa[selectedRow].visible = false;
246 else if (DELETE.equals(action))
248 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
249 ap.av.getCalcManager().removeWorkerForAnnotation(aa[selectedRow]);
251 else if (SHOWALL.equals(action))
253 for (int i = 0; i < aa.length; i++)
255 if (!aa[i].visible && aa[i].annotations != null)
257 aa[i].visible = true;
261 else if (OUTPUT_TEXT.equals(action))
263 new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]);
265 else if (COPYCONS_SEQ.equals(action))
267 SequenceI cons = null;
268 if (aa[selectedRow].groupRef != null)
270 cons = aa[selectedRow].groupRef.getConsensusSeq();
274 cons = av.getConsensusSeq();
278 copy_annotseqtoclipboard(cons);
281 else if (TOGGLE_LABELSCALE.equals(action))
283 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
290 * Shows a dialog where the annotation name and description may be edited. If
291 * parameter addNew is true, then on confirmation, a new AlignmentAnnotation
292 * is added, else an existing annotation is updated.
297 void editLabelDescription(AlignmentAnnotation annotation, boolean addNew)
299 String name = MessageManager.getString("label.annotation_name");
300 String description = MessageManager
301 .getString("label.annotation_description");
302 String title = MessageManager
303 .getString("label.edit_annotation_name_description");
304 EditNameDialog dialog = new EditNameDialog(annotation.label,
305 annotation.description, name, description);
307 dialog.showDialog(ap.alignFrame, title, () -> {
308 annotation.label = dialog.getName();
309 String text = dialog.getDescription();
310 if (text != null && text.length() == 0)
314 annotation.description = text;
317 ap.av.getAlignment().addAnnotation(annotation);
318 ap.av.getAlignment().setAnnotationIndex(annotation, 0);
325 public void mousePressed(MouseEvent evt)
327 getSelectedRow(evt.getY() - getScrollOffset());
329 if (evt.isPopupTrigger())
336 * Build and show the Pop-up menu at the right-click mouse position
340 void showPopupMenu(MouseEvent evt)
343 final AlignmentAnnotation[] aa = ap.av.getAlignment()
344 .getAlignmentAnnotation();
346 JPopupMenu pop = new JPopupMenu(
347 MessageManager.getString("label.annotations"));
348 JMenuItem item = new JMenuItem(ADDNEW);
349 item.addActionListener(this);
354 { // let the user make everything visible again
355 item = new JMenuItem(SHOWALL);
356 item.addActionListener(this);
359 pop.show(this, evt.getX(), evt.getY());
362 item = new JMenuItem(EDITNAME);
363 item.addActionListener(this);
365 item = new JMenuItem(HIDE);
366 item.addActionListener(this);
368 // JAL-1264 hide all sequence-specific annotations of this type
369 if (selectedRow < aa.length)
371 if (aa[selectedRow].sequenceRef != null)
373 final String label = aa[selectedRow].label;
374 JMenuItem hideType = new JMenuItem();
375 String text = MessageManager.getString("label.hide_all") + " "
377 hideType.setText(text);
378 hideType.addActionListener(new ActionListener()
381 public void actionPerformed(ActionEvent e)
383 AlignmentUtils.showOrHideSequenceAnnotations(
384 ap.av.getAlignment(), Collections.singleton(label),
392 item = new JMenuItem(DELETE);
393 item.addActionListener(this);
397 item = new JMenuItem(SHOWALL);
398 item.addActionListener(this);
401 item = new JMenuItem(OUTPUT_TEXT);
402 item.addActionListener(this);
404 // TODO: annotation object should be typed for autocalculated/derived
406 if (selectedRow < aa.length)
408 final String label = aa[selectedRow].label;
409 if (!aa[selectedRow].autoCalculated)
411 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
413 // display formatting settings for this row.
415 // av and sequencegroup need to implement same interface for
416 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
417 aa[selectedRow].scaleColLabel);
418 item.addActionListener(this);
422 else if (label.indexOf("Consensus") > -1)
424 addConsensusMenuOptions(ap, aa[selectedRow], pop);
426 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
427 consclipbrd.addActionListener(this);
428 pop.add(consclipbrd);
431 addColourOrFilterByOptions(ap, aa[selectedRow], pop);
433 if (aa[selectedRow].graph == AlignmentAnnotation.CONTACT_MAP)
435 addContactMatrixOptions(ap, aa[selectedRow], pop);
436 // Set/adjust threshold for grouping ?
437 // colour alignment by this [type]
438 // select/hide columns by this row
443 pop.show(this, evt.getX(), evt.getY());
446 static void addColourOrFilterByOptions(final AlignmentPanel ap,
447 final AlignmentAnnotation alignmentAnnotation,
448 final JPopupMenu pop)
451 item = new JMenuItem(
452 MessageManager.getString("label.colour_by_annotation"));
453 item.addActionListener(new ActionListener()
457 public void actionPerformed(ActionEvent e)
459 AnnotationColourChooser.displayFor(ap.av, ap, alignmentAnnotation,
464 if (alignmentAnnotation.sequenceRef != null)
466 item = new JMenuItem(
467 MessageManager.getString("label.colour_by_annotation") + " ("
468 + MessageManager.getString("label.per_seq") + ")");
469 item.addActionListener(new ActionListener()
472 public void actionPerformed(ActionEvent e)
474 AnnotationColourChooser.displayFor(ap.av, ap, alignmentAnnotation,
480 item = new JMenuItem(
481 MessageManager.getString("action.select_by_annotation"));
482 item.addActionListener(new ActionListener()
486 public void actionPerformed(ActionEvent e)
488 AnnotationColumnChooser.displayFor(ap.av, ap, alignmentAnnotation);
494 static void addContactMatrixOptions(final AlignmentPanel ap,
495 final AlignmentAnnotation alignmentAnnotation,
496 final JPopupMenu pop)
499 final ContactMatrixI cm = ap.av.getContactMatrix(alignmentAnnotation);
507 JCheckBoxMenuItem chitem = new JCheckBoxMenuItem(
508 MessageManager.getString("action.show_groups_on_matrix"));
509 chitem.setToolTipText(MessageManager
510 .getString("action.show_groups_on_matrix_tooltip"));
511 boolean showGroups = alignmentAnnotation
512 .isShowGroupsForContactMatrix();
513 final AlignmentAnnotation sel_row = alignmentAnnotation;
514 chitem.setState(showGroups);
515 chitem.addActionListener(new ActionListener()
519 public void actionPerformed(ActionEvent e)
521 sel_row.setShowGroupsForContactMatrix(chitem.getState());
522 // so any annotation colour changes are propagated - though they
523 // probably won't be unless the annotation row colours are removed
525 ap.alignmentChanged();
532 item = new JMenuItem(
533 MessageManager.getString("action.show_tree_for_matrix"));
534 item.setToolTipText(MessageManager
535 .getString("action.show_tree_for_matrix_tooltip"));
536 item.addActionListener(new ActionListener()
540 public void actionPerformed(ActionEvent e)
543 ap.alignFrame.showContactMapTree(alignmentAnnotation, cm);
551 item = new JMenuItem(
552 MessageManager.getString("action.cluster_matrix"));
554 MessageManager.getString("action.cluster_matrix_tooltip"));
555 item.addActionListener(new ActionListener()
558 public void actionPerformed(ActionEvent e)
560 new Thread(new Runnable()
566 ap.alignFrame.setProgressBar(
567 MessageManager.formatMessage(
568 "action.clustering_matrix_for",
569 cm.getAnnotDescr(), 5f),
570 progBar = IdUtils.newId(IdType.PROGRESS));
571 cm.setGroupSet(GroupSet.makeGroups(cm, true));
572 cm.randomlyReColourGroups();
573 cm.transferGroupColorsTo(alignmentAnnotation);
574 ap.alignmentChanged();
575 ap.alignFrame.showContactMapTree(alignmentAnnotation, cm);
576 ap.alignFrame.setProgressBar(null, progBar);
587 * A helper method that adds menu options for calculation and visualisation of
588 * group and/or alignment consensus annotation to a popup menu. This is
589 * designed to be reusable for either unwrapped mode (popup menu is shown on
590 * component AnnotationLabels), or wrapped mode (popup menu is shown on
591 * IdPanel when the mouse is over an annotation label).
597 static void addConsensusMenuOptions(AlignmentPanel ap,
598 AlignmentAnnotation ann, JPopupMenu pop)
602 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
603 MessageManager.getString("label.ignore_gaps_consensus"),
604 (ann.groupRef != null) ? ann.groupRef.getIgnoreGapsConsensus()
605 : ap.av.isIgnoreGapsConsensus());
606 final AlignmentAnnotation aaa = ann;
607 cbmi.addActionListener(new ActionListener()
610 public void actionPerformed(ActionEvent e)
612 if (aaa.groupRef != null)
614 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
615 ap.getAnnotationPanel()
616 .paint(ap.getAnnotationPanel().getGraphics());
620 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
622 ap.alignmentChanged();
627 if (aaa.groupRef != null)
630 * group consensus options
632 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
633 MessageManager.getString("label.show_group_histogram"),
634 ann.groupRef.isShowConsensusHistogram());
635 chist.addActionListener(new ActionListener()
638 public void actionPerformed(ActionEvent e)
640 aaa.groupRef.setShowConsensusHistogram(chist.getState());
645 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
646 MessageManager.getString("label.show_group_logo"),
647 ann.groupRef.isShowSequenceLogo());
648 cprofl.addActionListener(new ActionListener()
651 public void actionPerformed(ActionEvent e)
653 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
658 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
659 MessageManager.getString("label.normalise_group_logo"),
660 ann.groupRef.isNormaliseSequenceLogo());
661 cproflnorm.addActionListener(new ActionListener()
664 public void actionPerformed(ActionEvent e)
666 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
667 // automatically enable logo display if we're clicked
668 aaa.groupRef.setshowSequenceLogo(true);
677 * alignment consensus options
679 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
680 MessageManager.getString("label.show_histogram"),
681 ap.av.isShowConsensusHistogram());
682 chist.addActionListener(new ActionListener()
685 public void actionPerformed(ActionEvent e)
687 ap.av.setShowConsensusHistogram(chist.getState());
688 ap.alignFrame.setMenusForViewport();
693 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
694 MessageManager.getString("label.show_logo"),
695 ap.av.isShowSequenceLogo());
696 cprof.addActionListener(new ActionListener()
699 public void actionPerformed(ActionEvent e)
701 ap.av.setShowSequenceLogo(cprof.getState());
702 ap.alignFrame.setMenusForViewport();
707 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
708 MessageManager.getString("label.normalise_logo"),
709 ap.av.isNormaliseSequenceLogo());
710 cprofnorm.addActionListener(new ActionListener()
713 public void actionPerformed(ActionEvent e)
715 ap.av.setShowSequenceLogo(true);
716 ap.av.setNormaliseSequenceLogo(cprofnorm.getState());
717 ap.alignFrame.setMenusForViewport();
726 * Reorders annotation rows after a drag of a label
731 public void mouseReleased(MouseEvent evt)
733 if (evt.isPopupTrigger())
739 int start = selectedRow;
740 getSelectedRow(evt.getY() - getScrollOffset());
741 int end = selectedRow;
744 * if dragging to resize instead, start == end
748 // Swap these annotations
749 AlignmentAnnotation startAA = ap.av.getAlignment()
750 .getAlignmentAnnotation()[start];
753 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
755 AlignmentAnnotation endAA = ap.av.getAlignment()
756 .getAlignmentAnnotation()[end];
758 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
759 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
765 ap.getAnnotationPanel().repaint();
769 * Removes the height adjuster image on leaving the panel, unless currently
773 public void mouseExited(MouseEvent evt)
775 if (resizePanel && dragEvent == null)
783 * A mouse drag may be either an adjustment of the panel height (if flag
784 * resizePanel is set on), or a reordering of the annotation rows. The former
785 * is dealt with by this method, the latter in mouseReleased.
790 public void mouseDragged(MouseEvent evt)
796 Dimension d = ap.annotationScroller.getPreferredSize();
797 int dif = evt.getY() - oldY;
799 dif /= ap.av.getCharHeight();
800 dif *= ap.av.getCharHeight();
802 if ((d.height - dif) > 20)
804 ap.annotationScroller
805 .setPreferredSize(new Dimension(d.width, d.height - dif));
806 d = ap.annotationSpaceFillerHolder.getPreferredSize();
807 ap.annotationSpaceFillerHolder
808 .setPreferredSize(new Dimension(d.width, d.height - dif));
809 ap.paintAlignment(true, false);
821 * Updates the tooltip as the mouse moves over the labels
826 public void mouseMoved(MouseEvent evt)
828 showOrHideAdjuster(evt);
830 getSelectedRow(evt.getY() - getScrollOffset());
832 if (selectedRow > -1 && ap.av.getAlignment()
833 .getAlignmentAnnotation().length > selectedRow)
835 AlignmentAnnotation[] anns = ap.av.getAlignment()
836 .getAlignmentAnnotation();
837 AlignmentAnnotation aa = anns[selectedRow];
839 String desc = getTooltip(aa);
840 this.setToolTipText(desc);
841 String msg = getStatusMessage(aa, anns);
842 ap.alignFrame.setStatus(msg);
847 * Constructs suitable text to show in the status bar when over an annotation
848 * label, containing the associated sequence name (if any), and the annotation
849 * labels (or all labels for a graph group annotation)
855 static String getStatusMessage(AlignmentAnnotation aa,
856 AlignmentAnnotation[] anns)
863 StringBuilder msg = new StringBuilder(32);
864 if (aa.sequenceRef != null)
866 msg.append(aa.sequenceRef.getName()).append(" : ");
869 if (aa.graphGroup == -1)
871 msg.append(aa.label);
873 else if (anns != null)
875 boolean first = true;
876 for (int i = anns.length - 1; i >= 0; i--)
878 if (anns[i].graphGroup == aa.graphGroup)
884 msg.append(anns[i].label);
890 return msg.toString();
894 * Answers a tooltip, formatted as html, containing the annotation description
895 * (prefixed by associated sequence id if applicable), and the annotation
896 * (non-positional) score if it has one. Answers null if neither description
897 * nor score is found.
902 static String getTooltip(AlignmentAnnotation aa)
908 StringBuilder tooltip = new StringBuilder();
909 if (aa.description != null && !aa.description.equals("New description"))
911 // TODO: we could refactor and merge this code with the code in
912 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
914 String desc = aa.getDescription(true).trim();
915 if (!desc.toLowerCase(Locale.ROOT).startsWith(HTML_START_TAG))
917 tooltip.append(HTML_START_TAG);
918 desc = desc.replace("<", "<");
920 else if (desc.toLowerCase(Locale.ROOT).endsWith(HTML_END_TAG))
922 desc = desc.substring(0, desc.length() - HTML_END_TAG.length());
924 tooltip.append(desc);
928 // begin the tooltip's html fragment
929 tooltip.append(HTML_START_TAG);
933 if (tooltip.length() > HTML_START_TAG.length())
935 tooltip.append("<br/>");
937 // TODO: limit precision of score to avoid noise from imprecise
939 // (64.7 becomes 64.7+/some tiny value).
940 tooltip.append(" Score: ").append(String.valueOf(aa.score));
943 if (tooltip.length() > HTML_START_TAG.length())
945 return tooltip.append(HTML_END_TAG).toString();
949 * nothing in the tooltip (except "<html>")
955 * Shows the height adjuster image if the mouse moves into the top left
956 * region, or hides it if the mouse leaves the regio
960 protected void showOrHideAdjuster(MouseEvent evt)
962 boolean was = resizePanel;
963 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
964 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
966 if (resizePanel != was)
969 .getPredefinedCursor(resizePanel ? Cursor.S_RESIZE_CURSOR
970 : Cursor.DEFAULT_CURSOR));
976 public void mouseClicked(MouseEvent evt)
978 final AlignmentAnnotation[] aa = ap.av.getAlignment()
979 .getAlignmentAnnotation();
980 if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
982 if (selectedRow > -1 && selectedRow < aa.length)
984 if (aa[selectedRow].groupRef != null)
986 if (evt.getClickCount() >= 2)
988 // todo: make the ap scroll to the selection - not necessary, first
989 // click highlights/scrolls, second selects
990 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
992 SequenceGroup sg = ap.av.getSelectionGroup();
993 if (sg == null || sg == aa[selectedRow].groupRef
994 || !(Platform.isControlDown(evt) || evt.isShiftDown()))
996 if (Platform.isControlDown(evt) || evt.isShiftDown())
998 // clone a new selection group from the associated group
999 ap.av.setSelectionGroup(
1000 new SequenceGroup(aa[selectedRow].groupRef));
1004 // set selection to the associated group so it can be edited
1005 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
1010 // modify current selection with associated group
1011 int remainToAdd = aa[selectedRow].groupRef.getSize();
1012 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
1014 if (jalview.util.Platform.isControlDown(evt))
1016 sg.addOrRemove(sgs, --remainToAdd == 0);
1020 // notionally, we should also add intermediate sequences from
1021 // last added sequence ?
1022 sg.addSequence(sgs, --remainToAdd == 0);
1027 ap.paintAlignment(false, false);
1028 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
1029 ap.av.sendSelection();
1033 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
1034 aa[selectedRow].groupRef.getSequences(null));
1038 else if (aa[selectedRow].sequenceRef != null)
1040 if (evt.getClickCount() == 1)
1042 ap.getSeqPanel().ap.getIdPanel()
1043 .highlightSearchResults(Arrays.asList(new SequenceI[]
1044 { aa[selectedRow].sequenceRef }));
1046 else if (evt.getClickCount() >= 2)
1048 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
1049 SequenceGroup sg = ap.av.getSelectionGroup();
1052 // we make a copy rather than edit the current selection if no
1053 // modifiers pressed
1054 // see Enhancement JAL-1557
1055 if (!(Platform.isControlDown(evt) || evt.isShiftDown()))
1057 sg = new SequenceGroup(sg);
1059 sg.addSequence(aa[selectedRow].sequenceRef, false);
1063 if (Platform.isControlDown(evt))
1065 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
1069 // notionally, we should also add intermediate sequences from
1070 // last added sequence ?
1071 sg.addSequence(aa[selectedRow].sequenceRef, true);
1077 sg = new SequenceGroup();
1079 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
1080 sg.addSequence(aa[selectedRow].sequenceRef, false);
1082 ap.av.setSelectionGroup(sg);
1083 ap.paintAlignment(false, false);
1084 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
1085 ap.av.sendSelection();
1095 * do a single sequence copy to jalview and the system clipboard
1098 * sequence to be copied to clipboard
1100 protected void copy_annotseqtoclipboard(SequenceI sq)
1102 SequenceI[] seqs = new SequenceI[] { sq };
1103 String[] omitHidden = null;
1104 SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
1105 if (dseqs[0] == null)
1107 dseqs[0] = new Sequence(sq);
1108 dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars,
1109 sq.getSequenceAsString()));
1111 sq.setDatasetSequence(dseqs[0]);
1113 Alignment ds = new Alignment(dseqs);
1114 if (av.hasHiddenColumns())
1116 Iterator<int[]> it = av.getAlignment().getHiddenColumns()
1117 .getVisContigsIterator(0, sq.getLength(), false);
1118 omitHidden = new String[] { sq.getSequenceStringFromIterator(it) };
1121 int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
1122 if (av.hasHiddenColumns())
1124 alignmentStartEnd = av.getAlignment().getHiddenColumns()
1125 .getVisibleStartAndEndIndex(av.getAlignment().getWidth());
1128 String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
1129 seqs, omitHidden, alignmentStartEnd);
1131 Toolkit.getDefaultToolkit().getSystemClipboard()
1132 .setContents(new StringSelection(output), Desktop.instance);
1134 HiddenColumns hiddenColumns = null;
1136 if (av.hasHiddenColumns())
1138 hiddenColumns = new HiddenColumns(
1139 av.getAlignment().getHiddenColumns());
1142 Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
1144 // sequence ? need to
1146 // sequence as special.
1157 public void paintComponent(Graphics g)
1160 int width = getWidth();
1163 width = ap.calculateIdWidth().width;
1166 Graphics2D g2 = (Graphics2D) g;
1169 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1170 RenderingHints.VALUE_ANTIALIAS_ON);
1173 drawComponent(g2, true, width, true);
1177 * Draw the full set of annotation Labels for the alignment at the given
1181 * Graphics2D instance (needed for font scaling)
1183 * Width for scaling labels
1186 public void drawComponent(Graphics g, int width)
1188 drawComponent(g, false, width, true);
1192 * Draw the full set of annotation Labels for the alignment at the given
1196 * Graphics2D instance (needed for font scaling)
1198 * - true indicates that only current visible area needs to be
1201 * Width for scaling labels
1203 public void drawComponent(Graphics g, boolean clip, int givenWidth,
1206 int width = givenWidth;
1207 IdwidthAdjuster iwa = null;
1210 iwa = ap.idwidthAdjuster;
1211 if ((Cache.getDefault(ADJUST_ANNOTATION_LABELS_WIDTH_PREF, true)
1212 || Jalview.isHeadlessMode()))
1214 Graphics2D g2d = (Graphics2D) g;
1215 Graphics dummy = g2d.create();
1216 int newAnnotationIdWidth = drawLabels(dummy, clip, width, false,
1217 forGUI, null, false);
1219 Dimension d = ap.calculateDefaultAlignmentIdWidth();
1220 int alignmentIdWidth = d.width;
1221 if (iwa != null && !iwa.manuallyAdjusted())
1223 // If no manual adjustment to ID column with has been made then adjust
1224 // width match widest of alignment or annotation id widths
1225 boolean allowShrink = Cache.getDefault("ALLOW_SHRINK_ID_WIDTH",
1227 width = Math.max(alignmentIdWidth, newAnnotationIdWidth);
1228 if (clip && width < givenWidth && !allowShrink)
1233 else if (newAnnotationIdWidth != annotationIdWidth
1234 && newAnnotationIdWidth > givenWidth
1235 && newAnnotationIdWidth > alignmentIdWidth)
1237 // otherwise if the annotation id width has become larger than the
1238 // current id width, increase
1239 width = newAnnotationIdWidth;
1240 annotationIdWidth = newAnnotationIdWidth;
1242 // set the width if it's changed
1243 if (width != ap.av.getIdWidth())
1245 iwa.setWidth(width);
1251 int newAnnotationIdWidth = drawLabels(g, clip, width, false, forGUI,
1253 width = newAnnotationIdWidth < givenWidth ? givenWidth
1254 : Math.min(newAnnotationIdWidth, givenWidth);
1256 drawLabels(g, clip, width, true, forGUI, null, false);
1260 * Render the full set of annotation Labels for the alignment at the given
1261 * cursor. If actuallyDraw is false or g is null then no actual drawing will
1262 * occur, but the widest label width will be returned. If g is null then
1263 * fmetrics must be supplied.
1266 * Graphics2D instance (used for rendering and font scaling if no
1267 * fmetrics supplied)
1269 * - true indicates that only current visible area needs to be
1272 * Width for scaling labels
1273 * @param actuallyDraw
1274 * - when false, no graphics are rendered to g0
1276 * - when false, GUI relevant marks like indicators for dragging
1277 * annotation panel height are not rendered
1279 * FontMetrics if Graphics object g is null
1280 * @param includeHidden
1281 * - when true returned width includes labels in hidden row width
1283 * @return the width of the annotation labels.
1285 public int drawLabels(Graphics g0, boolean clip, int width,
1286 boolean actuallyDraw, boolean forGUI, FontMetrics fmetrics,
1287 boolean includeHidden)
1291 clip = Cache.getDefault("MOVE_SEQUENCE_ID_WITH_VISIBLE_ANNOTATIONS",
1295 // create a dummy Graphics object if not drawing and one is supplied
1300 Graphics2D g2d = (Graphics2D) g0;
1308 int actualWidth = 0;
1311 if (av.getFont().getSize() < 10)
1317 g.setFont(av.getFont());
1321 FontMetrics fm = fmetrics == null ? g.getFontMetrics(g.getFont())
1325 g.setColor(Color.white);
1326 g.fillRect(0, 0, getWidth(), getHeight());
1328 if (!Cache.getDefault(RESIZE_MARGINS_MARK_PREF, false)
1329 && !av.getWrapAlignment() && forGUI)
1331 g.setColor(Color.LIGHT_GRAY);
1332 g.drawLine(0, HEIGHT_ADJUSTER_HEIGHT / 4, HEIGHT_ADJUSTER_WIDTH / 4,
1333 HEIGHT_ADJUSTER_HEIGHT / 4);
1334 g.drawLine(0, 3 * HEIGHT_ADJUSTER_HEIGHT / 4,
1335 HEIGHT_ADJUSTER_WIDTH / 4, 3 * HEIGHT_ADJUSTER_HEIGHT / 4);
1342 g.translate(0, getScrollOffset());
1343 g.setColor(Color.black);
1345 SequenceI lastSeqRef = null;
1346 String lastLabel = null;
1347 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1348 int fontHeight = g != null ? g.getFont().getSize()
1349 : fm.getFont().getSize();
1352 int graphExtras = 0;
1354 Font baseFont = g != null ? g.getFont() : fm.getFont();
1355 FontMetrics baseMetrics = fm;
1356 int ofontH = fontHeight;
1359 int[] visr = (ap != null && ap.getAnnotationPanel() != null)
1360 ? ap.getAnnotationPanel().getVisibleVRange()
1362 if (clip && visr != null)
1365 visHeight = visr[1];
1367 boolean visible = true, before = false, after = false;
1370 hasHiddenRows = false;
1373 for (int i = 0; i < aa.length; i++)
1376 if (!aa[i].visible && !includeHidden)
1378 hasHiddenRows = true;
1382 // look ahead to next annotation
1383 for (nexAA = i + 1; nexAA < aa.length
1384 && (!aa[nexAA].visible && includeHidden); nexAA++)
1395 jalview.bin.Console.outPrintln("before vis: " + i);
1399 // don't draw what isn't visible
1402 if (olY > visHeight)
1409 jalview.bin.Console.outPrintln(
1410 "Scroll offset: " + sOffset + " after vis: " + i);
1414 // don't draw what isn't visible
1418 if (actuallyDraw && g != null)
1420 g.setColor(Color.black);
1422 offset = -aa[i].height / 2;
1426 offset += fm.getHeight() / 2;
1427 offset -= fm.getDescent();
1431 offset += fm.getDescent();
1433 String label = aa[i].label;
1434 boolean vertBar = false;
1435 if ((lastLabel != null && lastLabel.equals(label)))
1437 label = aa[i].description;
1441 if (nexAA < aa.length && label.equals(aa[nexAA].label)) // &&
1442 // aa[nexY].sequenceRef==aa[i].sequenceRef)
1445 // next label is the same as this label
1446 label = aa[i].description;
1453 if (aa[i].sequenceRef != null)
1455 if (aa[i].sequenceRef != lastSeqRef)
1457 label = aa[i].sequenceRef.getName() + " " + label;
1458 // TODO record relationship between sequence and this annotation and
1467 int labelWidth = fm.stringWidth(label) + 3;
1468 x = width - labelWidth;
1470 if (aa[i].graphGroup > -1)
1473 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1474 // computed efficiently for all visible labels
1475 for (int gg = 0; gg < aa.length; gg++)
1477 if (aa[gg].graphGroup == aa[i].graphGroup)
1482 if (groupSize * (fontHeight + 8) < aa[i].height)
1484 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1489 // scale font to fit
1490 float h = aa[i].height / (float) groupSize, s;
1497 fontHeight = -8 + (int) h;
1498 s = ((float) fontHeight) / (float) ofontH;
1500 .deriveFont(AffineTransform.getScaleInstance(s, s));
1501 Canvas c = new Canvas();
1502 fm = c.getFontMetrics(f);
1503 if (actuallyDraw && g != null)
1506 // fm = g.getFontMetrics();
1507 graphExtras = (aa[i].height
1508 - (groupSize * (fontHeight + 8))) / 2;
1514 for (int gg = 0; gg < aa.length; gg++)
1516 if (aa[gg].graphGroup == aa[i].graphGroup)
1518 labelWidth = fm.stringWidth(aa[gg].label) + 3;
1519 x = width - labelWidth;
1520 if (actuallyDraw && g != null)
1522 g.drawString(aa[gg].label, x, y - graphExtras);
1524 if (aa[gg]._linecolour != null)
1527 g.setColor(aa[gg]._linecolour);
1528 g.drawLine(x, y - graphExtras + 3,
1529 x + fm.stringWidth(aa[gg].label),
1530 y - graphExtras + 3);
1533 g.setColor(Color.black);
1535 graphExtras += fontHeight + 8;
1539 if (actuallyDraw && g != null)
1541 g.setFont(baseFont);
1544 fontHeight = ofontH;
1548 if (actuallyDraw && g != null)
1552 g.drawLine(width - 3, y + offset - fontHeight, width - 3,
1553 (int) (y - 1.5 * aa[i].height - offset - fontHeight));
1554 // g.drawLine(20, y + offset, x - 20, y + offset);
1557 g.drawString(label, x, y + offset);
1560 lastSeqRef = aa[i].sequenceRef;
1562 if (labelWidth > actualWidth)
1564 actualWidth = labelWidth;
1569 if (!resizePanel && dragEvent != null && aa != null && selectedRow > -1
1570 && selectedRow < aa.length)
1572 if (actuallyDraw && g != null)
1574 g.setColor(Color.lightGray);
1576 (aa[selectedRow].sequenceRef == null ? ""
1577 : aa[selectedRow].sequenceRef.getName())
1578 + aa[selectedRow].label,
1579 dragEvent.getX(), dragEvent.getY() - getScrollOffset());
1583 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1585 if (actuallyDraw && g != null)
1587 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1588 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1596 public int getScrollOffset()
1598 return scrollOffset;
1602 public void mouseEntered(MouseEvent e)
1606 public void drawComponentNotGUI(Graphics idGraphics, int idWidth)
1608 drawComponent(idGraphics, false, idWidth, false);