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;
67 * The panel that holds the labels for alignment annotations, providing
68 * tooltips, context menus, drag to reorder rows, and drag to adjust panel
71 public class AnnotationLabels extends JPanel
72 implements MouseListener, MouseMotionListener, ActionListener
74 private static final String HTML_END_TAG = "</html>";
76 private static final String HTML_START_TAG = "<html>";
79 * width in pixels within which height adjuster arrows are shown and active
81 private static final int HEIGHT_ADJUSTER_WIDTH = 50;
84 * height in pixels for allowing height adjuster to be active
86 private static int HEIGHT_ADJUSTER_HEIGHT = 10;
88 private static final Font font = new Font("Arial", Font.PLAIN, 11);
90 private static final String TOGGLE_LABELSCALE = MessageManager
91 .getString("label.scale_label_to_column");
93 private static final String ADDNEW = MessageManager
94 .getString("label.add_new_row");
96 private static final String EDITNAME = MessageManager
97 .getString("label.edit_label_description");
99 private static final String HIDE = MessageManager
100 .getString("label.hide_row");
102 private static final String DELETE = MessageManager
103 .getString("label.delete_row");
105 private static final String SHOWALL = MessageManager
106 .getString("label.show_all_hidden_rows");
108 private static final String OUTPUT_TEXT = MessageManager
109 .getString("label.export_annotation");
111 private static final String COPYCONS_SEQ = MessageManager
112 .getString("label.copy_consensus_sequence");
114 private final boolean debugRedraw = false;
116 private AlignmentPanel ap;
120 private MouseEvent dragEvent;
124 private int selectedRow;
126 private int scrollOffset = 0;
128 private boolean hasHiddenRows;
130 private boolean resizePanel = false;
133 * Creates a new AnnotationLabels object
137 public AnnotationLabels(AlignmentPanel ap)
142 ToolTipManager.sharedInstance().registerComponent(this);
144 addMouseListener(this);
145 addMouseMotionListener(this);
146 addMouseWheelListener(ap.getAnnotationPanel());
149 public AnnotationLabels(AlignViewport av)
160 public void setScrollOffset(int y)
167 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
171 * coordinate position to search for a row
173 void getSelectedRow(int y)
176 AlignmentAnnotation[] aa = ap.av.getAlignment()
177 .getAlignmentAnnotation();
181 for (int i = 0; i < aa.length; i++)
189 height += aa[i].height;
208 public void actionPerformed(ActionEvent evt)
210 AlignmentAnnotation[] aa = ap.av.getAlignment()
211 .getAlignmentAnnotation();
213 String action = evt.getActionCommand();
214 if (ADDNEW.equals(action))
217 * non-returning dialog
219 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
220 null, new Annotation[ap.av.getAlignment().getWidth()]);
221 editLabelDescription(newAnnotation, true);
223 else if (EDITNAME.equals(action))
226 * non-returning dialog
228 editLabelDescription(aa[selectedRow], false);
230 else if (HIDE.equals(action))
232 aa[selectedRow].visible = false;
234 else if (DELETE.equals(action))
236 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
237 ap.av.getCalcManager().removeWorkerForAnnotation(aa[selectedRow]);
239 else if (SHOWALL.equals(action))
241 for (int i = 0; i < aa.length; i++)
243 if (!aa[i].visible && aa[i].annotations != null)
245 aa[i].visible = true;
249 else if (OUTPUT_TEXT.equals(action))
251 new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]);
253 else if (COPYCONS_SEQ.equals(action))
255 SequenceI cons = null;
256 if (aa[selectedRow].groupRef != null)
258 cons = aa[selectedRow].groupRef.getConsensusSeq();
262 cons = av.getConsensusSeq();
266 copy_annotseqtoclipboard(cons);
269 else if (TOGGLE_LABELSCALE.equals(action))
271 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
278 * Shows a dialog where the annotation name and description may be edited. If
279 * parameter addNew is true, then on confirmation, a new AlignmentAnnotation
280 * is added, else an existing annotation is updated.
285 void editLabelDescription(AlignmentAnnotation annotation, boolean addNew)
287 String name = MessageManager.getString("label.annotation_name");
288 String description = MessageManager
289 .getString("label.annotation_description");
290 String title = MessageManager
291 .getString("label.edit_annotation_name_description");
292 EditNameDialog dialog = new EditNameDialog(annotation.label,
293 annotation.description, name, description);
295 dialog.showDialog(ap.alignFrame, title, () -> {
296 annotation.label = dialog.getName();
297 String text = dialog.getDescription();
298 if (text != null && text.length() == 0)
302 annotation.description = text;
305 ap.av.getAlignment().addAnnotation(annotation);
306 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);
420 pop.show(this, evt.getX(), evt.getY());
424 * A helper method that adds menu options for calculation and visualisation of
425 * group and/or alignment consensus annotation to a popup menu. This is
426 * designed to be reusable for either unwrapped mode (popup menu is shown on
427 * component AnnotationLabels), or wrapped mode (popup menu is shown on
428 * IdPanel when the mouse is over an annotation label).
434 static void addConsensusMenuOptions(AlignmentPanel ap,
435 AlignmentAnnotation ann, JPopupMenu pop)
439 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
440 MessageManager.getString("label.ignore_gaps_consensus"),
441 (ann.groupRef != null) ? ann.groupRef.getIgnoreGapsConsensus()
442 : ap.av.isIgnoreGapsConsensus());
443 final AlignmentAnnotation aaa = ann;
444 cbmi.addActionListener(new ActionListener()
447 public void actionPerformed(ActionEvent e)
449 if (aaa.groupRef != null)
451 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
452 ap.getAnnotationPanel()
453 .paint(ap.getAnnotationPanel().getGraphics());
457 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
459 ap.alignmentChanged();
464 if (aaa.groupRef != null)
467 * group consensus options
469 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
470 MessageManager.getString("label.show_group_histogram"),
471 ann.groupRef.isShowConsensusHistogram());
472 chist.addActionListener(new ActionListener()
475 public void actionPerformed(ActionEvent e)
477 aaa.groupRef.setShowConsensusHistogram(chist.getState());
482 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
483 MessageManager.getString("label.show_group_logo"),
484 ann.groupRef.isShowSequenceLogo());
485 cprofl.addActionListener(new ActionListener()
488 public void actionPerformed(ActionEvent e)
490 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
495 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
496 MessageManager.getString("label.normalise_group_logo"),
497 ann.groupRef.isNormaliseSequenceLogo());
498 cproflnorm.addActionListener(new ActionListener()
501 public void actionPerformed(ActionEvent e)
503 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
504 // automatically enable logo display if we're clicked
505 aaa.groupRef.setshowSequenceLogo(true);
514 * alignment consensus options
516 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
517 MessageManager.getString("label.show_histogram"),
518 ap.av.isShowConsensusHistogram());
519 chist.addActionListener(new ActionListener()
522 public void actionPerformed(ActionEvent e)
524 ap.av.setShowConsensusHistogram(chist.getState());
525 ap.alignFrame.setMenusForViewport();
530 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
531 MessageManager.getString("label.show_logo"),
532 ap.av.isShowSequenceLogo());
533 cprof.addActionListener(new ActionListener()
536 public void actionPerformed(ActionEvent e)
538 ap.av.setShowSequenceLogo(cprof.getState());
539 ap.alignFrame.setMenusForViewport();
544 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
545 MessageManager.getString("label.normalise_logo"),
546 ap.av.isNormaliseSequenceLogo());
547 cprofnorm.addActionListener(new ActionListener()
550 public void actionPerformed(ActionEvent e)
552 ap.av.setShowSequenceLogo(true);
553 ap.av.setNormaliseSequenceLogo(cprofnorm.getState());
554 ap.alignFrame.setMenusForViewport();
563 * Reorders annotation rows after a drag of a label
568 public void mouseReleased(MouseEvent evt)
570 if (evt.isPopupTrigger())
576 int start = selectedRow;
577 getSelectedRow(evt.getY() - getScrollOffset());
578 int end = selectedRow;
581 * if dragging to resize instead, start == end
585 // Swap these annotations
586 AlignmentAnnotation startAA = ap.av.getAlignment()
587 .getAlignmentAnnotation()[start];
590 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
592 AlignmentAnnotation endAA = ap.av.getAlignment()
593 .getAlignmentAnnotation()[end];
595 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
596 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
602 ap.getAnnotationPanel().repaint();
606 * Removes the height adjuster image on leaving the panel, unless currently
610 public void mouseExited(MouseEvent evt)
612 if (resizePanel && dragEvent == null)
620 * A mouse drag may be either an adjustment of the panel height (if flag
621 * resizePanel is set on), or a reordering of the annotation rows. The former
622 * is dealt with by this method, the latter in mouseReleased.
627 public void mouseDragged(MouseEvent evt)
633 Dimension d = ap.annotationScroller.getPreferredSize();
634 int dif = evt.getY() - oldY;
636 dif /= ap.av.getCharHeight();
637 dif *= ap.av.getCharHeight();
639 if ((d.height - dif) > 20)
641 ap.annotationScroller
642 .setPreferredSize(new Dimension(d.width, d.height - dif));
643 d = ap.annotationSpaceFillerHolder.getPreferredSize();
644 ap.annotationSpaceFillerHolder
645 .setPreferredSize(new Dimension(d.width, d.height - dif));
646 ap.paintAlignment(true, false);
658 * Updates the tooltip as the mouse moves over the labels
663 public void mouseMoved(MouseEvent evt)
665 showOrHideAdjuster(evt);
667 getSelectedRow(evt.getY() - getScrollOffset());
669 if (selectedRow > -1 && ap.av.getAlignment()
670 .getAlignmentAnnotation().length > selectedRow)
672 AlignmentAnnotation[] anns = ap.av.getAlignment()
673 .getAlignmentAnnotation();
674 AlignmentAnnotation aa = anns[selectedRow];
676 String desc = getTooltip(aa);
677 this.setToolTipText(desc);
678 String msg = getStatusMessage(aa, anns);
679 ap.alignFrame.setStatus(msg);
684 * Constructs suitable text to show in the status bar when over an annotation
685 * label, containing the associated sequence name (if any), and the annotation
686 * labels (or all labels for a graph group annotation)
692 static String getStatusMessage(AlignmentAnnotation aa,
693 AlignmentAnnotation[] anns)
700 StringBuilder msg = new StringBuilder(32);
701 if (aa.sequenceRef != null)
703 msg.append(aa.sequenceRef.getName()).append(" : ");
706 if (aa.graphGroup == -1)
708 msg.append(aa.label);
710 else if (anns != null)
712 boolean first = true;
713 for (int i = anns.length - 1; i >= 0; i--)
715 if (anns[i].graphGroup == aa.graphGroup)
721 msg.append(anns[i].label);
727 return msg.toString();
731 * Answers a tooltip, formatted as html, containing the annotation description
732 * (prefixed by associated sequence id if applicable), and the annotation
733 * (non-positional) score if it has one. Answers null if neither description
734 * nor score is found.
739 static String getTooltip(AlignmentAnnotation aa)
745 StringBuilder tooltip = new StringBuilder();
746 if (aa.description != null && !aa.description.equals("New description"))
748 // TODO: we could refactor and merge this code with the code in
749 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
751 String desc = aa.getDescription(true).trim();
752 if (!desc.toLowerCase(Locale.ROOT).startsWith(HTML_START_TAG))
754 tooltip.append(HTML_START_TAG);
755 desc = desc.replace("<", "<");
757 else if (desc.toLowerCase(Locale.ROOT).endsWith(HTML_END_TAG))
759 desc = desc.substring(0, desc.length() - HTML_END_TAG.length());
761 tooltip.append(desc);
765 // begin the tooltip's html fragment
766 tooltip.append(HTML_START_TAG);
770 if (tooltip.length() > HTML_START_TAG.length())
772 tooltip.append("<br/>");
774 // TODO: limit precision of score to avoid noise from imprecise
776 // (64.7 becomes 64.7+/some tiny value).
777 tooltip.append(" Score: ").append(String.valueOf(aa.score));
780 if (tooltip.length() > HTML_START_TAG.length())
782 return tooltip.append(HTML_END_TAG).toString();
786 * nothing in the tooltip (except "<html>")
792 * Shows the height adjuster image if the mouse moves into the top left
793 * region, or hides it if the mouse leaves the regio
797 protected void showOrHideAdjuster(MouseEvent evt)
799 boolean was = resizePanel;
800 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
801 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
803 if (resizePanel != was)
806 .getPredefinedCursor(resizePanel ? Cursor.S_RESIZE_CURSOR
807 : Cursor.DEFAULT_CURSOR));
813 public void mouseClicked(MouseEvent evt)
815 final AlignmentAnnotation[] aa = ap.av.getAlignment()
816 .getAlignmentAnnotation();
817 if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
819 if (selectedRow > -1 && selectedRow < aa.length)
821 if (aa[selectedRow].groupRef != null)
823 if (evt.getClickCount() >= 2)
825 // todo: make the ap scroll to the selection - not necessary, first
826 // click highlights/scrolls, second selects
827 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
829 SequenceGroup sg = ap.av.getSelectionGroup();
830 if (sg == null || sg == aa[selectedRow].groupRef
831 || !(Platform.isControlDown(evt) || evt.isShiftDown()))
833 if (Platform.isControlDown(evt) || evt.isShiftDown())
835 // clone a new selection group from the associated group
836 ap.av.setSelectionGroup(
837 new SequenceGroup(aa[selectedRow].groupRef));
841 // set selection to the associated group so it can be edited
842 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
847 // modify current selection with associated group
848 int remainToAdd = aa[selectedRow].groupRef.getSize();
849 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
851 if (jalview.util.Platform.isControlDown(evt))
853 sg.addOrRemove(sgs, --remainToAdd == 0);
857 // notionally, we should also add intermediate sequences from
858 // last added sequence ?
859 sg.addSequence(sgs, --remainToAdd == 0);
864 ap.paintAlignment(false, false);
865 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
866 ap.av.sendSelection();
870 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
871 aa[selectedRow].groupRef.getSequences(null));
875 else if (aa[selectedRow].sequenceRef != null)
877 if (evt.getClickCount() == 1)
879 ap.getSeqPanel().ap.getIdPanel()
880 .highlightSearchResults(Arrays.asList(new SequenceI[]
881 { aa[selectedRow].sequenceRef }));
883 else if (evt.getClickCount() >= 2)
885 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
886 SequenceGroup sg = ap.av.getSelectionGroup();
889 // we make a copy rather than edit the current selection if no
891 // see Enhancement JAL-1557
892 if (!(Platform.isControlDown(evt) || evt.isShiftDown()))
894 sg = new SequenceGroup(sg);
896 sg.addSequence(aa[selectedRow].sequenceRef, false);
900 if (Platform.isControlDown(evt))
902 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
906 // notionally, we should also add intermediate sequences from
907 // last added sequence ?
908 sg.addSequence(aa[selectedRow].sequenceRef, true);
914 sg = new SequenceGroup();
916 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
917 sg.addSequence(aa[selectedRow].sequenceRef, false);
919 ap.av.setSelectionGroup(sg);
920 ap.paintAlignment(false, false);
921 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
922 ap.av.sendSelection();
932 * do a single sequence copy to jalview and the system clipboard
935 * sequence to be copied to clipboard
937 protected void copy_annotseqtoclipboard(SequenceI sq)
939 SequenceI[] seqs = new SequenceI[] { sq };
940 String[] omitHidden = null;
941 SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
942 if (dseqs[0] == null)
944 dseqs[0] = new Sequence(sq);
945 dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars,
946 sq.getSequenceAsString()));
948 sq.setDatasetSequence(dseqs[0]);
950 Alignment ds = new Alignment(dseqs);
951 if (av.hasHiddenColumns())
953 Iterator<int[]> it = av.getAlignment().getHiddenColumns()
954 .getVisContigsIterator(0, sq.getLength(), false);
955 omitHidden = new String[] { sq.getSequenceStringFromIterator(it) };
958 int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
959 if (av.hasHiddenColumns())
961 alignmentStartEnd = av.getAlignment().getHiddenColumns()
962 .getVisibleStartAndEndIndex(av.getAlignment().getWidth());
965 String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
966 seqs, omitHidden, alignmentStartEnd);
968 Toolkit.getDefaultToolkit().getSystemClipboard()
969 .setContents(new StringSelection(output), Desktop.instance);
971 HiddenColumns hiddenColumns = null;
973 if (av.hasHiddenColumns())
975 hiddenColumns = new HiddenColumns(
976 av.getAlignment().getHiddenColumns());
979 Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
981 // sequence ? need to
983 // sequence as special.
994 public void paintComponent(Graphics g)
997 int width = getWidth();
1000 width = ap.calculateIdWidth().width;
1003 Graphics2D g2 = (Graphics2D) g;
1006 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1007 RenderingHints.VALUE_ANTIALIAS_ON);
1010 drawComponent(g2, true, width);
1015 * Draw the full set of annotation Labels for the alignment at the given
1019 * Graphics2D instance (needed for font scaling)
1021 * Width for scaling labels
1024 public void drawComponent(Graphics g, int width)
1026 drawComponent(g, false, width);
1030 * Draw the full set of annotation Labels for the alignment at the given
1034 * Graphics2D instance (needed for font scaling)
1036 * - true indicates that only current visible area needs to be
1039 * Width for scaling labels
1041 public void drawComponent(Graphics g, boolean clip, int width)
1043 if (av.getFont().getSize() < 10)
1049 g.setFont(av.getFont());
1052 FontMetrics fm = g.getFontMetrics(g.getFont());
1053 g.setColor(Color.white);
1054 g.fillRect(0, 0, getWidth(), getHeight());
1056 g.translate(0, getScrollOffset());
1057 g.setColor(Color.black);
1058 SequenceI lastSeqRef = null;
1059 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1060 int fontHeight = g.getFont().getSize();
1063 int graphExtras = 0;
1065 Font baseFont = g.getFont();
1066 FontMetrics baseMetrics = fm;
1067 int ofontH = fontHeight;
1070 int[] visr = (ap != null && ap.getAnnotationPanel() != null)
1071 ? ap.getAnnotationPanel().getVisibleVRange()
1073 if (clip && visr != null)
1076 visHeight = visr[1];
1078 boolean visible = true, before = false, after = false;
1081 hasHiddenRows = false;
1083 for (int i = 0; i < aa.length; i++)
1088 hasHiddenRows = true;
1101 System.out.println("before vis: " + i);
1105 // don't draw what isn't visible
1108 if (olY > visHeight)
1116 "Scroll offset: " + sOffset + " after vis: " + i);
1120 // don't draw what isn't visible
1124 g.setColor(Color.black);
1126 offset = -aa[i].height / 2;
1130 offset += fm.getHeight() / 2;
1131 offset -= fm.getDescent();
1135 offset += fm.getDescent();
1137 String label = aa[i].label;
1138 boolean vertBar = false;
1139 if (aa[i].sequenceRef != null)
1141 if (aa[i].sequenceRef != lastSeqRef)
1143 label = aa[i].sequenceRef.getName() + " " + label;
1144 // TODO record relationship between sequence and this annotation and
1152 x = width - fm.stringWidth(label) - 3;
1154 if (aa[i].graphGroup > -1)
1157 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1158 // computed efficiently for all visible labels
1159 for (int gg = 0; gg < aa.length; gg++)
1161 if (aa[gg].graphGroup == aa[i].graphGroup)
1166 if (groupSize * (fontHeight + 8) < aa[i].height)
1168 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1173 // scale font to fit
1174 float h = aa[i].height / (float) groupSize, s;
1181 fontHeight = -8 + (int) h;
1182 s = ((float) fontHeight) / (float) ofontH;
1184 .deriveFont(AffineTransform.getScaleInstance(s, s));
1186 fm = g.getFontMetrics();
1187 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1193 for (int gg = 0; gg < aa.length; gg++)
1195 if (aa[gg].graphGroup == aa[i].graphGroup)
1197 x = width - fm.stringWidth(aa[gg].label) - 3;
1198 g.drawString(aa[gg].label, x, y - graphExtras);
1200 if (aa[gg]._linecolour != null)
1203 g.setColor(aa[gg]._linecolour);
1204 g.drawLine(x, y - graphExtras + 3,
1205 x + fm.stringWidth(aa[gg].label),
1206 y - graphExtras + 3);
1209 g.setColor(Color.black);
1210 graphExtras += fontHeight + 8;
1214 g.setFont(baseFont);
1216 fontHeight = ofontH;
1222 g.drawLine(20, y + offset, 20, y - aa[i].height);
1223 g.drawLine(20, y + offset, x - 20, y + offset);
1226 g.drawString(label, x, y + offset);
1228 lastSeqRef = aa[i].sequenceRef;
1232 if (!resizePanel && dragEvent != null && aa != null)
1234 g.setColor(Color.lightGray);
1236 (aa[selectedRow].sequenceRef == null ? ""
1237 : aa[selectedRow].sequenceRef.getName())
1238 + aa[selectedRow].label,
1239 dragEvent.getX(), dragEvent.getY() - getScrollOffset());
1242 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1244 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1245 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1250 public int getScrollOffset()
1252 return scrollOffset;
1256 public void mouseEntered(MouseEvent e)