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.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.SequenceI;
27 import jalview.renderer.AnnotationRenderer;
28 import jalview.renderer.AwtRenderPanelI;
29 import jalview.util.Comparison;
30 import jalview.util.MessageManager;
32 import java.awt.AlphaComposite;
33 import java.awt.Color;
34 import java.awt.Dimension;
35 import java.awt.FontMetrics;
36 import java.awt.Graphics;
37 import java.awt.Graphics2D;
38 import java.awt.Image;
39 import java.awt.Rectangle;
40 import java.awt.RenderingHints;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.AdjustmentEvent;
44 import java.awt.event.AdjustmentListener;
45 import java.awt.event.MouseEvent;
46 import java.awt.event.MouseListener;
47 import java.awt.event.MouseMotionListener;
48 import java.awt.event.MouseWheelEvent;
49 import java.awt.event.MouseWheelListener;
50 import java.awt.image.BufferedImage;
52 import javax.swing.JColorChooser;
53 import javax.swing.JMenuItem;
54 import javax.swing.JOptionPane;
55 import javax.swing.JPanel;
56 import javax.swing.JPopupMenu;
57 import javax.swing.Scrollable;
58 import javax.swing.ToolTipManager;
61 * AnnotationPanel displays visible portion of annotation rows below unwrapped
67 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
68 MouseListener, MouseWheelListener, MouseMotionListener,
69 ActionListener, AdjustmentListener, Scrollable
71 String HELIX = MessageManager.getString("label.helix");
73 String SHEET = MessageManager.getString("label.sheet");
76 * For RNA secondary structure "stems" aka helices
78 String STEM = MessageManager.getString("label.rna_helix");
80 String LABEL = MessageManager.getString("label.label");
82 String REMOVE = MessageManager.getString("label.remove_annotation");
84 String COLOUR = MessageManager.getString("action.colour");
86 public final Color HELIX_COLOUR = Color.red.darker();
88 public final Color SHEET_COLOUR = Color.green.darker().darker();
90 public final Color STEM_COLOUR = Color.blue.darker();
93 public AlignViewport av;
97 public int activeRow = -1;
99 public BufferedImage image;
101 public volatile BufferedImage fadedImage;
105 public FontMetrics fm;
107 public int imgWidth = 0;
109 boolean fastPaint = false;
111 // Used For mouse Dragging and resizing graphs
112 int graphStretch = -1;
114 int graphStretchY = -1;
116 int min; // used by mouseDragged to see if user
118 int max; // used by mouseDragged to see if user
120 boolean mouseDragging = false;
124 // for editing cursor
129 public final AnnotationRenderer renderer;
131 private MouseWheelListener[] _mwl;
134 * Creates a new AnnotationPanel object.
139 public AnnotationPanel(AlignmentPanel ap)
142 MAC = jalview.util.Platform.isAMac();
144 ToolTipManager.sharedInstance().registerComponent(this);
145 ToolTipManager.sharedInstance().setInitialDelay(0);
146 ToolTipManager.sharedInstance().setDismissDelay(10000);
149 this.setLayout(null);
150 addMouseListener(this);
151 addMouseMotionListener(this);
152 ap.annotationScroller.getVerticalScrollBar()
153 .addAdjustmentListener(this);
154 // save any wheel listeners on the scroller, so we can propagate scroll
156 _mwl = ap.annotationScroller.getMouseWheelListeners();
157 // and then set our own listener to consume all mousewheel events
158 ap.annotationScroller.addMouseWheelListener(this);
159 renderer = new AnnotationRenderer();
162 public AnnotationPanel(AlignViewport av)
165 renderer = new AnnotationRenderer();
169 public void mouseWheelMoved(MouseWheelEvent e)
174 if (e.getWheelRotation() > 0)
176 ap.scrollRight(true);
180 ap.scrollRight(false);
185 // TODO: find the correct way to let the event bubble up to
186 // ap.annotationScroller
187 for (MouseWheelListener mwl : _mwl)
191 mwl.mouseWheelMoved(e);
202 public Dimension getPreferredScrollableViewportSize()
204 return getPreferredSize();
208 public int getScrollableBlockIncrement(Rectangle visibleRect,
209 int orientation, int direction)
215 public boolean getScrollableTracksViewportHeight()
221 public boolean getScrollableTracksViewportWidth()
227 public int getScrollableUnitIncrement(Rectangle visibleRect,
228 int orientation, int direction)
237 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
241 public void adjustmentValueChanged(AdjustmentEvent evt)
243 // update annotation label display
244 ap.getAlabels().setScrollOffset(-evt.getValue());
248 * Calculates the height of the annotation displayed in the annotation panel.
249 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
250 * all annotation associated components are updated correctly.
253 public int adjustPanelHeight()
255 int height = av.calcPanelHeight();
256 this.setPreferredSize(new Dimension(1, height));
259 // revalidate only when the alignment panel is fully constructed
273 public void actionPerformed(ActionEvent evt)
275 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
280 Annotation[] anot = aa[activeRow].annotations;
282 if (anot.length < av.getColumnSelection().getMax())
284 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
285 System.arraycopy(anot, 0, temp, 0, anot.length);
287 aa[activeRow].annotations = anot;
290 if (evt.getActionCommand().equals(REMOVE))
292 for (int sel : av.getColumnSelection().getSelected())
297 else if (evt.getActionCommand().equals(LABEL))
299 String exMesg = collectAnnotVals(anot, LABEL);
300 String label = JOptionPane.showInputDialog(this,
301 MessageManager.getString("label.enter_label"), exMesg);
308 if ((label.length() > 0) && !aa[activeRow].hasText)
310 aa[activeRow].hasText = true;
313 for (int index : av.getColumnSelection().getSelected())
315 if (!av.getColumnSelection().isVisible(index))
320 if (anot[index] == null)
322 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
329 anot[index].displayCharacter = label;
333 else if (evt.getActionCommand().equals(COLOUR))
335 Color col = JColorChooser.showDialog(this,
336 MessageManager.getString("label.select_foreground_colour"),
339 for (int index : av.getColumnSelection().getSelected())
341 if (!av.getColumnSelection().isVisible(index))
346 if (anot[index] == null)
348 anot[index] = new Annotation("", "", ' ', 0);
351 anot[index].colour = col;
355 // HELIX, SHEET or STEM
358 String symbol = "\u03B1";
360 if (evt.getActionCommand().equals(HELIX))
364 else if (evt.getActionCommand().equals(SHEET))
370 // Added by LML to color stems
371 else if (evt.getActionCommand().equals(STEM))
377 if (!aa[activeRow].hasIcons)
379 aa[activeRow].hasIcons = true;
382 String label = JOptionPane.showInputDialog(MessageManager
383 .getString("label.enter_label_for_the_structure"), symbol);
390 if ((label.length() > 0) && !aa[activeRow].hasText)
392 aa[activeRow].hasText = true;
393 if (evt.getActionCommand().equals(STEM))
395 aa[activeRow].showAllColLabels = true;
398 for (int index : av.getColumnSelection().getSelected())
400 if (!av.getColumnSelection().isVisible(index))
405 if (anot[index] == null)
407 anot[index] = new Annotation(label, "", type, 0);
410 anot[index].secondaryStructure = type != 'S' ? type : label
411 .length() == 0 ? ' ' : label.charAt(0);
412 anot[index].displayCharacter = label;
417 av.getAlignment().validateAnnotation(aa[activeRow]);
418 ap.alignmentChanged();
419 ap.alignFrame.setMenusForViewport();
426 private String collectAnnotVals(Annotation[] anot, String label2)
428 String collatedInput = "";
430 ColumnSelection viscols = av.getColumnSelection();
431 // TODO: refactor and save av.getColumnSelection for efficiency
432 for (int index : viscols.getSelected())
434 // always check for current display state - just in case
435 if (!viscols.isVisible(index))
439 String tlabel = null;
440 if (anot[index] != null)
441 { // LML added stem code
442 if (label2.equals(HELIX) || label2.equals(SHEET)
443 || label2.equals(STEM) || label2.equals(LABEL))
445 tlabel = anot[index].description;
446 if (tlabel == null || tlabel.length() < 1)
448 if (label2.equals(HELIX) || label2.equals(SHEET)
449 || label2.equals(STEM))
451 tlabel = "" + anot[index].secondaryStructure;
455 tlabel = "" + anot[index].displayCharacter;
459 if (tlabel != null && !tlabel.equals(last))
461 if (last.length() > 0)
463 collatedInput += " ";
465 collatedInput += tlabel;
469 return collatedInput;
479 public void mousePressed(MouseEvent evt)
482 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
491 for (int i = 0; i < aa.length; i++)
495 height += aa[i].height;
498 if (evt.getY() < height)
504 else if (aa[i].graph > 0)
508 graphStretchY = evt.getY();
515 if (evt.isPopupTrigger() && activeRow != -1)
517 if (av.getColumnSelection() == null)
522 JPopupMenu pop = new JPopupMenu(
523 MessageManager.getString("label.structure_type"));
526 * Just display the needed structure options
528 if (av.getAlignment().isNucleotide() == true)
530 item = new JMenuItem(STEM);
531 item.addActionListener(this);
536 item = new JMenuItem(HELIX);
537 item.addActionListener(this);
539 item = new JMenuItem(SHEET);
540 item.addActionListener(this);
543 item = new JMenuItem(LABEL);
544 item.addActionListener(this);
546 item = new JMenuItem(COLOUR);
547 item.addActionListener(this);
549 item = new JMenuItem(REMOVE);
550 item.addActionListener(this);
552 pop.show(this, evt.getX(), evt.getY());
562 ap.getScalePanel().mousePressed(evt);
573 public void mouseReleased(MouseEvent evt)
577 mouseDragging = false;
578 ap.getScalePanel().mouseReleased(evt);
588 public void mouseEntered(MouseEvent evt)
590 ap.getScalePanel().mouseEntered(evt);
600 public void mouseExited(MouseEvent evt)
602 ap.getScalePanel().mouseExited(evt);
612 public void mouseDragged(MouseEvent evt)
614 if (graphStretch > -1)
616 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
618 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
620 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
622 graphStretchY = evt.getY();
624 ap.paintAlignment(true);
628 ap.getScalePanel().mouseDragged(evt);
633 * Constructs the tooltip, and constructs and displays a status message, for
634 * the current mouse position
639 public void mouseMoved(MouseEvent evt)
641 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
645 this.setToolTipText(null);
652 for (int i = 0; i < aa.length; i++)
656 height += aa[i].height;
659 if (evt.getY() < height)
668 this.setToolTipText(null);
672 int column = (evt.getX() / av.getCharWidth()) + av.getStartRes();
674 if (av.hasHiddenColumns())
676 column = av.getColumnSelection().adjustForHiddenColumns(column);
679 AlignmentAnnotation ann = aa[row];
680 if (row > -1 && ann.annotations != null
681 && column < ann.annotations.length)
683 buildToolTip(ann, column, aa);
684 setStatusMessage(column, ann);
688 this.setToolTipText(null);
689 ap.alignFrame.statusBar.setText(" ");
694 * Builds a tooltip for the annotation at the current mouse position.
700 void buildToolTip(AlignmentAnnotation ann, int column,
701 AlignmentAnnotation[] anns)
703 if (ann.graphGroup > -1)
705 StringBuilder tip = new StringBuilder(32);
706 tip.append("<html>");
707 for (int i = 0; i < anns.length; i++)
709 if (anns[i].graphGroup == ann.graphGroup
710 && anns[i].annotations[column] != null)
712 tip.append(anns[i].label);
713 String description = anns[i].annotations[column].description;
714 if (description != null && description.length() > 0)
716 tip.append(" ").append(description);
721 if (tip.length() != 6)
723 tip.setLength(tip.length() - 4);
724 this.setToolTipText(tip.toString() + "</html>");
727 else if (ann.annotations[column] != null)
729 String description = ann.annotations[column].description;
730 if (description != null && description.length() > 0)
732 this.setToolTipText(JvSwingUtils.wrapTooltip(true, description));
737 // clear the tooltip.
738 this.setToolTipText(null);
743 * Constructs and displays the status bar message
748 void setStatusMessage(int column, AlignmentAnnotation ann)
751 * show alignment column and annotation description if any
753 StringBuilder text = new StringBuilder(32);
754 text.append(MessageManager.getString("label.column")).append(" ")
757 if (ann.annotations[column] != null)
759 String description = ann.annotations[column].description;
760 if (description != null && description.trim().length() > 0)
762 text.append(" ").append(description);
767 * if the annotation is sequence-specific, show the sequence number
768 * in the alignment, and (if not a gap) the residue and position
770 SequenceI seqref = ann.sequenceRef;
773 int seqIndex = av.getAlignment().findIndex(seqref);
777 .append(MessageManager.getString("label.sequence"))
779 .append(seqIndex + 1);
780 char residue = seqref.getCharAt(column);
781 if (!Comparison.isGap(residue))
783 int residuePos = seqref.findPosition(column);
784 text.append(": ").append(residue).append(" (")
785 .append(residuePos).append(")");
790 ap.alignFrame.statusBar.setText(text.toString());
800 public void mouseClicked(MouseEvent evt)
802 // if (activeRow != -1)
804 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
805 // AlignmentAnnotation anot = aa[activeRow];
809 // TODO mouseClicked-content and drawCursor are quite experimental!
810 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
813 int pady = av.getCharHeight() / 5;
815 graphics.setColor(Color.black);
816 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
818 if (av.validCharWidth)
820 graphics.setColor(Color.white);
822 char s = seq.getCharAt(res);
824 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
825 graphics.drawString(String.valueOf(s), charOffset + x1,
826 (y1 + av.getCharHeight()) - pady);
831 private volatile boolean imageFresh = false;
840 public void paintComponent(Graphics g)
842 g.setColor(Color.white);
843 g.fillRect(0, 0, getWidth(), getHeight());
847 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
848 || (getVisibleRect().height != g.getClipBounds().height))
850 g.drawImage(image, 0, 0, this);
855 imgWidth = (av.endRes - av.startRes + 1) * av.getCharWidth();
860 if (image == null || imgWidth != image.getWidth(this)
861 || image.getHeight(this) != getHeight())
865 image = new BufferedImage(imgWidth, ap.getAnnotationPanel()
866 .getHeight(), BufferedImage.TYPE_INT_RGB);
867 } catch (OutOfMemoryError oom)
872 } catch (Exception x)
877 "Couldn't allocate memory to redraw screen. Please restart Jalview",
881 gg = (Graphics2D) image.getGraphics();
885 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
886 RenderingHints.VALUE_ANTIALIAS_ON);
889 gg.setFont(av.getFont());
890 fm = gg.getFontMetrics();
891 gg.setColor(Color.white);
892 gg.fillRect(0, 0, imgWidth, image.getHeight());
896 drawComponent(gg, av.startRes, av.endRes + 1);
898 g.drawImage(image, 0, 0, this);
902 * set true to enable redraw timing debug output on stderr
904 private final boolean debugRedraw = false;
907 * non-Thread safe repaint
910 * repaint with horizontal shift in alignment
912 public void fastPaint(int horizontal)
914 if ((horizontal == 0) || gg == null
915 || av.getAlignment().getAlignmentAnnotation() == null
916 || av.getAlignment().getAlignmentAnnotation().length < 1
917 || av.isCalcInProgress())
922 long stime = System.currentTimeMillis();
923 gg.copyArea(0, 0, imgWidth, getHeight(),
924 -horizontal * av.getCharWidth(), 0);
925 long mtime = System.currentTimeMillis();
926 int sr = av.startRes;
927 int er = av.endRes + 1;
930 if (horizontal > 0) // scrollbar pulled right, image to the left
932 transX = (er - sr - horizontal) * av.getCharWidth();
933 sr = er - horizontal;
935 else if (horizontal < 0)
937 er = sr - horizontal;
940 gg.translate(transX, 0);
942 drawComponent(gg, sr, er);
944 gg.translate(-transX, 0);
945 long dtime = System.currentTimeMillis();
948 long rtime = System.currentTimeMillis();
951 System.err.println("Scroll:\t" + horizontal + "\tCopyArea:\t"
952 + (mtime - stime) + "\tDraw component:\t" + (dtime - mtime)
953 + "\tRepaint call:\t" + (rtime - dtime));
958 private volatile boolean lastImageGood = false;
970 public void drawComponent(Graphics g, int startRes, int endRes)
972 BufferedImage oldFaded = fadedImage;
973 if (av.isCalcInProgress())
977 lastImageGood = false;
980 // We'll keep a record of the old image,
981 // and draw a faded image until the calculation
984 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
985 .getHeight() != image.getHeight()))
987 // System.err.println("redraw faded image ("+(fadedImage==null ?
988 // "null image" : "") + " lastGood="+lastImageGood+")");
989 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
990 BufferedImage.TYPE_INT_RGB);
992 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
994 fadedG.setColor(Color.white);
995 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
997 fadedG.setComposite(AlphaComposite.getInstance(
998 AlphaComposite.SRC_OVER, .3f));
999 fadedG.drawImage(image, 0, 0, this);
1002 // make sure we don't overwrite the last good faded image until all
1003 // calculations have finished
1004 lastImageGood = false;
1009 if (fadedImage != null)
1011 oldFaded = fadedImage;
1016 g.setColor(Color.white);
1017 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(), getHeight());
1019 g.setFont(av.getFont());
1022 fm = g.getFontMetrics();
1025 if ((av.getAlignment().getAlignmentAnnotation() == null)
1026 || (av.getAlignment().getAlignmentAnnotation().length < 1))
1028 g.setColor(Color.white);
1029 g.fillRect(0, 0, getWidth(), getHeight());
1030 g.setColor(Color.black);
1031 if (av.validCharWidth)
1033 g.drawString(MessageManager
1034 .getString("label.alignment_has_no_annotations"), 20, 15);
1039 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
1041 if (!lastImageGood && fadedImage == null)
1043 fadedImage = oldFaded;
1048 public FontMetrics getFontMetrics()
1054 public Image getFadedImage()
1060 public int getFadedImageWidth()
1065 private int[] bounds = new int[2];
1068 public int[] getVisibleVRange()
1070 if (ap != null && ap.getAlabels() != null)
1072 int sOffset = -ap.getAlabels().getScrollOffset();
1073 int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
1074 bounds[0] = sOffset;
1075 bounds[1] = visHeight;