2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.SequenceI;
27 import jalview.renderer.AnnotationRenderer;
28 import jalview.renderer.AwtRenderPanelI;
29 import jalview.util.MessageManager;
31 import java.awt.AlphaComposite;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FontMetrics;
35 import java.awt.Graphics;
36 import java.awt.Graphics2D;
37 import java.awt.Image;
38 import java.awt.Rectangle;
39 import java.awt.RenderingHints;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.AdjustmentEvent;
43 import java.awt.event.AdjustmentListener;
44 import java.awt.event.MouseEvent;
45 import java.awt.event.MouseListener;
46 import java.awt.event.MouseMotionListener;
47 import java.awt.event.MouseWheelEvent;
48 import java.awt.event.MouseWheelListener;
49 import java.awt.image.BufferedImage;
51 import javax.swing.JColorChooser;
52 import javax.swing.JMenuItem;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPanel;
55 import javax.swing.JPopupMenu;
56 import javax.swing.Scrollable;
57 import javax.swing.ToolTipManager;
60 * AnnotationPanel displays visible portion of annotation rows below unwrapped
66 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
67 MouseListener, MouseWheelListener, MouseMotionListener,
68 ActionListener, AdjustmentListener, Scrollable
70 String HELIX = MessageManager.getString("label.helix");
72 String SHEET = MessageManager.getString("label.sheet");
75 * For RNA secondary structure "stems" aka helices
77 String STEM = MessageManager.getString("label.rna_helix");
79 String LABEL = MessageManager.getString("label.label");
81 String REMOVE = MessageManager.getString("label.remove_annotation");
83 String COLOUR = MessageManager.getString("action.colour");
85 public final Color HELIX_COLOUR = Color.red.darker();
87 public final Color SHEET_COLOUR = Color.green.darker().darker();
89 public final Color STEM_COLOUR = Color.blue.darker();
92 public AlignViewport av;
96 public int activeRow = -1;
98 public BufferedImage image;
100 public volatile BufferedImage fadedImage;
104 public FontMetrics fm;
106 public int imgWidth = 0;
108 boolean fastPaint = false;
110 // Used For mouse Dragging and resizing graphs
111 int graphStretch = -1;
113 int graphStretchY = -1;
115 int min; // used by mouseDragged to see if user
117 int max; // used by mouseDragged to see if user
119 boolean mouseDragging = false;
123 // for editing cursor
128 public final AnnotationRenderer renderer;
130 private MouseWheelListener[] _mwl;
133 * Creates a new AnnotationPanel object.
138 public AnnotationPanel(AlignmentPanel ap)
141 MAC = jalview.util.Platform.isAMac();
143 ToolTipManager.sharedInstance().registerComponent(this);
144 ToolTipManager.sharedInstance().setInitialDelay(0);
145 ToolTipManager.sharedInstance().setDismissDelay(10000);
148 this.setLayout(null);
149 addMouseListener(this);
150 addMouseMotionListener(this);
151 ap.annotationScroller.getVerticalScrollBar()
152 .addAdjustmentListener(this);
153 // save any wheel listeners on the scroller, so we can propagate scroll
155 _mwl = ap.annotationScroller.getMouseWheelListeners();
156 // and then set our own listener to consume all mousewheel events
157 ap.annotationScroller.addMouseWheelListener(this);
158 renderer = new AnnotationRenderer();
161 public AnnotationPanel(AlignViewport av)
164 renderer = new AnnotationRenderer();
168 public void mouseWheelMoved(MouseWheelEvent e)
173 if (e.getWheelRotation() > 0)
175 ap.scrollRight(true);
179 ap.scrollRight(false);
184 // TODO: find the correct way to let the event bubble up to
185 // ap.annotationScroller
186 for (MouseWheelListener mwl : _mwl)
190 mwl.mouseWheelMoved(e);
201 public Dimension getPreferredScrollableViewportSize()
203 return getPreferredSize();
207 public int getScrollableBlockIncrement(Rectangle visibleRect,
208 int orientation, int direction)
214 public boolean getScrollableTracksViewportHeight()
220 public boolean getScrollableTracksViewportWidth()
226 public int getScrollableUnitIncrement(Rectangle visibleRect,
227 int orientation, int direction)
236 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
240 public void adjustmentValueChanged(AdjustmentEvent evt)
242 // update annotation label display
243 ap.getAlabels().setScrollOffset(-evt.getValue());
247 * Calculates the height of the annotation displayed in the annotation panel.
248 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
249 * all annotation associated components are updated correctly.
252 public int adjustPanelHeight()
254 int height = av.calcPanelHeight();
255 this.setPreferredSize(new Dimension(1, height));
258 // revalidate only when the alignment panel is fully constructed
272 public void actionPerformed(ActionEvent evt)
274 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
279 Annotation[] anot = aa[activeRow].annotations;
281 if (anot.length < av.getColumnSelection().getMax())
283 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
284 System.arraycopy(anot, 0, temp, 0, anot.length);
286 aa[activeRow].annotations = anot;
289 if (evt.getActionCommand().equals(REMOVE))
291 for (int sel : av.getColumnSelection().getSelected())
296 else if (evt.getActionCommand().equals(LABEL))
298 String exMesg = collectAnnotVals(anot, LABEL);
299 String label = JOptionPane.showInputDialog(this,
300 MessageManager.getString("label.enter_label"), exMesg);
307 if ((label.length() > 0) && !aa[activeRow].hasText)
309 aa[activeRow].hasText = true;
312 for (int index : av.getColumnSelection().getSelected())
314 if (!av.getColumnSelection().isVisible(index))
319 if (anot[index] == null)
321 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
328 anot[index].displayCharacter = label;
332 else if (evt.getActionCommand().equals(COLOUR))
334 Color col = JColorChooser.showDialog(this,
335 MessageManager.getString("label.select_foreground_colour"),
338 for (int index : av.getColumnSelection().getSelected())
340 if (!av.getColumnSelection().isVisible(index))
345 if (anot[index] == null)
347 anot[index] = new Annotation("", "", ' ', 0);
350 anot[index].colour = col;
357 String symbol = "\u03B1";
359 if (evt.getActionCommand().equals(HELIX))
363 else if (evt.getActionCommand().equals(SHEET))
369 // Added by LML to color stems
370 else if (evt.getActionCommand().equals(STEM))
376 if (!aa[activeRow].hasIcons)
378 aa[activeRow].hasIcons = true;
381 String label = JOptionPane.showInputDialog(MessageManager
382 .getString("label.enter_label_for_the_structure"), symbol);
389 if ((label.length() > 0) && !aa[activeRow].hasText)
391 aa[activeRow].hasText = true;
392 if (evt.getActionCommand().equals(STEM))
394 aa[activeRow].showAllColLabels = true;
397 for (int index : av.getColumnSelection().getSelected())
399 if (!av.getColumnSelection().isVisible(index))
404 if (anot[index] == null)
406 anot[index] = new Annotation(label, "", type, 0);
409 anot[index].secondaryStructure = type != 'S' ? type : label
410 .length() == 0 ? ' ' : label.charAt(0);
411 anot[index].displayCharacter = label;
415 av.getAlignment().validateAnnotation(aa[activeRow]);
416 ap.alignmentChanged();
424 private String collectAnnotVals(Annotation[] anot, String label2)
426 String collatedInput = "";
428 ColumnSelection viscols = av.getColumnSelection();
429 // TODO: refactor and save av.getColumnSelection for efficiency
430 for (int index : viscols.getSelected())
432 // always check for current display state - just in case
433 if (!viscols.isVisible(index))
437 String tlabel = null;
438 if (anot[index] != null)
439 { // LML added stem code
440 if (label2.equals(HELIX) || label2.equals(SHEET)
441 || label2.equals(STEM) || label2.equals(LABEL))
443 tlabel = anot[index].description;
444 if (tlabel == null || tlabel.length() < 1)
446 if (label2.equals(HELIX) || label2.equals(SHEET)
447 || label2.equals(STEM))
449 tlabel = "" + anot[index].secondaryStructure;
453 tlabel = "" + anot[index].displayCharacter;
457 if (tlabel != null && !tlabel.equals(last))
459 if (last.length() > 0)
461 collatedInput += " ";
463 collatedInput += tlabel;
467 return collatedInput;
477 public void mousePressed(MouseEvent evt)
480 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
489 for (int i = 0; i < aa.length; i++)
493 height += aa[i].height;
496 if (evt.getY() < height)
502 else if (aa[i].graph > 0)
506 graphStretchY = evt.getY();
513 if (evt.isPopupTrigger() && activeRow != -1)
515 if (av.getColumnSelection() == null)
520 JPopupMenu pop = new JPopupMenu(
521 MessageManager.getString("label.structure_type"));
524 * Just display the needed structure options
526 if (av.getAlignment().isNucleotide() == true)
528 item = new JMenuItem(STEM);
529 item.addActionListener(this);
534 item = new JMenuItem(HELIX);
535 item.addActionListener(this);
537 item = new JMenuItem(SHEET);
538 item.addActionListener(this);
541 item = new JMenuItem(LABEL);
542 item.addActionListener(this);
544 item = new JMenuItem(COLOUR);
545 item.addActionListener(this);
547 item = new JMenuItem(REMOVE);
548 item.addActionListener(this);
550 pop.show(this, evt.getX(), evt.getY());
560 ap.getScalePanel().mousePressed(evt);
571 public void mouseReleased(MouseEvent evt)
575 mouseDragging = false;
576 ap.getScalePanel().mouseReleased(evt);
586 public void mouseEntered(MouseEvent evt)
588 ap.getScalePanel().mouseEntered(evt);
598 public void mouseExited(MouseEvent evt)
600 ap.getScalePanel().mouseExited(evt);
610 public void mouseDragged(MouseEvent evt)
612 if (graphStretch > -1)
614 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
616 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
618 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
620 graphStretchY = evt.getY();
622 ap.paintAlignment(true);
626 ap.getScalePanel().mouseDragged(evt);
637 public void mouseMoved(MouseEvent evt)
639 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
643 this.setToolTipText(null);
650 for (int i = 0; i < aa.length; i++)
654 height += aa[i].height;
657 if (evt.getY() < height)
667 this.setToolTipText(null);
671 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
673 if (av.hasHiddenColumns())
675 res = av.getColumnSelection().adjustForHiddenColumns(res);
678 if (row > -1 && aa[row].annotations != null
679 && res < aa[row].annotations.length)
681 if (aa[row].graphGroup > -1)
683 StringBuffer tip = new StringBuffer("<html>");
684 for (int gg = 0; gg < aa.length; gg++)
686 if (aa[gg].graphGroup == aa[row].graphGroup
687 && aa[gg].annotations[res] != null)
689 tip.append(aa[gg].label + " "
690 + aa[gg].annotations[res].description + "<br>");
693 if (tip.length() != 6)
695 tip.setLength(tip.length() - 4);
696 this.setToolTipText(tip.toString() + "</html>");
699 else if (aa[row].annotations[res] != null
700 && aa[row].annotations[res].description != null
701 && aa[row].annotations[res].description.length() > 0)
703 this.setToolTipText(JvSwingUtils.wrapTooltip(true,
704 aa[row].annotations[res].description));
708 // clear the tooltip.
709 this.setToolTipText(null);
712 if (aa[row].annotations[res] != null)
714 StringBuffer text = new StringBuffer("Sequence position "
717 if (aa[row].annotations[res].description != null)
719 text.append(" " + aa[row].annotations[res].description);
722 ap.alignFrame.statusBar.setText(text.toString());
727 this.setToolTipText(null);
738 public void mouseClicked(MouseEvent evt)
740 // if (activeRow != -1)
742 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
743 // AlignmentAnnotation anot = aa[activeRow];
747 // TODO mouseClicked-content and drawCursor are quite experimental!
748 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
751 int pady = av.getCharHeight() / 5;
753 graphics.setColor(Color.black);
754 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
756 if (av.validCharWidth)
758 graphics.setColor(Color.white);
760 char s = seq.getCharAt(res);
762 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
763 graphics.drawString(String.valueOf(s), charOffset + x1,
764 (y1 + av.getCharHeight()) - pady);
769 private volatile boolean imageFresh = false;
778 public void paintComponent(Graphics g)
780 g.setColor(Color.white);
781 g.fillRect(0, 0, getWidth(), getHeight());
785 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
786 || (getVisibleRect().height != g.getClipBounds().height))
788 g.drawImage(image, 0, 0, this);
793 imgWidth = (av.endRes - av.startRes + 1) * av.getCharWidth();
798 if (image == null || imgWidth != image.getWidth(this)
799 || image.getHeight(this) != getHeight())
803 image = new BufferedImage(imgWidth, ap.getAnnotationPanel()
804 .getHeight(), BufferedImage.TYPE_INT_RGB);
805 } catch (OutOfMemoryError oom)
810 } catch (Exception x)
815 "Couldn't allocate memory to redraw screen. Please restart Jalview",
819 gg = (Graphics2D) image.getGraphics();
823 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
824 RenderingHints.VALUE_ANTIALIAS_ON);
827 gg.setFont(av.getFont());
828 fm = gg.getFontMetrics();
829 gg.setColor(Color.white);
830 gg.fillRect(0, 0, imgWidth, image.getHeight());
834 drawComponent(gg, av.startRes, av.endRes + 1);
836 g.drawImage(image, 0, 0, this);
840 * set true to enable redraw timing debug output on stderr
842 private final boolean debugRedraw = false;
845 * non-Thread safe repaint
848 * repaint with horizontal shift in alignment
850 public void fastPaint(int horizontal)
852 if ((horizontal == 0) || gg == null
853 || av.getAlignment().getAlignmentAnnotation() == null
854 || av.getAlignment().getAlignmentAnnotation().length < 1
855 || av.isCalcInProgress())
860 long stime = System.currentTimeMillis();
861 gg.copyArea(0, 0, imgWidth, getHeight(),
862 -horizontal * av.getCharWidth(), 0);
863 long mtime = System.currentTimeMillis();
864 int sr = av.startRes;
865 int er = av.endRes + 1;
868 if (horizontal > 0) // scrollbar pulled right, image to the left
870 transX = (er - sr - horizontal) * av.getCharWidth();
871 sr = er - horizontal;
873 else if (horizontal < 0)
875 er = sr - horizontal;
878 gg.translate(transX, 0);
880 drawComponent(gg, sr, er);
882 gg.translate(-transX, 0);
883 long dtime = System.currentTimeMillis();
886 long rtime = System.currentTimeMillis();
889 System.err.println("Scroll:\t" + horizontal + "\tCopyArea:\t"
890 + (mtime - stime) + "\tDraw component:\t" + (dtime - mtime)
891 + "\tRepaint call:\t" + (rtime - dtime));
896 private volatile boolean lastImageGood = false;
908 public void drawComponent(Graphics g, int startRes, int endRes)
910 BufferedImage oldFaded = fadedImage;
911 if (av.isCalcInProgress())
915 lastImageGood = false;
918 // We'll keep a record of the old image,
919 // and draw a faded image until the calculation
922 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
923 .getHeight() != image.getHeight()))
925 // System.err.println("redraw faded image ("+(fadedImage==null ?
926 // "null image" : "") + " lastGood="+lastImageGood+")");
927 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
928 BufferedImage.TYPE_INT_RGB);
930 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
932 fadedG.setColor(Color.white);
933 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
935 fadedG.setComposite(AlphaComposite.getInstance(
936 AlphaComposite.SRC_OVER, .3f));
937 fadedG.drawImage(image, 0, 0, this);
940 // make sure we don't overwrite the last good faded image until all
941 // calculations have finished
942 lastImageGood = false;
947 if (fadedImage != null)
949 oldFaded = fadedImage;
954 g.setColor(Color.white);
955 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(), getHeight());
957 g.setFont(av.getFont());
960 fm = g.getFontMetrics();
963 if ((av.getAlignment().getAlignmentAnnotation() == null)
964 || (av.getAlignment().getAlignmentAnnotation().length < 1))
966 g.setColor(Color.white);
967 g.fillRect(0, 0, getWidth(), getHeight());
968 g.setColor(Color.black);
969 if (av.validCharWidth)
971 g.drawString(MessageManager
972 .getString("label.alignment_has_no_annotations"), 20, 15);
977 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
979 if (!lastImageGood && fadedImage == null)
981 fadedImage = oldFaded;
986 public FontMetrics getFontMetrics()
992 public Image getFadedImage()
998 public int getFadedImageWidth()
1003 private int[] bounds = new int[2];
1006 public int[] getVisibleVRange()
1008 if (ap != null && ap.getAlabels() != null)
1010 int sOffset = -ap.getAlabels().getScrollOffset();
1011 int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
1012 bounds[0] = sOffset;
1013 bounds[1] = visHeight;