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.*;
26 import jalview.datamodel.*;
27 import jalview.renderer.AnnotationRenderer;
28 import jalview.renderer.AwtRenderPanelI;
31 * AnnotationPanel displays visible portion of annotation rows below unwrapped
37 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
38 MouseListener, MouseWheelListener, MouseMotionListener,
39 ActionListener, AdjustmentListener, Scrollable
41 final String HELIX = "Helix";
43 final String SHEET = "Sheet";
46 * For RNA secondary structure "stems" aka helices
48 final String STEM = "RNA Helix";
50 final String LABEL = "Label";
52 final String REMOVE = "Remove Annotation";
54 final String COLOUR = "Colour";
56 public final Color HELIX_COLOUR = Color.red.darker();
58 public final Color SHEET_COLOUR = Color.green.darker().darker();
60 public final Color STEM_COLOUR = Color.blue.darker();
63 public AlignViewport av;
67 public int activeRow = -1;
69 public BufferedImage image;
71 public volatile BufferedImage fadedImage;
75 public FontMetrics fm;
77 public int imgWidth = 0;
79 boolean fastPaint = false;
81 // Used For mouse Dragging and resizing graphs
82 int graphStretch = -1;
84 int graphStretchY = -1;
86 int min; // used by mouseDragged to see if user
88 int max; // used by mouseDragged to see if user
90 boolean mouseDragging = false;
99 public final AnnotationRenderer renderer;
101 private MouseWheelListener[] _mwl;
104 * Creates a new AnnotationPanel object.
109 public AnnotationPanel(AlignmentPanel ap)
112 MAC = new jalview.util.Platform().isAMac();
114 ToolTipManager.sharedInstance().registerComponent(this);
115 ToolTipManager.sharedInstance().setInitialDelay(0);
116 ToolTipManager.sharedInstance().setDismissDelay(10000);
119 this.setLayout(null);
120 addMouseListener(this);
121 addMouseMotionListener(this);
122 ap.annotationScroller.getVerticalScrollBar()
123 .addAdjustmentListener(this);
124 // save any wheel listeners on the scroller, so we can propagate scroll
126 _mwl = ap.annotationScroller.getMouseWheelListeners();
127 // and then set our own listener to consume all mousewheel events
128 ap.annotationScroller.addMouseWheelListener(this);
129 renderer = new AnnotationRenderer();
132 public AnnotationPanel(AlignViewport av)
135 renderer = new AnnotationRenderer();
139 public void mouseWheelMoved(MouseWheelEvent e)
144 if (e.getWheelRotation() > 0)
146 ap.scrollRight(true);
150 ap.scrollRight(false);
155 // TODO: find the correct way to let the event bubble up to
156 // ap.annotationScroller
157 for (MouseWheelListener mwl : _mwl)
161 mwl.mouseWheelMoved(e);
172 public Dimension getPreferredScrollableViewportSize()
174 return getPreferredSize();
178 public int getScrollableBlockIncrement(Rectangle visibleRect,
179 int orientation, int direction)
185 public boolean getScrollableTracksViewportHeight()
191 public boolean getScrollableTracksViewportWidth()
197 public int getScrollableUnitIncrement(Rectangle visibleRect,
198 int orientation, int direction)
207 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
211 public void adjustmentValueChanged(AdjustmentEvent evt)
213 // update annotation label display
214 ap.alabels.setScrollOffset(-evt.getValue());
218 * Calculates the height of the annotation displayed in the annotation panel.
219 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
220 * all annotation associated components are updated correctly.
223 public int adjustPanelHeight()
225 int height = av.calcPanelHeight();
226 this.setPreferredSize(new Dimension(1, height));
229 // revalidate only when the alignment panel is fully constructed
243 public void actionPerformed(ActionEvent evt)
245 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
250 Annotation[] anot = aa[activeRow].annotations;
252 if (anot.length < av.getColumnSelection().getMax())
254 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
255 System.arraycopy(anot, 0, temp, 0, anot.length);
257 aa[activeRow].annotations = anot;
260 if (evt.getActionCommand().equals(REMOVE))
262 for (int i = 0; i < av.getColumnSelection().size(); i++)
264 anot[av.getColumnSelection().columnAt(i)] = null;
267 else if (evt.getActionCommand().equals(LABEL))
269 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
270 String label = JOptionPane.showInputDialog(this, "Enter label",
278 if ((label.length() > 0) && !aa[activeRow].hasText)
280 aa[activeRow].hasText = true;
283 for (int i = 0; i < av.getColumnSelection().size(); i++)
285 int index = av.getColumnSelection().columnAt(i);
287 if (!av.getColumnSelection().isVisible(index))
290 if (anot[index] == null)
292 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
299 anot[index].displayCharacter = label;
303 else if (evt.getActionCommand().equals(COLOUR))
305 Color col = JColorChooser.showDialog(this,
306 "Choose foreground colour", Color.black);
308 for (int i = 0; i < av.getColumnSelection().size(); i++)
310 int index = av.getColumnSelection().columnAt(i);
312 if (!av.getColumnSelection().isVisible(index))
315 if (anot[index] == null)
317 anot[index] = new Annotation("", "", ' ', 0);
320 anot[index].colour = col;
327 String symbol = "\u03B1";
329 if (evt.getActionCommand().equals(HELIX))
333 else if (evt.getActionCommand().equals(SHEET))
339 // Added by LML to color stems
340 else if (evt.getActionCommand().equals(STEM))
346 if (!aa[activeRow].hasIcons)
348 aa[activeRow].hasIcons = true;
351 String label = JOptionPane.showInputDialog(
352 "Enter a label for the structure?", symbol);
359 if ((label.length() > 0) && !aa[activeRow].hasText)
361 aa[activeRow].hasText = true;
364 for (int i = 0; i < av.getColumnSelection().size(); i++)
366 int index = av.getColumnSelection().columnAt(i);
368 if (!av.getColumnSelection().isVisible(index))
371 if (anot[index] == null)
373 anot[index] = new Annotation(label, "", type, 0);
376 anot[index].secondaryStructure = type;
377 anot[index].displayCharacter = label;
380 av.getAlignment().validateAnnotation(aa[activeRow]);
381 ap.alignmentChanged();
389 private String collectAnnotVals(Annotation[] anot,
390 ColumnSelection columnSelection, String label2)
392 String collatedInput = "";
394 ColumnSelection viscols = av.getColumnSelection();
395 // TODO: refactor and save av.getColumnSelection for efficiency
396 for (int i = 0; i < columnSelection.size(); i++)
398 int index = columnSelection.columnAt(i);
399 // always check for current display state - just in case
400 if (!viscols.isVisible(index))
402 String tlabel = null;
403 if (anot[index] != null)
404 { // LML added stem code
405 if (label2.equals(HELIX) || label2.equals(SHEET)
406 || label2.equals(STEM) || label2.equals(LABEL))
408 tlabel = anot[index].description;
409 if (tlabel == null || tlabel.length() < 1)
411 if (label2.equals(HELIX) || label2.equals(SHEET)
412 || label2.equals(STEM))
414 tlabel = "" + anot[index].secondaryStructure;
418 tlabel = "" + anot[index].displayCharacter;
422 if (tlabel != null && !tlabel.equals(last))
424 if (last.length() > 0)
426 collatedInput += " ";
428 collatedInput += tlabel;
432 return collatedInput;
442 public void mousePressed(MouseEvent evt)
445 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
454 for (int i = 0; i < aa.length; i++)
458 height += aa[i].height;
461 if (evt.getY() < height)
467 else if (aa[i].graph > 0)
471 graphStretchY = evt.getY();
478 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
480 if (av.getColumnSelection() == null)
485 JPopupMenu pop = new JPopupMenu("Structure type");
488 * Just display the needed structure options
490 if (av.getAlignment().isNucleotide() == true)
492 item = new JMenuItem(STEM);
493 item.addActionListener(this);
498 item = new JMenuItem(HELIX);
499 item.addActionListener(this);
501 item = new JMenuItem(SHEET);
502 item.addActionListener(this);
505 item = new JMenuItem(LABEL);
506 item.addActionListener(this);
508 item = new JMenuItem(COLOUR);
509 item.addActionListener(this);
511 item = new JMenuItem(REMOVE);
512 item.addActionListener(this);
514 pop.show(this, evt.getX(), evt.getY());
524 ap.scalePanel.mousePressed(evt);
535 public void mouseReleased(MouseEvent evt)
539 mouseDragging = false;
540 ap.scalePanel.mouseReleased(evt);
550 public void mouseEntered(MouseEvent evt)
552 ap.scalePanel.mouseEntered(evt);
562 public void mouseExited(MouseEvent evt)
564 ap.scalePanel.mouseExited(evt);
574 public void mouseDragged(MouseEvent evt)
576 if (graphStretch > -1)
578 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
580 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
582 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
584 graphStretchY = evt.getY();
586 ap.paintAlignment(true);
590 ap.scalePanel.mouseDragged(evt);
601 public void mouseMoved(MouseEvent evt)
603 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
607 this.setToolTipText(null);
614 for (int i = 0; i < aa.length; i++)
618 height += aa[i].height;
621 if (evt.getY() < height)
631 this.setToolTipText(null);
635 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
637 if (av.hasHiddenColumns())
639 res = av.getColumnSelection().adjustForHiddenColumns(res);
642 if (row > -1 && aa[row].annotations != null
643 && res < aa[row].annotations.length)
645 if (aa[row].graphGroup > -1)
647 StringBuffer tip = new StringBuffer("<html>");
648 for (int gg = 0; gg < aa.length; gg++)
650 if (aa[gg].graphGroup == aa[row].graphGroup
651 && aa[gg].annotations[res] != null)
653 tip.append(aa[gg].label + " "
654 + aa[gg].annotations[res].description + "<br>");
657 if (tip.length() != 6)
659 tip.setLength(tip.length() - 4);
660 this.setToolTipText(tip.toString() + "</html>");
663 else if (aa[row].annotations[res] != null
664 && aa[row].annotations[res].description != null
665 && aa[row].annotations[res].description.length() > 0)
667 this.setToolTipText(aa[row].annotations[res].description);
671 // clear the tooltip.
672 this.setToolTipText(null);
675 if (aa[row].annotations[res] != null)
677 StringBuffer text = new StringBuffer("Sequence position "
680 if (aa[row].annotations[res].description != null)
682 text.append(" " + aa[row].annotations[res].description);
685 ap.alignFrame.statusBar.setText(text.toString());
690 this.setToolTipText(null);
701 public void mouseClicked(MouseEvent evt)
703 // if (activeRow != -1)
705 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
706 // AlignmentAnnotation anot = aa[activeRow];
710 // TODO mouseClicked-content and drawCursor are quite experimental!
711 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
714 int pady = av.charHeight / 5;
716 graphics.setColor(Color.black);
717 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
719 if (av.validCharWidth)
721 graphics.setColor(Color.white);
723 char s = seq.getCharAt(res);
725 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
726 graphics.drawString(String.valueOf(s), charOffset + x1,
727 (y1 + av.charHeight) - pady);
732 private volatile boolean imageFresh = false;
741 public void paintComponent(Graphics g)
743 g.setColor(Color.white);
744 g.fillRect(0, 0, getWidth(), getHeight());
748 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
749 || (getVisibleRect().height != g.getClipBounds().height))
751 g.drawImage(image, 0, 0, this);
756 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
759 if (image == null || imgWidth != image.getWidth(this)
760 || image.getHeight(this) != getHeight())
764 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
765 BufferedImage.TYPE_INT_RGB);
766 } catch (OutOfMemoryError oom)
771 } catch (Exception x)
776 "Couldn't allocate memory to redraw screen. Please restart Jalview",
780 gg = (Graphics2D) image.getGraphics();
784 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
785 RenderingHints.VALUE_ANTIALIAS_ON);
788 gg.setFont(av.getFont());
789 fm = gg.getFontMetrics();
790 gg.setColor(Color.white);
791 gg.fillRect(0, 0, imgWidth, image.getHeight());
795 drawComponent(gg, av.startRes, av.endRes + 1);
797 g.drawImage(image, 0, 0, this);
800 * set true to enable redraw timing debug output on stderr
802 private final boolean debugRedraw = false;
804 * non-Thread safe repaint
807 * repaint with horizontal shift in alignment
809 public void fastPaint(int horizontal)
811 if ((horizontal == 0) || gg == null
812 || av.getAlignment().getAlignmentAnnotation() == null
813 || av.getAlignment().getAlignmentAnnotation().length < 1
814 || av.isCalcInProgress())
819 long stime=System.currentTimeMillis();
820 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
821 long mtime=System.currentTimeMillis();
822 int sr = av.startRes;
823 int er = av.endRes + 1;
826 if (horizontal > 0) // scrollbar pulled right, image to the left
828 transX = (er - sr - horizontal) * av.charWidth;
829 sr = er - horizontal;
831 else if (horizontal < 0)
833 er = sr - horizontal;
836 gg.translate(transX, 0);
838 drawComponent(gg, sr, er);
840 gg.translate(-transX, 0);
841 long dtime=System.currentTimeMillis();
844 long rtime=System.currentTimeMillis();
846 System.err.println("Scroll:\t"+horizontal+"\tCopyArea:\t"+(mtime-stime)+"\tDraw component:\t"+(dtime-mtime)+"\tRepaint call:\t"+(rtime-dtime));
851 private volatile boolean lastImageGood = false;
863 public void drawComponent(Graphics g, int startRes, int endRes)
865 BufferedImage oldFaded = fadedImage;
866 if (av.isCalcInProgress())
870 lastImageGood = false;
873 // We'll keep a record of the old image,
874 // and draw a faded image until the calculation
877 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
878 .getHeight() != image.getHeight()))
880 // System.err.println("redraw faded image ("+(fadedImage==null ?
881 // "null image" : "") + " lastGood="+lastImageGood+")");
882 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
883 BufferedImage.TYPE_INT_RGB);
885 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
887 fadedG.setColor(Color.white);
888 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
890 fadedG.setComposite(AlphaComposite.getInstance(
891 AlphaComposite.SRC_OVER, .3f));
892 fadedG.drawImage(image, 0, 0, this);
895 // make sure we don't overwrite the last good faded image until all
896 // calculations have finished
897 lastImageGood = false;
902 if (fadedImage != null)
904 oldFaded = fadedImage;
909 g.setColor(Color.white);
910 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
912 g.setFont(av.getFont());
915 fm = g.getFontMetrics();
918 if ((av.getAlignment().getAlignmentAnnotation() == null)
919 || (av.getAlignment().getAlignmentAnnotation().length < 1))
921 g.setColor(Color.white);
922 g.fillRect(0, 0, getWidth(), getHeight());
923 g.setColor(Color.black);
924 if (av.validCharWidth)
926 g.drawString("Alignment has no annotations", 20, 15);
931 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
933 if (!lastImageGood && fadedImage == null)
935 fadedImage = oldFaded;
940 public FontMetrics getFontMetrics()
946 public Image getFadedImage()
952 public int getFadedImageWidth()
956 private int[] bounds = new int[2];
958 public int[] getVisibleVRange()
960 if (ap!=null && ap.alabels!=null)
962 int sOffset=-ap.alabels.scrollOffset;
963 int visHeight = sOffset+ap.annotationSpaceFillerHolder.getHeight();
964 bounds[0] = sOffset; bounds[1]=visHeight;