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.util.Locale;
25 import jalview.analysis.AlignSeq;
26 import jalview.analysis.AlignmentUtils;
27 import jalview.datamodel.Alignment;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.HiddenColumns;
31 import jalview.datamodel.Sequence;
32 import jalview.datamodel.SequenceGroup;
33 import jalview.datamodel.SequenceI;
34 import jalview.io.FileFormat;
35 import jalview.io.FormatAdapter;
36 import jalview.util.Comparison;
37 import jalview.util.MessageManager;
38 import jalview.util.Platform;
40 import java.awt.Color;
41 import java.awt.Cursor;
42 import java.awt.Dimension;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.Graphics2D;
47 import java.awt.RenderingHints;
48 import java.awt.Toolkit;
49 import java.awt.datatransfer.StringSelection;
50 import java.awt.event.ActionEvent;
51 import java.awt.event.ActionListener;
52 import java.awt.event.MouseEvent;
53 import java.awt.event.MouseListener;
54 import java.awt.event.MouseMotionListener;
55 import java.awt.geom.AffineTransform;
56 import java.util.Arrays;
57 import java.util.Collections;
58 import java.util.Iterator;
60 import javax.swing.JCheckBoxMenuItem;
61 import javax.swing.JMenuItem;
62 import javax.swing.JPanel;
63 import javax.swing.JPopupMenu;
64 import javax.swing.SwingUtilities;
65 import javax.swing.ToolTipManager;
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,
302 annotation.label = dialog.getName();
303 String text = dialog.getDescription();
304 if (text != null && text.length() == 0)
308 annotation.description = text;
311 ap.av.getAlignment().addAnnotation(annotation);
312 ap.av.getAlignment().setAnnotationIndex(annotation, 0);
320 public void mousePressed(MouseEvent evt)
322 getSelectedRow(evt.getY() - getScrollOffset());
324 if (evt.isPopupTrigger())
331 * Build and show the Pop-up menu at the right-click mouse position
335 void showPopupMenu(MouseEvent evt)
338 final AlignmentAnnotation[] aa = ap.av.getAlignment()
339 .getAlignmentAnnotation();
341 JPopupMenu pop = new JPopupMenu(
342 MessageManager.getString("label.annotations"));
343 JMenuItem item = new JMenuItem(ADDNEW);
344 item.addActionListener(this);
349 { // let the user make everything visible again
350 item = new JMenuItem(SHOWALL);
351 item.addActionListener(this);
354 pop.show(this, evt.getX(), evt.getY());
357 item = new JMenuItem(EDITNAME);
358 item.addActionListener(this);
360 item = new JMenuItem(HIDE);
361 item.addActionListener(this);
363 // JAL-1264 hide all sequence-specific annotations of this type
364 if (selectedRow < aa.length)
366 if (aa[selectedRow].sequenceRef != null)
368 final String label = aa[selectedRow].label;
369 JMenuItem hideType = new JMenuItem();
370 String text = MessageManager.getString("label.hide_all") + " "
372 hideType.setText(text);
373 hideType.addActionListener(new ActionListener()
376 public void actionPerformed(ActionEvent e)
378 AlignmentUtils.showOrHideSequenceAnnotations(
379 ap.av.getAlignment(), Collections.singleton(label),
387 item = new JMenuItem(DELETE);
388 item.addActionListener(this);
392 item = new JMenuItem(SHOWALL);
393 item.addActionListener(this);
396 item = new JMenuItem(OUTPUT_TEXT);
397 item.addActionListener(this);
399 // TODO: annotation object should be typed for autocalculated/derived
401 if (selectedRow < aa.length)
403 final String label = aa[selectedRow].label;
404 if (!aa[selectedRow].autoCalculated)
406 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
408 // display formatting settings for this row.
410 // av and sequencegroup need to implement same interface for
411 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
412 aa[selectedRow].scaleColLabel);
413 item.addActionListener(this);
417 else if (label.indexOf("Consensus") > -1)
419 addConsensusMenuOptions(ap, aa[selectedRow], pop);
421 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
422 consclipbrd.addActionListener(this);
423 pop.add(consclipbrd);
426 pop.show(this, evt.getX(), evt.getY());
430 * A helper method that adds menu options for calculation and visualisation of
431 * group and/or alignment consensus annotation to a popup menu. This is
432 * designed to be reusable for either unwrapped mode (popup menu is shown on
433 * component AnnotationLabels), or wrapped mode (popup menu is shown on
434 * IdPanel when the mouse is over an annotation label).
440 static void addConsensusMenuOptions(AlignmentPanel ap,
441 AlignmentAnnotation ann,
446 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
447 MessageManager.getString("label.ignore_gaps_consensus"),
448 (ann.groupRef != null) ? ann.groupRef.getIgnoreGapsConsensus()
449 : ap.av.isIgnoreGapsConsensus());
450 final AlignmentAnnotation aaa = ann;
451 cbmi.addActionListener(new ActionListener()
454 public void actionPerformed(ActionEvent e)
456 if (aaa.groupRef != null)
458 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
459 ap.getAnnotationPanel()
460 .paint(ap.getAnnotationPanel().getGraphics());
464 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
466 ap.alignmentChanged();
471 if (aaa.groupRef != null)
474 * group consensus options
476 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
477 MessageManager.getString("label.show_group_histogram"),
478 ann.groupRef.isShowConsensusHistogram());
479 chist.addActionListener(new ActionListener()
482 public void actionPerformed(ActionEvent e)
484 aaa.groupRef.setShowConsensusHistogram(chist.getState());
489 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
490 MessageManager.getString("label.show_group_logo"),
491 ann.groupRef.isShowSequenceLogo());
492 cprofl.addActionListener(new ActionListener()
495 public void actionPerformed(ActionEvent e)
497 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
502 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
503 MessageManager.getString("label.normalise_group_logo"),
504 ann.groupRef.isNormaliseSequenceLogo());
505 cproflnorm.addActionListener(new ActionListener()
508 public void actionPerformed(ActionEvent e)
510 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
511 // automatically enable logo display if we're clicked
512 aaa.groupRef.setshowSequenceLogo(true);
521 * alignment consensus options
523 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
524 MessageManager.getString("label.show_histogram"),
525 ap.av.isShowConsensusHistogram());
526 chist.addActionListener(new ActionListener()
529 public void actionPerformed(ActionEvent e)
531 ap.av.setShowConsensusHistogram(chist.getState());
532 ap.alignFrame.setMenusForViewport();
537 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
538 MessageManager.getString("label.show_logo"),
539 ap.av.isShowSequenceLogo());
540 cprof.addActionListener(new ActionListener()
543 public void actionPerformed(ActionEvent e)
545 ap.av.setShowSequenceLogo(cprof.getState());
546 ap.alignFrame.setMenusForViewport();
551 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
552 MessageManager.getString("label.normalise_logo"),
553 ap.av.isNormaliseSequenceLogo());
554 cprofnorm.addActionListener(new ActionListener()
557 public void actionPerformed(ActionEvent e)
559 ap.av.setShowSequenceLogo(true);
560 ap.av.setNormaliseSequenceLogo(cprofnorm.getState());
561 ap.alignFrame.setMenusForViewport();
570 * Reorders annotation rows after a drag of a label
575 public void mouseReleased(MouseEvent evt)
577 if (evt.isPopupTrigger())
583 int start = selectedRow;
584 getSelectedRow(evt.getY() - getScrollOffset());
585 int end = selectedRow;
588 * if dragging to resize instead, start == end
592 // Swap these annotations
593 AlignmentAnnotation startAA = ap.av.getAlignment()
594 .getAlignmentAnnotation()[start];
597 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
599 AlignmentAnnotation endAA = ap.av.getAlignment()
600 .getAlignmentAnnotation()[end];
602 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
603 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
609 ap.getAnnotationPanel().repaint();
613 * Removes the height adjuster image on leaving the panel, unless currently
617 public void mouseExited(MouseEvent evt)
619 if (resizePanel && dragEvent == null)
627 * A mouse drag may be either an adjustment of the panel height (if flag
628 * resizePanel is set on), or a reordering of the annotation rows. The former
629 * is dealt with by this method, the latter in mouseReleased.
634 public void mouseDragged(MouseEvent evt)
640 Dimension d = ap.annotationScroller.getPreferredSize();
641 int dif = evt.getY() - oldY;
643 dif /= ap.av.getCharHeight();
644 dif *= ap.av.getCharHeight();
646 if ((d.height - dif) > 20)
648 ap.annotationScroller
649 .setPreferredSize(new Dimension(d.width, d.height - dif));
650 d = ap.annotationSpaceFillerHolder.getPreferredSize();
651 ap.annotationSpaceFillerHolder
652 .setPreferredSize(new Dimension(d.width, d.height - dif));
653 ap.paintAlignment(true, false);
665 * Updates the tooltip as the mouse moves over the labels
670 public void mouseMoved(MouseEvent evt)
672 showOrHideAdjuster(evt);
674 getSelectedRow(evt.getY() - getScrollOffset());
676 if (selectedRow > -1 && ap.av.getAlignment()
677 .getAlignmentAnnotation().length > selectedRow)
679 AlignmentAnnotation[] anns = ap.av.getAlignment()
680 .getAlignmentAnnotation();
681 AlignmentAnnotation aa = anns[selectedRow];
683 String desc = getTooltip(aa);
684 this.setToolTipText(desc);
685 String msg = getStatusMessage(aa, anns);
686 ap.alignFrame.setStatus(msg);
691 * Constructs suitable text to show in the status bar when over an annotation
692 * label, containing the associated sequence name (if any), and the annotation
693 * labels (or all labels for a graph group annotation)
699 static String getStatusMessage(AlignmentAnnotation aa,
700 AlignmentAnnotation[] anns)
707 StringBuilder msg = new StringBuilder(32);
708 if (aa.sequenceRef != null)
710 msg.append(aa.sequenceRef.getName()).append(" : ");
713 if (aa.graphGroup == -1)
715 msg.append(aa.label);
717 else if (anns != null)
719 boolean first = true;
720 for (int i = anns.length - 1; i >= 0; i--)
722 if (anns[i].graphGroup == aa.graphGroup)
728 msg.append(anns[i].label);
734 return msg.toString();
738 * Answers a tooltip, formatted as html, containing the annotation description
739 * (prefixed by associated sequence id if applicable), and the annotation
740 * (non-positional) score if it has one. Answers null if neither description
741 * nor score is found.
746 static String getTooltip(AlignmentAnnotation aa)
752 StringBuilder tooltip = new StringBuilder();
753 if (aa.description != null && !aa.description.equals("New description"))
755 // TODO: we could refactor and merge this code with the code in
756 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
758 String desc = aa.getDescription(true).trim();
759 if (!desc.toLowerCase(Locale.ROOT).startsWith(HTML_START_TAG))
761 tooltip.append(HTML_START_TAG);
762 desc = desc.replace("<", "<");
764 else if (desc.toLowerCase(Locale.ROOT).endsWith(HTML_END_TAG))
766 desc = desc.substring(0, desc.length() - HTML_END_TAG.length());
768 tooltip.append(desc);
772 // begin the tooltip's html fragment
773 tooltip.append(HTML_START_TAG);
777 if (tooltip.length() > HTML_START_TAG.length())
779 tooltip.append("<br/>");
781 // TODO: limit precision of score to avoid noise from imprecise
783 // (64.7 becomes 64.7+/some tiny value).
784 tooltip.append(" Score: ").append(String.valueOf(aa.score));
787 if (tooltip.length() > HTML_START_TAG.length())
789 return tooltip.append(HTML_END_TAG).toString();
793 * nothing in the tooltip (except "<html>")
799 * Shows the height adjuster image if the mouse moves into the top left
800 * region, or hides it if the mouse leaves the regio
804 protected void showOrHideAdjuster(MouseEvent evt)
806 boolean was = resizePanel;
807 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
808 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
810 if (resizePanel != was)
813 .getPredefinedCursor(resizePanel ? Cursor.S_RESIZE_CURSOR
814 : Cursor.DEFAULT_CURSOR));
820 public void mouseClicked(MouseEvent evt)
822 final AlignmentAnnotation[] aa = ap.av.getAlignment()
823 .getAlignmentAnnotation();
824 if (!evt.isPopupTrigger() && SwingUtilities.isLeftMouseButton(evt))
826 if (selectedRow > -1 && selectedRow < aa.length)
828 if (aa[selectedRow].groupRef != null)
830 if (evt.getClickCount() >= 2)
832 // todo: make the ap scroll to the selection - not necessary, first
833 // click highlights/scrolls, second selects
834 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
836 SequenceGroup sg = ap.av.getSelectionGroup();
837 if (sg == null || sg == aa[selectedRow].groupRef
838 || !(Platform.isControlDown(evt) || evt.isShiftDown()))
840 if (Platform.isControlDown(evt) || evt.isShiftDown())
842 // clone a new selection group from the associated group
843 ap.av.setSelectionGroup(
844 new SequenceGroup(aa[selectedRow].groupRef));
848 // set selection to the associated group so it can be edited
849 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
854 // modify current selection with associated group
855 int remainToAdd = aa[selectedRow].groupRef.getSize();
856 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
858 if (jalview.util.Platform.isControlDown(evt))
860 sg.addOrRemove(sgs, --remainToAdd == 0);
864 // notionally, we should also add intermediate sequences from
865 // last added sequence ?
866 sg.addSequence(sgs, --remainToAdd == 0);
871 ap.paintAlignment(false, false);
872 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
873 ap.av.sendSelection();
877 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(
878 aa[selectedRow].groupRef.getSequences(null));
882 else if (aa[selectedRow].sequenceRef != null)
884 if (evt.getClickCount() == 1)
886 ap.getSeqPanel().ap.getIdPanel()
887 .highlightSearchResults(Arrays.asList(new SequenceI[]
888 { aa[selectedRow].sequenceRef }));
890 else if (evt.getClickCount() >= 2)
892 ap.getSeqPanel().ap.getIdPanel().highlightSearchResults(null);
893 SequenceGroup sg = ap.av.getSelectionGroup();
896 // we make a copy rather than edit the current selection if no
898 // see Enhancement JAL-1557
899 if (!(Platform.isControlDown(evt) || evt.isShiftDown()))
901 sg = new SequenceGroup(sg);
903 sg.addSequence(aa[selectedRow].sequenceRef, false);
907 if (Platform.isControlDown(evt))
909 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
913 // notionally, we should also add intermediate sequences from
914 // last added sequence ?
915 sg.addSequence(aa[selectedRow].sequenceRef, true);
921 sg = new SequenceGroup();
923 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
924 sg.addSequence(aa[selectedRow].sequenceRef, false);
926 ap.av.setSelectionGroup(sg);
927 ap.paintAlignment(false, false);
928 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
929 ap.av.sendSelection();
939 * do a single sequence copy to jalview and the system clipboard
942 * sequence to be copied to clipboard
944 protected void copy_annotseqtoclipboard(SequenceI sq)
946 SequenceI[] seqs = new SequenceI[] { sq };
947 String[] omitHidden = null;
948 SequenceI[] dseqs = new SequenceI[] { sq.getDatasetSequence() };
949 if (dseqs[0] == null)
951 dseqs[0] = new Sequence(sq);
952 dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars,
953 sq.getSequenceAsString()));
955 sq.setDatasetSequence(dseqs[0]);
957 Alignment ds = new Alignment(dseqs);
958 if (av.hasHiddenColumns())
960 Iterator<int[]> it = av.getAlignment().getHiddenColumns()
961 .getVisContigsIterator(0, sq.getLength(), false);
962 omitHidden = new String[] { sq.getSequenceStringFromIterator(it) };
965 int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 };
966 if (av.hasHiddenColumns())
968 alignmentStartEnd = av.getAlignment().getHiddenColumns()
969 .getVisibleStartAndEndIndex(av.getAlignment().getWidth());
972 String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
973 seqs, omitHidden, alignmentStartEnd);
975 Toolkit.getDefaultToolkit().getSystemClipboard()
976 .setContents(new StringSelection(output), Desktop.instance);
978 HiddenColumns hiddenColumns = null;
980 if (av.hasHiddenColumns())
982 hiddenColumns = new HiddenColumns(
983 av.getAlignment().getHiddenColumns());
986 Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset
988 // sequence ? need to
990 // sequence as special.
1001 public void paintComponent(Graphics g)
1004 int width = getWidth();
1007 width = ap.calculateIdWidth().width;
1010 Graphics2D g2 = (Graphics2D) g;
1013 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1014 RenderingHints.VALUE_ANTIALIAS_ON);
1017 drawComponent(g2, true, width);
1022 * Draw the full set of annotation Labels for the alignment at the given
1026 * Graphics2D instance (needed for font scaling)
1028 * Width for scaling labels
1031 public void drawComponent(Graphics g, int width)
1033 drawComponent(g, false, width);
1037 * Draw the full set of annotation Labels for the alignment at the given
1041 * Graphics2D instance (needed for font scaling)
1043 * - true indicates that only current visible area needs to be
1046 * Width for scaling labels
1048 public void drawComponent(Graphics g, boolean clip, int width)
1050 if (av.getFont().getSize() < 10)
1056 g.setFont(av.getFont());
1059 FontMetrics fm = g.getFontMetrics(g.getFont());
1060 g.setColor(Color.white);
1061 g.fillRect(0, 0, getWidth(), getHeight());
1063 g.translate(0, getScrollOffset());
1064 g.setColor(Color.black);
1066 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1067 int fontHeight = g.getFont().getSize();
1070 int graphExtras = 0;
1072 Font baseFont = g.getFont();
1073 FontMetrics baseMetrics = fm;
1074 int ofontH = fontHeight;
1077 int[] visr = (ap != null && ap.getAnnotationPanel() != null)
1078 ? ap.getAnnotationPanel().getVisibleVRange()
1080 if (clip && visr != null)
1083 visHeight = visr[1];
1085 boolean visible = true, before = false, after = false;
1088 hasHiddenRows = false;
1090 for (int i = 0; i < aa.length; i++)
1095 hasHiddenRows = true;
1108 System.out.println("before vis: " + i);
1112 // don't draw what isn't visible
1115 if (olY > visHeight)
1123 "Scroll offset: " + sOffset + " after vis: " + i);
1127 // don't draw what isn't visible
1131 g.setColor(Color.black);
1133 offset = -aa[i].height / 2;
1137 offset += fm.getHeight() / 2;
1138 offset -= fm.getDescent();
1142 offset += fm.getDescent();
1145 x = width - fm.stringWidth(aa[i].label) - 3;
1147 if (aa[i].graphGroup > -1)
1150 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1151 // computed efficiently for all visible labels
1152 for (int gg = 0; gg < aa.length; gg++)
1154 if (aa[gg].graphGroup == aa[i].graphGroup)
1159 if (groupSize * (fontHeight + 8) < aa[i].height)
1161 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1166 // scale font to fit
1167 float h = aa[i].height / (float) groupSize, s;
1174 fontHeight = -8 + (int) h;
1175 s = ((float) fontHeight) / (float) ofontH;
1177 .deriveFont(AffineTransform.getScaleInstance(s, s));
1179 fm = g.getFontMetrics();
1180 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
1186 for (int gg = 0; gg < aa.length; gg++)
1188 if (aa[gg].graphGroup == aa[i].graphGroup)
1190 x = width - fm.stringWidth(aa[gg].label) - 3;
1191 g.drawString(aa[gg].label, x, y - graphExtras);
1193 if (aa[gg]._linecolour != null)
1196 g.setColor(aa[gg]._linecolour);
1197 g.drawLine(x, y - graphExtras + 3,
1198 x + fm.stringWidth(aa[gg].label),
1199 y - graphExtras + 3);
1202 g.setColor(Color.black);
1203 graphExtras += fontHeight + 8;
1207 g.setFont(baseFont);
1209 fontHeight = ofontH;
1213 g.drawString(aa[i].label, x, y + offset);
1218 if (!resizePanel && dragEvent != null && aa != null)
1220 g.setColor(Color.lightGray);
1221 g.drawString(aa[selectedRow].label, dragEvent.getX(),
1222 dragEvent.getY() - getScrollOffset());
1225 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
1227 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
1228 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,
1233 public int getScrollOffset()
1235 return scrollOffset;
1239 public void mouseEntered(MouseEvent e)