2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
24 import java.awt.event.*;
25 import java.awt.image.*;
29 import jalview.datamodel.*;
30 import jalview.renderer.AnnotationRenderer;
31 import jalview.renderer.AwtRenderPanelI;
32 import jalview.util.MessageManager;
35 * AnnotationPanel displays visible portion of annotation rows below unwrapped
41 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
42 MouseListener, MouseWheelListener, MouseMotionListener,
43 ActionListener, AdjustmentListener, Scrollable
45 final String HELIX = "Helix";
47 final String SHEET = "Sheet";
50 * For RNA secondary structure "stems" aka helices
52 final String STEM = "RNA Helix";
54 final String LABEL = "Label";
56 final String REMOVE = "Remove Annotation";
58 final String COLOUR = "Colour";
60 public final Color HELIX_COLOUR = Color.red.darker();
62 public final Color SHEET_COLOUR = Color.green.darker().darker();
64 public final Color STEM_COLOUR = Color.blue.darker();
67 public AlignViewport av;
71 public int activeRow = -1;
73 public BufferedImage image;
75 public volatile BufferedImage fadedImage;
79 public FontMetrics fm;
81 public int imgWidth = 0;
83 boolean fastPaint = false;
85 // Used For mouse Dragging and resizing graphs
86 int graphStretch = -1;
88 int graphStretchY = -1;
90 int min; // used by mouseDragged to see if user
92 int max; // used by mouseDragged to see if user
94 boolean mouseDragging = false;
103 public final AnnotationRenderer renderer;
105 private MouseWheelListener[] _mwl;
108 * Creates a new AnnotationPanel object.
113 public AnnotationPanel(AlignmentPanel ap)
116 MAC = new jalview.util.Platform().isAMac();
118 ToolTipManager.sharedInstance().registerComponent(this);
119 ToolTipManager.sharedInstance().setInitialDelay(0);
120 ToolTipManager.sharedInstance().setDismissDelay(10000);
123 this.setLayout(null);
124 addMouseListener(this);
125 addMouseMotionListener(this);
126 ap.annotationScroller.getVerticalScrollBar()
127 .addAdjustmentListener(this);
128 // save any wheel listeners on the scroller, so we can propagate scroll
130 _mwl = ap.annotationScroller.getMouseWheelListeners();
131 // and then set our own listener to consume all mousewheel events
132 ap.annotationScroller.addMouseWheelListener(this);
133 renderer = new AnnotationRenderer();
136 public AnnotationPanel(AlignViewport av)
139 renderer = new AnnotationRenderer();
143 public void mouseWheelMoved(MouseWheelEvent e)
148 if (e.getWheelRotation() > 0)
150 ap.scrollRight(true);
154 ap.scrollRight(false);
159 // TODO: find the correct way to let the event bubble up to
160 // ap.annotationScroller
161 for (MouseWheelListener mwl : _mwl)
165 mwl.mouseWheelMoved(e);
176 public Dimension getPreferredScrollableViewportSize()
178 return getPreferredSize();
182 public int getScrollableBlockIncrement(Rectangle visibleRect,
183 int orientation, int direction)
189 public boolean getScrollableTracksViewportHeight()
195 public boolean getScrollableTracksViewportWidth()
201 public int getScrollableUnitIncrement(Rectangle visibleRect,
202 int orientation, int direction)
211 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
215 public void adjustmentValueChanged(AdjustmentEvent evt)
217 // update annotation label display
218 ap.alabels.setScrollOffset(-evt.getValue());
222 * Calculates the height of the annotation displayed in the annotation panel.
223 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
224 * all annotation associated components are updated correctly.
227 public int adjustPanelHeight()
229 int height = av.calcPanelHeight();
230 this.setPreferredSize(new Dimension(1, height));
233 // revalidate only when the alignment panel is fully constructed
247 public void actionPerformed(ActionEvent evt)
249 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
254 Annotation[] anot = aa[activeRow].annotations;
256 if (anot.length < av.getColumnSelection().getMax())
258 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
259 System.arraycopy(anot, 0, temp, 0, anot.length);
261 aa[activeRow].annotations = anot;
264 if (evt.getActionCommand().equals(REMOVE))
266 for (int i = 0; i < av.getColumnSelection().size(); i++)
268 anot[av.getColumnSelection().columnAt(i)] = null;
271 else if (evt.getActionCommand().equals(LABEL))
273 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
274 String label = JOptionPane.showInputDialog(this,
275 MessageManager.getString("label.enter_label"), exMesg);
282 if ((label.length() > 0) && !aa[activeRow].hasText)
284 aa[activeRow].hasText = true;
287 for (int i = 0; i < av.getColumnSelection().size(); i++)
289 int index = av.getColumnSelection().columnAt(i);
291 if (!av.getColumnSelection().isVisible(index))
294 if (anot[index] == null)
296 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
303 anot[index].displayCharacter = label;
307 else if (evt.getActionCommand().equals(COLOUR))
309 Color col = JColorChooser.showDialog(this,
310 "Choose foreground colour", Color.black);
312 for (int i = 0; i < av.getColumnSelection().size(); i++)
314 int index = av.getColumnSelection().columnAt(i);
316 if (!av.getColumnSelection().isVisible(index))
319 if (anot[index] == null)
321 anot[index] = new Annotation("", "", ' ', 0);
324 anot[index].colour = col;
331 String symbol = "\u03B1";
333 if (evt.getActionCommand().equals(HELIX))
337 else if (evt.getActionCommand().equals(SHEET))
343 // Added by LML to color stems
344 else if (evt.getActionCommand().equals(STEM))
350 if (!aa[activeRow].hasIcons)
352 aa[activeRow].hasIcons = true;
355 String label = JOptionPane.showInputDialog(MessageManager
356 .getString("label.enter_label_for_the_structure"), symbol);
363 if ((label.length() > 0) && !aa[activeRow].hasText)
365 aa[activeRow].hasText = true;
366 if (evt.getActionCommand().equals(STEM))
368 aa[activeRow].showAllColLabels = true;
371 for (int i = 0; i < av.getColumnSelection().size(); i++)
373 int index = av.getColumnSelection().columnAt(i);
375 if (!av.getColumnSelection().isVisible(index))
378 if (anot[index] == null)
380 anot[index] = new Annotation(label, "", type, 0);
383 anot[index].secondaryStructure = type;
384 anot[index].displayCharacter = label;
388 av.getAlignment().validateAnnotation(aa[activeRow]);
389 ap.alignmentChanged();
397 private String collectAnnotVals(Annotation[] anot,
398 ColumnSelection columnSelection, String label2)
400 String collatedInput = "";
402 ColumnSelection viscols = av.getColumnSelection();
403 // TODO: refactor and save av.getColumnSelection for efficiency
404 for (int i = 0; i < columnSelection.size(); i++)
406 int index = columnSelection.columnAt(i);
407 // always check for current display state - just in case
408 if (!viscols.isVisible(index))
410 String tlabel = null;
411 if (anot[index] != null)
412 { // LML added stem code
413 if (label2.equals(HELIX) || label2.equals(SHEET)
414 || label2.equals(STEM) || label2.equals(LABEL))
416 tlabel = anot[index].description;
417 if (tlabel == null || tlabel.length() < 1)
419 if (label2.equals(HELIX) || label2.equals(SHEET)
420 || label2.equals(STEM))
422 tlabel = "" + anot[index].secondaryStructure;
426 tlabel = "" + anot[index].displayCharacter;
430 if (tlabel != null && !tlabel.equals(last))
432 if (last.length() > 0)
434 collatedInput += " ";
436 collatedInput += tlabel;
440 return collatedInput;
450 public void mousePressed(MouseEvent evt)
453 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
462 for (int i = 0; i < aa.length; i++)
466 height += aa[i].height;
469 if (evt.getY() < height)
475 else if (aa[i].graph > 0)
479 graphStretchY = evt.getY();
486 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
488 if (av.getColumnSelection() == null)
493 JPopupMenu pop = new JPopupMenu(
494 MessageManager.getString("label.structure_type"));
497 * Just display the needed structure options
499 if (av.getAlignment().isNucleotide() == true)
501 item = new JMenuItem(STEM);
502 item.addActionListener(this);
507 item = new JMenuItem(HELIX);
508 item.addActionListener(this);
510 item = new JMenuItem(SHEET);
511 item.addActionListener(this);
514 item = new JMenuItem(LABEL);
515 item.addActionListener(this);
517 item = new JMenuItem(COLOUR);
518 item.addActionListener(this);
520 item = new JMenuItem(REMOVE);
521 item.addActionListener(this);
523 pop.show(this, evt.getX(), evt.getY());
533 ap.scalePanel.mousePressed(evt);
544 public void mouseReleased(MouseEvent evt)
548 mouseDragging = false;
549 ap.scalePanel.mouseReleased(evt);
559 public void mouseEntered(MouseEvent evt)
561 ap.scalePanel.mouseEntered(evt);
571 public void mouseExited(MouseEvent evt)
573 ap.scalePanel.mouseExited(evt);
583 public void mouseDragged(MouseEvent evt)
585 if (graphStretch > -1)
587 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
589 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
591 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
593 graphStretchY = evt.getY();
595 ap.paintAlignment(true);
599 ap.scalePanel.mouseDragged(evt);
610 public void mouseMoved(MouseEvent evt)
612 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
616 this.setToolTipText(null);
623 for (int i = 0; i < aa.length; i++)
627 height += aa[i].height;
630 if (evt.getY() < height)
640 this.setToolTipText(null);
644 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
646 if (av.hasHiddenColumns())
648 res = av.getColumnSelection().adjustForHiddenColumns(res);
651 if (row > -1 && aa[row].annotations != null
652 && res < aa[row].annotations.length)
654 if (aa[row].graphGroup > -1)
656 StringBuffer tip = new StringBuffer("<html>");
657 for (int gg = 0; gg < aa.length; gg++)
659 if (aa[gg].graphGroup == aa[row].graphGroup
660 && aa[gg].annotations[res] != null)
662 tip.append(aa[gg].label + " "
663 + aa[gg].annotations[res].description + "<br>");
666 if (tip.length() != 6)
668 tip.setLength(tip.length() - 4);
669 this.setToolTipText(tip.toString() + "</html>");
672 else if (aa[row].annotations[res] != null
673 && aa[row].annotations[res].description != null
674 && aa[row].annotations[res].description.length() > 0)
676 this.setToolTipText("<html>"
678 .wrapTooltip(aa[row].annotations[res].description)
683 // clear the tooltip.
684 this.setToolTipText(null);
687 if (aa[row].annotations[res] != null)
689 StringBuffer text = new StringBuffer("Sequence position "
692 if (aa[row].annotations[res].description != null)
694 text.append(" " + aa[row].annotations[res].description);
697 ap.alignFrame.statusBar.setText(text.toString());
702 this.setToolTipText(null);
713 public void mouseClicked(MouseEvent evt)
715 // if (activeRow != -1)
717 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
718 // AlignmentAnnotation anot = aa[activeRow];
722 // TODO mouseClicked-content and drawCursor are quite experimental!
723 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
726 int pady = av.charHeight / 5;
728 graphics.setColor(Color.black);
729 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
731 if (av.validCharWidth)
733 graphics.setColor(Color.white);
735 char s = seq.getCharAt(res);
737 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
738 graphics.drawString(String.valueOf(s), charOffset + x1,
739 (y1 + av.charHeight) - pady);
744 private volatile boolean imageFresh = false;
753 public void paintComponent(Graphics g)
755 g.setColor(Color.white);
756 g.fillRect(0, 0, getWidth(), getHeight());
760 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
761 || (getVisibleRect().height != g.getClipBounds().height))
763 g.drawImage(image, 0, 0, this);
768 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
771 if (image == null || imgWidth != image.getWidth(this)
772 || image.getHeight(this) != getHeight())
776 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
777 BufferedImage.TYPE_INT_RGB);
778 } catch (OutOfMemoryError oom)
783 } catch (Exception x)
788 "Couldn't allocate memory to redraw screen. Please restart Jalview",
792 gg = (Graphics2D) image.getGraphics();
796 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
797 RenderingHints.VALUE_ANTIALIAS_ON);
800 gg.setFont(av.getFont());
801 fm = gg.getFontMetrics();
802 gg.setColor(Color.white);
803 gg.fillRect(0, 0, imgWidth, image.getHeight());
807 drawComponent(gg, av.startRes, av.endRes + 1);
809 g.drawImage(image, 0, 0, this);
813 * set true to enable redraw timing debug output on stderr
815 private final boolean debugRedraw = false;
818 * non-Thread safe repaint
821 * repaint with horizontal shift in alignment
823 public void fastPaint(int horizontal)
825 if ((horizontal == 0) || gg == null
826 || av.getAlignment().getAlignmentAnnotation() == null
827 || av.getAlignment().getAlignmentAnnotation().length < 1
828 || av.isCalcInProgress())
833 long stime = System.currentTimeMillis();
834 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
835 long mtime = System.currentTimeMillis();
836 int sr = av.startRes;
837 int er = av.endRes + 1;
840 if (horizontal > 0) // scrollbar pulled right, image to the left
842 transX = (er - sr - horizontal) * av.charWidth;
843 sr = er - horizontal;
845 else if (horizontal < 0)
847 er = sr - horizontal;
850 gg.translate(transX, 0);
852 drawComponent(gg, sr, er);
854 gg.translate(-transX, 0);
855 long dtime = System.currentTimeMillis();
858 long rtime = System.currentTimeMillis();
861 System.err.println("Scroll:\t" + horizontal + "\tCopyArea:\t"
862 + (mtime - stime) + "\tDraw component:\t" + (dtime - mtime)
863 + "\tRepaint call:\t" + (rtime - dtime));
868 private volatile boolean lastImageGood = false;
880 public void drawComponent(Graphics g, int startRes, int endRes)
882 BufferedImage oldFaded = fadedImage;
883 if (av.isCalcInProgress())
887 lastImageGood = false;
890 // We'll keep a record of the old image,
891 // and draw a faded image until the calculation
894 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
895 .getHeight() != image.getHeight()))
897 // System.err.println("redraw faded image ("+(fadedImage==null ?
898 // "null image" : "") + " lastGood="+lastImageGood+")");
899 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
900 BufferedImage.TYPE_INT_RGB);
902 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
904 fadedG.setColor(Color.white);
905 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
907 fadedG.setComposite(AlphaComposite.getInstance(
908 AlphaComposite.SRC_OVER, .3f));
909 fadedG.drawImage(image, 0, 0, this);
912 // make sure we don't overwrite the last good faded image until all
913 // calculations have finished
914 lastImageGood = false;
919 if (fadedImage != null)
921 oldFaded = fadedImage;
926 g.setColor(Color.white);
927 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
929 g.setFont(av.getFont());
932 fm = g.getFontMetrics();
935 if ((av.getAlignment().getAlignmentAnnotation() == null)
936 || (av.getAlignment().getAlignmentAnnotation().length < 1))
938 g.setColor(Color.white);
939 g.fillRect(0, 0, getWidth(), getHeight());
940 g.setColor(Color.black);
941 if (av.validCharWidth)
943 g.drawString(MessageManager
944 .getString("label.alignment_has_no_annotations"), 20, 15);
949 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
951 if (!lastImageGood && fadedImage == null)
953 fadedImage = oldFaded;
958 public FontMetrics getFontMetrics()
964 public Image getFadedImage()
970 public int getFadedImageWidth()
975 private int[] bounds = new int[2];
978 public int[] getVisibleVRange()
980 if (ap != null && ap.alabels != null)
982 int sOffset = -ap.alabels.scrollOffset;
983 int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
985 bounds[1] = visHeight;