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 jalview.analysis.AlignSeq;
24 import jalview.analysis.AlignmentUtils;
25 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.HiddenColumns;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.FileFormat;
34 import jalview.io.FormatAdapter;
35 import jalview.util.Comparison;
36 import jalview.util.MessageManager;
37 import jalview.util.Platform;
38 import jalview.viewmodel.AlignmentViewport.AutoAnnotation;
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)
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 boolean fullRepaint = false;
214 if (evt.getActionCommand().equals(ADDNEW))
216 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
217 null, new Annotation[ap.av.getAlignment().getWidth()]);
219 if (!editLabelDescription(newAnnotation))
224 ap.av.getAlignment().addAnnotation(newAnnotation);
225 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
228 else if (evt.getActionCommand().equals(EDITNAME))
230 String name = aa[selectedRow].label;
231 editLabelDescription(aa[selectedRow]);
232 if (!name.equalsIgnoreCase(aa[selectedRow].label))
237 else if (evt.getActionCommand().equals(HIDE))
239 aa[selectedRow].visible = false;
241 else if (evt.getActionCommand().equals(DELETE))
243 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
244 ap.av.getCalcManager().removeWorkerForAnnotation(aa[selectedRow]);
247 else if (evt.getActionCommand().equals(SHOWALL))
249 for (int i = 0; i < aa.length; i++)
251 if (!aa[i].visible && aa[i].annotations != null)
253 aa[i].visible = true;
258 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
260 new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]);
262 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
264 SequenceI cons = null;
265 if (aa[selectedRow].groupRef != null)
267 cons = aa[selectedRow].groupRef.getConsensusSeq();
271 cons = av.getConsensusSeq();
275 copy_annotseqtoclipboard(cons);
279 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
281 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
284 ap.refresh(fullRepaint);
294 boolean editLabelDescription(AlignmentAnnotation annotation)
297 EditNameDialog dialog = new EditNameDialog(annotation.label,
298 annotation.description, " Annotation Name ",
299 "Annotation Description ", "Edit Annotation Name/Description",
307 annotation.label = dialog.getName();
309 String text = dialog.getDescription();
310 if (text != null && text.length() == 0)
314 annotation.description = text;
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(AutoAnnotation.CONSENSUS.label) > -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;
604 av.setSortAnnotationsBy(SequenceAnnotationOrder.CUSTOM);
610 ap.getAnnotationPanel().repaint();
614 * Removes the height adjuster image on leaving the panel, unless currently
618 public void mouseExited(MouseEvent evt)
620 if (resizePanel && dragEvent == null)
628 * A mouse drag may be either an adjustment of the panel height (if flag
629 * resizePanel is set on), or a reordering of the annotation rows. The former
630 * is dealt with by this method, the latter in mouseReleased.
635 public void mouseDragged(MouseEvent evt)
641 Dimension d = ap.annotationScroller.getPreferredSize();
642 int dif = evt.getY() - oldY;
644 dif /= ap.av.getCharHeight();
645 dif *= ap.av.getCharHeight();
647 if ((d.height - dif) > 20)
649 ap.annotationScroller
650 .setPreferredSize(new Dimension(d.width, d.height - dif));
651 d = ap.annotationSpaceFillerHolder.getPreferredSize();
652 ap.annotationSpaceFillerHolder
653 .setPreferredSize(new Dimension(d.width, d.height - dif));
654 ap.paintAlignment(true, false);
666 * Updates the tooltip as the mouse moves over the labels
671 public void mouseMoved(MouseEvent evt)
673 showOrHideAdjuster(evt);
675 getSelectedRow(evt.getY() - getScrollOffset());
677 if (selectedRow > -1 && ap.av.getAlignment()
678 .getAlignmentAnnotation().length > selectedRow)
680 AlignmentAnnotation[] anns = ap.av.getAlignment()
681 .getAlignmentAnnotation();
682 AlignmentAnnotation aa = anns[selectedRow];
684 String desc = getTooltip(aa);
685 this.setToolTipText(desc);
686 String msg = getStatusMessage(aa, anns);
687 ap.alignFrame.setStatus(msg);
692 * Constructs suitable text to show in the status bar when over an annotation
693 * label, containing the associated sequence name (if any), and the annotation
694 * labels (or all labels for a graph group annotation)
700 static String getStatusMessage(AlignmentAnnotation aa,
701 AlignmentAnnotation[] anns)
708 StringBuilder msg = new StringBuilder(32);
709 if (aa.sequenceRef != null)
711 msg.append(aa.sequenceRef.getName()).append(" : ");
714 if (aa.graphGroup == -1)
716 msg.append(aa.label);
718 else if (anns != null)
720 boolean first = true;
721 for (int i = anns.length - 1; i >= 0; i--)
723 if (anns[i].graphGroup == aa.graphGroup)
729 msg.append(anns[i].label);
735 return msg.toString();
739 * Answers a tooltip, formatted as html, containing the annotation description
740 * (prefixed by associated sequence id if applicable), and the annotation
741 * (non-positional) score if it has one. Answers null if neither description
742 * nor score is found.
747 static String getTooltip(AlignmentAnnotation aa)
753 StringBuilder tooltip = new StringBuilder();
754 if (aa.description != null && !aa.description.equals("New description"))
756 // TODO: we could refactor and merge this code with the code in
757 // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature
759 String desc = aa.getDescription(true).trim();
760 if (!desc.toLowerCase().startsWith(HTML_START_TAG))
762 tooltip.append(HTML_START_TAG);
763 desc = desc.replace("<", "<");
765 else if (desc.toLowerCase().endsWith(HTML_END_TAG))
767 desc = desc.substring(0, desc.length() - HTML_END_TAG.length());
769 tooltip.append(desc);
773 // begin the tooltip's html fragment
774 tooltip.append(HTML_START_TAG);
778 if (tooltip.length() > HTML_START_TAG.length())
780 tooltip.append("<br/>");
782 // TODO: limit precision of score to avoid noise from imprecise
784 // (64.7 becomes 64.7+/some tiny value).
785 tooltip.append(" Score: ").append(String.valueOf(aa.score));
788 if (tooltip.length() > HTML_START_TAG.length())
790 return tooltip.append(HTML_END_TAG).toString();
794 * nothing in the tooltip (except "<html>")
800 * Shows the height adjuster image if the mouse moves into the top left
801 * region, or hides it if the mouse leaves the regio
805 protected void showOrHideAdjuster(MouseEvent evt)
807 boolean was = resizePanel;
808 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
810 if (resizePanel != was)
812 setCursor(Cursor.getPredefinedCursor(
813 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 + 4;
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)