2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
21 import java.awt.event.*;
22 import java.awt.image.*;
26 import jalview.datamodel.*;
27 import jalview.renderer.AnnotationRenderer;
28 import jalview.renderer.AwtRenderPanelI;
29 import jalview.util.MessageManager;
32 * AnnotationPanel displays visible portion of annotation rows below unwrapped
38 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
39 MouseListener, MouseWheelListener, MouseMotionListener,
40 ActionListener, AdjustmentListener, Scrollable
42 final String HELIX = "Helix";
44 final String SHEET = "Sheet";
47 * For RNA secondary structure "stems" aka helices
49 final String STEM = "RNA Helix";
51 final String LABEL = "Label";
53 final String REMOVE = "Remove Annotation";
55 final String COLOUR = "Colour";
57 public final Color HELIX_COLOUR = Color.red.darker();
59 public final Color SHEET_COLOUR = Color.green.darker().darker();
61 public final Color STEM_COLOUR = Color.blue.darker();
64 public AlignViewport av;
68 public int activeRow = -1;
70 public BufferedImage image;
72 public volatile BufferedImage fadedImage;
76 public FontMetrics fm;
78 public int imgWidth = 0;
80 boolean fastPaint = false;
82 // Used For mouse Dragging and resizing graphs
83 int graphStretch = -1;
85 int graphStretchY = -1;
87 int min; // used by mouseDragged to see if user
89 int max; // used by mouseDragged to see if user
91 boolean mouseDragging = false;
100 public final AnnotationRenderer renderer;
102 private MouseWheelListener[] _mwl;
105 * Creates a new AnnotationPanel object.
110 public AnnotationPanel(AlignmentPanel ap)
113 MAC = new jalview.util.Platform().isAMac();
115 ToolTipManager.sharedInstance().registerComponent(this);
116 ToolTipManager.sharedInstance().setInitialDelay(0);
117 ToolTipManager.sharedInstance().setDismissDelay(10000);
120 this.setLayout(null);
121 addMouseListener(this);
122 addMouseMotionListener(this);
123 ap.annotationScroller.getVerticalScrollBar()
124 .addAdjustmentListener(this);
125 // save any wheel listeners on the scroller, so we can propagate scroll
127 _mwl = ap.annotationScroller.getMouseWheelListeners();
128 // and then set our own listener to consume all mousewheel events
129 ap.annotationScroller.addMouseWheelListener(this);
130 renderer = new AnnotationRenderer();
133 public AnnotationPanel(AlignViewport av)
136 renderer = new AnnotationRenderer();
140 public void mouseWheelMoved(MouseWheelEvent e)
145 if (e.getWheelRotation() > 0)
147 ap.scrollRight(true);
151 ap.scrollRight(false);
156 // TODO: find the correct way to let the event bubble up to
157 // ap.annotationScroller
158 for (MouseWheelListener mwl : _mwl)
162 mwl.mouseWheelMoved(e);
173 public Dimension getPreferredScrollableViewportSize()
175 return getPreferredSize();
179 public int getScrollableBlockIncrement(Rectangle visibleRect,
180 int orientation, int direction)
186 public boolean getScrollableTracksViewportHeight()
192 public boolean getScrollableTracksViewportWidth()
198 public int getScrollableUnitIncrement(Rectangle visibleRect,
199 int orientation, int direction)
208 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
212 public void adjustmentValueChanged(AdjustmentEvent evt)
214 // update annotation label display
215 ap.alabels.setScrollOffset(-evt.getValue());
219 * Calculates the height of the annotation displayed in the annotation panel.
220 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
221 * all annotation associated components are updated correctly.
224 public int adjustPanelHeight()
226 int height = av.calcPanelHeight();
227 this.setPreferredSize(new Dimension(1, height));
230 // revalidate only when the alignment panel is fully constructed
244 public void actionPerformed(ActionEvent evt)
246 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
251 Annotation[] anot = aa[activeRow].annotations;
253 if (anot.length < av.getColumnSelection().getMax())
255 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
256 System.arraycopy(anot, 0, temp, 0, anot.length);
258 aa[activeRow].annotations = anot;
261 if (evt.getActionCommand().equals(REMOVE))
263 for (int i = 0; i < av.getColumnSelection().size(); i++)
265 anot[av.getColumnSelection().columnAt(i)] = null;
268 else if (evt.getActionCommand().equals(LABEL))
270 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
271 String label = JOptionPane.showInputDialog(this, MessageManager.getString("label.enter_label"),
279 if ((label.length() > 0) && !aa[activeRow].hasText)
281 aa[activeRow].hasText = true;
284 for (int i = 0; i < av.getColumnSelection().size(); i++)
286 int index = av.getColumnSelection().columnAt(i);
288 if (!av.getColumnSelection().isVisible(index))
291 if (anot[index] == null)
293 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
300 anot[index].displayCharacter = label;
304 else if (evt.getActionCommand().equals(COLOUR))
306 Color col = JColorChooser.showDialog(this,
307 "Choose foreground colour", Color.black);
309 for (int i = 0; i < av.getColumnSelection().size(); i++)
311 int index = av.getColumnSelection().columnAt(i);
313 if (!av.getColumnSelection().isVisible(index))
316 if (anot[index] == null)
318 anot[index] = new Annotation("", "", ' ', 0);
321 anot[index].colour = col;
328 String symbol = "\u03B1";
330 if (evt.getActionCommand().equals(HELIX))
334 else if (evt.getActionCommand().equals(SHEET))
340 // Added by LML to color stems
341 else if (evt.getActionCommand().equals(STEM))
347 if (!aa[activeRow].hasIcons)
349 aa[activeRow].hasIcons = true;
352 String label = JOptionPane.showInputDialog(
353 MessageManager.getString("label.enter_label_for_the_structure"), symbol);
360 if ((label.length() > 0) && !aa[activeRow].hasText)
362 aa[activeRow].hasText = true;
365 for (int i = 0; i < av.getColumnSelection().size(); i++)
367 int index = av.getColumnSelection().columnAt(i);
369 if (!av.getColumnSelection().isVisible(index))
372 if (anot[index] == null)
374 anot[index] = new Annotation(label, "", type, 0);
377 anot[index].secondaryStructure = type;
378 anot[index].displayCharacter = label;
381 av.getAlignment().validateAnnotation(aa[activeRow]);
382 ap.alignmentChanged();
390 private String collectAnnotVals(Annotation[] anot,
391 ColumnSelection columnSelection, String label2)
393 String collatedInput = "";
395 ColumnSelection viscols = av.getColumnSelection();
396 // TODO: refactor and save av.getColumnSelection for efficiency
397 for (int i = 0; i < columnSelection.size(); i++)
399 int index = columnSelection.columnAt(i);
400 // always check for current display state - just in case
401 if (!viscols.isVisible(index))
403 String tlabel = null;
404 if (anot[index] != null)
405 { // LML added stem code
406 if (label2.equals(HELIX) || label2.equals(SHEET)
407 || label2.equals(STEM) || label2.equals(LABEL))
409 tlabel = anot[index].description;
410 if (tlabel == null || tlabel.length() < 1)
412 if (label2.equals(HELIX) || label2.equals(SHEET)
413 || label2.equals(STEM))
415 tlabel = "" + anot[index].secondaryStructure;
419 tlabel = "" + anot[index].displayCharacter;
423 if (tlabel != null && !tlabel.equals(last))
425 if (last.length() > 0)
427 collatedInput += " ";
429 collatedInput += tlabel;
433 return collatedInput;
443 public void mousePressed(MouseEvent evt)
446 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
455 for (int i = 0; i < aa.length; i++)
459 height += aa[i].height;
462 if (evt.getY() < height)
468 else if (aa[i].graph > 0)
472 graphStretchY = evt.getY();
479 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
481 if (av.getColumnSelection() == null)
486 JPopupMenu pop = new JPopupMenu("Structure type");
489 * Just display the needed structure options
491 if (av.getAlignment().isNucleotide() == true)
493 item = new JMenuItem(STEM);
494 item.addActionListener(this);
499 item = new JMenuItem(HELIX);
500 item.addActionListener(this);
502 item = new JMenuItem(SHEET);
503 item.addActionListener(this);
506 item = new JMenuItem(LABEL);
507 item.addActionListener(this);
509 item = new JMenuItem(COLOUR);
510 item.addActionListener(this);
512 item = new JMenuItem(REMOVE);
513 item.addActionListener(this);
515 pop.show(this, evt.getX(), evt.getY());
525 ap.scalePanel.mousePressed(evt);
536 public void mouseReleased(MouseEvent evt)
540 mouseDragging = false;
541 ap.scalePanel.mouseReleased(evt);
551 public void mouseEntered(MouseEvent evt)
553 ap.scalePanel.mouseEntered(evt);
563 public void mouseExited(MouseEvent evt)
565 ap.scalePanel.mouseExited(evt);
575 public void mouseDragged(MouseEvent evt)
577 if (graphStretch > -1)
579 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
581 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
583 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
585 graphStretchY = evt.getY();
587 ap.paintAlignment(true);
591 ap.scalePanel.mouseDragged(evt);
602 public void mouseMoved(MouseEvent evt)
604 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
608 this.setToolTipText(null);
615 for (int i = 0; i < aa.length; i++)
619 height += aa[i].height;
622 if (evt.getY() < height)
632 this.setToolTipText(null);
636 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
638 if (av.hasHiddenColumns())
640 res = av.getColumnSelection().adjustForHiddenColumns(res);
643 if (row > -1 && aa[row].annotations != null
644 && res < aa[row].annotations.length)
646 if (aa[row].graphGroup > -1)
648 StringBuffer tip = new StringBuffer("<html>");
649 for (int gg = 0; gg < aa.length; gg++)
651 if (aa[gg].graphGroup == aa[row].graphGroup
652 && aa[gg].annotations[res] != null)
654 tip.append(aa[gg].label + " "
655 + aa[gg].annotations[res].description + "<br>");
658 if (tip.length() != 6)
660 tip.setLength(tip.length() - 4);
661 this.setToolTipText(tip.toString() + "</html>");
664 else if (aa[row].annotations[res] != null
665 && aa[row].annotations[res].description != null
666 && aa[row].annotations[res].description.length() > 0)
668 this.setToolTipText("<html>"+JvSwingUtils.wrapTooltip(aa[row].annotations[res].description)+"</html>");
672 // clear the tooltip.
673 this.setToolTipText(null);
676 if (aa[row].annotations[res] != null)
678 StringBuffer text = new StringBuffer("Sequence position "
681 if (aa[row].annotations[res].description != null)
683 text.append(" " + aa[row].annotations[res].description);
686 ap.alignFrame.statusBar.setText(text.toString());
691 this.setToolTipText(null);
702 public void mouseClicked(MouseEvent evt)
704 // if (activeRow != -1)
706 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
707 // AlignmentAnnotation anot = aa[activeRow];
711 // TODO mouseClicked-content and drawCursor are quite experimental!
712 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
715 int pady = av.charHeight / 5;
717 graphics.setColor(Color.black);
718 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
720 if (av.validCharWidth)
722 graphics.setColor(Color.white);
724 char s = seq.getCharAt(res);
726 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
727 graphics.drawString(String.valueOf(s), charOffset + x1,
728 (y1 + av.charHeight) - pady);
733 private volatile boolean imageFresh = false;
742 public void paintComponent(Graphics g)
744 g.setColor(Color.white);
745 g.fillRect(0, 0, getWidth(), getHeight());
749 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
750 || (getVisibleRect().height != g.getClipBounds().height))
752 g.drawImage(image, 0, 0, this);
757 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
760 if (image == null || imgWidth != image.getWidth(this)
761 || image.getHeight(this) != getHeight())
765 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
766 BufferedImage.TYPE_INT_RGB);
767 } catch (OutOfMemoryError oom)
772 } catch (Exception x)
777 "Couldn't allocate memory to redraw screen. Please restart Jalview",
781 gg = (Graphics2D) image.getGraphics();
785 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
786 RenderingHints.VALUE_ANTIALIAS_ON);
789 gg.setFont(av.getFont());
790 fm = gg.getFontMetrics();
791 gg.setColor(Color.white);
792 gg.fillRect(0, 0, imgWidth, image.getHeight());
796 drawComponent(gg, av.startRes, av.endRes + 1);
798 g.drawImage(image, 0, 0, this);
801 * set true to enable redraw timing debug output on stderr
803 private final boolean debugRedraw = false;
805 * non-Thread safe repaint
808 * repaint with horizontal shift in alignment
810 public void fastPaint(int horizontal)
812 if ((horizontal == 0) || gg == null
813 || av.getAlignment().getAlignmentAnnotation() == null
814 || av.getAlignment().getAlignmentAnnotation().length < 1
815 || av.isCalcInProgress())
820 long stime=System.currentTimeMillis();
821 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
822 long mtime=System.currentTimeMillis();
823 int sr = av.startRes;
824 int er = av.endRes + 1;
827 if (horizontal > 0) // scrollbar pulled right, image to the left
829 transX = (er - sr - horizontal) * av.charWidth;
830 sr = er - horizontal;
832 else if (horizontal < 0)
834 er = sr - horizontal;
837 gg.translate(transX, 0);
839 drawComponent(gg, sr, er);
841 gg.translate(-transX, 0);
842 long dtime=System.currentTimeMillis();
845 long rtime=System.currentTimeMillis();
847 System.err.println("Scroll:\t"+horizontal+"\tCopyArea:\t"+(mtime-stime)+"\tDraw component:\t"+(dtime-mtime)+"\tRepaint call:\t"+(rtime-dtime));
852 private volatile boolean lastImageGood = false;
864 public void drawComponent(Graphics g, int startRes, int endRes)
866 BufferedImage oldFaded = fadedImage;
867 if (av.isCalcInProgress())
871 lastImageGood = false;
874 // We'll keep a record of the old image,
875 // and draw a faded image until the calculation
878 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
879 .getHeight() != image.getHeight()))
881 // System.err.println("redraw faded image ("+(fadedImage==null ?
882 // "null image" : "") + " lastGood="+lastImageGood+")");
883 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
884 BufferedImage.TYPE_INT_RGB);
886 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
888 fadedG.setColor(Color.white);
889 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
891 fadedG.setComposite(AlphaComposite.getInstance(
892 AlphaComposite.SRC_OVER, .3f));
893 fadedG.drawImage(image, 0, 0, this);
896 // make sure we don't overwrite the last good faded image until all
897 // calculations have finished
898 lastImageGood = false;
903 if (fadedImage != null)
905 oldFaded = fadedImage;
910 g.setColor(Color.white);
911 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
913 g.setFont(av.getFont());
916 fm = g.getFontMetrics();
919 if ((av.getAlignment().getAlignmentAnnotation() == null)
920 || (av.getAlignment().getAlignmentAnnotation().length < 1))
922 g.setColor(Color.white);
923 g.fillRect(0, 0, getWidth(), getHeight());
924 g.setColor(Color.black);
925 if (av.validCharWidth)
927 g.drawString(MessageManager.getString("label.alignment_has_no_annotations"), 20, 15);
932 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
934 if (!lastImageGood && fadedImage == null)
936 fadedImage = oldFaded;
941 public FontMetrics getFontMetrics()
947 public Image getFadedImage()
953 public int getFadedImageWidth()
957 private int[] bounds = new int[2];
959 public int[] getVisibleVRange()
961 if (ap!=null && ap.alabels!=null)
963 int sOffset=-ap.alabels.scrollOffset;
964 int visHeight = sOffset+ap.annotationSpaceFillerHolder.getHeight();
965 bounds[0] = sOffset; bounds[1]=visHeight;