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.*;
25 import jalview.datamodel.*;
26 import jalview.renderer.AnnotationRenderer;
27 import jalview.renderer.AwtRenderPanelI;
30 * AnnotationPanel displays visible portion of annotation rows below unwrapped
36 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
37 MouseListener, MouseWheelListener, MouseMotionListener,
38 ActionListener, AdjustmentListener, Scrollable
40 final String HELIX = "Helix";
42 final String SHEET = "Sheet";
45 * For RNA secondary structure "stems" aka helices
47 final String STEM = "RNA Helix";
49 final String LABEL = "Label";
51 final String REMOVE = "Remove Annotation";
53 final String COLOUR = "Colour";
55 public final Color HELIX_COLOUR = Color.red.darker();
57 public final Color SHEET_COLOUR = Color.green.darker().darker();
59 public final Color STEM_COLOUR = Color.blue.darker();
62 public AlignViewport av;
66 public int activeRow = -1;
68 public BufferedImage image;
70 public volatile BufferedImage fadedImage;
74 public FontMetrics fm;
76 public int imgWidth = 0;
78 boolean fastPaint = false;
80 // Used For mouse Dragging and resizing graphs
81 int graphStretch = -1;
83 int graphStretchY = -1;
85 int min; // used by mouseDragged to see if user
87 int max; // used by mouseDragged to see if user
89 boolean mouseDragging = false;
98 public final AnnotationRenderer renderer;
100 private MouseWheelListener[] _mwl;
103 * Creates a new AnnotationPanel object.
108 public AnnotationPanel(AlignmentPanel ap)
111 MAC = new jalview.util.Platform().isAMac();
113 ToolTipManager.sharedInstance().registerComponent(this);
114 ToolTipManager.sharedInstance().setInitialDelay(0);
115 ToolTipManager.sharedInstance().setDismissDelay(10000);
118 this.setLayout(null);
119 addMouseListener(this);
120 addMouseMotionListener(this);
121 ap.annotationScroller.getVerticalScrollBar()
122 .addAdjustmentListener(this);
123 // save any wheel listeners on the scroller, so we can propagate scroll
125 _mwl = ap.annotationScroller.getMouseWheelListeners();
126 // and then set our own listener to consume all mousewheel events
127 ap.annotationScroller.addMouseWheelListener(this);
128 renderer = new AnnotationRenderer();
131 public AnnotationPanel(AlignViewport av)
134 renderer = new AnnotationRenderer();
138 public void mouseWheelMoved(MouseWheelEvent e)
143 if (e.getWheelRotation() > 0)
145 ap.scrollRight(true);
149 ap.scrollRight(false);
154 // TODO: find the correct way to let the event bubble up to
155 // ap.annotationScroller
156 for (MouseWheelListener mwl : _mwl)
160 mwl.mouseWheelMoved(e);
171 public Dimension getPreferredScrollableViewportSize()
173 return getPreferredSize();
177 public int getScrollableBlockIncrement(Rectangle visibleRect,
178 int orientation, int direction)
184 public boolean getScrollableTracksViewportHeight()
190 public boolean getScrollableTracksViewportWidth()
196 public int getScrollableUnitIncrement(Rectangle visibleRect,
197 int orientation, int direction)
206 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
210 public void adjustmentValueChanged(AdjustmentEvent evt)
212 // update annotation label display
213 ap.alabels.setScrollOffset(-evt.getValue());
217 * Calculates the height of the annotation displayed in the annotation panel.
218 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
219 * all annotation associated components are updated correctly.
222 public int adjustPanelHeight()
224 int height = av.calcPanelHeight();
225 this.setPreferredSize(new Dimension(1, height));
228 // revalidate only when the alignment panel is fully constructed
242 public void actionPerformed(ActionEvent evt)
244 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
249 Annotation[] anot = aa[activeRow].annotations;
251 if (anot.length < av.getColumnSelection().getMax())
253 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
254 System.arraycopy(anot, 0, temp, 0, anot.length);
256 aa[activeRow].annotations = anot;
259 if (evt.getActionCommand().equals(REMOVE))
261 for (int i = 0; i < av.getColumnSelection().size(); i++)
263 anot[av.getColumnSelection().columnAt(i)] = null;
266 else if (evt.getActionCommand().equals(LABEL))
268 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
269 String label = JOptionPane.showInputDialog(this, "Enter label",
277 if ((label.length() > 0) && !aa[activeRow].hasText)
279 aa[activeRow].hasText = true;
282 for (int i = 0; i < av.getColumnSelection().size(); i++)
284 int index = av.getColumnSelection().columnAt(i);
286 if (!av.getColumnSelection().isVisible(index))
289 if (anot[index] == null)
291 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
298 anot[index].displayCharacter = label;
302 else if (evt.getActionCommand().equals(COLOUR))
304 Color col = JColorChooser.showDialog(this,
305 "Choose foreground colour", Color.black);
307 for (int i = 0; i < av.getColumnSelection().size(); i++)
309 int index = av.getColumnSelection().columnAt(i);
311 if (!av.getColumnSelection().isVisible(index))
314 if (anot[index] == null)
316 anot[index] = new Annotation("", "", ' ', 0);
319 anot[index].colour = col;
326 String symbol = "\u03B1";
328 if (evt.getActionCommand().equals(HELIX))
332 else if (evt.getActionCommand().equals(SHEET))
338 // Added by LML to color stems
339 else if (evt.getActionCommand().equals(STEM))
345 if (!aa[activeRow].hasIcons)
347 aa[activeRow].hasIcons = true;
350 String label = JOptionPane.showInputDialog(
351 "Enter a label for the structure?", symbol);
358 if ((label.length() > 0) && !aa[activeRow].hasText)
360 aa[activeRow].hasText = true;
363 for (int i = 0; i < av.getColumnSelection().size(); i++)
365 int index = av.getColumnSelection().columnAt(i);
367 if (!av.getColumnSelection().isVisible(index))
370 if (anot[index] == null)
372 anot[index] = new Annotation(label, "", type, 0);
375 anot[index].secondaryStructure = type;
376 anot[index].displayCharacter = label;
379 aa[activeRow].validateRangeAndDisplay();
382 ap.alignmentChanged();
388 private String collectAnnotVals(Annotation[] anot,
389 ColumnSelection columnSelection, String label2)
391 String collatedInput = "";
393 ColumnSelection viscols = av.getColumnSelection();
394 // TODO: refactor and save av.getColumnSelection for efficiency
395 for (int i = 0; i < columnSelection.size(); i++)
397 int index = columnSelection.columnAt(i);
398 // always check for current display state - just in case
399 if (!viscols.isVisible(index))
401 String tlabel = null;
402 if (anot[index] != null)
403 { // LML added stem code
404 if (label2.equals(HELIX) || label2.equals(SHEET)
405 || label2.equals(STEM) || label2.equals(LABEL))
407 tlabel = anot[index].description;
408 if (tlabel == null || tlabel.length() < 1)
410 if (label2.equals(HELIX) || label2.equals(SHEET)
411 || label2.equals(STEM))
413 tlabel = "" + anot[index].secondaryStructure;
417 tlabel = "" + anot[index].displayCharacter;
421 if (tlabel != null && !tlabel.equals(last))
423 if (last.length() > 0)
425 collatedInput += " ";
427 collatedInput += tlabel;
431 return collatedInput;
441 public void mousePressed(MouseEvent evt)
444 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
453 for (int i = 0; i < aa.length; i++)
457 height += aa[i].height;
460 if (evt.getY() < height)
466 else if (aa[i].graph > 0)
470 graphStretchY = evt.getY();
477 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
479 if (av.getColumnSelection() == null)
484 JPopupMenu pop = new JPopupMenu("Structure type");
487 * Just display the needed structure options
489 if (av.getAlignment().isNucleotide() == true)
491 item = new JMenuItem(STEM);
492 item.addActionListener(this);
497 item = new JMenuItem(HELIX);
498 item.addActionListener(this);
500 item = new JMenuItem(SHEET);
501 item.addActionListener(this);
504 item = new JMenuItem(LABEL);
505 item.addActionListener(this);
507 item = new JMenuItem(COLOUR);
508 item.addActionListener(this);
510 item = new JMenuItem(REMOVE);
511 item.addActionListener(this);
513 pop.show(this, evt.getX(), evt.getY());
523 ap.scalePanel.mousePressed(evt);
534 public void mouseReleased(MouseEvent evt)
538 mouseDragging = false;
539 ap.scalePanel.mouseReleased(evt);
549 public void mouseEntered(MouseEvent evt)
551 ap.scalePanel.mouseEntered(evt);
561 public void mouseExited(MouseEvent evt)
563 ap.scalePanel.mouseExited(evt);
573 public void mouseDragged(MouseEvent evt)
575 if (graphStretch > -1)
577 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
579 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
581 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
583 graphStretchY = evt.getY();
585 ap.paintAlignment(true);
589 ap.scalePanel.mouseDragged(evt);
600 public void mouseMoved(MouseEvent evt)
602 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
606 this.setToolTipText(null);
613 for (int i = 0; i < aa.length; i++)
617 height += aa[i].height;
620 if (evt.getY() < height)
630 this.setToolTipText(null);
634 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
636 if (av.hasHiddenColumns())
638 res = av.getColumnSelection().adjustForHiddenColumns(res);
641 if (row > -1 && aa[row].annotations != null
642 && res < aa[row].annotations.length)
644 if (aa[row].graphGroup > -1)
646 StringBuffer tip = new StringBuffer("<html>");
647 for (int gg = 0; gg < aa.length; gg++)
649 if (aa[gg].graphGroup == aa[row].graphGroup
650 && aa[gg].annotations[res] != null)
652 tip.append(aa[gg].label + " "
653 + aa[gg].annotations[res].description + "<br>");
656 if (tip.length() != 6)
658 tip.setLength(tip.length() - 4);
659 this.setToolTipText(tip.toString() + "</html>");
662 else if (aa[row].annotations[res] != null
663 && aa[row].annotations[res].description != null
664 && aa[row].annotations[res].description.length() > 0)
666 this.setToolTipText(aa[row].annotations[res].description);
670 // clear the tooltip.
671 this.setToolTipText(null);
674 if (aa[row].annotations[res] != null)
676 StringBuffer text = new StringBuffer("Sequence position "
679 if (aa[row].annotations[res].description != null)
681 text.append(" " + aa[row].annotations[res].description);
684 ap.alignFrame.statusBar.setText(text.toString());
689 this.setToolTipText(null);
700 public void mouseClicked(MouseEvent evt)
704 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
705 AlignmentAnnotation anot = aa[activeRow];
707 if (anot.description.equals("secondary structure"))
709 // System.out.println(anot.description+" "+anot.getRNAStruc());
714 // TODO mouseClicked-content and drawCursor are quite experimental!
715 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
718 int pady = av.charHeight / 5;
720 graphics.setColor(Color.black);
721 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
723 if (av.validCharWidth)
725 graphics.setColor(Color.white);
727 char s = seq.getCharAt(res);
729 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
730 graphics.drawString(String.valueOf(s), charOffset + x1,
731 (y1 + av.charHeight) - pady);
736 private volatile boolean imageFresh = false;
745 public void paintComponent(Graphics g)
747 g.setColor(Color.white);
748 g.fillRect(0, 0, getWidth(), getHeight());
752 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
753 || (getVisibleRect().height != g.getClipBounds().height))
755 g.drawImage(image, 0, 0, this);
760 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
763 if (image == null || imgWidth != image.getWidth()
764 || image.getHeight(this) != getHeight())
768 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
769 BufferedImage.TYPE_INT_RGB);
770 } catch (OutOfMemoryError oom)
775 } catch (Exception x)
780 "Couldn't allocate memory to redraw screen. Please restart Jalview",
784 gg = (Graphics2D) image.getGraphics();
788 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
789 RenderingHints.VALUE_ANTIALIAS_ON);
792 gg.setFont(av.getFont());
793 fm = gg.getFontMetrics();
794 gg.setColor(Color.white);
795 gg.fillRect(0, 0, imgWidth, image.getHeight());
799 drawComponent(gg, av.startRes, av.endRes + 1);
801 g.drawImage(image, 0, 0, this);
805 * non-Thread safe repaint
808 * repaint with horizontal shift in alignment
810 public void fastPaint(int horizontal)
813 if ((horizontal == 0) || gg == null
814 || av.getAlignment().getAlignmentAnnotation() == null
815 || av.getAlignment().getAlignmentAnnotation().length < 1
816 || av.isCalcInProgress())
821 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
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);
848 private volatile boolean lastImageGood = false;
860 public void drawComponent(Graphics g, int startRes, int endRes)
862 BufferedImage oldFaded = fadedImage;
863 if (av.isCalcInProgress())
867 lastImageGood = false;
870 // We'll keep a record of the old image,
871 // and draw a faded image until the calculation
874 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
875 .getHeight() != image.getHeight()))
877 // System.err.println("redraw faded image ("+(fadedImage==null ?
878 // "null image" : "") + " lastGood="+lastImageGood+")");
879 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
880 BufferedImage.TYPE_INT_RGB);
882 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
884 fadedG.setColor(Color.white);
885 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
887 fadedG.setComposite(AlphaComposite.getInstance(
888 AlphaComposite.SRC_OVER, .3f));
889 fadedG.drawImage(image, 0, 0, this);
892 // make sure we don't overwrite the last good faded image until all
893 // calculations have finished
894 lastImageGood = false;
899 if (fadedImage != null)
901 oldFaded = fadedImage;
906 g.setColor(Color.white);
907 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
909 g.setFont(av.getFont());
912 fm = g.getFontMetrics();
915 if ((av.getAlignment().getAlignmentAnnotation() == null)
916 || (av.getAlignment().getAlignmentAnnotation().length < 1))
918 g.setColor(Color.white);
919 g.fillRect(0, 0, getWidth(), getHeight());
920 g.setColor(Color.black);
921 if (av.validCharWidth)
923 g.drawString("Alignment has no annotations", 20, 15);
928 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
930 if (!lastImageGood && fadedImage == null)
932 fadedImage = oldFaded;
937 public FontMetrics getFontMetrics()
943 public Image getFadedImage()
949 public int getFadedImageWidth()