2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.Color;
24 import java.awt.Cursor;
25 import java.awt.Dimension;
27 import java.awt.FontMetrics;
28 import java.awt.Graphics;
29 import java.awt.Graphics2D;
30 import java.awt.RenderingHints;
31 import java.awt.Toolkit;
32 import java.awt.datatransfer.StringSelection;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseMotionListener;
38 import java.awt.geom.AffineTransform;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.Iterator;
42 import java.util.Locale;
44 import javax.swing.JCheckBoxMenuItem;
45 import javax.swing.JMenuItem;
46 import javax.swing.JPanel;
47 import javax.swing.JPopupMenu;
48 import javax.swing.SwingUtilities;
49 import javax.swing.ToolTipManager;
51 import jalview.analysis.AlignSeq;
52 import jalview.analysis.AlignmentUtils;
53 import jalview.datamodel.Alignment;
54 import jalview.datamodel.AlignmentAnnotation;
55 import jalview.datamodel.Annotation;
56 import jalview.datamodel.HiddenColumns;
57 import jalview.datamodel.Sequence;
58 import jalview.datamodel.SequenceGroup;
59 import jalview.datamodel.SequenceI;
60 import jalview.io.FileFormat;
61 import jalview.io.FormatAdapter;
62 import jalview.util.Comparison;
63 import jalview.util.MessageManager;
64 import jalview.util.Platform;
65 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
68 * The panel that holds the labels for alignment annotations, providing
69 * tooltips, context menus, drag to reorder rows, and drag to adjust panel
72 public class AnnotationLabels extends JPanel
73 implements MouseListener, MouseMotionListener, ActionListener
75 private static final String HTML_END_TAG = "</html>";
77 private static final String HTML_START_TAG = "<html>";
80 * width in pixels within which height adjuster arrows are shown and active
82 private static final int HEIGHT_ADJUSTER_WIDTH = 50;
85 * height in pixels for allowing height adjuster to be active
87 private static int HEIGHT_ADJUSTER_HEIGHT = 10;
89 private static final Font font = new Font("Arial", Font.PLAIN, 11);
91 private static final String TOGGLE_LABELSCALE = MessageManager
92 .getString("label.scale_label_to_column");
94 private static final String ADDNEW = MessageManager
95 .getString("label.add_new_row");
97 private static final String EDITNAME = MessageManager
98 .getString("label.edit_label_description");
100 private static final String HIDE = MessageManager
101 .getString("label.hide_row");
103 private static final String DELETE = MessageManager
104 .getString("label.delete_row");
106 private static final String SHOWALL = MessageManager
107 .getString("label.show_all_hidden_rows");
109 private static final String OUTPUT_TEXT = MessageManager
110 .getString("label.export_annotation");
112 private static final String COPYCONS_SEQ = MessageManager
113 .getString("label.copy_consensus_sequence");
115 private final boolean debugRedraw = false;
117 private AlignmentPanel ap;
121 private MouseEvent dragEvent;
125 private int selectedRow;
127 private int scrollOffset = 0;
129 private boolean hasHiddenRows;
131 private boolean resizePanel = false;
134 * Creates a new AnnotationLabels object
138 public AnnotationLabels(AlignmentPanel ap)
143 ToolTipManager.sharedInstance().registerComponent(this);
145 addMouseListener(this);
146 addMouseMotionListener(this);
147 addMouseWheelListener(ap.getAnnotationPanel());
150 public AnnotationLabels(AlignViewport av)
161 public void setScrollOffset(int y)
168 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
172 * coordinate position to search for a row
174 void getSelectedRow(int y)
177 AlignmentAnnotation[] aa = ap.av.getAlignment()
178 .getAlignmentAnnotation();
182 for (int i = 0; i < aa.length; i++)
190 height += aa[i].height;
209 public void actionPerformed(ActionEvent evt)
211 AlignmentAnnotation[] aa = ap.av.getAlignment()
212 .getAlignmentAnnotation();
214 String action = evt.getActionCommand();
215 if (ADDNEW.equals(action))
218 * non-returning dialog
220 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
221 null, new Annotation[ap.av.getAlignment().getWidth()]);
222 editLabelDescription(newAnnotation, true);
224 else if (EDITNAME.equals(action))
227 * non-returning dialog
229 editLabelDescription(aa[selectedRow], false);
231 else if (HIDE.equals(action))
233 aa[selectedRow].visible = false;
235 else if (DELETE.equals(action))
237 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
238 ap.av.getCalcManager().removeWorkerForAnnotation(aa[selectedRow]);
240 else if (SHOWALL.equals(action))
242 for (int i = 0; i < aa.length; i++)
244 if (!aa[i].visible && aa[i].annotations != null)
246 aa[i].visible = true;
250 else if (OUTPUT_TEXT.equals(action))
252 new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]);
254 else if (COPYCONS_SEQ.equals(action))
256 SequenceI cons = null;
257 if (aa[selectedRow].groupRef != null)
259 cons = aa[selectedRow].groupRef.getConsensusSeq();
263 cons = av.getConsensusSeq();
267 copy_annotseqtoclipboard(cons);
270 else if (TOGGLE_LABELSCALE.equals(action))
272 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
279 * Shows a dialog where the annotation name and description may be edited. If
280 * parameter addNew is true, then on confirmation, a new AlignmentAnnotation
281 * is added, else an existing annotation is updated.
286 void editLabelDescription(AlignmentAnnotation annotation, boolean addNew)
288 String name = MessageManager.getString("label.annotation_name");
289 String description = MessageManager
290 .getString("label.annotation_description");
291 String title = MessageManager
292 .getString("label.edit_annotation_name_description");
293 EditNameDialog dialog = new EditNameDialog(annotation.label,
294 annotation.description, name, description);
296 dialog.showDialog(ap.alignFrame, title, () -> {
297 annotation.label = dialog.getName();
298 String text = dialog.getDescription();
299 if (text != null && text.length() == 0)
303 annotation.description = text;
306 ap.av.getAlignment().addAnnotation(annotation);
307 ap.av.getAlignment().setAnnotationIndex(annotation, 0);
314 public void mousePressed(MouseEvent evt)
316 getSelectedRow(evt.getY() - getScrollOffset());
318 if (evt.isPopupTrigger())
325 * Build and show the Pop-up menu at the right-click mouse position
329 void showPopupMenu(MouseEvent evt)
332 final AlignmentAnnotation[] aa = ap.av.getAlignment()
333 .getAlignmentAnnotation();
335 JPopupMenu pop = new JPopupMenu(
336 MessageManager.getString("label.annotations"));
337 JMenuItem item = new JMenuItem(ADDNEW);
338 item.addActionListener(this);
343 { // let the user make everything visible again
344 item = new JMenuItem(SHOWALL);
345 item.addActionListener(this);
348 pop.show(this, evt.getX(), evt.getY());
351 item = new JMenuItem(EDITNAME);
352 item.addActionListener(this);
354 item = new JMenuItem(HIDE);
355 item.addActionListener(this);
357 // JAL-1264 hide all sequence-specific annotations of this type
358 if (selectedRow < aa.length)
360 if (aa[selectedRow].sequenceRef != null)
362 final String label = aa[selectedRow].label;
363 JMenuItem hideType = new JMenuItem();
364 String text = MessageManager.getString("label.hide_all") + " "
366 hideType.setText(text);
367 hideType.addActionListener(new ActionListener()
370 public void actionPerformed(ActionEvent e)
372 AlignmentUtils.showOrHideSequenceAnnotations(
373 ap.av.getAlignment(), Collections.singleton(label),
381 item = new JMenuItem(DELETE);
382 item.addActionListener(this);
386 item = new JMenuItem(SHOWALL);
387 item.addActionListener(this);
390 item = new JMenuItem(OUTPUT_TEXT);
391 item.addActionListener(this);
393 // TODO: annotation object should be typed for autocalculated/derived
395 if (selectedRow < aa.length)
397 final String label = aa[selectedRow].label;
398 if (!aa[selectedRow].autoCalculated)
400 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
402 // display formatting settings for this row.
404 // av and sequencegroup need to implement same interface for
405 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
406 aa[selectedRow].scaleColLabel);
407 item.addActionListener(this);
411 else if (label.indexOf("Consensus") > -1)
413 addConsensusMenuOptions(ap, aa[selectedRow], pop);
415 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
416 consclipbrd.addActionListener(this);
417 pop.add(consclipbrd);
419 if (aa[selectedRow].graph == AlignmentAnnotation.CONTACT_MAP
420 && PAEContactMatrix.PAEMATRIX
421 .equals(aa[selectedRow].getCalcId()))
423 final PAEContactMatrix cm = (PAEContactMatrix) av
424 .getContactMatrix(aa[selectedRow]);
425 if (cm != null && cm.getNewick() != null
426 && cm.getNewick().length() > 0)
428 item = new JMenuItem("Show Tree for Matrix");
429 item.addActionListener(new ActionListener()
433 public void actionPerformed(ActionEvent e)
436 ap.alignFrame.showContactMapTree(aa[selectedRow], cm);
446 pop.show(this, evt.getX(), evt.getY());
450 * A helper method that adds menu options for calculation and visualisation of
451 * group and/or alignment consensus annotation to a popup menu. This is
452 * designed to be reusable for either unwrapped mode (popup menu is shown on
453 * component AnnotationLabels), or wrapped mode (popup menu is shown on
454 * IdPanel when the mouse is over an annotation label).
460 static void addConsensusMenuOptions(AlignmentPanel ap,
461 AlignmentAnnotation ann, JPopupMenu pop)
465 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
466 MessageManager.getString("label.ignore_gaps_consensus"),
467 (ann.groupRef != null) ? ann.groupRef.getIgnoreGapsConsensus()
468 : ap.av.isIgnoreGapsConsensus());
469 final AlignmentAnnotation aaa = ann;
470 cbmi.addActionListener(new ActionListener()
473 public void actionPerformed(ActionEvent e)
475 if (aaa.groupRef != null)
477 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
478 ap.getAnnotationPanel()
479 .paint(ap.getAnnotationPanel().getGraphics());
483 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
485 ap.alignmentChanged();
490 if (aaa.groupRef != null)
493 * group consensus options
495 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
496 MessageManager.getString("label.show_group_histogram"),
497 ann.groupRef.isShowConsensusHistogram());
498 chist.addActionListener(new ActionListener()
501 public void actionPerformed(ActionEvent e)
503 aaa.groupRef.setShowConsensusHistogram(chist.getState());
508 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
509 MessageManager.getString("label.show_group_logo"),
510 ann.groupRef.isShowSequenceLogo());
511 cprofl.addActionListener(new ActionListener()
514 public void actionPerformed(ActionEvent e)
516 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
521 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
522 MessageManager.getString("label.normalise_group_logo"),
523 ann.groupRef.isNormaliseSequenceLogo());
524 cproflnorm.addActionListener(new ActionListener()
527 public void actionPerformed(ActionEvent e)
529 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
530 // automatically enable logo display if we're clicked
531 aaa.groupRef.setshowSequenceLogo(true);
540 * alignment consensus options
542 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
543 MessageManager.getString("label.show_histogram"),
544 ap.av.isShowConsensusHistogram());
545 chist.addActionListener(new ActionListener()
548 public void actionPerformed(ActionEvent e)
550 ap.av.setShowConsensusHistogram(chist.getState());
551 ap.alignFrame.setMenusForViewport();
556 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
557 MessageManager.getString("label.show_logo"),
558 ap.av.isShowSequenceLogo());
559 cprof.addActionListener(new ActionListener()
562 public void actionPerformed(ActionEvent e)
564 ap.av.setShowSequenceLogo(cprof.getState());
565 ap.alignFrame.setMenusForViewport();
570 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
571 MessageManager.getString("label.normalise_logo"),
572 ap.av.isNormaliseSequenceLogo());
573 cprofnorm.addActionListener(new ActionListener()
576 public void actionPerformed(ActionEvent e)
578 ap.av.setShowSequenceLogo(true);
579 ap.av.setNormaliseSequenceLogo(cprofnorm.getState());
580 ap.alignFrame.setMenusForViewport();
589 * Reorders annotation rows after a drag of a label
594 public void mouseReleased(MouseEvent evt)
596 if (evt.isPopupTrigger())
602 int start = selectedRow;
603 getSelectedRow(evt.getY() - getScrollOffset());
604 int end = selectedRow;
607 * if dragging to resize instead, start == end
611 // Swap these annotations
612 AlignmentAnnotation startAA = ap.av.getAlignment()
613 .getAlignmentAnnotation()[start];
616 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
618 AlignmentAnnotation endAA = ap.av.getAlignment()
619 .getAlignmentAnnotation()[end];
621 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
622 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
628 ap.getAnnotationPanel().repaint();
632 * Removes the height adjuster image on leaving the panel, unless currently
636 public void mouseExited(MouseEvent evt)
638 if (resizePanel && dragEvent == null)
646 * A mouse drag may be either an adjustment of the panel height (if flag
647 * resizePanel is set on), or a reordering of the annotation rows. The former
648 * is dealt with by this method, the latter in mouseReleased.
653 public void mouseDragged(MouseEvent evt)
659 Dimension d = ap.annotationScroller.getPreferredSize();
660 int dif = evt.getY() - oldY;
662 dif /= ap.av.getCharHeight();
663 dif *= ap.av.getCharHeight();
665 if ((d.height - dif) > 20)
667 ap.annotationScroller
668 .setPreferredSize(new Dimension(d.width, d.height - dif));
669 d = ap.annotationSpaceFillerHolder.getPreferredSize();
670 ap.annotationSpaceFillerHolder
671 .setPreferredSize(new Dimension(d.width, d.height - dif));
672 ap.paintAlignment(true, false);
684 * Updates the tooltip as the mouse moves over the labels
689 public void mouseMoved(MouseEvent evt)
691 showOrHideAdjuster(evt);
693 getSelectedRow(evt.getY() - getScrollOffset());
695 if (selectedRow > -1 && ap.av.getAlignment()
696 .getAlignmentAnnotation().length > selectedRow)
698 AlignmentAnnotation[] anns = ap.av.getAlignment()
699 .getAlignmentAnnotation();
700 AlignmentAnnotation aa = anns[selectedRow];
702 String desc = getTooltip(aa);
703 this.setToolTipText(desc);
704 String msg = getStatusMessage(aa, anns);
705 ap.alignFrame.setStatus(msg);
710 * Constructs suitable text to show in the status bar when over an annotation
711 * label, containing the associated sequence name (if any), and the annotation
712 * labels (or all labels for a graph group annotation)
718 static String getStatusMessage(AlignmentAnnotation aa,
719 AlignmentAnnotation[] anns)
726 StringBuilder msg = new StringBuilder(32);
727 if (aa.sequenceRef != null)
729 msg.append(aa.sequenceRef.getName()).append(" : ");
732 if (aa.graphGroup == -1)
734 msg.append(aa.label);
736 else if (anns != null)
738 boolean first = true;
739 for (int i = anns.length - 1; i >= 0; i--)
741 if (anns[i].graphGroup == aa.graphGroup)
747 msg.append(anns[i].label);
753 return msg.toString();
757 * Answers a tooltip, formatted as html, containing the annotation description
758 * (prefixed by associated sequence id if applicable), and the annotation
759 * (non-positional) score if it has one. Answers null if neither description
760 * nor score is found.
765 static String getTooltip(AlignmentAnnotation aa)
771 StringBuilder tooltip = new StringBuilder();
772 if (aa.description != null && !aa.description.equals("New description"))
774 // TODO: we could refactor and merge this code with the code in
775 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
777 String desc = aa.getDescription(true).trim();
778 if (!desc.toLowerCase(Locale.ROOT).startsWith(HTML_START_TAG))
780 tooltip.append(HTML_START_TAG);
781 desc = desc.replace("<", "<");
783 else if (desc.toLowerCase(Locale.ROOT).endsWith(HTML_END_TAG))
785 desc = desc.substring(0, desc.length() - HTML_END_TAG.length());
787 tooltip.append(desc);
791 // begin the tooltip's html fragment
792 tooltip.append(HTML_START_TAG);
796 if (tooltip.length() > HTML_START_TAG.length())
798 tooltip.append("<br/>");
800 // TODO: limit precision of score to avoid noise from imprecise
802 // (64.7 becomes 64.7+/some tiny value).
803 tooltip.append(" Score: ").append(String.valueOf(aa.score));
806 if (tooltip.length() > HTML_START_TAG.length())
808 return tooltip.append(HTML_END_TAG).toString();
812 * nothing in the tooltip (except "<html>")
818 * Shows the height adjuster image if the mouse moves into the top left
819 * region, or hides it if the mouse leaves the regio
823 protected void showOrHideAdjuster(MouseEvent evt)
825 boolean was = resizePanel;
826 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
827 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
829 if (resizePanel != was)
832 .getPredefinedCursor(resizePanel ? Cursor.S_RESIZE_CURSOR
833 : Cursor.DEFAULT_CURSOR));
839 public void mouseClicked(MouseEvent evt)
841 final AlignmentAnnotation[] aa = ap.av.getAlignment()
842 .getAlignmentAnnotation();
843 if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
845 if (selectedRow > -1 && selectedRow < aa.length)
847 if (aa[selectedRow].groupRef != null)
849 if (evt.getClickCount() >= 2)
851 // todo: make the ap scroll to the selection - not necessary, first
852 // click highlights/scrolls, second selects
853 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
855 SequenceGroup sg = ap.av.getSelectionGroup();
856 if (sg == null || sg == aa[selectedRow].groupRef
857 || !(Platform.isControlDown(evt) || evt.isShiftDown()))
859 if (Platform.isControlDown(evt) || evt.isShiftDown())
861 // clone a new selection group from the associated group
862 ap.av.setSelectionGroup(
863 new SequenceGroup(aa[selectedRow].groupRef));
867 // set selection to the associated group so it can be edited
868 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
873 // modify current selection with associated group
874 int remainToAdd = aa[selectedRow].groupRef.getSize();
875 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
877 if (jalview.util.Platform.isControlDown(evt))
879 sg.addOrRemove(sgs, --remainToAdd == 0);
883 // notionally, we should also add intermediate sequences from
884 // last added sequence ?
885 sg.addSequence(sgs, --remainToAdd == 0);
890 ap.paintAlignment(false, false);
891 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
892 ap.av.sendSelection();
896 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
897 aa[selectedRow].groupRef.getSequences(null));
901 else if (aa[selectedRow].sequenceRef != null)
903 if (evt.getClickCount() == 1)
905 ap.getSeqPanel().ap.getIdPanel()
906 .highlightSearchResults(Arrays.asList(new SequenceI[]
907 { aa[selectedRow].sequenceRef }));
909 else if (evt.getClickCount() >= 2)
911 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
912 SequenceGroup sg = ap.av.getSelectionGroup();
915 // we make a copy rather than edit the current selection if no
917 // see Enhancement JAL-1557
918 if (!(Platform.isControlDown(evt) || evt.isShiftDown()))
920 sg = new SequenceGroup(sg);
922 sg.addSequence(aa[selectedRow].sequenceRef, false);
926 if (Platform.isControlDown(evt))
928 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
932 // notionally, we should also add intermediate sequences from
933 // last added sequence ?
934 sg.addSequence(aa[selectedRow].sequenceRef, true);
940 sg = new SequenceGroup();
942 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
943 sg.addSequence(aa[selectedRow].sequenceRef, false);
945 ap.av.setSelectionGroup(sg);
946 ap.paintAlignment(false, false);
947 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
948 ap.av.sendSelection();
958 * do a single sequence copy to jalview and the system clipboard
961 * sequence to be copied to clipboard
963 protected void copy_annotseqtoclipboard(SequenceI sq)
965 SequenceI[] seqs = new SequenceI[] { sq };
966 String[] omitHidden = null;
967 SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
968 if (dseqs[0] == null)
970 dseqs[0] = new Sequence(sq);
971 dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars,
972 sq.getSequenceAsString()));
974 sq.setDatasetSequence(dseqs[0]);
976 Alignment ds = new Alignment(dseqs);
977 if (av.hasHiddenColumns())
979 Iterator<int[]> it = av.getAlignment().getHiddenColumns()
980 .getVisContigsIterator(0, sq.getLength(), false);
981 omitHidden = new String[] { sq.getSequenceStringFromIterator(it) };
984 int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
985 if (av.hasHiddenColumns())
987 alignmentStartEnd = av.getAlignment().getHiddenColumns()
988 .getVisibleStartAndEndIndex(av.getAlignment().getWidth());
991 String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
992 seqs, omitHidden, alignmentStartEnd);
994 Toolkit.getDefaultToolkit().getSystemClipboard()
995 .setContents(new StringSelection(output), Desktop.instance);
997 HiddenColumns hiddenColumns = null;
999 if (av.hasHiddenColumns())
1001 hiddenColumns = new HiddenColumns(
1002 av.getAlignment().getHiddenColumns());
1005 Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
1007 // sequence ? need to
1009 // sequence as special.
1020 public void paintComponent(Graphics g)
1023 int width = getWidth();
1026 width = ap.calculateIdWidth().width;
1029 Graphics2D g2 = (Graphics2D) g;
1032 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1033 RenderingHints.VALUE_ANTIALIAS_ON);
1036 drawComponent(g2, true, width);
1041 * Draw the full set of annotation Labels for the alignment at the given
1045 * Graphics2D instance (needed for font scaling)
1047 * Width for scaling labels
1050 public void drawComponent(Graphics g, int width)
1052 drawComponent(g, false, width);
1056 * Draw the full set of annotation Labels for the alignment at the given
1060 * Graphics2D instance (needed for font scaling)
1062 * - true indicates that only current visible area needs to be
1065 * Width for scaling labels
1067 public void drawComponent(Graphics g, boolean clip, int width)
1069 if (av.getFont().getSize() < 10)
1075 g.setFont(av.getFont());
1078 FontMetrics fm = g.getFontMetrics(g.getFont());
1079 g.setColor(Color.white);
1080 g.fillRect(0, 0, getWidth(), getHeight());
1082 g.translate(0, getScrollOffset());
1083 g.setColor(Color.black);
1084 SequenceI lastSeqRef = null;
1085 String lastLabel = null;
1086 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1087 int fontHeight = g.getFont().getSize();
1090 int graphExtras = 0;
1092 Font baseFont = g.getFont();
1093 FontMetrics baseMetrics = fm;
1094 int ofontH = fontHeight;
1097 int[] visr = (ap != null && ap.getAnnotationPanel() != null)
1098 ? ap.getAnnotationPanel().getVisibleVRange()
1100 if (clip && visr != null)
1103 visHeight = visr[1];
1105 boolean visible = true, before = false, after = false;
1108 hasHiddenRows = false;
1111 for (int i = 0; i < aa.length; i++)
1116 hasHiddenRows = true;
1120 // look ahead to next annotation
1121 for (nexAA = i + 1; nexAA < aa.length
1122 && !aa[nexAA].visible; nexAA++)
1133 System.out.println("before vis: " + i);
1137 // don't draw what isn't visible
1140 if (olY > visHeight)
1148 "Scroll offset: " + sOffset + " after vis: " + i);
1152 // don't draw what isn't visible
1156 g.setColor(Color.black);
1158 offset = -aa[i].height / 2;
1162 offset += fm.getHeight() / 2;
1163 offset -= fm.getDescent();
1167 offset += fm.getDescent();
1169 String label = aa[i].label;
1170 boolean vertBar = false;
1171 if ((lastLabel != null && lastLabel.equals(label)))
1173 label = aa[i].description;
1177 if (nexAA < aa.length && label.equals(aa[nexAA].label)) // &&
1178 // aa[nexY].sequenceRef==aa[i].sequenceRef)
1181 // next label is the same as this label
1182 label = aa[i].description;
1189 if (aa[i].sequenceRef != null)
1191 if (aa[i].sequenceRef != lastSeqRef)
1193 label = aa[i].sequenceRef.getName() + " " + label;
1194 // TODO record relationship between sequence and this annotation and
1202 x = width - fm.stringWidth(label) - 3;
1204 if (aa[i].graphGroup > -1)
1207 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1208 // computed efficiently for all visible labels
1209 for (int gg = 0; gg < aa.length; gg++)
1211 if (aa[gg].graphGroup == aa[i].graphGroup)
1216 if (groupSize * (fontHeight + 8) < aa[i].height)
1218 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1223 // scale font to fit
1224 float h = aa[i].height / (float) groupSize, s;
1231 fontHeight = -8 + (int) h;
1232 s = ((float) fontHeight) / (float) ofontH;
1234 .deriveFont(AffineTransform.getScaleInstance(s, s));
1236 fm = g.getFontMetrics();
1237 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1243 for (int gg = 0; gg < aa.length; gg++)
1245 if (aa[gg].graphGroup == aa[i].graphGroup)
1247 x = width - fm.stringWidth(aa[gg].label) - 3;
1248 g.drawString(aa[gg].label, x, y - graphExtras);
1250 if (aa[gg]._linecolour != null)
1253 g.setColor(aa[gg]._linecolour);
1254 g.drawLine(x, y - graphExtras + 3,
1255 x + fm.stringWidth(aa[gg].label),
1256 y - graphExtras + 3);
1259 g.setColor(Color.black);
1260 graphExtras += fontHeight + 8;
1264 g.setFont(baseFont);
1266 fontHeight = ofontH;
1272 g.drawLine(width - 3, y + offset - fontHeight, width - 3,
1273 (int) (y - 1.5 * aa[i].height - offset - fontHeight));
1274 // g.drawLine(20, y + offset, x - 20, y + offset);
1277 g.drawString(label, x, y + offset);
1279 lastSeqRef = aa[i].sequenceRef;
1283 if (!resizePanel && dragEvent != null && aa != null)
1285 g.setColor(Color.lightGray);
1287 (aa[selectedRow].sequenceRef == null ? ""
1288 : aa[selectedRow].sequenceRef.getName())
1289 + aa[selectedRow].label,
1290 dragEvent.getX(), dragEvent.getY() - getScrollOffset());
1293 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1295 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1296 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1301 public int getScrollOffset()
1303 return scrollOffset;
1307 public void mouseEntered(MouseEvent e)