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 item = new JMenuItem("Show Tree for Matrix");
438 item.addActionListener(new ActionListener()
442 public void actionPerformed(ActionEvent e)
445 ap.alignFrame.showContactMapTree(aa[selectedRow], cm);
453 item = new JMenuItem("Calculate Tree for Matrix");
454 item.addActionListener(new ActionListener()
456 // TODO - refactor to analysis background thread
458 public void actionPerformed(ActionEvent e)
460 new Thread(new Runnable()
465 AlignmentAnnotation alan = aa[selectedRow];
466 cm.setGroupSet(GroupSet.makeGroups(cm, 5f, true));
467 ap.alignFrame.showContactMapTree(alan, cm);
475 // Show/Hide group shading on matrix view
476 // Set/adjust threshold for grouping ?
477 // colour alignment by this [type]
478 // select/hide columns by this row
483 pop.show(this, evt.getX(), evt.getY());
487 * A helper method that adds menu options for calculation and visualisation of
488 * group and/or alignment consensus annotation to a popup menu. This is
489 * designed to be reusable for either unwrapped mode (popup menu is shown on
490 * component AnnotationLabels), or wrapped mode (popup menu is shown on
491 * IdPanel when the mouse is over an annotation label).
497 static void addConsensusMenuOptions(AlignmentPanel ap,
498 AlignmentAnnotation ann, JPopupMenu pop)
502 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
503 MessageManager.getString("label.ignore_gaps_consensus"),
504 (ann.groupRef != null) ? ann.groupRef.getIgnoreGapsConsensus()
505 : ap.av.isIgnoreGapsConsensus());
506 final AlignmentAnnotation aaa = ann;
507 cbmi.addActionListener(new ActionListener()
510 public void actionPerformed(ActionEvent e)
512 if (aaa.groupRef != null)
514 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
515 ap.getAnnotationPanel()
516 .paint(ap.getAnnotationPanel().getGraphics());
520 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
522 ap.alignmentChanged();
527 if (aaa.groupRef != null)
530 * group consensus options
532 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
533 MessageManager.getString("label.show_group_histogram"),
534 ann.groupRef.isShowConsensusHistogram());
535 chist.addActionListener(new ActionListener()
538 public void actionPerformed(ActionEvent e)
540 aaa.groupRef.setShowConsensusHistogram(chist.getState());
545 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
546 MessageManager.getString("label.show_group_logo"),
547 ann.groupRef.isShowSequenceLogo());
548 cprofl.addActionListener(new ActionListener()
551 public void actionPerformed(ActionEvent e)
553 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
558 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
559 MessageManager.getString("label.normalise_group_logo"),
560 ann.groupRef.isNormaliseSequenceLogo());
561 cproflnorm.addActionListener(new ActionListener()
564 public void actionPerformed(ActionEvent e)
566 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
567 // automatically enable logo display if we're clicked
568 aaa.groupRef.setshowSequenceLogo(true);
577 * alignment consensus options
579 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
580 MessageManager.getString("label.show_histogram"),
581 ap.av.isShowConsensusHistogram());
582 chist.addActionListener(new ActionListener()
585 public void actionPerformed(ActionEvent e)
587 ap.av.setShowConsensusHistogram(chist.getState());
588 ap.alignFrame.setMenusForViewport();
593 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
594 MessageManager.getString("label.show_logo"),
595 ap.av.isShowSequenceLogo());
596 cprof.addActionListener(new ActionListener()
599 public void actionPerformed(ActionEvent e)
601 ap.av.setShowSequenceLogo(cprof.getState());
602 ap.alignFrame.setMenusForViewport();
607 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
608 MessageManager.getString("label.normalise_logo"),
609 ap.av.isNormaliseSequenceLogo());
610 cprofnorm.addActionListener(new ActionListener()
613 public void actionPerformed(ActionEvent e)
615 ap.av.setShowSequenceLogo(true);
616 ap.av.setNormaliseSequenceLogo(cprofnorm.getState());
617 ap.alignFrame.setMenusForViewport();
626 * Reorders annotation rows after a drag of a label
631 public void mouseReleased(MouseEvent evt)
633 if (evt.isPopupTrigger())
639 int start = selectedRow;
640 getSelectedRow(evt.getY() - getScrollOffset());
641 int end = selectedRow;
644 * if dragging to resize instead, start == end
648 // Swap these annotations
649 AlignmentAnnotation startAA = ap.av.getAlignment()
650 .getAlignmentAnnotation()[start];
653 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
655 AlignmentAnnotation endAA = ap.av.getAlignment()
656 .getAlignmentAnnotation()[end];
658 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
659 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
665 ap.getAnnotationPanel().repaint();
669 * Removes the height adjuster image on leaving the panel, unless currently
673 public void mouseExited(MouseEvent evt)
675 if (resizePanel && dragEvent == null)
683 * A mouse drag may be either an adjustment of the panel height (if flag
684 * resizePanel is set on), or a reordering of the annotation rows. The former
685 * is dealt with by this method, the latter in mouseReleased.
690 public void mouseDragged(MouseEvent evt)
696 Dimension d = ap.annotationScroller.getPreferredSize();
697 int dif = evt.getY() - oldY;
699 dif /= ap.av.getCharHeight();
700 dif *= ap.av.getCharHeight();
702 if ((d.height - dif) > 20)
704 ap.annotationScroller
705 .setPreferredSize(new Dimension(d.width, d.height - dif));
706 d = ap.annotationSpaceFillerHolder.getPreferredSize();
707 ap.annotationSpaceFillerHolder
708 .setPreferredSize(new Dimension(d.width, d.height - dif));
709 ap.paintAlignment(true, false);
721 * Updates the tooltip as the mouse moves over the labels
726 public void mouseMoved(MouseEvent evt)
728 showOrHideAdjuster(evt);
730 getSelectedRow(evt.getY() - getScrollOffset());
732 if (selectedRow > -1 && ap.av.getAlignment()
733 .getAlignmentAnnotation().length > selectedRow)
735 AlignmentAnnotation[] anns = ap.av.getAlignment()
736 .getAlignmentAnnotation();
737 AlignmentAnnotation aa = anns[selectedRow];
739 String desc = getTooltip(aa);
740 this.setToolTipText(desc);
741 String msg = getStatusMessage(aa, anns);
742 ap.alignFrame.setStatus(msg);
747 * Constructs suitable text to show in the status bar when over an annotation
748 * label, containing the associated sequence name (if any), and the annotation
749 * labels (or all labels for a graph group annotation)
755 static String getStatusMessage(AlignmentAnnotation aa,
756 AlignmentAnnotation[] anns)
763 StringBuilder msg = new StringBuilder(32);
764 if (aa.sequenceRef != null)
766 msg.append(aa.sequenceRef.getName()).append(" : ");
769 if (aa.graphGroup == -1)
771 msg.append(aa.label);
773 else if (anns != null)
775 boolean first = true;
776 for (int i = anns.length - 1; i >= 0; i--)
778 if (anns[i].graphGroup == aa.graphGroup)
784 msg.append(anns[i].label);
790 return msg.toString();
794 * Answers a tooltip, formatted as html, containing the annotation description
795 * (prefixed by associated sequence id if applicable), and the annotation
796 * (non-positional) score if it has one. Answers null if neither description
797 * nor score is found.
802 static String getTooltip(AlignmentAnnotation aa)
808 StringBuilder tooltip = new StringBuilder();
809 if (aa.description != null && !aa.description.equals("New description"))
811 // TODO: we could refactor and merge this code with the code in
812 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
814 String desc = aa.getDescription(true).trim();
815 if (!desc.toLowerCase(Locale.ROOT).startsWith(HTML_START_TAG))
817 tooltip.append(HTML_START_TAG);
818 desc = desc.replace("<", "<");
820 else if (desc.toLowerCase(Locale.ROOT).endsWith(HTML_END_TAG))
822 desc = desc.substring(0, desc.length() - HTML_END_TAG.length());
824 tooltip.append(desc);
828 // begin the tooltip's html fragment
829 tooltip.append(HTML_START_TAG);
833 if (tooltip.length() > HTML_START_TAG.length())
835 tooltip.append("<br/>");
837 // TODO: limit precision of score to avoid noise from imprecise
839 // (64.7 becomes 64.7+/some tiny value).
840 tooltip.append(" Score: ").append(String.valueOf(aa.score));
843 if (tooltip.length() > HTML_START_TAG.length())
845 return tooltip.append(HTML_END_TAG).toString();
849 * nothing in the tooltip (except "<html>")
855 * Shows the height adjuster image if the mouse moves into the top left
856 * region, or hides it if the mouse leaves the regio
860 protected void showOrHideAdjuster(MouseEvent evt)
862 boolean was = resizePanel;
863 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
864 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
866 if (resizePanel != was)
869 .getPredefinedCursor(resizePanel ? Cursor.S_RESIZE_CURSOR
870 : Cursor.DEFAULT_CURSOR));
876 public void mouseClicked(MouseEvent evt)
878 final AlignmentAnnotation[] aa = ap.av.getAlignment()
879 .getAlignmentAnnotation();
880 if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
882 if (selectedRow > -1 && selectedRow < aa.length)
884 if (aa[selectedRow].groupRef != null)
886 if (evt.getClickCount() >= 2)
888 // todo: make the ap scroll to the selection - not necessary, first
889 // click highlights/scrolls, second selects
890 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
892 SequenceGroup sg = ap.av.getSelectionGroup();
893 if (sg == null || sg == aa[selectedRow].groupRef
894 || !(Platform.isControlDown(evt) || evt.isShiftDown()))
896 if (Platform.isControlDown(evt) || evt.isShiftDown())
898 // clone a new selection group from the associated group
899 ap.av.setSelectionGroup(
900 new SequenceGroup(aa[selectedRow].groupRef));
904 // set selection to the associated group so it can be edited
905 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
910 // modify current selection with associated group
911 int remainToAdd = aa[selectedRow].groupRef.getSize();
912 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
914 if (jalview.util.Platform.isControlDown(evt))
916 sg.addOrRemove(sgs, --remainToAdd == 0);
920 // notionally, we should also add intermediate sequences from
921 // last added sequence ?
922 sg.addSequence(sgs, --remainToAdd == 0);
927 ap.paintAlignment(false, false);
928 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
929 ap.av.sendSelection();
933 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
934 aa[selectedRow].groupRef.getSequences(null));
938 else if (aa[selectedRow].sequenceRef != null)
940 if (evt.getClickCount() == 1)
942 ap.getSeqPanel().ap.getIdPanel()
943 .highlightSearchResults(Arrays.asList(new SequenceI[]
944 { aa[selectedRow].sequenceRef }));
946 else if (evt.getClickCount() >= 2)
948 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
949 SequenceGroup sg = ap.av.getSelectionGroup();
952 // we make a copy rather than edit the current selection if no
954 // see Enhancement JAL-1557
955 if (!(Platform.isControlDown(evt) || evt.isShiftDown()))
957 sg = new SequenceGroup(sg);
959 sg.addSequence(aa[selectedRow].sequenceRef, false);
963 if (Platform.isControlDown(evt))
965 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
969 // notionally, we should also add intermediate sequences from
970 // last added sequence ?
971 sg.addSequence(aa[selectedRow].sequenceRef, true);
977 sg = new SequenceGroup();
979 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
980 sg.addSequence(aa[selectedRow].sequenceRef, false);
982 ap.av.setSelectionGroup(sg);
983 ap.paintAlignment(false, false);
984 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
985 ap.av.sendSelection();
995 * do a single sequence copy to jalview and the system clipboard
998 * sequence to be copied to clipboard
1000 protected void copy_annotseqtoclipboard(SequenceI sq)
1002 SequenceI[] seqs = new SequenceI[] { sq };
1003 String[] omitHidden = null;
1004 SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
1005 if (dseqs[0] == null)
1007 dseqs[0] = new Sequence(sq);
1008 dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars,
1009 sq.getSequenceAsString()));
1011 sq.setDatasetSequence(dseqs[0]);
1013 Alignment ds = new Alignment(dseqs);
1014 if (av.hasHiddenColumns())
1016 Iterator<int[]> it = av.getAlignment().getHiddenColumns()
1017 .getVisContigsIterator(0, sq.getLength(), false);
1018 omitHidden = new String[] { sq.getSequenceStringFromIterator(it) };
1021 int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
1022 if (av.hasHiddenColumns())
1024 alignmentStartEnd = av.getAlignment().getHiddenColumns()
1025 .getVisibleStartAndEndIndex(av.getAlignment().getWidth());
1028 String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
1029 seqs, omitHidden, alignmentStartEnd);
1031 Toolkit.getDefaultToolkit().getSystemClipboard()
1032 .setContents(new StringSelection(output), Desktop.instance);
1034 HiddenColumns hiddenColumns = null;
1036 if (av.hasHiddenColumns())
1038 hiddenColumns = new HiddenColumns(
1039 av.getAlignment().getHiddenColumns());
1042 Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
1044 // sequence ? need to
1046 // sequence as special.
1057 public void paintComponent(Graphics g)
1060 int width = getWidth();
1063 width = ap.calculateIdWidth().width;
1066 Graphics2D g2 = (Graphics2D) g;
1069 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1070 RenderingHints.VALUE_ANTIALIAS_ON);
1073 drawComponent(g2, true, width);
1078 * Draw the full set of annotation Labels for the alignment at the given
1082 * Graphics2D instance (needed for font scaling)
1084 * Width for scaling labels
1087 public void drawComponent(Graphics g, int width)
1089 drawComponent(g, false, width);
1093 * Draw the full set of annotation Labels for the alignment at the given
1097 * Graphics2D instance (needed for font scaling)
1099 * - true indicates that only current visible area needs to be
1102 * Width for scaling labels
1104 public void drawComponent(Graphics g, boolean clip, int width)
1106 if (av.getFont().getSize() < 10)
1112 g.setFont(av.getFont());
1115 FontMetrics fm = g.getFontMetrics(g.getFont());
1116 g.setColor(Color.white);
1117 g.fillRect(0, 0, getWidth(), getHeight());
1119 g.translate(0, getScrollOffset());
1120 g.setColor(Color.black);
1121 SequenceI lastSeqRef = null;
1122 String lastLabel = null;
1123 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1124 int fontHeight = g.getFont().getSize();
1127 int graphExtras = 0;
1129 Font baseFont = g.getFont();
1130 FontMetrics baseMetrics = fm;
1131 int ofontH = fontHeight;
1134 int[] visr = (ap != null && ap.getAnnotationPanel() != null)
1135 ? ap.getAnnotationPanel().getVisibleVRange()
1137 if (clip && visr != null)
1140 visHeight = visr[1];
1142 boolean visible = true, before = false, after = false;
1145 hasHiddenRows = false;
1148 for (int i = 0; i < aa.length; i++)
1153 hasHiddenRows = true;
1157 // look ahead to next annotation
1158 for (nexAA = i + 1; nexAA < aa.length
1159 && !aa[nexAA].visible; nexAA++)
1170 System.out.println("before vis: " + i);
1174 // don't draw what isn't visible
1177 if (olY > visHeight)
1185 "Scroll offset: " + sOffset + " after vis: " + i);
1189 // don't draw what isn't visible
1193 g.setColor(Color.black);
1195 offset = -aa[i].height / 2;
1199 offset += fm.getHeight() / 2;
1200 offset -= fm.getDescent();
1204 offset += fm.getDescent();
1206 String label = aa[i].label;
1207 boolean vertBar = false;
1208 if ((lastLabel != null && lastLabel.equals(label)))
1210 label = aa[i].description;
1214 if (nexAA < aa.length && label.equals(aa[nexAA].label)) // &&
1215 // aa[nexY].sequenceRef==aa[i].sequenceRef)
1218 // next label is the same as this label
1219 label = aa[i].description;
1226 if (aa[i].sequenceRef != null)
1228 if (aa[i].sequenceRef != lastSeqRef)
1230 label = aa[i].sequenceRef.getName() + " " + label;
1231 // TODO record relationship between sequence and this annotation and
1239 x = width - fm.stringWidth(label) - 3;
1241 if (aa[i].graphGroup > -1)
1244 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1245 // computed efficiently for all visible labels
1246 for (int gg = 0; gg < aa.length; gg++)
1248 if (aa[gg].graphGroup == aa[i].graphGroup)
1253 if (groupSize * (fontHeight + 8) < aa[i].height)
1255 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1260 // scale font to fit
1261 float h = aa[i].height / (float) groupSize, s;
1268 fontHeight = -8 + (int) h;
1269 s = ((float) fontHeight) / (float) ofontH;
1271 .deriveFont(AffineTransform.getScaleInstance(s, s));
1273 fm = g.getFontMetrics();
1274 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1280 for (int gg = 0; gg < aa.length; gg++)
1282 if (aa[gg].graphGroup == aa[i].graphGroup)
1284 x = width - fm.stringWidth(aa[gg].label) - 3;
1285 g.drawString(aa[gg].label, x, y - graphExtras);
1287 if (aa[gg]._linecolour != null)
1290 g.setColor(aa[gg]._linecolour);
1291 g.drawLine(x, y - graphExtras + 3,
1292 x + fm.stringWidth(aa[gg].label),
1293 y - graphExtras + 3);
1296 g.setColor(Color.black);
1297 graphExtras += fontHeight + 8;
1301 g.setFont(baseFont);
1303 fontHeight = ofontH;
1309 g.drawLine(width - 3, y + offset - fontHeight, width - 3,
1310 (int) (y - 1.5 * aa[i].height - offset - fontHeight));
1311 // g.drawLine(20, y + offset, x - 20, y + offset);
1314 g.drawString(label, x, y + offset);
1316 lastSeqRef = aa[i].sequenceRef;
1320 if (!resizePanel && dragEvent != null && aa != null)
1322 g.setColor(Color.lightGray);
1324 (aa[selectedRow].sequenceRef == null ? ""
1325 : aa[selectedRow].sequenceRef.getName())
1326 + aa[selectedRow].label,
1327 dragEvent.getX(), dragEvent.getY() - getScrollOffset());
1330 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1332 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1333 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1338 public int getScrollOffset()
1340 return scrollOffset;
1344 public void mouseEntered(MouseEvent e)