2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import java.awt.event.*;
25 import java.awt.image.*;
29 import jalview.datamodel.*;
30 import jalview.renderer.AnnotationRenderer;
31 import jalview.renderer.AwtRenderPanelI;
32 import jalview.util.MessageManager;
35 * AnnotationPanel displays visible portion of annotation rows below unwrapped
41 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
42 MouseListener, MouseWheelListener, MouseMotionListener,
43 ActionListener, AdjustmentListener, Scrollable
45 String HELIX = MessageManager.getString("label.helix");
47 String SHEET = MessageManager.getString("label.sheet");
50 * For RNA secondary structure "stems" aka helices
52 String STEM = MessageManager.getString("label.rna_helix");
54 String LABEL = MessageManager.getString("label.label");
56 String REMOVE = MessageManager.getString("label.remove_annotation");
58 String COLOUR = MessageManager.getString("action.colour");
60 public final Color HELIX_COLOUR = Color.red.darker();
62 public final Color SHEET_COLOUR = Color.green.darker().darker();
64 public final Color STEM_COLOUR = Color.blue.darker();
67 public AlignViewport av;
71 public int activeRow = -1;
73 public BufferedImage image;
75 public volatile BufferedImage fadedImage;
79 public FontMetrics fm;
81 public int imgWidth = 0;
83 boolean fastPaint = false;
85 // Used For mouse Dragging and resizing graphs
86 int graphStretch = -1;
88 int graphStretchY = -1;
90 int min; // used by mouseDragged to see if user
92 int max; // used by mouseDragged to see if user
94 boolean mouseDragging = false;
103 public final AnnotationRenderer renderer;
105 private MouseWheelListener[] _mwl;
108 * Creates a new AnnotationPanel object.
113 public AnnotationPanel(AlignmentPanel ap)
116 MAC = new jalview.util.Platform().isAMac();
118 ToolTipManager.sharedInstance().registerComponent(this);
119 ToolTipManager.sharedInstance().setInitialDelay(0);
120 ToolTipManager.sharedInstance().setDismissDelay(10000);
123 this.setLayout(null);
124 addMouseListener(this);
125 addMouseMotionListener(this);
126 ap.annotationScroller.getVerticalScrollBar()
127 .addAdjustmentListener(this);
128 // save any wheel listeners on the scroller, so we can propagate scroll
130 _mwl = ap.annotationScroller.getMouseWheelListeners();
131 // and then set our own listener to consume all mousewheel events
132 ap.annotationScroller.addMouseWheelListener(this);
133 renderer = new AnnotationRenderer();
136 public AnnotationPanel(AlignViewport av)
139 renderer = new AnnotationRenderer();
143 public void mouseWheelMoved(MouseWheelEvent e)
148 if (e.getWheelRotation() > 0)
150 ap.scrollRight(true);
154 ap.scrollRight(false);
159 // TODO: find the correct way to let the event bubble up to
160 // ap.annotationScroller
161 for (MouseWheelListener mwl : _mwl)
165 mwl.mouseWheelMoved(e);
176 public Dimension getPreferredScrollableViewportSize()
178 return getPreferredSize();
182 public int getScrollableBlockIncrement(Rectangle visibleRect,
183 int orientation, int direction)
189 public boolean getScrollableTracksViewportHeight()
195 public boolean getScrollableTracksViewportWidth()
201 public int getScrollableUnitIncrement(Rectangle visibleRect,
202 int orientation, int direction)
211 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
215 public void adjustmentValueChanged(AdjustmentEvent evt)
217 // update annotation label display
218 ap.alabels.setScrollOffset(-evt.getValue());
222 * Calculates the height of the annotation displayed in the annotation panel.
223 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
224 * all annotation associated components are updated correctly.
227 public int adjustPanelHeight()
229 int height = av.calcPanelHeight();
230 this.setPreferredSize(new Dimension(1, height));
233 // revalidate only when the alignment panel is fully constructed
247 public void actionPerformed(ActionEvent evt)
249 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
254 Annotation[] anot = aa[activeRow].annotations;
256 if (anot.length < av.getColumnSelection().getMax())
258 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
259 System.arraycopy(anot, 0, temp, 0, anot.length);
261 aa[activeRow].annotations = anot;
264 if (evt.getActionCommand().equals(REMOVE))
266 for (int i = 0; i < av.getColumnSelection().size(); i++)
268 anot[av.getColumnSelection().columnAt(i)] = null;
271 else if (evt.getActionCommand().equals(LABEL))
273 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
274 String label = JOptionPane.showInputDialog(this,
275 MessageManager.getString("label.enter_label"), exMesg);
282 if ((label.length() > 0) && !aa[activeRow].hasText)
284 aa[activeRow].hasText = true;
287 for (int i = 0; i < av.getColumnSelection().size(); i++)
289 int index = av.getColumnSelection().columnAt(i);
291 if (!av.getColumnSelection().isVisible(index))
294 if (anot[index] == null)
296 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
303 anot[index].displayCharacter = label;
307 else if (evt.getActionCommand().equals(COLOUR))
309 Color col = JColorChooser.showDialog(this,
310 MessageManager.getString("label.select_foreground_colour"), Color.black);
312 for (int i = 0; i < av.getColumnSelection().size(); i++)
314 int index = av.getColumnSelection().columnAt(i);
316 if (!av.getColumnSelection().isVisible(index))
319 if (anot[index] == null)
321 anot[index] = new Annotation("", "", ' ', 0);
324 anot[index].colour = col;
331 String symbol = "\u03B1";
333 if (evt.getActionCommand().equals(HELIX))
337 else if (evt.getActionCommand().equals(SHEET))
343 // Added by LML to color stems
344 else if (evt.getActionCommand().equals(STEM))
350 if (!aa[activeRow].hasIcons)
352 aa[activeRow].hasIcons = true;
355 String label = JOptionPane.showInputDialog(MessageManager
356 .getString("label.enter_label_for_the_structure"), symbol);
363 if ((label.length() > 0) && !aa[activeRow].hasText)
365 aa[activeRow].hasText = true;
366 if (evt.getActionCommand().equals(STEM))
368 aa[activeRow].showAllColLabels = true;
371 for (int i = 0; i < av.getColumnSelection().size(); i++)
373 int index = av.getColumnSelection().columnAt(i);
375 if (!av.getColumnSelection().isVisible(index))
378 if (anot[index] == null)
380 anot[index] = new Annotation(label, "", type, 0);
383 anot[index].secondaryStructure = type;
384 anot[index].displayCharacter = label;
388 av.getAlignment().validateAnnotation(aa[activeRow]);
389 ap.alignmentChanged();
397 private String collectAnnotVals(Annotation[] anot,
398 ColumnSelection columnSelection, String label2)
400 String collatedInput = "";
402 ColumnSelection viscols = av.getColumnSelection();
403 // TODO: refactor and save av.getColumnSelection for efficiency
404 for (int i = 0; i < columnSelection.size(); i++)
406 int index = columnSelection.columnAt(i);
407 // always check for current display state - just in case
408 if (!viscols.isVisible(index))
410 String tlabel = null;
411 if (anot[index] != null)
412 { // LML added stem code
413 if (label2.equals(HELIX) || label2.equals(SHEET)
414 || label2.equals(STEM) || label2.equals(LABEL))
416 tlabel = anot[index].description;
417 if (tlabel == null || tlabel.length() < 1)
419 if (label2.equals(HELIX) || label2.equals(SHEET)
420 || label2.equals(STEM))
422 tlabel = "" + anot[index].secondaryStructure;
426 tlabel = "" + anot[index].displayCharacter;
430 if (tlabel != null && !tlabel.equals(last))
432 if (last.length() > 0)
434 collatedInput += " ";
436 collatedInput += tlabel;
440 return collatedInput;
450 public void mousePressed(MouseEvent evt)
453 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
462 for (int i = 0; i < aa.length; i++)
466 height += aa[i].height;
469 if (evt.getY() < height)
475 else if (aa[i].graph > 0)
479 graphStretchY = evt.getY();
486 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
488 if (av.getColumnSelection() == null)
493 JPopupMenu pop = new JPopupMenu(
494 MessageManager.getString("label.structure_type"));
497 * Just display the needed structure options
499 if (av.getAlignment().isNucleotide() == true)
501 item = new JMenuItem(STEM);
502 item.addActionListener(this);
507 item = new JMenuItem(HELIX);
508 item.addActionListener(this);
510 item = new JMenuItem(SHEET);
511 item.addActionListener(this);
514 item = new JMenuItem(LABEL);
515 item.addActionListener(this);
517 item = new JMenuItem(COLOUR);
518 item.addActionListener(this);
520 item = new JMenuItem(REMOVE);
521 item.addActionListener(this);
523 pop.show(this, evt.getX(), evt.getY());
533 ap.scalePanel.mousePressed(evt);
544 public void mouseReleased(MouseEvent evt)
548 mouseDragging = false;
549 ap.scalePanel.mouseReleased(evt);
559 public void mouseEntered(MouseEvent evt)
561 ap.scalePanel.mouseEntered(evt);
571 public void mouseExited(MouseEvent evt)
573 ap.scalePanel.mouseExited(evt);
583 public void mouseDragged(MouseEvent evt)
585 if (graphStretch > -1)
587 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
589 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
591 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
593 graphStretchY = evt.getY();
595 ap.paintAlignment(true);
599 ap.scalePanel.mouseDragged(evt);
610 public void mouseMoved(MouseEvent evt)
612 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
616 this.setToolTipText(null);
623 for (int i = 0; i < aa.length; i++)
627 height += aa[i].height;
630 if (evt.getY() < height)
640 this.setToolTipText(null);
644 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
646 if (av.hasHiddenColumns())
648 res = av.getColumnSelection().adjustForHiddenColumns(res);
651 if (row > -1 && aa[row].annotations != null
652 && res < aa[row].annotations.length)
654 if (aa[row].graphGroup > -1)
656 StringBuffer tip = new StringBuffer("<html>");
657 for (int gg = 0; gg < aa.length; gg++)
659 if (aa[gg].graphGroup == aa[row].graphGroup
660 && aa[gg].annotations[res] != null)
662 tip.append(aa[gg].label + " "
663 + aa[gg].annotations[res].description + "<br>");
666 if (tip.length() != 6)
668 tip.setLength(tip.length() - 4);
669 this.setToolTipText(tip.toString() + "</html>");
672 else if (aa[row].annotations[res] != null
673 && aa[row].annotations[res].description != null
674 && aa[row].annotations[res].description.length() > 0)
676 this.setToolTipText(JvSwingUtils
677 .wrapTooltip(true, aa[row].annotations[res].description));
681 // clear the tooltip.
682 this.setToolTipText(null);
685 if (aa[row].annotations[res] != null)
687 StringBuffer text = new StringBuffer("Sequence position "
690 if (aa[row].annotations[res].description != null)
692 text.append(" " + aa[row].annotations[res].description);
695 ap.alignFrame.statusBar.setText(text.toString());
700 this.setToolTipText(null);
711 public void mouseClicked(MouseEvent evt)
713 // if (activeRow != -1)
715 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
716 // AlignmentAnnotation anot = aa[activeRow];
720 // TODO mouseClicked-content and drawCursor are quite experimental!
721 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
724 int pady = av.charHeight / 5;
726 graphics.setColor(Color.black);
727 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
729 if (av.validCharWidth)
731 graphics.setColor(Color.white);
733 char s = seq.getCharAt(res);
735 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
736 graphics.drawString(String.valueOf(s), charOffset + x1,
737 (y1 + av.charHeight) - pady);
742 private volatile boolean imageFresh = false;
751 public void paintComponent(Graphics g)
753 g.setColor(Color.white);
754 g.fillRect(0, 0, getWidth(), getHeight());
758 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
759 || (getVisibleRect().height != g.getClipBounds().height))
761 g.drawImage(image, 0, 0, this);
766 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
769 if (image == null || imgWidth != image.getWidth(this)
770 || image.getHeight(this) != getHeight())
774 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
775 BufferedImage.TYPE_INT_RGB);
776 } catch (OutOfMemoryError oom)
781 } catch (Exception x)
786 "Couldn't allocate memory to redraw screen. Please restart Jalview",
790 gg = (Graphics2D) image.getGraphics();
794 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
795 RenderingHints.VALUE_ANTIALIAS_ON);
798 gg.setFont(av.getFont());
799 fm = gg.getFontMetrics();
800 gg.setColor(Color.white);
801 gg.fillRect(0, 0, imgWidth, image.getHeight());
805 drawComponent(gg, av.startRes, av.endRes + 1);
807 g.drawImage(image, 0, 0, this);
811 * set true to enable redraw timing debug output on stderr
813 private final boolean debugRedraw = false;
816 * non-Thread safe repaint
819 * repaint with horizontal shift in alignment
821 public void fastPaint(int horizontal)
823 if ((horizontal == 0) || gg == null
824 || av.getAlignment().getAlignmentAnnotation() == null
825 || av.getAlignment().getAlignmentAnnotation().length < 1
826 || av.isCalcInProgress())
831 long stime = System.currentTimeMillis();
832 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
833 long mtime = System.currentTimeMillis();
834 int sr = av.startRes;
835 int er = av.endRes + 1;
838 if (horizontal > 0) // scrollbar pulled right, image to the left
840 transX = (er - sr - horizontal) * av.charWidth;
841 sr = er - horizontal;
843 else if (horizontal < 0)
845 er = sr - horizontal;
848 gg.translate(transX, 0);
850 drawComponent(gg, sr, er);
852 gg.translate(-transX, 0);
853 long dtime = System.currentTimeMillis();
856 long rtime = System.currentTimeMillis();
859 System.err.println("Scroll:\t" + horizontal + "\tCopyArea:\t"
860 + (mtime - stime) + "\tDraw component:\t" + (dtime - mtime)
861 + "\tRepaint call:\t" + (rtime - dtime));
866 private volatile boolean lastImageGood = false;
878 public void drawComponent(Graphics g, int startRes, int endRes)
880 BufferedImage oldFaded = fadedImage;
881 if (av.isCalcInProgress())
885 lastImageGood = false;
888 // We'll keep a record of the old image,
889 // and draw a faded image until the calculation
892 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
893 .getHeight() != image.getHeight()))
895 // System.err.println("redraw faded image ("+(fadedImage==null ?
896 // "null image" : "") + " lastGood="+lastImageGood+")");
897 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
898 BufferedImage.TYPE_INT_RGB);
900 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
902 fadedG.setColor(Color.white);
903 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
905 fadedG.setComposite(AlphaComposite.getInstance(
906 AlphaComposite.SRC_OVER, .3f));
907 fadedG.drawImage(image, 0, 0, this);
910 // make sure we don't overwrite the last good faded image until all
911 // calculations have finished
912 lastImageGood = false;
917 if (fadedImage != null)
919 oldFaded = fadedImage;
924 g.setColor(Color.white);
925 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
927 g.setFont(av.getFont());
930 fm = g.getFontMetrics();
933 if ((av.getAlignment().getAlignmentAnnotation() == null)
934 || (av.getAlignment().getAlignmentAnnotation().length < 1))
936 g.setColor(Color.white);
937 g.fillRect(0, 0, getWidth(), getHeight());
938 g.setColor(Color.black);
939 if (av.validCharWidth)
941 g.drawString(MessageManager
942 .getString("label.alignment_has_no_annotations"), 20, 15);
947 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
949 if (!lastImageGood && fadedImage == null)
951 fadedImage = oldFaded;
956 public FontMetrics getFontMetrics()
962 public Image getFadedImage()
968 public int getFadedImageWidth()
973 private int[] bounds = new int[2];
976 public int[] getVisibleVRange()
978 if (ap != null && ap.alabels != null)
980 int sOffset = -ap.alabels.scrollOffset;
981 int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
983 bounds[1] = visHeight;