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;
354 // HELIX, SHEET or STEM
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;
416 av.getAlignment().validateAnnotation(aa[activeRow]);
417 ap.alignmentChanged();
418 ap.alignFrame.setMenusForViewport();
425 private String collectAnnotVals(Annotation[] anot, String label2)
427 String collatedInput = "";
429 ColumnSelection viscols = av.getColumnSelection();
430 // TODO: refactor and save av.getColumnSelection for efficiency
431 for (int index : viscols.getSelected())
433 // always check for current display state - just in case
434 if (!viscols.isVisible(index))
438 String tlabel = null;
439 if (anot[index] != null)
440 { // LML added stem code
441 if (label2.equals(HELIX) || label2.equals(SHEET)
442 || label2.equals(STEM) || label2.equals(LABEL))
444 tlabel = anot[index].description;
445 if (tlabel == null || tlabel.length() < 1)
447 if (label2.equals(HELIX) || label2.equals(SHEET)
448 || label2.equals(STEM))
450 tlabel = "" + anot[index].secondaryStructure;
454 tlabel = "" + anot[index].displayCharacter;
458 if (tlabel != null && !tlabel.equals(last))
460 if (last.length() > 0)
462 collatedInput += " ";
464 collatedInput += tlabel;
468 return collatedInput;
478 public void mousePressed(MouseEvent evt)
481 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
490 for (int i = 0; i < aa.length; i++)
494 height += aa[i].height;
497 if (evt.getY() < height)
503 else if (aa[i].graph > 0)
507 graphStretchY = evt.getY();
514 if (evt.isPopupTrigger() && activeRow != -1)
516 if (av.getColumnSelection() == null)
521 JPopupMenu pop = new JPopupMenu(
522 MessageManager.getString("label.structure_type"));
525 * Just display the needed structure options
527 if (av.getAlignment().isNucleotide() == true)
529 item = new JMenuItem(STEM);
530 item.addActionListener(this);
535 item = new JMenuItem(HELIX);
536 item.addActionListener(this);
538 item = new JMenuItem(SHEET);
539 item.addActionListener(this);
542 item = new JMenuItem(LABEL);
543 item.addActionListener(this);
545 item = new JMenuItem(COLOUR);
546 item.addActionListener(this);
548 item = new JMenuItem(REMOVE);
549 item.addActionListener(this);
551 pop.show(this, evt.getX(), evt.getY());
561 ap.getScalePanel().mousePressed(evt);
572 public void mouseReleased(MouseEvent evt)
576 mouseDragging = false;
577 ap.getScalePanel().mouseReleased(evt);
587 public void mouseEntered(MouseEvent evt)
589 ap.getScalePanel().mouseEntered(evt);
599 public void mouseExited(MouseEvent evt)
601 ap.getScalePanel().mouseExited(evt);
611 public void mouseDragged(MouseEvent evt)
613 if (graphStretch > -1)
615 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
617 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
619 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
621 graphStretchY = evt.getY();
623 ap.paintAlignment(true);
627 ap.getScalePanel().mouseDragged(evt);
638 public void mouseMoved(MouseEvent evt)
640 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
644 this.setToolTipText(null);
651 for (int i = 0; i < aa.length; i++)
655 height += aa[i].height;
658 if (evt.getY() < height)
668 this.setToolTipText(null);
672 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
674 if (av.hasHiddenColumns())
676 res = av.getColumnSelection().adjustForHiddenColumns(res);
679 if (row > -1 && aa[row].annotations != null
680 && res < aa[row].annotations.length)
682 if (aa[row].graphGroup > -1)
684 StringBuffer tip = new StringBuffer("<html>");
685 for (int gg = 0; gg < aa.length; gg++)
687 if (aa[gg].graphGroup == aa[row].graphGroup
688 && aa[gg].annotations[res] != null)
690 tip.append(aa[gg].label + " "
691 + aa[gg].annotations[res].description + "<br>");
694 if (tip.length() != 6)
696 tip.setLength(tip.length() - 4);
697 this.setToolTipText(tip.toString() + "</html>");
700 else if (aa[row].annotations[res] != null
701 && aa[row].annotations[res].description != null
702 && aa[row].annotations[res].description.length() > 0)
704 this.setToolTipText(JvSwingUtils.wrapTooltip(true,
705 aa[row].annotations[res].description));
709 // clear the tooltip.
710 this.setToolTipText(null);
713 if (aa[row].annotations[res] != null)
715 StringBuffer text = new StringBuffer("Sequence position "
718 if (aa[row].annotations[res].description != null)
720 text.append(" " + aa[row].annotations[res].description);
723 ap.alignFrame.statusBar.setText(text.toString());
728 this.setToolTipText(null);
739 public void mouseClicked(MouseEvent evt)
741 // if (activeRow != -1)
743 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
744 // AlignmentAnnotation anot = aa[activeRow];
748 // TODO mouseClicked-content and drawCursor are quite experimental!
749 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
752 int pady = av.getCharHeight() / 5;
754 graphics.setColor(Color.black);
755 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
757 if (av.validCharWidth)
759 graphics.setColor(Color.white);
761 char s = seq.getCharAt(res);
763 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
764 graphics.drawString(String.valueOf(s), charOffset + x1,
765 (y1 + av.getCharHeight()) - pady);
770 private volatile boolean imageFresh = false;
779 public void paintComponent(Graphics g)
781 g.setColor(Color.white);
782 g.fillRect(0, 0, getWidth(), getHeight());
786 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
787 || (getVisibleRect().height != g.getClipBounds().height))
789 g.drawImage(image, 0, 0, this);
794 imgWidth = (av.endRes - av.startRes + 1) * av.getCharWidth();
799 if (image == null || imgWidth != image.getWidth(this)
800 || image.getHeight(this) != getHeight())
804 image = new BufferedImage(imgWidth, ap.getAnnotationPanel()
805 .getHeight(), BufferedImage.TYPE_INT_RGB);
806 } catch (OutOfMemoryError oom)
811 } catch (Exception x)
816 "Couldn't allocate memory to redraw screen. Please restart Jalview",
820 gg = (Graphics2D) image.getGraphics();
824 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
825 RenderingHints.VALUE_ANTIALIAS_ON);
828 gg.setFont(av.getFont());
829 fm = gg.getFontMetrics();
830 gg.setColor(Color.white);
831 gg.fillRect(0, 0, imgWidth, image.getHeight());
835 drawComponent(gg, av.startRes, av.endRes + 1);
837 g.drawImage(image, 0, 0, this);
841 * set true to enable redraw timing debug output on stderr
843 private final boolean debugRedraw = false;
846 * non-Thread safe repaint
849 * repaint with horizontal shift in alignment
851 public void fastPaint(int horizontal)
853 if ((horizontal == 0) || gg == null
854 || av.getAlignment().getAlignmentAnnotation() == null
855 || av.getAlignment().getAlignmentAnnotation().length < 1
856 || av.isCalcInProgress())
861 long stime = System.currentTimeMillis();
862 gg.copyArea(0, 0, imgWidth, getHeight(),
863 -horizontal * av.getCharWidth(), 0);
864 long mtime = System.currentTimeMillis();
865 int sr = av.startRes;
866 int er = av.endRes + 1;
869 if (horizontal > 0) // scrollbar pulled right, image to the left
871 transX = (er - sr - horizontal) * av.getCharWidth();
872 sr = er - horizontal;
874 else if (horizontal < 0)
876 er = sr - horizontal;
879 gg.translate(transX, 0);
881 drawComponent(gg, sr, er);
883 gg.translate(-transX, 0);
884 long dtime = System.currentTimeMillis();
887 long rtime = System.currentTimeMillis();
890 System.err.println("Scroll:\t" + horizontal + "\tCopyArea:\t"
891 + (mtime - stime) + "\tDraw component:\t" + (dtime - mtime)
892 + "\tRepaint call:\t" + (rtime - dtime));
897 private volatile boolean lastImageGood = false;
909 public void drawComponent(Graphics g, int startRes, int endRes)
911 BufferedImage oldFaded = fadedImage;
912 if (av.isCalcInProgress())
916 lastImageGood = false;
919 // We'll keep a record of the old image,
920 // and draw a faded image until the calculation
923 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
924 .getHeight() != image.getHeight()))
926 // System.err.println("redraw faded image ("+(fadedImage==null ?
927 // "null image" : "") + " lastGood="+lastImageGood+")");
928 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
929 BufferedImage.TYPE_INT_RGB);
931 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
933 fadedG.setColor(Color.white);
934 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
936 fadedG.setComposite(AlphaComposite.getInstance(
937 AlphaComposite.SRC_OVER, .3f));
938 fadedG.drawImage(image, 0, 0, this);
941 // make sure we don't overwrite the last good faded image until all
942 // calculations have finished
943 lastImageGood = false;
948 if (fadedImage != null)
950 oldFaded = fadedImage;
955 g.setColor(Color.white);
956 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(), getHeight());
958 g.setFont(av.getFont());
961 fm = g.getFontMetrics();
964 if ((av.getAlignment().getAlignmentAnnotation() == null)
965 || (av.getAlignment().getAlignmentAnnotation().length < 1))
967 g.setColor(Color.white);
968 g.fillRect(0, 0, getWidth(), getHeight());
969 g.setColor(Color.black);
970 if (av.validCharWidth)
972 g.drawString(MessageManager
973 .getString("label.alignment_has_no_annotations"), 20, 15);
978 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
980 if (!lastImageGood && fadedImage == null)
982 fadedImage = oldFaded;
987 public FontMetrics getFontMetrics()
993 public Image getFadedImage()
999 public int getFadedImageWidth()
1004 private int[] bounds = new int[2];
1007 public int[] getVisibleVRange()
1009 if (ap != null && ap.getAlabels() != null)
1011 int sOffset = -ap.getAlabels().getScrollOffset();
1012 int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
1013 bounds[0] = sOffset;
1014 bounds[1] = visHeight;