2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.*;
25 import jalview.datamodel.*;
26 import jalview.renderer.AnnotationRenderer;
27 import jalview.renderer.AwtRenderPanelI;
30 * AnnotationPanel displays visible portion of annotation rows below unwrapped alignment
34 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
35 MouseListener, MouseWheelListener, MouseMotionListener,
36 ActionListener, AdjustmentListener, Scrollable
38 final String HELIX = "Helix";
40 final String SHEET = "Sheet";
43 * For RNA secondary structure "stems" aka helices
45 final String STEM = "RNA Helix";
47 final String LABEL = "Label";
49 final String REMOVE = "Remove Annotation";
51 final String COLOUR = "Colour";
53 public final Color HELIX_COLOUR = Color.red.darker();
55 public final Color SHEET_COLOUR = Color.green.darker().darker();
57 public final Color STEM_COLOUR = Color.blue.darker();
60 public AlignViewport av;
64 public int activeRow = -1;
66 public BufferedImage image;
68 public volatile BufferedImage fadedImage;
72 public FontMetrics fm;
74 public int imgWidth = 0;
76 boolean fastPaint = false;
78 // Used For mouse Dragging and resizing graphs
79 int graphStretch = -1;
81 int graphStretchY = -1;
83 int min; // used by mouseDragged to see if user
85 int max; // used by mouseDragged to see if user
87 boolean mouseDragging = false;
96 public final AnnotationRenderer renderer;
98 private MouseWheelListener[] _mwl;
101 * Creates a new AnnotationPanel object.
106 public AnnotationPanel(AlignmentPanel ap)
109 MAC = new jalview.util.Platform().isAMac();
111 ToolTipManager.sharedInstance().registerComponent(this);
112 ToolTipManager.sharedInstance().setInitialDelay(0);
113 ToolTipManager.sharedInstance().setDismissDelay(10000);
116 this.setLayout(null);
117 addMouseListener(this);
118 addMouseMotionListener(this);
119 ap.annotationScroller.getVerticalScrollBar()
120 .addAdjustmentListener(this);
121 // save any wheel listeners on the scroller, so we can propagate scroll events to them.
122 _mwl = ap.annotationScroller.getMouseWheelListeners();
123 // and then set our own listener to consume all mousewheel events
124 ap.annotationScroller.addMouseWheelListener(this);
125 renderer = new AnnotationRenderer();
128 public AnnotationPanel(AlignViewport av)
131 renderer = new AnnotationRenderer();
135 public void mouseWheelMoved(MouseWheelEvent e)
140 if (e.getWheelRotation() > 0)
142 ap.scrollRight(true);
146 ap.scrollRight(false);
151 // TODO: find the correct way to let the event bubble up to ap.annotationScroller
152 for (MouseWheelListener mwl : _mwl)
156 mwl.mouseWheelMoved(e);
167 public Dimension getPreferredScrollableViewportSize()
169 return getPreferredSize();
173 public int getScrollableBlockIncrement(Rectangle visibleRect,
174 int orientation, int direction)
180 public boolean getScrollableTracksViewportHeight()
186 public boolean getScrollableTracksViewportWidth()
192 public int getScrollableUnitIncrement(Rectangle visibleRect,
193 int orientation, int direction)
200 * @see java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event.AdjustmentEvent)
203 public void adjustmentValueChanged(AdjustmentEvent evt)
205 // update annotation label display
206 ap.alabels.setScrollOffset(-evt.getValue());
210 * Calculates the height of the annotation displayed in the annotation panel.
211 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
212 * all annotation associated components are updated correctly.
215 public int adjustPanelHeight()
217 int height = av.calcPanelHeight();
218 this.setPreferredSize(new Dimension(1, height));
221 // revalidate only when the alignment panel is fully constructed
235 public void actionPerformed(ActionEvent evt)
237 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
242 Annotation[] anot = aa[activeRow].annotations;
244 if (anot.length < av.getColumnSelection().getMax())
246 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
247 System.arraycopy(anot, 0, temp, 0, anot.length);
249 aa[activeRow].annotations = anot;
252 if (evt.getActionCommand().equals(REMOVE))
254 for (int i = 0; i < av.getColumnSelection().size(); i++)
256 anot[av.getColumnSelection().columnAt(i)] = null;
259 else if (evt.getActionCommand().equals(LABEL))
261 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
262 String label = JOptionPane.showInputDialog(this, "Enter label",
270 if ((label.length() > 0) && !aa[activeRow].hasText)
272 aa[activeRow].hasText = true;
275 for (int i = 0; i < av.getColumnSelection().size(); i++)
277 int index = av.getColumnSelection().columnAt(i);
279 if (!av.getColumnSelection().isVisible(index))
282 if (anot[index] == null)
284 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
291 anot[index].displayCharacter = label;
295 else if (evt.getActionCommand().equals(COLOUR))
297 Color col = JColorChooser.showDialog(this,
298 "Choose foreground colour", Color.black);
300 for (int i = 0; i < av.getColumnSelection().size(); i++)
302 int index = av.getColumnSelection().columnAt(i);
304 if (!av.getColumnSelection().isVisible(index))
307 if (anot[index] == null)
309 anot[index] = new Annotation("", "", ' ', 0);
312 anot[index].colour = col;
319 String symbol = "\u03B1";
321 if (evt.getActionCommand().equals(HELIX))
325 else if (evt.getActionCommand().equals(SHEET))
331 // Added by LML to color stems
332 else if (evt.getActionCommand().equals(STEM))
338 if (!aa[activeRow].hasIcons)
340 aa[activeRow].hasIcons = true;
343 String label = JOptionPane.showInputDialog(
344 "Enter a label for the structure?", symbol);
351 if ((label.length() > 0) && !aa[activeRow].hasText)
353 aa[activeRow].hasText = true;
356 for (int i = 0; i < av.getColumnSelection().size(); i++)
358 int index = av.getColumnSelection().columnAt(i);
360 if (!av.getColumnSelection().isVisible(index))
363 if (anot[index] == null)
365 anot[index] = new Annotation(label, "", type, 0);
368 anot[index].secondaryStructure = type;
369 anot[index].displayCharacter = label;
372 aa[activeRow].validateRangeAndDisplay();
375 ap.alignmentChanged();
381 private String collectAnnotVals(Annotation[] anot,
382 ColumnSelection columnSelection, String label2)
384 String collatedInput = "";
386 ColumnSelection viscols=av.getColumnSelection();
387 // TODO: refactor and save av.getColumnSelection for efficiency
388 for (int i = 0; i < columnSelection.size(); i++)
390 int index = columnSelection.columnAt(i);
391 // always check for current display state - just in case
392 if (!viscols.isVisible(index))
394 String tlabel = null;
395 if (anot[index] != null)
396 { // LML added stem code
397 if (label2.equals(HELIX) || label2.equals(SHEET)
398 || label2.equals(STEM) || label2.equals(LABEL))
400 tlabel = anot[index].description;
401 if (tlabel == null || tlabel.length() < 1)
403 if (label2.equals(HELIX) || label2.equals(SHEET)
404 || label2.equals(STEM))
406 tlabel = "" + anot[index].secondaryStructure;
410 tlabel = "" + anot[index].displayCharacter;
414 if (tlabel != null && !tlabel.equals(last))
416 if (last.length() > 0)
418 collatedInput += " ";
420 collatedInput += tlabel;
424 return collatedInput;
434 public void mousePressed(MouseEvent evt)
437 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
446 for (int i = 0; i < aa.length; i++)
450 height += aa[i].height;
453 if (evt.getY() < height)
459 else if (aa[i].graph > 0)
463 graphStretchY = evt.getY();
470 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
472 if (av.getColumnSelection() == null)
477 JPopupMenu pop = new JPopupMenu("Structure type");
480 * Just display the needed structure options
482 if (av.getAlignment().isNucleotide() == true)
484 item = new JMenuItem(STEM);
485 item.addActionListener(this);
490 item = new JMenuItem(HELIX);
491 item.addActionListener(this);
493 item = new JMenuItem(SHEET);
494 item.addActionListener(this);
497 item = new JMenuItem(LABEL);
498 item.addActionListener(this);
500 item = new JMenuItem(COLOUR);
501 item.addActionListener(this);
503 item = new JMenuItem(REMOVE);
504 item.addActionListener(this);
506 pop.show(this, evt.getX(), evt.getY());
516 ap.scalePanel.mousePressed(evt);
527 public void mouseReleased(MouseEvent evt)
531 mouseDragging = false;
532 ap.scalePanel.mouseReleased(evt);
542 public void mouseEntered(MouseEvent evt)
544 ap.scalePanel.mouseEntered(evt);
554 public void mouseExited(MouseEvent evt)
556 ap.scalePanel.mouseExited(evt);
566 public void mouseDragged(MouseEvent evt)
568 if (graphStretch > -1)
570 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
572 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
574 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
576 graphStretchY = evt.getY();
578 ap.paintAlignment(true);
582 ap.scalePanel.mouseDragged(evt);
593 public void mouseMoved(MouseEvent evt)
595 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
599 this.setToolTipText(null);
606 for (int i = 0; i < aa.length; i++)
610 height += aa[i].height;
613 if (evt.getY() < height)
623 this.setToolTipText(null);
627 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
629 if (av.hasHiddenColumns())
631 res = av.getColumnSelection().adjustForHiddenColumns(res);
634 if (row > -1 && aa[row].annotations != null
635 && res < aa[row].annotations.length)
637 if (aa[row].graphGroup > -1)
639 StringBuffer tip = new StringBuffer("<html>");
640 for (int gg = 0; gg < aa.length; gg++)
642 if (aa[gg].graphGroup == aa[row].graphGroup
643 && aa[gg].annotations[res] != null)
645 tip.append(aa[gg].label + " "
646 + aa[gg].annotations[res].description + "<br>");
649 if (tip.length() != 6)
651 tip.setLength(tip.length() - 4);
652 this.setToolTipText(tip.toString() + "</html>");
655 else if (aa[row].annotations[res] != null
656 && aa[row].annotations[res].description != null
657 && aa[row].annotations[res].description.length() > 0)
659 this.setToolTipText(aa[row].annotations[res].description);
663 // clear the tooltip.
664 this.setToolTipText(null);
667 if (aa[row].annotations[res] != null)
669 StringBuffer text = new StringBuffer("Sequence position "
672 if (aa[row].annotations[res].description != null)
674 text.append(" " + aa[row].annotations[res].description);
677 ap.alignFrame.statusBar.setText(text.toString());
682 this.setToolTipText(null);
693 public void mouseClicked(MouseEvent evt)
697 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
698 AlignmentAnnotation anot = aa[activeRow];
700 if (anot.description.equals("secondary structure"))
702 // System.out.println(anot.description+" "+anot.getRNAStruc());
707 // TODO mouseClicked-content and drawCursor are quite experimental!
708 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
711 int pady = av.charHeight / 5;
713 graphics.setColor(Color.black);
714 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
716 if (av.validCharWidth)
718 graphics.setColor(Color.white);
720 char s = seq.getCharAt(res);
722 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
723 graphics.drawString(String.valueOf(s), charOffset + x1,
724 (y1 + av.charHeight) - pady);
728 private volatile boolean imageFresh=false;
736 public void paintComponent(Graphics g)
738 g.setColor(Color.white);
739 g.fillRect(0, 0, getWidth(), getHeight());
743 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
744 || (getVisibleRect().height != g.getClipBounds().height))
746 g.drawImage(image, 0, 0, this);
751 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
754 if (image == null || imgWidth != image.getWidth()
755 || image.getHeight(this) != getHeight())
757 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
758 BufferedImage.TYPE_INT_RGB);
759 gg = (Graphics2D) image.getGraphics();
763 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
764 RenderingHints.VALUE_ANTIALIAS_ON);
767 gg.setFont(av.getFont());
768 fm = gg.getFontMetrics();
769 gg.setColor(Color.white);
770 gg.fillRect(0, 0, imgWidth, image.getHeight());
774 drawComponent(gg, av.startRes, av.endRes + 1);
776 g.drawImage(image, 0, 0, this);
780 * non-Thread safe repaint
783 * repaint with horizontal shift in alignment
785 public void fastPaint(int horizontal)
788 if ((horizontal == 0) || gg == null
789 || av.getAlignment().getAlignmentAnnotation() == null
790 || av.getAlignment().getAlignmentAnnotation().length < 1
791 || av.isCalcInProgress())
796 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
798 int sr = av.startRes;
799 int er = av.endRes + 1;
802 if (horizontal > 0) // scrollbar pulled right, image to the left
804 transX = (er - sr - horizontal) * av.charWidth;
805 sr = er - horizontal;
807 else if (horizontal < 0)
809 er = sr - horizontal;
812 gg.translate(transX, 0);
814 drawComponent(gg, sr, er);
816 gg.translate(-transX, 0);
822 private volatile boolean lastImageGood=false;
833 public void drawComponent(Graphics g, int startRes, int endRes)
835 BufferedImage oldFaded=fadedImage;
836 if (av.isCalcInProgress())
843 // We'll keep a record of the old image,
844 // and draw a faded image until the calculation
846 if (lastImageGood && (fadedImage == null || fadedImage.getWidth() != imgWidth
847 || fadedImage.getHeight() != image.getHeight()))
849 // System.err.println("redraw faded image ("+(fadedImage==null ? "null image" : "") + " lastGood="+lastImageGood+")");
850 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
851 BufferedImage.TYPE_INT_RGB);
853 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
855 fadedG.setColor(Color.white);
856 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
858 fadedG.setComposite(AlphaComposite.getInstance(
859 AlphaComposite.SRC_OVER, .3f));
860 fadedG.drawImage(image, 0, 0, this);
863 // make sure we don't overwrite the last good faded image until all calculations have finished
869 if (fadedImage!=null)
876 g.setColor(Color.white);
877 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
879 g.setFont(av.getFont());
882 fm = g.getFontMetrics();
885 if ((av.getAlignment().getAlignmentAnnotation() == null)
886 || (av.getAlignment().getAlignmentAnnotation().length < 1))
888 g.setColor(Color.white);
889 g.fillRect(0, 0, getWidth(), getHeight());
890 g.setColor(Color.black);
891 if (av.validCharWidth)
893 g.drawString("Alignment has no annotations", 20, 15);
898 lastImageGood = renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
899 if (!lastImageGood && fadedImage==null)
906 public FontMetrics getFontMetrics()
912 public Image getFadedImage()
918 public int getFadedImageWidth()