2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 import java.awt.event.*;
23 import java.awt.image.*;
27 import jalview.datamodel.*;
28 import jalview.renderer.AnnotationRenderer;
29 import jalview.renderer.AwtRenderPanelI;
30 import jalview.util.MessageManager;
33 * AnnotationPanel displays visible portion of annotation rows below unwrapped
39 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
40 MouseListener, MouseWheelListener, MouseMotionListener,
41 ActionListener, AdjustmentListener, Scrollable
43 final String HELIX = "Helix";
45 final String SHEET = "Sheet";
48 * For RNA secondary structure "stems" aka helices
50 final String STEM = "RNA Helix";
52 final String LABEL = "Label";
54 final String REMOVE = "Remove Annotation";
56 final String COLOUR = "Colour";
58 public final Color HELIX_COLOUR = Color.red.darker();
60 public final Color SHEET_COLOUR = Color.green.darker().darker();
62 public final Color STEM_COLOUR = Color.blue.darker();
65 public AlignViewport av;
69 public int activeRow = -1;
71 public BufferedImage image;
73 public volatile BufferedImage fadedImage;
77 public FontMetrics fm;
79 public int imgWidth = 0;
81 boolean fastPaint = false;
83 // Used For mouse Dragging and resizing graphs
84 int graphStretch = -1;
86 int graphStretchY = -1;
88 int min; // used by mouseDragged to see if user
90 int max; // used by mouseDragged to see if user
92 boolean mouseDragging = false;
101 public final AnnotationRenderer renderer;
103 private MouseWheelListener[] _mwl;
106 * Creates a new AnnotationPanel object.
111 public AnnotationPanel(AlignmentPanel ap)
114 MAC = new jalview.util.Platform().isAMac();
116 ToolTipManager.sharedInstance().registerComponent(this);
117 ToolTipManager.sharedInstance().setInitialDelay(0);
118 ToolTipManager.sharedInstance().setDismissDelay(10000);
121 this.setLayout(null);
122 addMouseListener(this);
123 addMouseMotionListener(this);
124 ap.annotationScroller.getVerticalScrollBar()
125 .addAdjustmentListener(this);
126 // save any wheel listeners on the scroller, so we can propagate scroll
128 _mwl = ap.annotationScroller.getMouseWheelListeners();
129 // and then set our own listener to consume all mousewheel events
130 ap.annotationScroller.addMouseWheelListener(this);
131 renderer = new AnnotationRenderer();
134 public AnnotationPanel(AlignViewport av)
137 renderer = new AnnotationRenderer();
141 public void mouseWheelMoved(MouseWheelEvent e)
146 if (e.getWheelRotation() > 0)
148 ap.scrollRight(true);
152 ap.scrollRight(false);
157 // TODO: find the correct way to let the event bubble up to
158 // ap.annotationScroller
159 for (MouseWheelListener mwl : _mwl)
163 mwl.mouseWheelMoved(e);
174 public Dimension getPreferredScrollableViewportSize()
176 return getPreferredSize();
180 public int getScrollableBlockIncrement(Rectangle visibleRect,
181 int orientation, int direction)
187 public boolean getScrollableTracksViewportHeight()
193 public boolean getScrollableTracksViewportWidth()
199 public int getScrollableUnitIncrement(Rectangle visibleRect,
200 int orientation, int direction)
209 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
213 public void adjustmentValueChanged(AdjustmentEvent evt)
215 // update annotation label display
216 ap.alabels.setScrollOffset(-evt.getValue());
220 * Calculates the height of the annotation displayed in the annotation panel.
221 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
222 * all annotation associated components are updated correctly.
225 public int adjustPanelHeight()
227 int height = av.calcPanelHeight();
228 this.setPreferredSize(new Dimension(1, height));
231 // revalidate only when the alignment panel is fully constructed
245 public void actionPerformed(ActionEvent evt)
247 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
252 Annotation[] anot = aa[activeRow].annotations;
254 if (anot.length < av.getColumnSelection().getMax())
256 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
257 System.arraycopy(anot, 0, temp, 0, anot.length);
259 aa[activeRow].annotations = anot;
262 if (evt.getActionCommand().equals(REMOVE))
264 for (int i = 0; i < av.getColumnSelection().size(); i++)
266 anot[av.getColumnSelection().columnAt(i)] = null;
269 else if (evt.getActionCommand().equals(LABEL))
271 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
272 String label = JOptionPane.showInputDialog(this, MessageManager.getString("label.enter_label"),
280 if ((label.length() > 0) && !aa[activeRow].hasText)
282 aa[activeRow].hasText = true;
285 for (int i = 0; i < av.getColumnSelection().size(); i++)
287 int index = av.getColumnSelection().columnAt(i);
289 if (!av.getColumnSelection().isVisible(index))
292 if (anot[index] == null)
294 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
301 anot[index].displayCharacter = label;
305 else if (evt.getActionCommand().equals(COLOUR))
307 Color col = JColorChooser.showDialog(this,
308 "Choose foreground colour", Color.black);
310 for (int i = 0; i < av.getColumnSelection().size(); i++)
312 int index = av.getColumnSelection().columnAt(i);
314 if (!av.getColumnSelection().isVisible(index))
317 if (anot[index] == null)
319 anot[index] = new Annotation("", "", ' ', 0);
322 anot[index].colour = col;
329 String symbol = "\u03B1";
331 if (evt.getActionCommand().equals(HELIX))
335 else if (evt.getActionCommand().equals(SHEET))
341 // Added by LML to color stems
342 else if (evt.getActionCommand().equals(STEM))
348 if (!aa[activeRow].hasIcons)
350 aa[activeRow].hasIcons = true;
353 String label = JOptionPane.showInputDialog(
354 MessageManager.getString("label.enter_label_for_the_structure"), symbol);
361 if ((label.length() > 0) && !aa[activeRow].hasText)
363 aa[activeRow].hasText = true;
364 if (evt.getActionCommand().equals(STEM))
366 aa[activeRow].showAllColLabels=true;
369 for (int i = 0; i < av.getColumnSelection().size(); i++)
371 int index = av.getColumnSelection().columnAt(i);
373 if (!av.getColumnSelection().isVisible(index))
376 if (anot[index] == null)
378 anot[index] = new Annotation(label, "", type, 0);
381 anot[index].secondaryStructure = type;
382 anot[index].displayCharacter = label;
386 av.getAlignment().validateAnnotation(aa[activeRow]);
387 ap.alignmentChanged();
395 private String collectAnnotVals(Annotation[] anot,
396 ColumnSelection columnSelection, String label2)
398 String collatedInput = "";
400 ColumnSelection viscols = av.getColumnSelection();
401 // TODO: refactor and save av.getColumnSelection for efficiency
402 for (int i = 0; i < columnSelection.size(); i++)
404 int index = columnSelection.columnAt(i);
405 // always check for current display state - just in case
406 if (!viscols.isVisible(index))
408 String tlabel = null;
409 if (anot[index] != null)
410 { // LML added stem code
411 if (label2.equals(HELIX) || label2.equals(SHEET)
412 || label2.equals(STEM) || label2.equals(LABEL))
414 tlabel = anot[index].description;
415 if (tlabel == null || tlabel.length() < 1)
417 if (label2.equals(HELIX) || label2.equals(SHEET)
418 || label2.equals(STEM))
420 tlabel = "" + anot[index].secondaryStructure;
424 tlabel = "" + anot[index].displayCharacter;
428 if (tlabel != null && !tlabel.equals(last))
430 if (last.length() > 0)
432 collatedInput += " ";
434 collatedInput += tlabel;
438 return collatedInput;
448 public void mousePressed(MouseEvent evt)
451 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
460 for (int i = 0; i < aa.length; i++)
464 height += aa[i].height;
467 if (evt.getY() < height)
473 else if (aa[i].graph > 0)
477 graphStretchY = evt.getY();
484 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
486 if (av.getColumnSelection() == null)
491 JPopupMenu pop = new JPopupMenu(MessageManager.getString("label.structure_type"));
494 * Just display the needed structure options
496 if (av.getAlignment().isNucleotide() == true)
498 item = new JMenuItem(STEM);
499 item.addActionListener(this);
504 item = new JMenuItem(HELIX);
505 item.addActionListener(this);
507 item = new JMenuItem(SHEET);
508 item.addActionListener(this);
511 item = new JMenuItem(LABEL);
512 item.addActionListener(this);
514 item = new JMenuItem(COLOUR);
515 item.addActionListener(this);
517 item = new JMenuItem(REMOVE);
518 item.addActionListener(this);
520 pop.show(this, evt.getX(), evt.getY());
530 ap.scalePanel.mousePressed(evt);
541 public void mouseReleased(MouseEvent evt)
545 mouseDragging = false;
546 ap.scalePanel.mouseReleased(evt);
556 public void mouseEntered(MouseEvent evt)
558 ap.scalePanel.mouseEntered(evt);
568 public void mouseExited(MouseEvent evt)
570 ap.scalePanel.mouseExited(evt);
580 public void mouseDragged(MouseEvent evt)
582 if (graphStretch > -1)
584 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
586 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
588 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
590 graphStretchY = evt.getY();
592 ap.paintAlignment(true);
596 ap.scalePanel.mouseDragged(evt);
607 public void mouseMoved(MouseEvent evt)
609 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
613 this.setToolTipText(null);
620 for (int i = 0; i < aa.length; i++)
624 height += aa[i].height;
627 if (evt.getY() < height)
637 this.setToolTipText(null);
641 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
643 if (av.hasHiddenColumns())
645 res = av.getColumnSelection().adjustForHiddenColumns(res);
648 if (row > -1 && aa[row].annotations != null
649 && res < aa[row].annotations.length)
651 if (aa[row].graphGroup > -1)
653 StringBuffer tip = new StringBuffer("<html>");
654 for (int gg = 0; gg < aa.length; gg++)
656 if (aa[gg].graphGroup == aa[row].graphGroup
657 && aa[gg].annotations[res] != null)
659 tip.append(aa[gg].label + " "
660 + aa[gg].annotations[res].description + "<br>");
663 if (tip.length() != 6)
665 tip.setLength(tip.length() - 4);
666 this.setToolTipText(tip.toString() + "</html>");
669 else if (aa[row].annotations[res] != null
670 && aa[row].annotations[res].description != null
671 && aa[row].annotations[res].description.length() > 0)
673 this.setToolTipText("<html>"+JvSwingUtils.wrapTooltip(aa[row].annotations[res].description)+"</html>");
677 // clear the tooltip.
678 this.setToolTipText(null);
681 if (aa[row].annotations[res] != null)
683 StringBuffer text = new StringBuffer("Sequence position "
686 if (aa[row].annotations[res].description != null)
688 text.append(" " + aa[row].annotations[res].description);
691 ap.alignFrame.statusBar.setText(text.toString());
696 this.setToolTipText(null);
707 public void mouseClicked(MouseEvent evt)
709 // if (activeRow != -1)
711 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
712 // AlignmentAnnotation anot = aa[activeRow];
716 // TODO mouseClicked-content and drawCursor are quite experimental!
717 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
720 int pady = av.charHeight / 5;
722 graphics.setColor(Color.black);
723 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
725 if (av.validCharWidth)
727 graphics.setColor(Color.white);
729 char s = seq.getCharAt(res);
731 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
732 graphics.drawString(String.valueOf(s), charOffset + x1,
733 (y1 + av.charHeight) - pady);
738 private volatile boolean imageFresh = false;
747 public void paintComponent(Graphics g)
749 g.setColor(Color.white);
750 g.fillRect(0, 0, getWidth(), getHeight());
754 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
755 || (getVisibleRect().height != g.getClipBounds().height))
757 g.drawImage(image, 0, 0, this);
762 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
765 if (image == null || imgWidth != image.getWidth(this)
766 || image.getHeight(this) != getHeight())
770 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
771 BufferedImage.TYPE_INT_RGB);
772 } catch (OutOfMemoryError oom)
777 } catch (Exception x)
782 "Couldn't allocate memory to redraw screen. Please restart Jalview",
786 gg = (Graphics2D) image.getGraphics();
790 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
791 RenderingHints.VALUE_ANTIALIAS_ON);
794 gg.setFont(av.getFont());
795 fm = gg.getFontMetrics();
796 gg.setColor(Color.white);
797 gg.fillRect(0, 0, imgWidth, image.getHeight());
801 drawComponent(gg, av.startRes, av.endRes + 1);
803 g.drawImage(image, 0, 0, this);
806 * set true to enable redraw timing debug output on stderr
808 private final boolean debugRedraw = false;
810 * non-Thread safe repaint
813 * repaint with horizontal shift in alignment
815 public void fastPaint(int horizontal)
817 if ((horizontal == 0) || gg == null
818 || av.getAlignment().getAlignmentAnnotation() == null
819 || av.getAlignment().getAlignmentAnnotation().length < 1
820 || av.isCalcInProgress())
825 long stime=System.currentTimeMillis();
826 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
827 long mtime=System.currentTimeMillis();
828 int sr = av.startRes;
829 int er = av.endRes + 1;
832 if (horizontal > 0) // scrollbar pulled right, image to the left
834 transX = (er - sr - horizontal) * av.charWidth;
835 sr = er - horizontal;
837 else if (horizontal < 0)
839 er = sr - horizontal;
842 gg.translate(transX, 0);
844 drawComponent(gg, sr, er);
846 gg.translate(-transX, 0);
847 long dtime=System.currentTimeMillis();
850 long rtime=System.currentTimeMillis();
852 System.err.println("Scroll:\t"+horizontal+"\tCopyArea:\t"+(mtime-stime)+"\tDraw component:\t"+(dtime-mtime)+"\tRepaint call:\t"+(rtime-dtime));
857 private volatile boolean lastImageGood = false;
869 public void drawComponent(Graphics g, int startRes, int endRes)
871 BufferedImage oldFaded = fadedImage;
872 if (av.isCalcInProgress())
876 lastImageGood = false;
879 // We'll keep a record of the old image,
880 // and draw a faded image until the calculation
883 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
884 .getHeight() != image.getHeight()))
886 // System.err.println("redraw faded image ("+(fadedImage==null ?
887 // "null image" : "") + " lastGood="+lastImageGood+")");
888 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
889 BufferedImage.TYPE_INT_RGB);
891 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
893 fadedG.setColor(Color.white);
894 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
896 fadedG.setComposite(AlphaComposite.getInstance(
897 AlphaComposite.SRC_OVER, .3f));
898 fadedG.drawImage(image, 0, 0, this);
901 // make sure we don't overwrite the last good faded image until all
902 // calculations have finished
903 lastImageGood = false;
908 if (fadedImage != null)
910 oldFaded = fadedImage;
915 g.setColor(Color.white);
916 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
918 g.setFont(av.getFont());
921 fm = g.getFontMetrics();
924 if ((av.getAlignment().getAlignmentAnnotation() == null)
925 || (av.getAlignment().getAlignmentAnnotation().length < 1))
927 g.setColor(Color.white);
928 g.fillRect(0, 0, getWidth(), getHeight());
929 g.setColor(Color.black);
930 if (av.validCharWidth)
932 g.drawString(MessageManager.getString("label.alignment_has_no_annotations"), 20, 15);
937 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
939 if (!lastImageGood && fadedImage == null)
941 fadedImage = oldFaded;
946 public FontMetrics getFontMetrics()
952 public Image getFadedImage()
958 public int getFadedImageWidth()
962 private int[] bounds = new int[2];
964 public int[] getVisibleVRange()
966 if (ap!=null && ap.alabels!=null)
968 int sOffset=-ap.alabels.scrollOffset;
969 int visHeight = sOffset+ap.annotationSpaceFillerHolder.getHeight();
970 bounds[0] = sOffset; bounds[1]=visHeight;