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;
30 import jalview.util.Platform;
32 import java.awt.AlphaComposite;
33 import java.awt.Color;
34 import java.awt.Dimension;
35 import java.awt.FontMetrics;
36 import java.awt.Graphics;
37 import java.awt.Graphics2D;
38 import java.awt.Image;
39 import java.awt.Rectangle;
40 import java.awt.RenderingHints;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.AdjustmentEvent;
44 import java.awt.event.AdjustmentListener;
45 import java.awt.event.MouseEvent;
46 import java.awt.event.MouseListener;
47 import java.awt.event.MouseMotionListener;
48 import java.awt.event.MouseWheelEvent;
49 import java.awt.event.MouseWheelListener;
50 import java.awt.image.BufferedImage;
52 import javax.swing.JColorChooser;
53 import javax.swing.JMenuItem;
54 import javax.swing.JOptionPane;
55 import javax.swing.JPanel;
56 import javax.swing.JPopupMenu;
57 import javax.swing.Scrollable;
58 import javax.swing.SwingUtilities;
59 import javax.swing.ToolTipManager;
62 * AnnotationPanel displays visible portion of annotation rows below unwrapped
68 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
69 MouseListener, MouseWheelListener, MouseMotionListener,
70 ActionListener, AdjustmentListener, Scrollable
72 String HELIX = MessageManager.getString("label.helix");
74 String SHEET = MessageManager.getString("label.sheet");
77 * For RNA secondary structure "stems" aka helices
79 String STEM = MessageManager.getString("label.rna_helix");
81 String LABEL = MessageManager.getString("label.label");
83 String REMOVE = MessageManager.getString("label.remove_annotation");
85 String COLOUR = MessageManager.getString("action.colour");
87 public final Color HELIX_COLOUR = Color.red.darker();
89 public final Color SHEET_COLOUR = Color.green.darker().darker();
91 public final Color STEM_COLOUR = Color.blue.darker();
94 public AlignViewport av;
98 public int activeRow = -1;
100 public BufferedImage image;
102 public volatile BufferedImage fadedImage;
106 public FontMetrics fm;
108 public int imgWidth = 0;
110 boolean fastPaint = false;
112 // Used For mouse Dragging and resizing graphs
113 int graphStretch = -1;
115 int graphStretchY = -1;
117 int min; // used by mouseDragged to see if user
119 int max; // used by mouseDragged to see if user
121 boolean mouseDragging = false;
125 // for editing cursor
130 public final AnnotationRenderer renderer;
132 private MouseWheelListener[] _mwl;
135 * Creates a new AnnotationPanel object.
140 public AnnotationPanel(AlignmentPanel ap)
143 MAC = Platform.isAMac();
145 ToolTipManager.sharedInstance().registerComponent(this);
146 ToolTipManager.sharedInstance().setInitialDelay(0);
147 ToolTipManager.sharedInstance().setDismissDelay(10000);
150 this.setLayout(null);
151 addMouseListener(this);
152 addMouseMotionListener(this);
153 ap.annotationScroller.getVerticalScrollBar()
154 .addAdjustmentListener(this);
155 // save any wheel listeners on the scroller, so we can propagate scroll
157 _mwl = ap.annotationScroller.getMouseWheelListeners();
158 // and then set our own listener to consume all mousewheel events
159 ap.annotationScroller.addMouseWheelListener(this);
160 renderer = new AnnotationRenderer();
163 public AnnotationPanel(AlignViewport av)
166 renderer = new AnnotationRenderer();
170 public void mouseWheelMoved(MouseWheelEvent e)
175 if (e.getWheelRotation() > 0)
177 ap.scrollRight(true);
181 ap.scrollRight(false);
186 // TODO: find the correct way to let the event bubble up to
187 // ap.annotationScroller
188 for (MouseWheelListener mwl : _mwl)
192 mwl.mouseWheelMoved(e);
203 public Dimension getPreferredScrollableViewportSize()
205 return getPreferredSize();
209 public int getScrollableBlockIncrement(Rectangle visibleRect,
210 int orientation, int direction)
216 public boolean getScrollableTracksViewportHeight()
222 public boolean getScrollableTracksViewportWidth()
228 public int getScrollableUnitIncrement(Rectangle visibleRect,
229 int orientation, int direction)
238 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
242 public void adjustmentValueChanged(AdjustmentEvent evt)
244 // update annotation label display
245 ap.getAlabels().setScrollOffset(-evt.getValue());
249 * Calculates the height of the annotation displayed in the annotation panel.
250 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
251 * all annotation associated components are updated correctly.
254 public int adjustPanelHeight()
256 int height = av.calcPanelHeight();
257 this.setPreferredSize(new Dimension(1, height));
260 // revalidate only when the alignment panel is fully constructed
274 public void actionPerformed(ActionEvent evt)
276 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
281 Annotation[] anot = aa[activeRow].annotations;
283 if (anot.length < av.getColumnSelection().getMax())
285 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
286 System.arraycopy(anot, 0, temp, 0, anot.length);
288 aa[activeRow].annotations = anot;
291 if (evt.getActionCommand().equals(REMOVE))
293 for (int i = 0; i < av.getColumnSelection().size(); i++)
295 anot[av.getColumnSelection().columnAt(i)] = null;
298 else if (evt.getActionCommand().equals(LABEL))
300 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
301 String label = JOptionPane.showInputDialog(this,
302 MessageManager.getString("label.enter_label"), exMesg);
309 if ((label.length() > 0) && !aa[activeRow].hasText)
311 aa[activeRow].hasText = true;
314 for (int i = 0; i < av.getColumnSelection().size(); i++)
316 int index = av.getColumnSelection().columnAt(i);
318 if (!av.getColumnSelection().isVisible(index))
323 if (anot[index] == null)
325 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
332 anot[index].displayCharacter = label;
336 else if (evt.getActionCommand().equals(COLOUR))
338 Color col = JColorChooser.showDialog(this,
339 MessageManager.getString("label.select_foreground_colour"), Color.black);
341 for (int i = 0; i < av.getColumnSelection().size(); i++)
343 int index = av.getColumnSelection().columnAt(i);
345 if (!av.getColumnSelection().isVisible(index))
350 if (anot[index] == null)
352 anot[index] = new Annotation("", "", ' ', 0);
355 anot[index].colour = col;
362 String symbol = "\u03B1";
364 if (evt.getActionCommand().equals(HELIX))
368 else if (evt.getActionCommand().equals(SHEET))
374 // Added by LML to color stems
375 else if (evt.getActionCommand().equals(STEM))
381 if (!aa[activeRow].hasIcons)
383 aa[activeRow].hasIcons = true;
386 String label = JOptionPane.showInputDialog(MessageManager
387 .getString("label.enter_label_for_the_structure"), symbol);
394 if ((label.length() > 0) && !aa[activeRow].hasText)
396 aa[activeRow].hasText = true;
397 if (evt.getActionCommand().equals(STEM))
399 aa[activeRow].showAllColLabels = true;
402 for (int i = 0; i < av.getColumnSelection().size(); i++)
404 int index = av.getColumnSelection().columnAt(i);
406 if (!av.getColumnSelection().isVisible(index))
411 if (anot[index] == null)
413 anot[index] = new Annotation(label, "", type, 0);
417 anot[index].secondaryStructure = type != 'S' ? type : label
418 .length() == 0 ? ' ' : label.charAt(0);
419 anot[index].displayCharacter = label;
423 av.getAlignment().validateAnnotation(aa[activeRow]);
424 ap.alignmentChanged();
432 private String collectAnnotVals(Annotation[] anot,
433 ColumnSelection columnSelection, String label2)
435 String collatedInput = "";
437 ColumnSelection viscols = av.getColumnSelection();
438 // TODO: refactor and save av.getColumnSelection for efficiency
439 for (int i = 0; i < columnSelection.size(); i++)
441 int index = columnSelection.columnAt(i);
442 // always check for current display state - just in case
443 if (!viscols.isVisible(index))
447 String tlabel = null;
448 if (anot[index] != null)
449 { // LML added stem code
450 if (label2.equals(HELIX) || label2.equals(SHEET)
451 || label2.equals(STEM) || label2.equals(LABEL))
453 tlabel = anot[index].description;
454 if (tlabel == null || tlabel.length() < 1)
456 if (label2.equals(HELIX) || label2.equals(SHEET)
457 || label2.equals(STEM))
459 tlabel = "" + anot[index].secondaryStructure;
463 tlabel = "" + anot[index].displayCharacter;
467 if (tlabel != null && !tlabel.equals(last))
469 if (last.length() > 0)
471 collatedInput += " ";
473 collatedInput += tlabel;
477 return collatedInput;
487 public void mousePressed(MouseEvent evt)
490 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
499 for (int i = 0; i < aa.length; i++)
503 height += aa[i].height;
506 if (evt.getY() < height)
512 else if (aa[i].graph > 0)
516 graphStretchY = evt.getY();
523 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
525 if (av.getColumnSelection() == null)
530 JPopupMenu pop = new JPopupMenu(
531 MessageManager.getString("label.structure_type"));
534 * Just display the needed structure options
536 if (av.getAlignment().isNucleotide() == true)
538 item = new JMenuItem(STEM);
539 item.addActionListener(this);
544 item = new JMenuItem(HELIX);
545 item.addActionListener(this);
547 item = new JMenuItem(SHEET);
548 item.addActionListener(this);
551 item = new JMenuItem(LABEL);
552 item.addActionListener(this);
554 item = new JMenuItem(COLOUR);
555 item.addActionListener(this);
557 item = new JMenuItem(REMOVE);
558 item.addActionListener(this);
560 pop.show(this, evt.getX(), evt.getY());
570 ap.getScalePanel().mousePressed(evt);
581 public void mouseReleased(MouseEvent evt)
585 mouseDragging = false;
586 ap.getScalePanel().mouseReleased(evt);
596 public void mouseEntered(MouseEvent evt)
598 ap.getScalePanel().mouseEntered(evt);
608 public void mouseExited(MouseEvent evt)
610 ap.getScalePanel().mouseExited(evt);
620 public void mouseDragged(MouseEvent evt)
622 if (graphStretch > -1)
624 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
626 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
628 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
630 graphStretchY = evt.getY();
632 ap.paintAlignment(true);
636 ap.getScalePanel().mouseDragged(evt);
647 public void mouseMoved(MouseEvent evt)
649 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
653 this.setToolTipText(null);
660 for (int i = 0; i < aa.length; i++)
664 height += aa[i].height;
667 if (evt.getY() < height)
677 this.setToolTipText(null);
681 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
683 if (av.hasHiddenColumns())
685 res = av.getColumnSelection().adjustForHiddenColumns(res);
688 if (row > -1 && aa[row].annotations != null
689 && res < aa[row].annotations.length)
691 if (aa[row].graphGroup > -1)
693 StringBuffer tip = new StringBuffer("<html>");
694 for (int gg = 0; gg < aa.length; gg++)
696 if (aa[gg].graphGroup == aa[row].graphGroup
697 && aa[gg].annotations[res] != null)
699 tip.append(aa[gg].label + " "
700 + aa[gg].annotations[res].description + "<br>");
703 if (tip.length() != 6)
705 tip.setLength(tip.length() - 4);
706 this.setToolTipText(tip.toString() + "</html>");
709 else if (aa[row].annotations[res] != null
710 && aa[row].annotations[res].description != null
711 && aa[row].annotations[res].description.length() > 0)
713 this.setToolTipText(JvSwingUtils
714 .wrapTooltip(true, aa[row].annotations[res].description));
718 // clear the tooltip.
719 this.setToolTipText(null);
722 if (aa[row].annotations[res] != null)
724 StringBuffer text = new StringBuffer("Sequence position "
727 if (aa[row].annotations[res].description != null)
729 text.append(" " + aa[row].annotations[res].description);
732 ap.alignFrame.statusBar.setText(text.toString());
737 this.setToolTipText(null);
748 public void mouseClicked(MouseEvent evt)
750 // if (activeRow != -1)
752 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
753 // AlignmentAnnotation anot = aa[activeRow];
757 // TODO mouseClicked-content and drawCursor are quite experimental!
758 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
761 int pady = av.getCharHeight() / 5;
763 graphics.setColor(Color.black);
764 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
766 if (av.validCharWidth)
768 graphics.setColor(Color.white);
770 char s = seq.getCharAt(res);
772 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
773 graphics.drawString(String.valueOf(s), charOffset + x1,
774 (y1 + av.getCharHeight()) - pady);
779 private volatile boolean imageFresh = false;
788 public void paintComponent(Graphics g)
790 g.setColor(Color.white);
791 g.fillRect(0, 0, getWidth(), getHeight());
795 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
796 || (getVisibleRect().height != g.getClipBounds().height))
798 g.drawImage(image, 0, 0, this);
803 imgWidth = (av.endRes - av.startRes + 1) * av.getCharWidth();
808 if (image == null || imgWidth != image.getWidth(this)
809 || image.getHeight(this) != getHeight())
813 image = new BufferedImage(imgWidth, ap.getAnnotationPanel().getHeight(),
814 BufferedImage.TYPE_INT_RGB);
815 } catch (OutOfMemoryError oom)
820 } catch (Exception x)
825 "Couldn't allocate memory to redraw screen. Please restart Jalview",
829 gg = (Graphics2D) image.getGraphics();
833 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
834 RenderingHints.VALUE_ANTIALIAS_ON);
837 gg.setFont(av.getFont());
838 fm = gg.getFontMetrics();
839 gg.setColor(Color.white);
840 gg.fillRect(0, 0, imgWidth, image.getHeight());
844 drawComponent(gg, av.startRes, av.endRes + 1);
846 g.drawImage(image, 0, 0, this);
850 * set true to enable redraw timing debug output on stderr
852 private final boolean debugRedraw = false;
855 * non-Thread safe repaint
858 * repaint with horizontal shift in alignment
860 public void fastPaint(int horizontal)
862 if ((horizontal == 0) || gg == null
863 || av.getAlignment().getAlignmentAnnotation() == null
864 || av.getAlignment().getAlignmentAnnotation().length < 1
865 || av.isCalcInProgress())
870 long stime = System.currentTimeMillis();
871 gg.copyArea(0, 0, imgWidth, getHeight(),
872 -horizontal * av.getCharWidth(), 0);
873 long mtime = System.currentTimeMillis();
874 int sr = av.startRes;
875 int er = av.endRes + 1;
878 if (horizontal > 0) // scrollbar pulled right, image to the left
880 transX = (er - sr - horizontal) * av.getCharWidth();
881 sr = er - horizontal;
883 else if (horizontal < 0)
885 er = sr - horizontal;
888 gg.translate(transX, 0);
890 drawComponent(gg, sr, er);
892 gg.translate(-transX, 0);
893 long dtime = System.currentTimeMillis();
896 long rtime = System.currentTimeMillis();
899 System.err.println("Scroll:\t" + horizontal + "\tCopyArea:\t"
900 + (mtime - stime) + "\tDraw component:\t" + (dtime - mtime)
901 + "\tRepaint call:\t" + (rtime - dtime));
906 private volatile boolean lastImageGood = false;
918 public void drawComponent(Graphics g, int startRes, int endRes)
920 BufferedImage oldFaded = fadedImage;
921 if (av.isCalcInProgress())
925 lastImageGood = false;
928 // We'll keep a record of the old image,
929 // and draw a faded image until the calculation
932 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
933 .getHeight() != image.getHeight()))
935 // System.err.println("redraw faded image ("+(fadedImage==null ?
936 // "null image" : "") + " lastGood="+lastImageGood+")");
937 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
938 BufferedImage.TYPE_INT_RGB);
940 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
942 fadedG.setColor(Color.white);
943 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
945 fadedG.setComposite(AlphaComposite.getInstance(
946 AlphaComposite.SRC_OVER, .3f));
947 fadedG.drawImage(image, 0, 0, this);
950 // make sure we don't overwrite the last good faded image until all
951 // calculations have finished
952 lastImageGood = false;
957 if (fadedImage != null)
959 oldFaded = fadedImage;
964 g.setColor(Color.white);
965 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(), getHeight());
967 g.setFont(av.getFont());
970 fm = g.getFontMetrics();
973 if ((av.getAlignment().getAlignmentAnnotation() == null)
974 || (av.getAlignment().getAlignmentAnnotation().length < 1))
976 g.setColor(Color.white);
977 g.fillRect(0, 0, getWidth(), getHeight());
978 g.setColor(Color.black);
979 if (av.validCharWidth)
981 g.drawString(MessageManager
982 .getString("label.alignment_has_no_annotations"), 20, 15);
987 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
989 if (!lastImageGood && fadedImage == null)
991 fadedImage = oldFaded;
996 public FontMetrics getFontMetrics()
1002 public Image getFadedImage()
1008 public int getFadedImageWidth()
1013 private int[] bounds = new int[2];
1016 public int[] getVisibleVRange()
1018 if (ap != null && ap.getAlabels() != null)
1020 int sOffset = -ap.getAlabels().getScrollOffset();
1021 int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
1022 bounds[0] = sOffset;
1023 bounds[1] = visHeight;