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.io.IOException;
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.Iterator;
43 import java.util.Locale;
44 import java.util.concurrent.Callable;
46 import javax.swing.JCheckBoxMenuItem;
47 import javax.swing.JMenuItem;
48 import javax.swing.JPanel;
49 import javax.swing.JPopupMenu;
50 import javax.swing.SwingUtilities;
51 import javax.swing.ToolTipManager;
53 import jalview.analysis.AlignSeq;
54 import jalview.analysis.AlignmentUtils;
55 import jalview.bin.Console;
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.DataSourceType;
66 import jalview.io.FileFormat;
67 import jalview.io.FormatAdapter;
68 import jalview.io.NewickFile;
69 import jalview.schemes.AnnotationColourGradient;
70 import jalview.util.Comparison;
71 import jalview.util.MessageManager;
72 import jalview.util.Platform;
73 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
76 * The panel that holds the labels for alignment annotations, providing
77 * tooltips, context menus, drag to reorder rows, and drag to adjust panel
80 public class AnnotationLabels extends JPanel
81 implements MouseListener, MouseMotionListener, ActionListener
83 private static final String HTML_END_TAG = "</html>";
85 private static final String HTML_START_TAG = "<html>";
88 * width in pixels within which height adjuster arrows are shown and active
90 private static final int HEIGHT_ADJUSTER_WIDTH = 50;
93 * height in pixels for allowing height adjuster to be active
95 private static int HEIGHT_ADJUSTER_HEIGHT = 10;
97 private static final Font font = new Font("Arial", Font.PLAIN, 11);
99 private static final String TOGGLE_LABELSCALE = MessageManager
100 .getString("label.scale_label_to_column");
102 private static final String ADDNEW = MessageManager
103 .getString("label.add_new_row");
105 private static final String EDITNAME = MessageManager
106 .getString("label.edit_label_description");
108 private static final String HIDE = MessageManager
109 .getString("label.hide_row");
111 private static final String DELETE = MessageManager
112 .getString("label.delete_row");
114 private static final String SHOWALL = MessageManager
115 .getString("label.show_all_hidden_rows");
117 private static final String OUTPUT_TEXT = MessageManager
118 .getString("label.export_annotation");
120 private static final String COPYCONS_SEQ = MessageManager
121 .getString("label.copy_consensus_sequence");
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;
142 * Creates a new AnnotationLabels object
146 public AnnotationLabels(AlignmentPanel ap)
151 ToolTipManager.sharedInstance().registerComponent(this);
153 addMouseListener(this);
154 addMouseMotionListener(this);
155 addMouseWheelListener(ap.getAnnotationPanel());
158 public AnnotationLabels(AlignViewport av)
169 public void setScrollOffset(int y)
176 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
180 * coordinate position to search for a row
182 void getSelectedRow(int y)
185 AlignmentAnnotation[] aa = ap.av.getAlignment()
186 .getAlignmentAnnotation();
190 for (int i = 0; i < aa.length; i++)
198 height += aa[i].height;
217 public void actionPerformed(ActionEvent evt)
219 AlignmentAnnotation[] aa = ap.av.getAlignment()
220 .getAlignmentAnnotation();
222 String action = evt.getActionCommand();
223 if (ADDNEW.equals(action))
226 * non-returning dialog
228 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
229 null, new Annotation[ap.av.getAlignment().getWidth()]);
230 editLabelDescription(newAnnotation, true);
232 else if (EDITNAME.equals(action))
235 * non-returning dialog
237 editLabelDescription(aa[selectedRow], false);
239 else if (HIDE.equals(action))
241 aa[selectedRow].visible = false;
243 else if (DELETE.equals(action))
245 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
246 ap.av.getCalcManager().removeWorkerForAnnotation(aa[selectedRow]);
248 else if (SHOWALL.equals(action))
250 for (int i = 0; i < aa.length; i++)
252 if (!aa[i].visible && aa[i].annotations != null)
254 aa[i].visible = true;
258 else if (OUTPUT_TEXT.equals(action))
260 new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]);
262 else if (COPYCONS_SEQ.equals(action))
264 SequenceI cons = null;
265 if (aa[selectedRow].groupRef != null)
267 cons = aa[selectedRow].groupRef.getConsensusSeq();
271 cons = av.getConsensusSeq();
275 copy_annotseqtoclipboard(cons);
278 else if (TOGGLE_LABELSCALE.equals(action))
280 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
287 * Shows a dialog where the annotation name and description may be edited. If
288 * parameter addNew is true, then on confirmation, a new AlignmentAnnotation
289 * is added, else an existing annotation is updated.
294 void editLabelDescription(AlignmentAnnotation annotation, boolean addNew)
296 String name = MessageManager.getString("label.annotation_name");
297 String description = MessageManager
298 .getString("label.annotation_description");
299 String title = MessageManager
300 .getString("label.edit_annotation_name_description");
301 EditNameDialog dialog = new EditNameDialog(annotation.label,
302 annotation.description, name, description);
304 dialog.showDialog(ap.alignFrame, title, () -> {
305 annotation.label = dialog.getName();
306 String text = dialog.getDescription();
307 if (text != null && text.length() == 0)
311 annotation.description = text;
314 ap.av.getAlignment().addAnnotation(annotation);
315 ap.av.getAlignment().setAnnotationIndex(annotation, 0);
323 public void mousePressed(MouseEvent evt)
325 getSelectedRow(evt.getY() - getScrollOffset());
327 if (evt.isPopupTrigger())
334 * Build and show the Pop-up menu at the right-click mouse position
338 void showPopupMenu(MouseEvent evt)
341 final AlignmentAnnotation[] aa = ap.av.getAlignment()
342 .getAlignmentAnnotation();
344 JPopupMenu pop = new JPopupMenu(
345 MessageManager.getString("label.annotations"));
346 JMenuItem item = new JMenuItem(ADDNEW);
347 item.addActionListener(this);
352 { // let the user make everything visible again
353 item = new JMenuItem(SHOWALL);
354 item.addActionListener(this);
357 pop.show(this, evt.getX(), evt.getY());
360 item = new JMenuItem(EDITNAME);
361 item.addActionListener(this);
363 item = new JMenuItem(HIDE);
364 item.addActionListener(this);
366 // JAL-1264 hide all sequence-specific annotations of this type
367 if (selectedRow < aa.length)
369 if (aa[selectedRow].sequenceRef != null)
371 final String label = aa[selectedRow].label;
372 JMenuItem hideType = new JMenuItem();
373 String text = MessageManager.getString("label.hide_all") + " "
375 hideType.setText(text);
376 hideType.addActionListener(new ActionListener()
379 public void actionPerformed(ActionEvent e)
381 AlignmentUtils.showOrHideSequenceAnnotations(
382 ap.av.getAlignment(), Collections.singleton(label),
390 item = new JMenuItem(DELETE);
391 item.addActionListener(this);
395 item = new JMenuItem(SHOWALL);
396 item.addActionListener(this);
399 item = new JMenuItem(OUTPUT_TEXT);
400 item.addActionListener(this);
402 // TODO: annotation object should be typed for autocalculated/derived
404 if (selectedRow < aa.length)
406 final String label = aa[selectedRow].label;
407 if (!aa[selectedRow].autoCalculated)
409 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
411 // display formatting settings for this row.
413 // av and sequencegroup need to implement same interface for
414 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
415 aa[selectedRow].scaleColLabel);
416 item.addActionListener(this);
420 else if (label.indexOf("Consensus") > -1)
422 addConsensusMenuOptions(ap, aa[selectedRow], pop);
424 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
425 consclipbrd.addActionListener(this);
426 pop.add(consclipbrd);
429 addColourOrFilterByOptions(ap,aa[selectedRow],pop);
431 if (aa[selectedRow].graph == AlignmentAnnotation.CONTACT_MAP)
433 addContactMatrixOptions(ap,aa[selectedRow],pop);
434 // Set/adjust threshold for grouping ?
435 // colour alignment by this [type]
436 // select/hide columns by this row
441 pop.show(this, evt.getX(), evt.getY());
444 static void addColourOrFilterByOptions(final AlignmentPanel ap,
445 final AlignmentAnnotation alignmentAnnotation, final JPopupMenu pop)
448 item = new JMenuItem(MessageManager.getString("label.colour_by_annotation"));
449 item.addActionListener(new ActionListener()
453 public void actionPerformed(ActionEvent e)
455 AnnotationColourChooser.displayFor(ap.av, ap,alignmentAnnotation,false);
459 if (alignmentAnnotation.sequenceRef!=null)
461 item = new JMenuItem(MessageManager.getString("label.colour_by_annotation")+" ("+MessageManager.getString("label.per_seq")+")");
462 item.addActionListener(new ActionListener()
465 public void actionPerformed(ActionEvent e)
467 AnnotationColourChooser.displayFor(ap.av, ap,alignmentAnnotation,true);
472 item = new JMenuItem(MessageManager.getString("action.select_by_annotation"));
473 item.addActionListener(new ActionListener()
477 public void actionPerformed(ActionEvent e)
479 AnnotationColumnChooser.displayFor(ap.av,ap,alignmentAnnotation);
484 static void addContactMatrixOptions(final AlignmentPanel ap,
485 final AlignmentAnnotation alignmentAnnotation, final JPopupMenu pop)
488 final ContactMatrixI cm = ap.av.getContactMatrix(alignmentAnnotation);
496 JCheckBoxMenuItem chitem = new JCheckBoxMenuItem("Show Groups on Matrix");
497 boolean showGroups = alignmentAnnotation.isShowGroupsForContactMatrix();
498 final AlignmentAnnotation sel_row=alignmentAnnotation;
499 chitem.setState(showGroups);
500 chitem.addActionListener(new ActionListener()
504 public void actionPerformed(ActionEvent e)
506 sel_row.setShowGroupsForContactMatrix(chitem.getState());
507 ap.getAnnotationPanel()
508 .paint(ap.getAnnotationPanel().getGraphics());
515 item = new JMenuItem("Show Tree for Matrix");
516 item.addActionListener(new ActionListener()
520 public void actionPerformed(ActionEvent e)
523 ap.alignFrame.showContactMapTree(alignmentAnnotation, cm);
531 item = new JMenuItem("Calculate Tree for Matrix");
532 item.addActionListener(new ActionListener()
534 // TODO - refactor to analysis background thread
536 public void actionPerformed(ActionEvent e)
538 new Thread(new Runnable()
543 AlignmentAnnotation alan = alignmentAnnotation;
544 cm.setGroupSet(GroupSet.makeGroups(cm, 5f, true));
545 ap.alignFrame.showContactMapTree(alan, cm);
556 * A helper method that adds menu options for calculation and visualisation of
557 * group and/or alignment consensus annotation to a popup menu. This is
558 * designed to be reusable for either unwrapped mode (popup menu is shown on
559 * component AnnotationLabels), or wrapped mode (popup menu is shown on
560 * IdPanel when the mouse is over an annotation label).
566 static void addConsensusMenuOptions(AlignmentPanel ap,
567 AlignmentAnnotation ann, JPopupMenu pop)
571 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
572 MessageManager.getString("label.ignore_gaps_consensus"),
573 (ann.groupRef != null) ? ann.groupRef.getIgnoreGapsConsensus()
574 : ap.av.isIgnoreGapsConsensus());
575 final AlignmentAnnotation aaa = ann;
576 cbmi.addActionListener(new ActionListener()
579 public void actionPerformed(ActionEvent e)
581 if (aaa.groupRef != null)
583 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
584 ap.getAnnotationPanel()
585 .paint(ap.getAnnotationPanel().getGraphics());
589 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
591 ap.alignmentChanged();
596 if (aaa.groupRef != null)
599 * group consensus options
601 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
602 MessageManager.getString("label.show_group_histogram"),
603 ann.groupRef.isShowConsensusHistogram());
604 chist.addActionListener(new ActionListener()
607 public void actionPerformed(ActionEvent e)
609 aaa.groupRef.setShowConsensusHistogram(chist.getState());
614 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
615 MessageManager.getString("label.show_group_logo"),
616 ann.groupRef.isShowSequenceLogo());
617 cprofl.addActionListener(new ActionListener()
620 public void actionPerformed(ActionEvent e)
622 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
627 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
628 MessageManager.getString("label.normalise_group_logo"),
629 ann.groupRef.isNormaliseSequenceLogo());
630 cproflnorm.addActionListener(new ActionListener()
633 public void actionPerformed(ActionEvent e)
635 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
636 // automatically enable logo display if we're clicked
637 aaa.groupRef.setshowSequenceLogo(true);
646 * alignment consensus options
648 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
649 MessageManager.getString("label.show_histogram"),
650 ap.av.isShowConsensusHistogram());
651 chist.addActionListener(new ActionListener()
654 public void actionPerformed(ActionEvent e)
656 ap.av.setShowConsensusHistogram(chist.getState());
657 ap.alignFrame.setMenusForViewport();
662 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
663 MessageManager.getString("label.show_logo"),
664 ap.av.isShowSequenceLogo());
665 cprof.addActionListener(new ActionListener()
668 public void actionPerformed(ActionEvent e)
670 ap.av.setShowSequenceLogo(cprof.getState());
671 ap.alignFrame.setMenusForViewport();
676 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
677 MessageManager.getString("label.normalise_logo"),
678 ap.av.isNormaliseSequenceLogo());
679 cprofnorm.addActionListener(new ActionListener()
682 public void actionPerformed(ActionEvent e)
684 ap.av.setShowSequenceLogo(true);
685 ap.av.setNormaliseSequenceLogo(cprofnorm.getState());
686 ap.alignFrame.setMenusForViewport();
695 * Reorders annotation rows after a drag of a label
700 public void mouseReleased(MouseEvent evt)
702 if (evt.isPopupTrigger())
708 int start = selectedRow;
709 getSelectedRow(evt.getY() - getScrollOffset());
710 int end = selectedRow;
713 * if dragging to resize instead, start == end
717 // Swap these annotations
718 AlignmentAnnotation startAA = ap.av.getAlignment()
719 .getAlignmentAnnotation()[start];
722 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
724 AlignmentAnnotation endAA = ap.av.getAlignment()
725 .getAlignmentAnnotation()[end];
727 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
728 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
734 ap.getAnnotationPanel().repaint();
738 * Removes the height adjuster image on leaving the panel, unless currently
742 public void mouseExited(MouseEvent evt)
744 if (resizePanel && dragEvent == null)
752 * A mouse drag may be either an adjustment of the panel height (if flag
753 * resizePanel is set on), or a reordering of the annotation rows. The former
754 * is dealt with by this method, the latter in mouseReleased.
759 public void mouseDragged(MouseEvent evt)
765 Dimension d = ap.annotationScroller.getPreferredSize();
766 int dif = evt.getY() - oldY;
768 dif /= ap.av.getCharHeight();
769 dif *= ap.av.getCharHeight();
771 if ((d.height - dif) > 20)
773 ap.annotationScroller
774 .setPreferredSize(new Dimension(d.width, d.height - dif));
775 d = ap.annotationSpaceFillerHolder.getPreferredSize();
776 ap.annotationSpaceFillerHolder
777 .setPreferredSize(new Dimension(d.width, d.height - dif));
778 ap.paintAlignment(true, false);
790 * Updates the tooltip as the mouse moves over the labels
795 public void mouseMoved(MouseEvent evt)
797 showOrHideAdjuster(evt);
799 getSelectedRow(evt.getY() - getScrollOffset());
801 if (selectedRow > -1 && ap.av.getAlignment()
802 .getAlignmentAnnotation().length > selectedRow)
804 AlignmentAnnotation[] anns = ap.av.getAlignment()
805 .getAlignmentAnnotation();
806 AlignmentAnnotation aa = anns[selectedRow];
808 String desc = getTooltip(aa);
809 this.setToolTipText(desc);
810 String msg = getStatusMessage(aa, anns);
811 ap.alignFrame.setStatus(msg);
816 * Constructs suitable text to show in the status bar when over an annotation
817 * label, containing the associated sequence name (if any), and the annotation
818 * labels (or all labels for a graph group annotation)
824 static String getStatusMessage(AlignmentAnnotation aa,
825 AlignmentAnnotation[] anns)
832 StringBuilder msg = new StringBuilder(32);
833 if (aa.sequenceRef != null)
835 msg.append(aa.sequenceRef.getName()).append(" : ");
838 if (aa.graphGroup == -1)
840 msg.append(aa.label);
842 else if (anns != null)
844 boolean first = true;
845 for (int i = anns.length - 1; i >= 0; i--)
847 if (anns[i].graphGroup == aa.graphGroup)
853 msg.append(anns[i].label);
859 return msg.toString();
863 * Answers a tooltip, formatted as html, containing the annotation description
864 * (prefixed by associated sequence id if applicable), and the annotation
865 * (non-positional) score if it has one. Answers null if neither description
866 * nor score is found.
871 static String getTooltip(AlignmentAnnotation aa)
877 StringBuilder tooltip = new StringBuilder();
878 if (aa.description != null && !aa.description.equals("New description"))
880 // TODO: we could refactor and merge this code with the code in
881 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
883 String desc = aa.getDescription(true).trim();
884 if (!desc.toLowerCase(Locale.ROOT).startsWith(HTML_START_TAG))
886 tooltip.append(HTML_START_TAG);
887 desc = desc.replace("<", "<");
889 else if (desc.toLowerCase(Locale.ROOT).endsWith(HTML_END_TAG))
891 desc = desc.substring(0, desc.length() - HTML_END_TAG.length());
893 tooltip.append(desc);
897 // begin the tooltip's html fragment
898 tooltip.append(HTML_START_TAG);
902 if (tooltip.length() > HTML_START_TAG.length())
904 tooltip.append("<br/>");
906 // TODO: limit precision of score to avoid noise from imprecise
908 // (64.7 becomes 64.7+/some tiny value).
909 tooltip.append(" Score: ").append(String.valueOf(aa.score));
912 if (tooltip.length() > HTML_START_TAG.length())
914 return tooltip.append(HTML_END_TAG).toString();
918 * nothing in the tooltip (except "<html>")
924 * Shows the height adjuster image if the mouse moves into the top left
925 * region, or hides it if the mouse leaves the regio
929 protected void showOrHideAdjuster(MouseEvent evt)
931 boolean was = resizePanel;
932 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
933 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
935 if (resizePanel != was)
938 .getPredefinedCursor(resizePanel ? Cursor.S_RESIZE_CURSOR
939 : Cursor.DEFAULT_CURSOR));
945 public void mouseClicked(MouseEvent evt)
947 final AlignmentAnnotation[] aa = ap.av.getAlignment()
948 .getAlignmentAnnotation();
949 if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
951 if (selectedRow > -1 && selectedRow < aa.length)
953 if (aa[selectedRow].groupRef != null)
955 if (evt.getClickCount() >= 2)
957 // todo: make the ap scroll to the selection - not necessary, first
958 // click highlights/scrolls, second selects
959 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
961 SequenceGroup sg = ap.av.getSelectionGroup();
962 if (sg == null || sg == aa[selectedRow].groupRef
963 || !(Platform.isControlDown(evt) || evt.isShiftDown()))
965 if (Platform.isControlDown(evt) || evt.isShiftDown())
967 // clone a new selection group from the associated group
968 ap.av.setSelectionGroup(
969 new SequenceGroup(aa[selectedRow].groupRef));
973 // set selection to the associated group so it can be edited
974 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
979 // modify current selection with associated group
980 int remainToAdd = aa[selectedRow].groupRef.getSize();
981 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
983 if (jalview.util.Platform.isControlDown(evt))
985 sg.addOrRemove(sgs, --remainToAdd == 0);
989 // notionally, we should also add intermediate sequences from
990 // last added sequence ?
991 sg.addSequence(sgs, --remainToAdd == 0);
996 ap.paintAlignment(false, false);
997 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
998 ap.av.sendSelection();
1002 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
1003 aa[selectedRow].groupRef.getSequences(null));
1007 else if (aa[selectedRow].sequenceRef != null)
1009 if (evt.getClickCount() == 1)
1011 ap.getSeqPanel().ap.getIdPanel()
1012 .highlightSearchResults(Arrays.asList(new SequenceI[]
1013 { aa[selectedRow].sequenceRef }));
1015 else if (evt.getClickCount() >= 2)
1017 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
1018 SequenceGroup sg = ap.av.getSelectionGroup();
1021 // we make a copy rather than edit the current selection if no
1022 // modifiers pressed
1023 // see Enhancement JAL-1557
1024 if (!(Platform.isControlDown(evt) || evt.isShiftDown()))
1026 sg = new SequenceGroup(sg);
1028 sg.addSequence(aa[selectedRow].sequenceRef, false);
1032 if (Platform.isControlDown(evt))
1034 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
1038 // notionally, we should also add intermediate sequences from
1039 // last added sequence ?
1040 sg.addSequence(aa[selectedRow].sequenceRef, true);
1046 sg = new SequenceGroup();
1048 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
1049 sg.addSequence(aa[selectedRow].sequenceRef, false);
1051 ap.av.setSelectionGroup(sg);
1052 ap.paintAlignment(false, false);
1053 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
1054 ap.av.sendSelection();
1064 * do a single sequence copy to jalview and the system clipboard
1067 * sequence to be copied to clipboard
1069 protected void copy_annotseqtoclipboard(SequenceI sq)
1071 SequenceI[] seqs = new SequenceI[] { sq };
1072 String[] omitHidden = null;
1073 SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
1074 if (dseqs[0] == null)
1076 dseqs[0] = new Sequence(sq);
1077 dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars,
1078 sq.getSequenceAsString()));
1080 sq.setDatasetSequence(dseqs[0]);
1082 Alignment ds = new Alignment(dseqs);
1083 if (av.hasHiddenColumns())
1085 Iterator<int[]> it = av.getAlignment().getHiddenColumns()
1086 .getVisContigsIterator(0, sq.getLength(), false);
1087 omitHidden = new String[] { sq.getSequenceStringFromIterator(it) };
1090 int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
1091 if (av.hasHiddenColumns())
1093 alignmentStartEnd = av.getAlignment().getHiddenColumns()
1094 .getVisibleStartAndEndIndex(av.getAlignment().getWidth());
1097 String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
1098 seqs, omitHidden, alignmentStartEnd);
1100 Toolkit.getDefaultToolkit().getSystemClipboard()
1101 .setContents(new StringSelection(output), Desktop.instance);
1103 HiddenColumns hiddenColumns = null;
1105 if (av.hasHiddenColumns())
1107 hiddenColumns = new HiddenColumns(
1108 av.getAlignment().getHiddenColumns());
1111 Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
1113 // sequence ? need to
1115 // sequence as special.
1126 public void paintComponent(Graphics g)
1129 int width = getWidth();
1132 width = ap.calculateIdWidth().width;
1135 Graphics2D g2 = (Graphics2D) g;
1138 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1139 RenderingHints.VALUE_ANTIALIAS_ON);
1142 drawComponent(g2, true, width);
1147 * Draw the full set of annotation Labels for the alignment at the given
1151 * Graphics2D instance (needed for font scaling)
1153 * Width for scaling labels
1156 public void drawComponent(Graphics g, int width)
1158 drawComponent(g, false, width);
1162 * Draw the full set of annotation Labels for the alignment at the given
1166 * Graphics2D instance (needed for font scaling)
1168 * - true indicates that only current visible area needs to be
1171 * Width for scaling labels
1173 public void drawComponent(Graphics g, boolean clip, int width)
1175 if (av.getFont().getSize() < 10)
1181 g.setFont(av.getFont());
1184 FontMetrics fm = g.getFontMetrics(g.getFont());
1185 g.setColor(Color.white);
1186 g.fillRect(0, 0, getWidth(), getHeight());
1188 g.translate(0, getScrollOffset());
1189 g.setColor(Color.black);
1190 SequenceI lastSeqRef = null;
1191 String lastLabel = null;
1192 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1193 int fontHeight = g.getFont().getSize();
1196 int graphExtras = 0;
1198 Font baseFont = g.getFont();
1199 FontMetrics baseMetrics = fm;
1200 int ofontH = fontHeight;
1203 int[] visr = (ap != null && ap.getAnnotationPanel() != null)
1204 ? ap.getAnnotationPanel().getVisibleVRange()
1206 if (clip && visr != null)
1209 visHeight = visr[1];
1211 boolean visible = true, before = false, after = false;
1214 hasHiddenRows = false;
1217 for (int i = 0; i < aa.length; i++)
1222 hasHiddenRows = true;
1226 // look ahead to next annotation
1227 for (nexAA = i + 1; nexAA < aa.length
1228 && !aa[nexAA].visible; nexAA++)
1239 System.out.println("before vis: " + i);
1243 // don't draw what isn't visible
1246 if (olY > visHeight)
1254 "Scroll offset: " + sOffset + " after vis: " + i);
1258 // don't draw what isn't visible
1262 g.setColor(Color.black);
1264 offset = -aa[i].height / 2;
1268 offset += fm.getHeight() / 2;
1269 offset -= fm.getDescent();
1273 offset += fm.getDescent();
1275 String label = aa[i].label;
1276 boolean vertBar = false;
1277 if ((lastLabel != null && lastLabel.equals(label)))
1279 label = aa[i].description;
1283 if (nexAA < aa.length && label.equals(aa[nexAA].label)) // &&
1284 // aa[nexY].sequenceRef==aa[i].sequenceRef)
1287 // next label is the same as this label
1288 label = aa[i].description;
1295 if (aa[i].sequenceRef != null)
1297 if (aa[i].sequenceRef != lastSeqRef)
1299 label = aa[i].sequenceRef.getName() + " " + label;
1300 // TODO record relationship between sequence and this annotation and
1308 x = width - fm.stringWidth(label) - 3;
1310 if (aa[i].graphGroup > -1)
1313 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1314 // computed efficiently for all visible labels
1315 for (int gg = 0; gg < aa.length; gg++)
1317 if (aa[gg].graphGroup == aa[i].graphGroup)
1322 if (groupSize * (fontHeight + 8) < aa[i].height)
1324 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1329 // scale font to fit
1330 float h = aa[i].height / (float) groupSize, s;
1337 fontHeight = -8 + (int) h;
1338 s = ((float) fontHeight) / (float) ofontH;
1340 .deriveFont(AffineTransform.getScaleInstance(s, s));
1342 fm = g.getFontMetrics();
1343 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1349 for (int gg = 0; gg < aa.length; gg++)
1351 if (aa[gg].graphGroup == aa[i].graphGroup)
1353 x = width - fm.stringWidth(aa[gg].label) - 3;
1354 g.drawString(aa[gg].label, x, y - graphExtras);
1356 if (aa[gg]._linecolour != null)
1359 g.setColor(aa[gg]._linecolour);
1360 g.drawLine(x, y - graphExtras + 3,
1361 x + fm.stringWidth(aa[gg].label),
1362 y - graphExtras + 3);
1365 g.setColor(Color.black);
1366 graphExtras += fontHeight + 8;
1370 g.setFont(baseFont);
1372 fontHeight = ofontH;
1378 g.drawLine(width - 3, y + offset - fontHeight, width - 3,
1379 (int) (y - 1.5 * aa[i].height - offset - fontHeight));
1380 // g.drawLine(20, y + offset, x - 20, y + offset);
1383 g.drawString(label, x, y + offset);
1385 lastSeqRef = aa[i].sequenceRef;
1389 if (!resizePanel && dragEvent != null && aa != null)
1391 g.setColor(Color.lightGray);
1393 (aa[selectedRow].sequenceRef == null ? ""
1394 : aa[selectedRow].sequenceRef.getName())
1395 + aa[selectedRow].label,
1396 dragEvent.getX(), dragEvent.getY() - getScrollOffset());
1399 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1401 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1402 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1407 public int getScrollOffset()
1409 return scrollOffset;
1413 public void mouseEntered(MouseEvent e)