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.util.Comparison;
70 import jalview.util.MessageManager;
71 import jalview.util.Platform;
72 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
75 * The panel that holds the labels for alignment annotations, providing
76 * tooltips, context menus, drag to reorder rows, and drag to adjust panel
79 public class AnnotationLabels extends JPanel
80 implements MouseListener, MouseMotionListener, ActionListener
82 private static final String HTML_END_TAG = "</html>";
84 private static final String HTML_START_TAG = "<html>";
87 * width in pixels within which height adjuster arrows are shown and active
89 private static final int HEIGHT_ADJUSTER_WIDTH = 50;
92 * height in pixels for allowing height adjuster to be active
94 private static int HEIGHT_ADJUSTER_HEIGHT = 10;
96 private static final Font font = new Font("Arial", Font.PLAIN, 11);
98 private static final String TOGGLE_LABELSCALE = MessageManager
99 .getString("label.scale_label_to_column");
101 private static final String ADDNEW = MessageManager
102 .getString("label.add_new_row");
104 private static final String EDITNAME = MessageManager
105 .getString("label.edit_label_description");
107 private static final String HIDE = MessageManager
108 .getString("label.hide_row");
110 private static final String DELETE = MessageManager
111 .getString("label.delete_row");
113 private static final String SHOWALL = MessageManager
114 .getString("label.show_all_hidden_rows");
116 private static final String OUTPUT_TEXT = MessageManager
117 .getString("label.export_annotation");
119 private static final String COPYCONS_SEQ = MessageManager
120 .getString("label.copy_consensus_sequence");
122 private final boolean debugRedraw = false;
124 private AlignmentPanel ap;
128 private MouseEvent dragEvent;
132 private int selectedRow;
134 private int scrollOffset = 0;
136 private boolean hasHiddenRows;
138 private boolean resizePanel = false;
141 * Creates a new AnnotationLabels object
145 public AnnotationLabels(AlignmentPanel ap)
150 ToolTipManager.sharedInstance().registerComponent(this);
152 addMouseListener(this);
153 addMouseMotionListener(this);
154 addMouseWheelListener(ap.getAnnotationPanel());
157 public AnnotationLabels(AlignViewport av)
168 public void setScrollOffset(int y)
175 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
179 * coordinate position to search for a row
181 void getSelectedRow(int y)
184 AlignmentAnnotation[] aa = ap.av.getAlignment()
185 .getAlignmentAnnotation();
189 for (int i = 0; i < aa.length; i++)
197 height += aa[i].height;
216 public void actionPerformed(ActionEvent evt)
218 AlignmentAnnotation[] aa = ap.av.getAlignment()
219 .getAlignmentAnnotation();
221 String action = evt.getActionCommand();
222 if (ADDNEW.equals(action))
225 * non-returning dialog
227 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
228 null, new Annotation[ap.av.getAlignment().getWidth()]);
229 editLabelDescription(newAnnotation, true);
231 else if (EDITNAME.equals(action))
234 * non-returning dialog
236 editLabelDescription(aa[selectedRow], false);
238 else if (HIDE.equals(action))
240 aa[selectedRow].visible = false;
242 else if (DELETE.equals(action))
244 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
245 ap.av.getCalcManager().removeWorkerForAnnotation(aa[selectedRow]);
247 else if (SHOWALL.equals(action))
249 for (int i = 0; i < aa.length; i++)
251 if (!aa[i].visible && aa[i].annotations != null)
253 aa[i].visible = true;
257 else if (OUTPUT_TEXT.equals(action))
259 new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]);
261 else if (COPYCONS_SEQ.equals(action))
263 SequenceI cons = null;
264 if (aa[selectedRow].groupRef != null)
266 cons = aa[selectedRow].groupRef.getConsensusSeq();
270 cons = av.getConsensusSeq();
274 copy_annotseqtoclipboard(cons);
277 else if (TOGGLE_LABELSCALE.equals(action))
279 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
286 * Shows a dialog where the annotation name and description may be edited. If
287 * parameter addNew is true, then on confirmation, a new AlignmentAnnotation
288 * is added, else an existing annotation is updated.
293 void editLabelDescription(AlignmentAnnotation annotation, boolean addNew)
295 String name = MessageManager.getString("label.annotation_name");
296 String description = MessageManager
297 .getString("label.annotation_description");
298 String title = MessageManager
299 .getString("label.edit_annotation_name_description");
300 EditNameDialog dialog = new EditNameDialog(annotation.label,
301 annotation.description, name, description);
303 dialog.showDialog(ap.alignFrame, title, () -> {
304 annotation.label = dialog.getName();
305 String text = dialog.getDescription();
306 if (text != null && text.length() == 0)
310 annotation.description = text;
313 ap.av.getAlignment().addAnnotation(annotation);
314 ap.av.getAlignment().setAnnotationIndex(annotation, 0);
322 public void mousePressed(MouseEvent evt)
324 getSelectedRow(evt.getY() - getScrollOffset());
326 if (evt.isPopupTrigger())
333 * Build and show the Pop-up menu at the right-click mouse position
337 void showPopupMenu(MouseEvent evt)
340 final AlignmentAnnotation[] aa = ap.av.getAlignment()
341 .getAlignmentAnnotation();
343 JPopupMenu pop = new JPopupMenu(
344 MessageManager.getString("label.annotations"));
345 JMenuItem item = new JMenuItem(ADDNEW);
346 item.addActionListener(this);
351 { // let the user make everything visible again
352 item = new JMenuItem(SHOWALL);
353 item.addActionListener(this);
356 pop.show(this, evt.getX(), evt.getY());
359 item = new JMenuItem(EDITNAME);
360 item.addActionListener(this);
362 item = new JMenuItem(HIDE);
363 item.addActionListener(this);
365 // JAL-1264 hide all sequence-specific annotations of this type
366 if (selectedRow < aa.length)
368 if (aa[selectedRow].sequenceRef != null)
370 final String label = aa[selectedRow].label;
371 JMenuItem hideType = new JMenuItem();
372 String text = MessageManager.getString("label.hide_all") + " "
374 hideType.setText(text);
375 hideType.addActionListener(new ActionListener()
378 public void actionPerformed(ActionEvent e)
380 AlignmentUtils.showOrHideSequenceAnnotations(
381 ap.av.getAlignment(), Collections.singleton(label),
389 item = new JMenuItem(DELETE);
390 item.addActionListener(this);
394 item = new JMenuItem(SHOWALL);
395 item.addActionListener(this);
398 item = new JMenuItem(OUTPUT_TEXT);
399 item.addActionListener(this);
401 // TODO: annotation object should be typed for autocalculated/derived
403 if (selectedRow < aa.length)
405 final String label = aa[selectedRow].label;
406 if (!aa[selectedRow].autoCalculated)
408 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
410 // display formatting settings for this row.
412 // av and sequencegroup need to implement same interface for
413 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
414 aa[selectedRow].scaleColLabel);
415 item.addActionListener(this);
419 else if (label.indexOf("Consensus") > -1)
421 addConsensusMenuOptions(ap, aa[selectedRow], pop);
423 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
424 consclipbrd.addActionListener(this);
425 pop.add(consclipbrd);
427 if (aa[selectedRow].graph == AlignmentAnnotation.CONTACT_MAP)
430 final ContactMatrixI cm = av.getContactMatrix(aa[selectedRow]);
437 JCheckBoxMenuItem chitem = new JCheckBoxMenuItem("Show Groups on Matrix");
438 boolean showGroups = aa[selectedRow].isShowGroupsForContactMatrix();
439 final AlignmentAnnotation sel_row=aa[selectedRow];
440 chitem.setState(showGroups);
441 chitem.addActionListener(new ActionListener()
445 public void actionPerformed(ActionEvent e)
447 sel_row.setShowGroupsForContactMatrix(chitem.getState());
448 ap.getAnnotationPanel()
449 .paint(ap.getAnnotationPanel().getGraphics());
456 item = new JMenuItem("Show Tree for Matrix");
457 item.addActionListener(new ActionListener()
461 public void actionPerformed(ActionEvent e)
464 ap.alignFrame.showContactMapTree(aa[selectedRow], cm);
472 item = new JMenuItem("Calculate Tree for Matrix");
473 item.addActionListener(new ActionListener()
475 // TODO - refactor to analysis background thread
477 public void actionPerformed(ActionEvent e)
479 new Thread(new Runnable()
484 AlignmentAnnotation alan = aa[selectedRow];
485 cm.setGroupSet(GroupSet.makeGroups(cm, 5f, true));
486 ap.alignFrame.showContactMapTree(alan, cm);
494 // Show/Hide group shading on matrix view
495 // Set/adjust threshold for grouping ?
496 // colour alignment by this [type]
497 // select/hide columns by this row
502 pop.show(this, evt.getX(), evt.getY());
506 * A helper method that adds menu options for calculation and visualisation of
507 * group and/or alignment consensus annotation to a popup menu. This is
508 * designed to be reusable for either unwrapped mode (popup menu is shown on
509 * component AnnotationLabels), or wrapped mode (popup menu is shown on
510 * IdPanel when the mouse is over an annotation label).
516 static void addConsensusMenuOptions(AlignmentPanel ap,
517 AlignmentAnnotation ann, JPopupMenu pop)
521 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
522 MessageManager.getString("label.ignore_gaps_consensus"),
523 (ann.groupRef != null) ? ann.groupRef.getIgnoreGapsConsensus()
524 : ap.av.isIgnoreGapsConsensus());
525 final AlignmentAnnotation aaa = ann;
526 cbmi.addActionListener(new ActionListener()
529 public void actionPerformed(ActionEvent e)
531 if (aaa.groupRef != null)
533 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
534 ap.getAnnotationPanel()
535 .paint(ap.getAnnotationPanel().getGraphics());
539 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
541 ap.alignmentChanged();
546 if (aaa.groupRef != null)
549 * group consensus options
551 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
552 MessageManager.getString("label.show_group_histogram"),
553 ann.groupRef.isShowConsensusHistogram());
554 chist.addActionListener(new ActionListener()
557 public void actionPerformed(ActionEvent e)
559 aaa.groupRef.setShowConsensusHistogram(chist.getState());
564 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
565 MessageManager.getString("label.show_group_logo"),
566 ann.groupRef.isShowSequenceLogo());
567 cprofl.addActionListener(new ActionListener()
570 public void actionPerformed(ActionEvent e)
572 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
577 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
578 MessageManager.getString("label.normalise_group_logo"),
579 ann.groupRef.isNormaliseSequenceLogo());
580 cproflnorm.addActionListener(new ActionListener()
583 public void actionPerformed(ActionEvent e)
585 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
586 // automatically enable logo display if we're clicked
587 aaa.groupRef.setshowSequenceLogo(true);
596 * alignment consensus options
598 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
599 MessageManager.getString("label.show_histogram"),
600 ap.av.isShowConsensusHistogram());
601 chist.addActionListener(new ActionListener()
604 public void actionPerformed(ActionEvent e)
606 ap.av.setShowConsensusHistogram(chist.getState());
607 ap.alignFrame.setMenusForViewport();
612 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
613 MessageManager.getString("label.show_logo"),
614 ap.av.isShowSequenceLogo());
615 cprof.addActionListener(new ActionListener()
618 public void actionPerformed(ActionEvent e)
620 ap.av.setShowSequenceLogo(cprof.getState());
621 ap.alignFrame.setMenusForViewport();
626 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
627 MessageManager.getString("label.normalise_logo"),
628 ap.av.isNormaliseSequenceLogo());
629 cprofnorm.addActionListener(new ActionListener()
632 public void actionPerformed(ActionEvent e)
634 ap.av.setShowSequenceLogo(true);
635 ap.av.setNormaliseSequenceLogo(cprofnorm.getState());
636 ap.alignFrame.setMenusForViewport();
645 * Reorders annotation rows after a drag of a label
650 public void mouseReleased(MouseEvent evt)
652 if (evt.isPopupTrigger())
658 int start = selectedRow;
659 getSelectedRow(evt.getY() - getScrollOffset());
660 int end = selectedRow;
663 * if dragging to resize instead, start == end
667 // Swap these annotations
668 AlignmentAnnotation startAA = ap.av.getAlignment()
669 .getAlignmentAnnotation()[start];
672 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
674 AlignmentAnnotation endAA = ap.av.getAlignment()
675 .getAlignmentAnnotation()[end];
677 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
678 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
684 ap.getAnnotationPanel().repaint();
688 * Removes the height adjuster image on leaving the panel, unless currently
692 public void mouseExited(MouseEvent evt)
694 if (resizePanel && dragEvent == null)
702 * A mouse drag may be either an adjustment of the panel height (if flag
703 * resizePanel is set on), or a reordering of the annotation rows. The former
704 * is dealt with by this method, the latter in mouseReleased.
709 public void mouseDragged(MouseEvent evt)
715 Dimension d = ap.annotationScroller.getPreferredSize();
716 int dif = evt.getY() - oldY;
718 dif /= ap.av.getCharHeight();
719 dif *= ap.av.getCharHeight();
721 if ((d.height - dif) > 20)
723 ap.annotationScroller
724 .setPreferredSize(new Dimension(d.width, d.height - dif));
725 d = ap.annotationSpaceFillerHolder.getPreferredSize();
726 ap.annotationSpaceFillerHolder
727 .setPreferredSize(new Dimension(d.width, d.height - dif));
728 ap.paintAlignment(true, false);
740 * Updates the tooltip as the mouse moves over the labels
745 public void mouseMoved(MouseEvent evt)
747 showOrHideAdjuster(evt);
749 getSelectedRow(evt.getY() - getScrollOffset());
751 if (selectedRow > -1 && ap.av.getAlignment()
752 .getAlignmentAnnotation().length > selectedRow)
754 AlignmentAnnotation[] anns = ap.av.getAlignment()
755 .getAlignmentAnnotation();
756 AlignmentAnnotation aa = anns[selectedRow];
758 String desc = getTooltip(aa);
759 this.setToolTipText(desc);
760 String msg = getStatusMessage(aa, anns);
761 ap.alignFrame.setStatus(msg);
766 * Constructs suitable text to show in the status bar when over an annotation
767 * label, containing the associated sequence name (if any), and the annotation
768 * labels (or all labels for a graph group annotation)
774 static String getStatusMessage(AlignmentAnnotation aa,
775 AlignmentAnnotation[] anns)
782 StringBuilder msg = new StringBuilder(32);
783 if (aa.sequenceRef != null)
785 msg.append(aa.sequenceRef.getName()).append(" : ");
788 if (aa.graphGroup == -1)
790 msg.append(aa.label);
792 else if (anns != null)
794 boolean first = true;
795 for (int i = anns.length - 1; i >= 0; i--)
797 if (anns[i].graphGroup == aa.graphGroup)
803 msg.append(anns[i].label);
809 return msg.toString();
813 * Answers a tooltip, formatted as html, containing the annotation description
814 * (prefixed by associated sequence id if applicable), and the annotation
815 * (non-positional) score if it has one. Answers null if neither description
816 * nor score is found.
821 static String getTooltip(AlignmentAnnotation aa)
827 StringBuilder tooltip = new StringBuilder();
828 if (aa.description != null && !aa.description.equals("New description"))
830 // TODO: we could refactor and merge this code with the code in
831 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
833 String desc = aa.getDescription(true).trim();
834 if (!desc.toLowerCase(Locale.ROOT).startsWith(HTML_START_TAG))
836 tooltip.append(HTML_START_TAG);
837 desc = desc.replace("<", "<");
839 else if (desc.toLowerCase(Locale.ROOT).endsWith(HTML_END_TAG))
841 desc = desc.substring(0, desc.length() - HTML_END_TAG.length());
843 tooltip.append(desc);
847 // begin the tooltip's html fragment
848 tooltip.append(HTML_START_TAG);
852 if (tooltip.length() > HTML_START_TAG.length())
854 tooltip.append("<br/>");
856 // TODO: limit precision of score to avoid noise from imprecise
858 // (64.7 becomes 64.7+/some tiny value).
859 tooltip.append(" Score: ").append(String.valueOf(aa.score));
862 if (tooltip.length() > HTML_START_TAG.length())
864 return tooltip.append(HTML_END_TAG).toString();
868 * nothing in the tooltip (except "<html>")
874 * Shows the height adjuster image if the mouse moves into the top left
875 * region, or hides it if the mouse leaves the regio
879 protected void showOrHideAdjuster(MouseEvent evt)
881 boolean was = resizePanel;
882 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
883 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
885 if (resizePanel != was)
888 .getPredefinedCursor(resizePanel ? Cursor.S_RESIZE_CURSOR
889 : Cursor.DEFAULT_CURSOR));
895 public void mouseClicked(MouseEvent evt)
897 final AlignmentAnnotation[] aa = ap.av.getAlignment()
898 .getAlignmentAnnotation();
899 if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
901 if (selectedRow > -1 && selectedRow < aa.length)
903 if (aa[selectedRow].groupRef != null)
905 if (evt.getClickCount() >= 2)
907 // todo: make the ap scroll to the selection - not necessary, first
908 // click highlights/scrolls, second selects
909 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
911 SequenceGroup sg = ap.av.getSelectionGroup();
912 if (sg == null || sg == aa[selectedRow].groupRef
913 || !(Platform.isControlDown(evt) || evt.isShiftDown()))
915 if (Platform.isControlDown(evt) || evt.isShiftDown())
917 // clone a new selection group from the associated group
918 ap.av.setSelectionGroup(
919 new SequenceGroup(aa[selectedRow].groupRef));
923 // set selection to the associated group so it can be edited
924 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
929 // modify current selection with associated group
930 int remainToAdd = aa[selectedRow].groupRef.getSize();
931 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
933 if (jalview.util.Platform.isControlDown(evt))
935 sg.addOrRemove(sgs, --remainToAdd == 0);
939 // notionally, we should also add intermediate sequences from
940 // last added sequence ?
941 sg.addSequence(sgs, --remainToAdd == 0);
946 ap.paintAlignment(false, false);
947 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
948 ap.av.sendSelection();
952 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
953 aa[selectedRow].groupRef.getSequences(null));
957 else if (aa[selectedRow].sequenceRef != null)
959 if (evt.getClickCount() == 1)
961 ap.getSeqPanel().ap.getIdPanel()
962 .highlightSearchResults(Arrays.asList(new SequenceI[]
963 { aa[selectedRow].sequenceRef }));
965 else if (evt.getClickCount() >= 2)
967 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
968 SequenceGroup sg = ap.av.getSelectionGroup();
971 // we make a copy rather than edit the current selection if no
973 // see Enhancement JAL-1557
974 if (!(Platform.isControlDown(evt) || evt.isShiftDown()))
976 sg = new SequenceGroup(sg);
978 sg.addSequence(aa[selectedRow].sequenceRef, false);
982 if (Platform.isControlDown(evt))
984 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
988 // notionally, we should also add intermediate sequences from
989 // last added sequence ?
990 sg.addSequence(aa[selectedRow].sequenceRef, true);
996 sg = new SequenceGroup();
998 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
999 sg.addSequence(aa[selectedRow].sequenceRef, false);
1001 ap.av.setSelectionGroup(sg);
1002 ap.paintAlignment(false, false);
1003 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
1004 ap.av.sendSelection();
1014 * do a single sequence copy to jalview and the system clipboard
1017 * sequence to be copied to clipboard
1019 protected void copy_annotseqtoclipboard(SequenceI sq)
1021 SequenceI[] seqs = new SequenceI[] { sq };
1022 String[] omitHidden = null;
1023 SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
1024 if (dseqs[0] == null)
1026 dseqs[0] = new Sequence(sq);
1027 dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars,
1028 sq.getSequenceAsString()));
1030 sq.setDatasetSequence(dseqs[0]);
1032 Alignment ds = new Alignment(dseqs);
1033 if (av.hasHiddenColumns())
1035 Iterator<int[]> it = av.getAlignment().getHiddenColumns()
1036 .getVisContigsIterator(0, sq.getLength(), false);
1037 omitHidden = new String[] { sq.getSequenceStringFromIterator(it) };
1040 int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
1041 if (av.hasHiddenColumns())
1043 alignmentStartEnd = av.getAlignment().getHiddenColumns()
1044 .getVisibleStartAndEndIndex(av.getAlignment().getWidth());
1047 String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
1048 seqs, omitHidden, alignmentStartEnd);
1050 Toolkit.getDefaultToolkit().getSystemClipboard()
1051 .setContents(new StringSelection(output), Desktop.instance);
1053 HiddenColumns hiddenColumns = null;
1055 if (av.hasHiddenColumns())
1057 hiddenColumns = new HiddenColumns(
1058 av.getAlignment().getHiddenColumns());
1061 Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
1063 // sequence ? need to
1065 // sequence as special.
1076 public void paintComponent(Graphics g)
1079 int width = getWidth();
1082 width = ap.calculateIdWidth().width;
1085 Graphics2D g2 = (Graphics2D) g;
1088 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1089 RenderingHints.VALUE_ANTIALIAS_ON);
1092 drawComponent(g2, true, width);
1097 * Draw the full set of annotation Labels for the alignment at the given
1101 * Graphics2D instance (needed for font scaling)
1103 * Width for scaling labels
1106 public void drawComponent(Graphics g, int width)
1108 drawComponent(g, false, width);
1112 * Draw the full set of annotation Labels for the alignment at the given
1116 * Graphics2D instance (needed for font scaling)
1118 * - true indicates that only current visible area needs to be
1121 * Width for scaling labels
1123 public void drawComponent(Graphics g, boolean clip, int width)
1125 if (av.getFont().getSize() < 10)
1131 g.setFont(av.getFont());
1134 FontMetrics fm = g.getFontMetrics(g.getFont());
1135 g.setColor(Color.white);
1136 g.fillRect(0, 0, getWidth(), getHeight());
1138 g.translate(0, getScrollOffset());
1139 g.setColor(Color.black);
1140 SequenceI lastSeqRef = null;
1141 String lastLabel = null;
1142 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1143 int fontHeight = g.getFont().getSize();
1146 int graphExtras = 0;
1148 Font baseFont = g.getFont();
1149 FontMetrics baseMetrics = fm;
1150 int ofontH = fontHeight;
1153 int[] visr = (ap != null && ap.getAnnotationPanel() != null)
1154 ? ap.getAnnotationPanel().getVisibleVRange()
1156 if (clip && visr != null)
1159 visHeight = visr[1];
1161 boolean visible = true, before = false, after = false;
1164 hasHiddenRows = false;
1167 for (int i = 0; i < aa.length; i++)
1172 hasHiddenRows = true;
1176 // look ahead to next annotation
1177 for (nexAA = i + 1; nexAA < aa.length
1178 && !aa[nexAA].visible; nexAA++)
1189 System.out.println("before vis: " + i);
1193 // don't draw what isn't visible
1196 if (olY > visHeight)
1204 "Scroll offset: " + sOffset + " after vis: " + i);
1208 // don't draw what isn't visible
1212 g.setColor(Color.black);
1214 offset = -aa[i].height / 2;
1218 offset += fm.getHeight() / 2;
1219 offset -= fm.getDescent();
1223 offset += fm.getDescent();
1225 String label = aa[i].label;
1226 boolean vertBar = false;
1227 if ((lastLabel != null && lastLabel.equals(label)))
1229 label = aa[i].description;
1233 if (nexAA < aa.length && label.equals(aa[nexAA].label)) // &&
1234 // aa[nexY].sequenceRef==aa[i].sequenceRef)
1237 // next label is the same as this label
1238 label = aa[i].description;
1245 if (aa[i].sequenceRef != null)
1247 if (aa[i].sequenceRef != lastSeqRef)
1249 label = aa[i].sequenceRef.getName() + " " + label;
1250 // TODO record relationship between sequence and this annotation and
1258 x = width - fm.stringWidth(label) - 3;
1260 if (aa[i].graphGroup > -1)
1263 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1264 // computed efficiently for all visible labels
1265 for (int gg = 0; gg < aa.length; gg++)
1267 if (aa[gg].graphGroup == aa[i].graphGroup)
1272 if (groupSize * (fontHeight + 8) < aa[i].height)
1274 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1279 // scale font to fit
1280 float h = aa[i].height / (float) groupSize, s;
1287 fontHeight = -8 + (int) h;
1288 s = ((float) fontHeight) / (float) ofontH;
1290 .deriveFont(AffineTransform.getScaleInstance(s, s));
1292 fm = g.getFontMetrics();
1293 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1299 for (int gg = 0; gg < aa.length; gg++)
1301 if (aa[gg].graphGroup == aa[i].graphGroup)
1303 x = width - fm.stringWidth(aa[gg].label) - 3;
1304 g.drawString(aa[gg].label, x, y - graphExtras);
1306 if (aa[gg]._linecolour != null)
1309 g.setColor(aa[gg]._linecolour);
1310 g.drawLine(x, y - graphExtras + 3,
1311 x + fm.stringWidth(aa[gg].label),
1312 y - graphExtras + 3);
1315 g.setColor(Color.black);
1316 graphExtras += fontHeight + 8;
1320 g.setFont(baseFont);
1322 fontHeight = ofontH;
1328 g.drawLine(width - 3, y + offset - fontHeight, width - 3,
1329 (int) (y - 1.5 * aa[i].height - offset - fontHeight));
1330 // g.drawLine(20, y + offset, x - 20, y + offset);
1333 g.drawString(label, x, y + offset);
1335 lastSeqRef = aa[i].sequenceRef;
1339 if (!resizePanel && dragEvent != null && aa != null)
1341 g.setColor(Color.lightGray);
1343 (aa[selectedRow].sequenceRef == null ? ""
1344 : aa[selectedRow].sequenceRef.getName())
1345 + aa[selectedRow].label,
1346 dragEvent.getX(), dragEvent.getY() - getScrollOffset());
1349 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1351 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1352 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1357 public int getScrollOffset()
1359 return scrollOffset;
1363 public void mouseEntered(MouseEvent e)