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.*;
27 import jalview.datamodel.*;
28 import jalview.renderer.AnnotationRenderer;
29 import jalview.renderer.AwtRenderPanelI;
30 import jalview.util.MessageManager;
33 * AnnotationPanel displays visible portion of annotation rows below unwrapped
39 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
40 MouseListener, MouseWheelListener, MouseMotionListener,
41 ActionListener, AdjustmentListener, Scrollable
43 final String HELIX = "Helix";
45 final String SHEET = "Sheet";
48 * For RNA secondary structure "stems" aka helices
50 final String STEM = "RNA Helix";
52 final String LABEL = "Label";
54 final String REMOVE = "Remove Annotation";
56 final String COLOUR = "Colour";
58 public final Color HELIX_COLOUR = Color.red.darker();
60 public final Color SHEET_COLOUR = Color.green.darker().darker();
62 public final Color STEM_COLOUR = Color.blue.darker();
65 public AlignViewport av;
69 public int activeRow = -1;
71 public BufferedImage image;
73 public volatile BufferedImage fadedImage;
77 public FontMetrics fm;
79 public int imgWidth = 0;
81 boolean fastPaint = false;
83 // Used For mouse Dragging and resizing graphs
84 int graphStretch = -1;
86 int graphStretchY = -1;
88 int min; // used by mouseDragged to see if user
90 int max; // used by mouseDragged to see if user
92 boolean mouseDragging = false;
101 public final AnnotationRenderer renderer;
103 private MouseWheelListener[] _mwl;
106 * Creates a new AnnotationPanel object.
111 public AnnotationPanel(AlignmentPanel ap)
114 MAC = new jalview.util.Platform().isAMac();
116 ToolTipManager.sharedInstance().registerComponent(this);
117 ToolTipManager.sharedInstance().setInitialDelay(0);
118 ToolTipManager.sharedInstance().setDismissDelay(10000);
121 this.setLayout(null);
122 addMouseListener(this);
123 addMouseMotionListener(this);
124 ap.annotationScroller.getVerticalScrollBar()
125 .addAdjustmentListener(this);
126 // save any wheel listeners on the scroller, so we can propagate scroll
128 _mwl = ap.annotationScroller.getMouseWheelListeners();
129 // and then set our own listener to consume all mousewheel events
130 ap.annotationScroller.addMouseWheelListener(this);
131 renderer = new AnnotationRenderer();
134 public AnnotationPanel(AlignViewport av)
137 renderer = new AnnotationRenderer();
141 public void mouseWheelMoved(MouseWheelEvent e)
146 if (e.getWheelRotation() > 0)
148 ap.scrollRight(true);
152 ap.scrollRight(false);
157 // TODO: find the correct way to let the event bubble up to
158 // ap.annotationScroller
159 for (MouseWheelListener mwl : _mwl)
163 mwl.mouseWheelMoved(e);
174 public Dimension getPreferredScrollableViewportSize()
176 return getPreferredSize();
180 public int getScrollableBlockIncrement(Rectangle visibleRect,
181 int orientation, int direction)
187 public boolean getScrollableTracksViewportHeight()
193 public boolean getScrollableTracksViewportWidth()
199 public int getScrollableUnitIncrement(Rectangle visibleRect,
200 int orientation, int direction)
209 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
213 public void adjustmentValueChanged(AdjustmentEvent evt)
215 // update annotation label display
216 ap.alabels.setScrollOffset(-evt.getValue());
220 * Calculates the height of the annotation displayed in the annotation panel.
221 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
222 * all annotation associated components are updated correctly.
225 public int adjustPanelHeight()
227 int height = av.calcPanelHeight();
228 this.setPreferredSize(new Dimension(1, height));
231 // revalidate only when the alignment panel is fully constructed
245 public void actionPerformed(ActionEvent evt)
247 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
252 Annotation[] anot = aa[activeRow].annotations;
254 if (anot.length < av.getColumnSelection().getMax())
256 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
257 System.arraycopy(anot, 0, temp, 0, anot.length);
259 aa[activeRow].annotations = anot;
262 if (evt.getActionCommand().equals(REMOVE))
264 for (int i = 0; i < av.getColumnSelection().size(); i++)
266 anot[av.getColumnSelection().columnAt(i)] = null;
269 else if (evt.getActionCommand().equals(LABEL))
271 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
272 String label = JOptionPane.showInputDialog(this, MessageManager.getString("label.enter_label"),
280 if ((label.length() > 0) && !aa[activeRow].hasText)
282 aa[activeRow].hasText = true;
285 for (int i = 0; i < av.getColumnSelection().size(); i++)
287 int index = av.getColumnSelection().columnAt(i);
289 if (!av.getColumnSelection().isVisible(index))
292 if (anot[index] == null)
294 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
301 anot[index].displayCharacter = label;
305 else if (evt.getActionCommand().equals(COLOUR))
307 Color col = JColorChooser.showDialog(this,
308 "Choose foreground colour", Color.black);
310 for (int i = 0; i < av.getColumnSelection().size(); i++)
312 int index = av.getColumnSelection().columnAt(i);
314 if (!av.getColumnSelection().isVisible(index))
317 if (anot[index] == null)
319 anot[index] = new Annotation("", "", ' ', 0);
322 anot[index].colour = col;
329 String symbol = "\u03B1";
331 if (evt.getActionCommand().equals(HELIX))
335 else if (evt.getActionCommand().equals(SHEET))
341 // Added by LML to color stems
342 else if (evt.getActionCommand().equals(STEM))
348 if (!aa[activeRow].hasIcons)
350 aa[activeRow].hasIcons = true;
353 String label = JOptionPane.showInputDialog(
354 MessageManager.getString("label.enter_label_for_the_structure"), symbol);
361 if ((label.length() > 0) && !aa[activeRow].hasText)
363 aa[activeRow].hasText = true;
366 for (int i = 0; i < av.getColumnSelection().size(); i++)
368 int index = av.getColumnSelection().columnAt(i);
370 if (!av.getColumnSelection().isVisible(index))
373 if (anot[index] == null)
375 anot[index] = new Annotation(label, "", type, 0);
378 anot[index].secondaryStructure = type;
379 anot[index].displayCharacter = label;
382 av.getAlignment().validateAnnotation(aa[activeRow]);
383 ap.alignmentChanged();
391 private String collectAnnotVals(Annotation[] anot,
392 ColumnSelection columnSelection, String label2)
394 String collatedInput = "";
396 ColumnSelection viscols = av.getColumnSelection();
397 // TODO: refactor and save av.getColumnSelection for efficiency
398 for (int i = 0; i < columnSelection.size(); i++)
400 int index = columnSelection.columnAt(i);
401 // always check for current display state - just in case
402 if (!viscols.isVisible(index))
404 String tlabel = null;
405 if (anot[index] != null)
406 { // LML added stem code
407 if (label2.equals(HELIX) || label2.equals(SHEET)
408 || label2.equals(STEM) || label2.equals(LABEL))
410 tlabel = anot[index].description;
411 if (tlabel == null || tlabel.length() < 1)
413 if (label2.equals(HELIX) || label2.equals(SHEET)
414 || label2.equals(STEM))
416 tlabel = "" + anot[index].secondaryStructure;
420 tlabel = "" + anot[index].displayCharacter;
424 if (tlabel != null && !tlabel.equals(last))
426 if (last.length() > 0)
428 collatedInput += " ";
430 collatedInput += tlabel;
434 return collatedInput;
444 public void mousePressed(MouseEvent evt)
447 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
456 for (int i = 0; i < aa.length; i++)
460 height += aa[i].height;
463 if (evt.getY() < height)
469 else if (aa[i].graph > 0)
473 graphStretchY = evt.getY();
480 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
482 if (av.getColumnSelection() == null)
487 JPopupMenu pop = new JPopupMenu("Structure type");
490 * Just display the needed structure options
492 if (av.getAlignment().isNucleotide() == true)
494 item = new JMenuItem(STEM);
495 item.addActionListener(this);
500 item = new JMenuItem(HELIX);
501 item.addActionListener(this);
503 item = new JMenuItem(SHEET);
504 item.addActionListener(this);
507 item = new JMenuItem(LABEL);
508 item.addActionListener(this);
510 item = new JMenuItem(COLOUR);
511 item.addActionListener(this);
513 item = new JMenuItem(REMOVE);
514 item.addActionListener(this);
516 pop.show(this, evt.getX(), evt.getY());
526 ap.scalePanel.mousePressed(evt);
537 public void mouseReleased(MouseEvent evt)
541 mouseDragging = false;
542 ap.scalePanel.mouseReleased(evt);
552 public void mouseEntered(MouseEvent evt)
554 ap.scalePanel.mouseEntered(evt);
564 public void mouseExited(MouseEvent evt)
566 ap.scalePanel.mouseExited(evt);
576 public void mouseDragged(MouseEvent evt)
578 if (graphStretch > -1)
580 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
582 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
584 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
586 graphStretchY = evt.getY();
588 ap.paintAlignment(true);
592 ap.scalePanel.mouseDragged(evt);
603 public void mouseMoved(MouseEvent evt)
605 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
609 this.setToolTipText(null);
616 for (int i = 0; i < aa.length; i++)
620 height += aa[i].height;
623 if (evt.getY() < height)
633 this.setToolTipText(null);
637 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
639 if (av.hasHiddenColumns())
641 res = av.getColumnSelection().adjustForHiddenColumns(res);
644 if (row > -1 && aa[row].annotations != null
645 && res < aa[row].annotations.length)
647 if (aa[row].graphGroup > -1)
649 StringBuffer tip = new StringBuffer("<html>");
650 for (int gg = 0; gg < aa.length; gg++)
652 if (aa[gg].graphGroup == aa[row].graphGroup
653 && aa[gg].annotations[res] != null)
655 tip.append(aa[gg].label + " "
656 + aa[gg].annotations[res].description + "<br>");
659 if (tip.length() != 6)
661 tip.setLength(tip.length() - 4);
662 this.setToolTipText(tip.toString() + "</html>");
665 else if (aa[row].annotations[res] != null
666 && aa[row].annotations[res].description != null
667 && aa[row].annotations[res].description.length() > 0)
669 this.setToolTipText("<html>"+JvSwingUtils.wrapTooltip(aa[row].annotations[res].description)+"</html>");
673 // clear the tooltip.
674 this.setToolTipText(null);
677 if (aa[row].annotations[res] != null)
679 StringBuffer text = new StringBuffer("Sequence position "
682 if (aa[row].annotations[res].description != null)
684 text.append(" " + aa[row].annotations[res].description);
687 ap.alignFrame.statusBar.setText(text.toString());
692 this.setToolTipText(null);
703 public void mouseClicked(MouseEvent evt)
705 // if (activeRow != -1)
707 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
708 // AlignmentAnnotation anot = aa[activeRow];
712 // TODO mouseClicked-content and drawCursor are quite experimental!
713 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
716 int pady = av.charHeight / 5;
718 graphics.setColor(Color.black);
719 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
721 if (av.validCharWidth)
723 graphics.setColor(Color.white);
725 char s = seq.getCharAt(res);
727 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
728 graphics.drawString(String.valueOf(s), charOffset + x1,
729 (y1 + av.charHeight) - pady);
734 private volatile boolean imageFresh = false;
743 public void paintComponent(Graphics g)
745 g.setColor(Color.white);
746 g.fillRect(0, 0, getWidth(), getHeight());
750 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
751 || (getVisibleRect().height != g.getClipBounds().height))
753 g.drawImage(image, 0, 0, this);
758 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
761 if (image == null || imgWidth != image.getWidth(this)
762 || image.getHeight(this) != getHeight())
766 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
767 BufferedImage.TYPE_INT_RGB);
768 } catch (OutOfMemoryError oom)
773 } catch (Exception x)
778 "Couldn't allocate memory to redraw screen. Please restart Jalview",
782 gg = (Graphics2D) image.getGraphics();
786 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
787 RenderingHints.VALUE_ANTIALIAS_ON);
790 gg.setFont(av.getFont());
791 fm = gg.getFontMetrics();
792 gg.setColor(Color.white);
793 gg.fillRect(0, 0, imgWidth, image.getHeight());
797 drawComponent(gg, av.startRes, av.endRes + 1);
799 g.drawImage(image, 0, 0, this);
802 * set true to enable redraw timing debug output on stderr
804 private final boolean debugRedraw = false;
806 * non-Thread safe repaint
809 * repaint with horizontal shift in alignment
811 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 long stime=System.currentTimeMillis();
822 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
823 long mtime=System.currentTimeMillis();
824 int sr = av.startRes;
825 int er = av.endRes + 1;
828 if (horizontal > 0) // scrollbar pulled right, image to the left
830 transX = (er - sr - horizontal) * av.charWidth;
831 sr = er - horizontal;
833 else if (horizontal < 0)
835 er = sr - horizontal;
838 gg.translate(transX, 0);
840 drawComponent(gg, sr, er);
842 gg.translate(-transX, 0);
843 long dtime=System.currentTimeMillis();
846 long rtime=System.currentTimeMillis();
848 System.err.println("Scroll:\t"+horizontal+"\tCopyArea:\t"+(mtime-stime)+"\tDraw component:\t"+(dtime-mtime)+"\tRepaint call:\t"+(rtime-dtime));
853 private volatile boolean lastImageGood = false;
865 public void drawComponent(Graphics g, int startRes, int endRes)
867 BufferedImage oldFaded = fadedImage;
868 if (av.isCalcInProgress())
872 lastImageGood = false;
875 // We'll keep a record of the old image,
876 // and draw a faded image until the calculation
879 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
880 .getHeight() != image.getHeight()))
882 // System.err.println("redraw faded image ("+(fadedImage==null ?
883 // "null image" : "") + " lastGood="+lastImageGood+")");
884 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
885 BufferedImage.TYPE_INT_RGB);
887 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
889 fadedG.setColor(Color.white);
890 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
892 fadedG.setComposite(AlphaComposite.getInstance(
893 AlphaComposite.SRC_OVER, .3f));
894 fadedG.drawImage(image, 0, 0, this);
897 // make sure we don't overwrite the last good faded image until all
898 // calculations have finished
899 lastImageGood = false;
904 if (fadedImage != null)
906 oldFaded = fadedImage;
911 g.setColor(Color.white);
912 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
914 g.setFont(av.getFont());
917 fm = g.getFontMetrics();
920 if ((av.getAlignment().getAlignmentAnnotation() == null)
921 || (av.getAlignment().getAlignmentAnnotation().length < 1))
923 g.setColor(Color.white);
924 g.fillRect(0, 0, getWidth(), getHeight());
925 g.setColor(Color.black);
926 if (av.validCharWidth)
928 g.drawString(MessageManager.getString("label.alignment_has_no_annotations"), 20, 15);
933 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
935 if (!lastImageGood && fadedImage == null)
937 fadedImage = oldFaded;
942 public FontMetrics getFontMetrics()
948 public Image getFadedImage()
954 public int getFadedImageWidth()
958 private int[] bounds = new int[2];
960 public int[] getVisibleVRange()
962 if (ap!=null && ap.alabels!=null)
964 int sOffset=-ap.alabels.scrollOffset;
965 int visHeight = sOffset+ap.annotationSpaceFillerHolder.getHeight();
966 bounds[0] = sOffset; bounds[1]=visHeight;