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.AlphaComposite;
24 import java.awt.Color;
25 import java.awt.Dimension;
26 import java.awt.FontMetrics;
27 import java.awt.Graphics;
28 import java.awt.Graphics2D;
29 import java.awt.Image;
30 import java.awt.Rectangle;
31 import java.awt.RenderingHints;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.AdjustmentEvent;
35 import java.awt.event.AdjustmentListener;
36 import java.awt.event.MouseEvent;
37 import java.awt.event.MouseListener;
38 import java.awt.event.MouseMotionListener;
39 import java.awt.event.MouseWheelEvent;
40 import java.awt.event.MouseWheelListener;
41 import java.awt.image.BufferedImage;
42 import java.beans.PropertyChangeEvent;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
47 import javax.swing.JMenuItem;
48 import javax.swing.JPanel;
49 import javax.swing.JPopupMenu;
50 import javax.swing.Scrollable;
51 import javax.swing.ToolTipManager;
53 import jalview.api.AlignViewportI;
54 import jalview.datamodel.AlignmentAnnotation;
55 import jalview.datamodel.AlignmentI;
56 import jalview.datamodel.Annotation;
57 import jalview.datamodel.ColumnSelection;
58 import jalview.datamodel.ContactListI;
59 import jalview.datamodel.ContactRange;
60 import jalview.datamodel.HiddenColumns;
61 import jalview.datamodel.SequenceI;
62 import jalview.gui.JalviewColourChooser.ColourChooserListener;
63 import jalview.renderer.AnnotationRenderer;
64 import jalview.renderer.AwtRenderPanelI;
65 import jalview.renderer.ContactGeometry;
66 import jalview.schemes.ResidueProperties;
67 import jalview.util.Comparison;
68 import jalview.util.MessageManager;
69 import jalview.util.Platform;
70 import jalview.viewmodel.ViewportListenerI;
71 import jalview.viewmodel.ViewportRanges;
74 * AnnotationPanel displays visible portion of annotation rows below unwrapped
80 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
81 MouseListener, MouseWheelListener, MouseMotionListener,
82 ActionListener, AdjustmentListener, Scrollable, ViewportListenerI
86 Select, Resize, Undefined, MatrixSelect
89 String HELIX = MessageManager.getString("label.helix");
91 String SHEET = MessageManager.getString("label.sheet");
94 * For RNA secondary structure "stems" aka helices
96 String STEM = MessageManager.getString("label.rna_helix");
98 String LABEL = MessageManager.getString("label.label");
100 String REMOVE = MessageManager.getString("label.remove_annotation");
102 String COLOUR = MessageManager.getString("action.colour");
104 public final Color HELIX_COLOUR = Color.red.darker();
106 public final Color SHEET_COLOUR = Color.green.darker().darker();
108 public final Color STEM_COLOUR = Color.blue.darker();
111 public AlignViewport av;
115 public int activeRow = -1;
117 public BufferedImage image;
119 public volatile BufferedImage fadedImage;
121 // private Graphics2D gg;
123 public FontMetrics fm;
125 public int imgWidth = 0;
127 boolean fastPaint = false;
129 // Used For mouse Dragging and resizing graphs
130 int graphStretch = -1;
132 int mouseDragLastX = -1;
134 int mouseDragLastY = -1;
140 DragMode dragMode = DragMode.Undefined;
142 boolean mouseDragging = false;
144 // for editing cursor
149 public final AnnotationRenderer renderer;
151 private MouseWheelListener[] _mwl;
154 * Creates a new AnnotationPanel object.
159 public AnnotationPanel(AlignmentPanel ap)
161 ToolTipManager.sharedInstance().registerComponent(this);
162 ToolTipManager.sharedInstance().setInitialDelay(0);
163 ToolTipManager.sharedInstance().setDismissDelay(10000);
166 this.setLayout(null);
167 addMouseListener(this);
168 addMouseMotionListener(this);
169 ap.annotationScroller.getVerticalScrollBar()
170 .addAdjustmentListener(this);
171 // save any wheel listeners on the scroller, so we can propagate scroll
173 _mwl = ap.annotationScroller.getMouseWheelListeners();
174 // and then set our own listener to consume all mousewheel events
175 ap.annotationScroller.addMouseWheelListener(this);
176 renderer = new AnnotationRenderer();
178 av.getRanges().addPropertyChangeListener(this);
181 public AnnotationPanel(AlignViewport av)
184 renderer = new AnnotationRenderer();
188 public void mouseWheelMoved(MouseWheelEvent e)
193 double wheelRotation = e.getPreciseWheelRotation();
194 if (wheelRotation > 0)
196 av.getRanges().scrollRight(true);
198 else if (wheelRotation < 0)
200 av.getRanges().scrollRight(false);
205 // TODO: find the correct way to let the event bubble up to
206 // ap.annotationScroller
207 for (MouseWheelListener mwl : _mwl)
211 mwl.mouseWheelMoved(e);
222 public Dimension getPreferredScrollableViewportSize()
224 Dimension ps = getPreferredSize();
225 return new Dimension(ps.width, adjustForAlignFrame(false, ps.height));
229 public int getScrollableBlockIncrement(Rectangle visibleRect,
230 int orientation, int direction)
236 public boolean getScrollableTracksViewportHeight()
242 public boolean getScrollableTracksViewportWidth()
248 public int getScrollableUnitIncrement(Rectangle visibleRect,
249 int orientation, int direction)
258 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
262 public void adjustmentValueChanged(AdjustmentEvent evt)
264 // update annotation label display
265 ap.getAlabels().setScrollOffset(-evt.getValue());
269 * Calculates the height of the annotation displayed in the annotation panel.
270 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
271 * all annotation associated components are updated correctly.
274 public int adjustPanelHeight()
276 int height = av.calcPanelHeight();
277 this.setPreferredSize(new Dimension(1, height));
280 // revalidate only when the alignment panel is fully constructed
294 public void actionPerformed(ActionEvent evt)
296 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
301 Annotation[] anot = aa[activeRow].annotations;
303 if (anot.length < av.getColumnSelection().getMax())
305 Annotation[] temp = new Annotation[av.getColumnSelection().getMax()
307 System.arraycopy(anot, 0, temp, 0, anot.length);
309 aa[activeRow].annotations = anot;
312 String action = evt.getActionCommand();
313 if (action.equals(REMOVE))
315 for (int index : av.getColumnSelection().getSelected())
317 if (av.getAlignment().getHiddenColumns().isVisible(index))
323 else if (action.equals(LABEL))
325 String exMesg = collectAnnotVals(anot, LABEL);
326 String label = JvOptionPane.showInputDialog(
327 MessageManager.getString("label.enter_label"), exMesg);
334 if ((label.length() > 0) && !aa[activeRow].hasText)
336 aa[activeRow].hasText = true;
339 for (int index : av.getColumnSelection().getSelected())
341 if (!av.getAlignment().getHiddenColumns().isVisible(index))
346 if (anot[index] == null)
348 anot[index] = new Annotation(label, "", ' ', 0);
352 anot[index].displayCharacter = label;
356 else if (action.equals(COLOUR))
358 final Annotation[] fAnot = anot;
359 String title = MessageManager
360 .getString("label.select_foreground_colour");
361 ColourChooserListener listener = new ColourChooserListener()
364 public void colourSelected(Color c)
366 HiddenColumns hiddenColumns = av.getAlignment()
368 for (int index : av.getColumnSelection().getSelected())
370 if (hiddenColumns.isVisible(index))
372 if (fAnot[index] == null)
374 fAnot[index] = new Annotation("", "", ' ', 0);
376 fAnot[index].colour = c;
381 JalviewColourChooser.showColourChooser(this, title, Color.black,
385 // HELIX, SHEET or STEM
388 String symbol = "\u03B1"; // alpha
390 if (action.equals(HELIX))
394 else if (action.equals(SHEET))
397 symbol = "\u03B2"; // beta
400 // Added by LML to color stems
401 else if (action.equals(STEM))
404 int column = av.getColumnSelection().getSelectedRanges().get(0)[0];
405 symbol = aa[activeRow].getDefaultRnaHelixSymbol(column);
408 if (!aa[activeRow].hasIcons)
410 aa[activeRow].hasIcons = true;
413 String label = JvOptionPane.showInputDialog(MessageManager
414 .getString("label.enter_label_for_the_structure"), symbol);
421 if ((label.length() > 0) && !aa[activeRow].hasText)
423 aa[activeRow].hasText = true;
424 if (action.equals(STEM))
426 aa[activeRow].showAllColLabels = true;
429 for (int index : av.getColumnSelection().getSelected())
431 if (!av.getAlignment().getHiddenColumns().isVisible(index))
436 if (anot[index] == null)
438 anot[index] = new Annotation(label, "", type, 0);
441 anot[index].secondaryStructure = type != 'S' ? type
442 : label.length() == 0 ? ' ' : label.charAt(0);
443 anot[index].displayCharacter = label;
448 av.getAlignment().validateAnnotation(aa[activeRow]);
449 ap.alignmentChanged();
450 ap.alignFrame.setMenusForViewport();
458 * Returns any existing annotation concatenated as a string. For each
459 * annotation, takes the description, if any, else the secondary structure
460 * character (if type is HELIX, SHEET or STEM), else the display character (if
467 private String collectAnnotVals(Annotation[] anots, String type)
469 // TODO is this method wanted? why? 'last' is not used
471 StringBuilder collatedInput = new StringBuilder(64);
473 ColumnSelection viscols = av.getColumnSelection();
474 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
477 * the selection list (read-only view) is in selection order, not
478 * column order; make a copy so we can sort it
480 List<Integer> selected = new ArrayList<>(viscols.getSelected());
481 Collections.sort(selected);
482 for (int index : selected)
484 // always check for current display state - just in case
485 if (!hidden.isVisible(index))
489 String tlabel = null;
490 if (anots[index] != null)
491 { // LML added stem code
492 if (type.equals(HELIX) || type.equals(SHEET) || type.equals(STEM)
493 || type.equals(LABEL))
495 tlabel = anots[index].description;
496 if (tlabel == null || tlabel.length() < 1)
498 if (type.equals(HELIX) || type.equals(SHEET)
499 || type.equals(STEM))
501 tlabel = "" + anots[index].secondaryStructure;
505 tlabel = "" + anots[index].displayCharacter;
509 if (tlabel != null && !tlabel.equals(last))
511 if (last.length() > 0)
513 collatedInput.append(" ");
515 collatedInput.append(tlabel);
519 return collatedInput.toString();
523 * Action on right mouse pressed on Mac is to show a pop-up menu for the
524 * annotation. Action on left mouse pressed is to find which annotation is
525 * pressed and mark the start of a column selection or graph resize operation.
530 public void mousePressed(MouseEvent evt)
533 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
538 mouseDragLastX = evt.getX();
539 mouseDragLastY = evt.getY();
542 * add visible annotation heights until we reach the y
543 * position, to find which annotation it is in
548 // todo could reuse getRowIndexAndOffset ?
549 final int y = evt.getY();
551 for (int i = 0; i < aa.length; i++)
555 height += aa[i].height;
564 else if (aa[i].graph != 0)
567 * we have clicked on a resizable graph annotation
570 yOffset = height - y;
577 * isPopupTrigger fires in mousePressed on Mac,
578 * not until mouseRelease on Windows
580 if (evt.isPopupTrigger() && activeRow != -1)
582 showPopupMenu(y, evt.getX());
586 if (graphStretch != -1)
589 if (aa[graphStretch].graph == AlignmentAnnotation.CUSTOMRENDERER)
591 if (evt.isAltDown() || evt.isAltGraphDown())
593 dragMode = DragMode.MatrixSelect;
594 firstDragX = mouseDragLastX;
595 firstDragY = mouseDragLastY;
599 int currentX = getColumnForXPos(evt.getX());
600 ContactListI forCurrentX = av.getContactList(aa[graphStretch],
602 if (forCurrentX != null)
604 ContactGeometry cXcgeom = new ContactGeometry(forCurrentX,
605 aa[graphStretch].graphHeight);
606 ContactGeometry.contactInterval cXci = cXcgeom.mapFor(yOffset,
609 fr = Math.min(cXci.cStart, cXci.cEnd);
610 to = Math.max(cXci.cStart, cXci.cEnd);
611 for (int c = fr; c <= to; c++)
613 av.getColumnSelection().addElement(c);
615 av.getColumnSelection().addElement(currentX);
622 ap.getScalePanel().mousePressed(evt);
627 * Construct and display a context menu at the right-click position
632 void showPopupMenu(final int y, int x)
634 if (av.getColumnSelection() == null
635 || av.getColumnSelection().isEmpty())
640 JPopupMenu pop = new JPopupMenu(
641 MessageManager.getString("label.structure_type"));
644 * Just display the needed structure options
646 if (av.getAlignment().isNucleotide())
648 item = new JMenuItem(STEM);
649 item.addActionListener(this);
654 item = new JMenuItem(HELIX);
655 item.addActionListener(this);
657 item = new JMenuItem(SHEET);
658 item.addActionListener(this);
661 item = new JMenuItem(LABEL);
662 item.addActionListener(this);
664 item = new JMenuItem(COLOUR);
665 item.addActionListener(this);
667 item = new JMenuItem(REMOVE);
668 item.addActionListener(this);
670 pop.show(this, x, y);
674 * Action on mouse up is to clear mouse drag data and call mouseReleased on
675 * ScalePanel, to deal with defining the selection group (if any) defined by
681 public void mouseReleased(MouseEvent evt)
683 if (dragMode == DragMode.MatrixSelect)
685 matrixSelectRange(evt);
692 mouseDragging = false;
693 if (dragMode == DragMode.Resize)
695 ap.adjustAnnotationHeight();
697 dragMode = DragMode.Undefined;
698 ap.getScalePanel().mouseReleased(evt);
701 * isPopupTrigger is set in mouseReleased on Windows
702 * (in mousePressed on Mac)
704 if (evt.isPopupTrigger() && activeRow != -1)
706 showPopupMenu(evt.getY(), evt.getX());
718 public void mouseEntered(MouseEvent evt)
720 this.mouseDragging = false;
721 ap.getScalePanel().mouseEntered(evt);
725 * On leaving the panel, calls ScalePanel.mouseExited to deal with scrolling
726 * with column selection on a mouse drag
731 public void mouseExited(MouseEvent evt)
733 ap.getScalePanel().mouseExited(evt);
737 * Action on starting or continuing a mouse drag. There are two possible
740 * <li>drag up or down on a graphed annotation increases or decreases the
741 * height of the graph</li>
742 * <li>dragging left or right selects the columns dragged across</li>
744 * A drag on a graph annotation is treated as column selection if it starts
745 * with more horizontal than vertical movement, and as resize if it starts
746 * with more vertical than horizontal movement. Once started, the drag does
752 public void mouseDragged(MouseEvent evt)
755 * if dragMode is Undefined:
756 * - set to Select if dx > dy
757 * - set to Resize if dy > dx
758 * - do nothing if dx == dy
760 final int x = evt.getX();
761 final int y = evt.getY();
762 if (dragMode == DragMode.Undefined)
764 int dx = Math.abs(x - mouseDragLastX);
765 int dy = Math.abs(y - mouseDragLastY);
766 if (graphStretch == -1 || dx > dy)
769 * mostly horizontal drag, or not a graph annotation
771 dragMode = DragMode.Select;
776 * mostly vertical drag
778 dragMode = DragMode.Resize;
781 * but could also be a matrix drag
783 if ((evt.isAltDown() || evt.isAltGraphDown()) && (av.getAlignment()
784 .getAlignmentAnnotation()[graphStretch].graph == AlignmentAnnotation.CUSTOMRENDERER))
787 * dragging in a matrix
789 dragMode = DragMode.MatrixSelect;
790 firstDragX = mouseDragLastX;
791 firstDragY = mouseDragLastY;
796 if (dragMode == DragMode.Undefined)
800 * drag is diagonal - defer deciding whether to
801 * treat as up/down or left/right
808 if (dragMode == DragMode.Resize)
811 * resize graph annotation if mouse was dragged up or down
813 int deltaY = mouseDragLastY - evt.getY();
816 AlignmentAnnotation graphAnnotation = av.getAlignment()
817 .getAlignmentAnnotation()[graphStretch];
818 int newHeight = Math.max(0, graphAnnotation.graphHeight + deltaY);
819 graphAnnotation.graphHeight = newHeight;
821 ap.paintAlignment(false, false);
824 else if (dragMode == DragMode.MatrixSelect)
827 * TODO draw a rubber band for range
831 ap.paintAlignment(false, false);
836 * for mouse drag left or right, delegate to
837 * ScalePanel to adjust the column selection
839 ap.getScalePanel().mouseDragged(evt);
848 public void matrixSelectRange(MouseEvent evt)
851 * get geometry of drag
853 int fromY = Math.min(firstDragY, evt.getY());
854 int toY = Math.max(firstDragY, evt.getY());
855 int deltaY = toY - fromY;
857 int[] rowIndex = getRowIndexAndOffset(fromY,
858 av.getAlignment().getAlignmentAnnotation());
859 int[] toRowIndex = getRowIndexAndOffset(toY,
860 av.getAlignment().getAlignmentAnnotation());
862 if (rowIndex == null || toRowIndex == null)
864 System.out.println("Drag out of range. needs to be clipped");
867 if (rowIndex[0] != toRowIndex[0])
869 System.out.println("Drag went to another row. needs to be clipped");
872 // rectangular selection on matrix style annotation
873 AlignmentAnnotation cma = av.getAlignment()
874 .getAlignmentAnnotation()[rowIndex[0]];
876 int fromXp = Math.min(firstDragX, evt.getX());
877 int toXp = Math.max(firstDragX, evt.getX());
878 int lastX = getColumnForXPos(fromXp);
879 int currentX = getColumnForXPos(toXp);
880 ContactListI forLastX = av.getContactList(cma, lastX);
881 ContactListI forCurrentX = av.getContactList(cma, currentX);
882 if (forLastX != null && forCurrentX != null)
884 ContactGeometry lastXcgeom = new ContactGeometry(forLastX,
886 ContactGeometry.contactInterval lastXci = lastXcgeom.mapFor(
888 rowIndex[1] + ((fromY == firstDragY) ? -deltaY : deltaY));
889 ContactGeometry cXcgeom = new ContactGeometry(forCurrentX,
891 ContactGeometry.contactInterval cXci = cXcgeom.mapFor(rowIndex[1],
892 rowIndex[1] + deltaY);
893 // mark rectangular region formed by drag
894 System.err.println("Matrix Selection from last(" + lastXci.cStart
895 + "," + lastXci.cEnd + ") to cur(" + cXci.cStart + ","
898 fr = Math.min(lastXci.cStart, lastXci.cEnd);
899 to = Math.max(lastXci.cStart, lastXci.cEnd);
900 System.err.println("Marking " + fr + " to " + to);
901 for (int c = fr; c <= to; c++)
903 av.getColumnSelection().addElement(c);
905 fr = Math.min(cXci.cStart, cXci.cEnd);
906 to = Math.max(cXci.cStart, cXci.cEnd);
907 System.err.println("Marking " + fr + " to " + to);
908 for (int c = fr; c <= to; c++)
910 av.getColumnSelection().addElement(c);
912 fr = Math.min(lastX, currentX);
913 to = Math.max(lastX, currentX);
915 System.err.println("Marking " + fr + " to " + to);
916 for (int c = fr; c <= to; c++)
918 av.getColumnSelection().addElement(c);
925 * Constructs the tooltip, and constructs and displays a status message, for
926 * the current mouse position
931 public void mouseMoved(MouseEvent evt)
933 int yPos = evt.getY();
934 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
935 int rowAndOffset[] = getRowIndexAndOffset(yPos, aa);
936 int row = rowAndOffset[0];
940 this.setToolTipText(null);
944 int column = getColumnForXPos(evt.getX());
946 AlignmentAnnotation ann = aa[row];
947 if (row > -1 && ann.annotations != null
948 && column < ann.annotations.length)
950 String toolTip = buildToolTip(ann, column, aa, rowAndOffset[1], av,
952 setToolTipText(toolTip == null ? null
953 : JvSwingUtils.wrapTooltip(true, toolTip));
954 String msg = getStatusMessage(av.getAlignment(), column, ann,
955 rowAndOffset[1], av);
956 ap.alignFrame.setStatus(msg);
960 this.setToolTipText(null);
961 ap.alignFrame.setStatus(" ");
965 private int getColumnForXPos(int x)
967 int column = (x / av.getCharWidth()) + av.getRanges().getStartRes();
968 column = Math.min(column, av.getRanges().getEndRes());
970 if (av.hasHiddenColumns())
972 column = av.getAlignment().getHiddenColumns()
973 .visibleToAbsoluteColumn(column);
979 * Answers the index in the annotations array of the visible annotation at the
980 * given y position. This is done by adding the heights of visible annotations
981 * until the y position has been exceeded. Answers -1 if no annotations are
982 * visible, or the y position is below all annotations.
988 static int getRowIndex(int yPos, AlignmentAnnotation[] aa)
994 return getRowIndexAndOffset(yPos, aa)[0];
997 static int[] getRowIndexAndOffset(int yPos, AlignmentAnnotation[] aa)
999 int[] res = new int[2];
1007 int height = 0, lheight = 0;
1009 for (int i = 0; i < aa.length; i++)
1014 height += aa[i].height;
1021 res[1] = height - yPos;
1029 * Answers a tooltip for the annotation at the current mouse position, not
1030 * wrapped in <html> tags (apply if wanted). Answers null if there is no
1036 * @param rowAndOffset
1038 static String buildToolTip(AlignmentAnnotation ann, int column,
1039 AlignmentAnnotation[] anns, int rowAndOffset, AlignViewportI av,
1042 String tooltip = null;
1043 if (ann.graphGroup > -1)
1045 StringBuilder tip = new StringBuilder(32);
1046 boolean first = true;
1047 for (int i = 0; i < anns.length; i++)
1049 if (anns[i].graphGroup == ann.graphGroup
1050 && anns[i].annotations[column] != null)
1057 tip.append(anns[i].label);
1058 String description = anns[i].annotations[column].description;
1059 if (description != null && description.length() > 0)
1061 tip.append(" ").append(description);
1065 tooltip = first ? null : tip.toString();
1067 else if (column < ann.annotations.length
1068 && ann.annotations[column] != null)
1070 tooltip = ann.annotations[column].description;
1072 // TODO abstract tooltip generator so different implementations can be built
1073 if (ann.graph == AlignmentAnnotation.CUSTOMRENDERER)
1075 ContactListI clist = av.getContactList(ann, column);
1078 ContactGeometry cgeom = new ContactGeometry(clist, ann.graphHeight);
1079 ContactGeometry.contactInterval ci = cgeom.mapFor(rowAndOffset,
1081 tooltip += "Contact from " + ci.cStart + " to " + ci.cEnd;
1083 // ap.getStructureSelectionManager().mouseOverSequence(ann.sequenceRef,
1084 // new int[] {column, ci.cStart,ci.cEnd}, -1, null)
1091 * Constructs and returns the status bar message
1096 * @param rowAndOffset
1098 static String getStatusMessage(AlignmentI al, int column,
1099 AlignmentAnnotation ann, int rowAndOffset, AlignViewportI av)
1102 * show alignment column and annotation description if any
1104 StringBuilder text = new StringBuilder(32);
1105 text.append(MessageManager.getString("label.column")).append(" ")
1106 .append(column + 1);
1108 if (column < ann.annotations.length && ann.annotations[column] != null)
1110 String description = ann.annotations[column].description;
1111 if (description != null && description.trim().length() > 0)
1113 text.append(" ").append(description);
1118 * if the annotation is sequence-specific, show the sequence number
1119 * in the alignment, and (if not a gap) the residue and position
1121 SequenceI seqref = ann.sequenceRef;
1124 int seqIndex = al.findIndex(seqref);
1127 text.append(", ").append(MessageManager.getString("label.sequence"))
1128 .append(" ").append(seqIndex + 1);
1129 char residue = seqref.getCharAt(column);
1130 if (!Comparison.isGap(residue))
1134 if (al.isNucleotide())
1136 name = ResidueProperties.nucleotideName
1137 .get(String.valueOf(residue));
1138 text.append(" Nucleotide: ")
1139 .append(name != null ? name : residue);
1143 name = 'X' == residue ? "X"
1144 : ('*' == residue ? "STOP"
1145 : ResidueProperties.aa2Triplet
1146 .get(String.valueOf(residue)));
1147 text.append(" Residue: ").append(name != null ? name : residue);
1149 int residuePos = seqref.findPosition(column);
1150 text.append(" (").append(residuePos).append(")");
1155 return text.toString();
1165 public void mouseClicked(MouseEvent evt)
1167 // if (activeRow != -1)
1169 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
1170 // AlignmentAnnotation anot = aa[activeRow];
1174 // TODO mouseClicked-content and drawCursor are quite experimental!
1175 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
1178 int pady = av.getCharHeight() / 5;
1180 graphics.setColor(Color.black);
1181 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
1183 if (av.validCharWidth)
1185 graphics.setColor(Color.white);
1187 char s = seq.getCharAt(res);
1189 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
1190 graphics.drawString(String.valueOf(s), charOffset + x1,
1191 (y1 + av.getCharHeight()) - pady);
1196 private volatile boolean imageFresh = false;
1198 private Rectangle visibleRect = new Rectangle(),
1199 clipBounds = new Rectangle();
1208 public void paintComponent(Graphics g)
1211 // BH: note that this method is generally recommended to
1212 // call super.paintComponent(g). Otherwise, the children of this
1213 // component will not be rendered. That is not needed here
1214 // because AnnotationPanel does not have any children. It is
1215 // just a JPanel contained in a JViewPort.
1217 computeVisibleRect(visibleRect);
1219 g.setColor(Color.white);
1220 g.fillRect(0, 0, visibleRect.width, visibleRect.height);
1224 // BH 2018 optimizing generation of new Rectangle().
1226 || (visibleRect.width != (clipBounds = g
1227 .getClipBounds(clipBounds)).width)
1228 || (visibleRect.height != clipBounds.height))
1231 g.drawImage(image, 0, 0, this);
1236 imgWidth = (av.getRanges().getEndRes() - av.getRanges().getStartRes()
1237 + 1) * av.getCharWidth();
1243 if (image == null || imgWidth != image.getWidth(this)
1244 || image.getHeight(this) != getHeight())
1248 image = new BufferedImage(imgWidth,
1249 ap.getAnnotationPanel().getHeight(),
1250 BufferedImage.TYPE_INT_RGB);
1251 } catch (OutOfMemoryError oom)
1256 } catch (Exception x)
1261 "Couldn't allocate memory to redraw screen. Please restart Jalview",
1265 gg = (Graphics2D) image.getGraphics();
1269 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
1270 RenderingHints.VALUE_ANTIALIAS_ON);
1273 gg.setFont(av.getFont());
1274 fm = gg.getFontMetrics();
1275 gg.setColor(Color.white);
1276 gg.fillRect(0, 0, imgWidth, image.getHeight());
1281 gg = (Graphics2D) image.getGraphics();
1285 drawComponent(gg, av.getRanges().getStartRes(),
1286 av.getRanges().getEndRes() + 1);
1289 g.drawImage(image, 0, 0, this);
1293 * set true to enable redraw timing debug output on stderr
1295 private final boolean debugRedraw = false;
1298 * non-Thread safe repaint
1301 * repaint with horizontal shift in alignment
1303 public void fastPaint(int horizontal)
1305 if ((horizontal == 0) || image == null
1306 || av.getAlignment().getAlignmentAnnotation() == null
1307 || av.getAlignment().getAlignmentAnnotation().length < 1
1308 || av.isCalcInProgress())
1314 int sr = av.getRanges().getStartRes();
1315 int er = av.getRanges().getEndRes() + 1;
1318 Graphics2D gg = (Graphics2D) image.getGraphics();
1320 if (imgWidth > Math.abs(horizontal * av.getCharWidth()))
1322 // scroll is less than imgWidth away so can re-use buffered graphics
1323 gg.copyArea(0, 0, imgWidth, getHeight(),
1324 -horizontal * av.getCharWidth(), 0);
1326 if (horizontal > 0) // scrollbar pulled right, image to the left
1328 transX = (er - sr - horizontal) * av.getCharWidth();
1329 sr = er - horizontal;
1331 else if (horizontal < 0)
1333 er = sr - horizontal;
1336 gg.translate(transX, 0);
1338 drawComponent(gg, sr, er);
1340 gg.translate(-transX, 0);
1346 // Call repaint on alignment panel so that repaints from other alignment
1347 // panel components can be aggregated. Otherwise performance of the overview
1348 // window and others may be adversely affected.
1349 av.getAlignPanel().repaint();
1352 private volatile boolean lastImageGood = false;
1364 public void drawComponent(Graphics g, int startRes, int endRes)
1366 BufferedImage oldFaded = fadedImage;
1367 if (av.isCalcInProgress())
1371 lastImageGood = false;
1374 // We'll keep a record of the old image,
1375 // and draw a faded image until the calculation
1378 && (fadedImage == null || fadedImage.getWidth() != imgWidth
1379 || fadedImage.getHeight() != image.getHeight()))
1381 // System.err.println("redraw faded image ("+(fadedImage==null ?
1382 // "null image" : "") + " lastGood="+lastImageGood+")");
1383 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
1384 BufferedImage.TYPE_INT_RGB);
1386 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
1388 fadedG.setColor(Color.white);
1389 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
1391 fadedG.setComposite(
1392 AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .3f));
1393 fadedG.drawImage(image, 0, 0, this);
1396 // make sure we don't overwrite the last good faded image until all
1397 // calculations have finished
1398 lastImageGood = false;
1403 if (fadedImage != null)
1405 oldFaded = fadedImage;
1410 g.setColor(Color.white);
1411 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(), getHeight());
1413 g.setFont(av.getFont());
1416 fm = g.getFontMetrics();
1419 if ((av.getAlignment().getAlignmentAnnotation() == null)
1420 || (av.getAlignment().getAlignmentAnnotation().length < 1))
1422 g.setColor(Color.white);
1423 g.fillRect(0, 0, getWidth(), getHeight());
1424 g.setColor(Color.black);
1425 if (av.validCharWidth)
1427 g.drawString(MessageManager
1428 .getString("label.alignment_has_no_annotations"), 20, 15);
1433 lastImageGood = renderer.drawComponent(this, av, g, activeRow, startRes,
1435 if (!lastImageGood && fadedImage == null)
1437 fadedImage = oldFaded;
1439 if (dragMode == DragMode.MatrixSelect)
1441 g.setColor(Color.yellow);
1442 g.drawRect(Math.min(firstDragX, mouseDragLastX),
1443 Math.min(firstDragY, mouseDragLastY),
1444 Math.max(firstDragX, mouseDragLastX)
1445 - Math.min(firstDragX, mouseDragLastX),
1446 Math.max(firstDragY, mouseDragLastY)
1447 - Math.min(firstDragY, mouseDragLastY));
1453 public FontMetrics getFontMetrics()
1459 public Image getFadedImage()
1465 public int getFadedImageWidth()
1470 private int[] bounds = new int[2];
1473 public int[] getVisibleVRange()
1475 if (ap != null && ap.getAlabels() != null)
1477 int sOffset = -ap.getAlabels().getScrollOffset();
1478 int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
1479 bounds[0] = sOffset;
1480 bounds[1] = visHeight;
1490 * Try to ensure any references held are nulled
1492 public void dispose()
1502 * I created the renderer so I will dispose of it
1504 if (renderer != null)
1511 public void propertyChange(PropertyChangeEvent evt)
1513 // Respond to viewport range changes (e.g. alignment panel was scrolled)
1514 // Both scrolling and resizing change viewport ranges: scrolling changes
1515 // both start and end points, but resize only changes end values.
1516 // Here we only want to fastpaint on a scroll, with resize using a normal
1517 // paint, so scroll events are identified as changes to the horizontal or
1518 // vertical start value.
1519 if (evt.getPropertyName().equals(ViewportRanges.STARTRES))
1521 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
1523 else if (evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ))
1525 fastPaint(((int[]) evt.getNewValue())[0]
1526 - ((int[]) evt.getOldValue())[0]);
1528 else if (evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
1535 * computes the visible height of the annotation panel
1537 * @param adjustPanelHeight
1538 * - when false, just adjust existing height according to other
1540 * @param annotationHeight
1541 * @return height to use for the ScrollerPreferredVisibleSize
1543 public int adjustForAlignFrame(boolean adjustPanelHeight,
1544 int annotationHeight)
1547 * Estimate available height in the AlignFrame for alignment +
1548 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
1549 * hscroll, status bar, insets.
1551 int stuff = (ap.getViewName() != null ? 30 : 0)
1552 + (Platform.isAMacAndNotJS() ? 120 : 140);
1553 int availableHeight = ap.alignFrame.getHeight() - stuff;
1554 int rowHeight = av.getCharHeight();
1556 if (adjustPanelHeight)
1558 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
1561 * If not enough vertical space, maximize annotation height while keeping
1562 * at least two rows of alignment visible
1564 if (annotationHeight + alignmentHeight > availableHeight)
1566 annotationHeight = Math.min(annotationHeight,
1567 availableHeight - 2 * rowHeight);
1572 // maintain same window layout whilst updating sliders
1573 annotationHeight = Math.min(ap.annotationScroller.getSize().height,
1574 availableHeight - 2 * rowHeight);
1576 return annotationHeight;