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.SwingUtilities;
58 import javax.swing.ToolTipManager;
61 * AnnotationPanel displays visible portion of annotation rows below unwrapped
67 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
68 MouseListener, MouseWheelListener, MouseMotionListener,
69 ActionListener, AdjustmentListener, Scrollable
71 String HELIX = MessageManager.getString("label.helix");
73 String SHEET = MessageManager.getString("label.sheet");
76 * For RNA secondary structure "stems" aka helices
78 String STEM = MessageManager.getString("label.rna_helix");
80 String LABEL = MessageManager.getString("label.label");
82 String REMOVE = MessageManager.getString("label.remove_annotation");
84 String COLOUR = MessageManager.getString("action.colour");
86 public final Color HELIX_COLOUR = Color.red.darker();
88 public final Color SHEET_COLOUR = Color.green.darker().darker();
90 public final Color STEM_COLOUR = Color.blue.darker();
93 public AlignViewport av;
97 public int activeRow = -1;
99 public BufferedImage image;
101 public volatile BufferedImage fadedImage;
105 public FontMetrics fm;
107 public int imgWidth = 0;
109 boolean fastPaint = false;
111 // Used For mouse Dragging and resizing graphs
112 int graphStretch = -1;
114 int graphStretchY = -1;
116 int min; // used by mouseDragged to see if user
118 int max; // used by mouseDragged to see if user
120 boolean mouseDragging = false;
124 // for editing cursor
129 public final AnnotationRenderer renderer;
131 private MouseWheelListener[] _mwl;
134 * Creates a new AnnotationPanel object.
139 public AnnotationPanel(AlignmentPanel ap)
142 MAC = jalview.util.Platform.isAMac();
144 ToolTipManager.sharedInstance().registerComponent(this);
145 ToolTipManager.sharedInstance().setInitialDelay(0);
146 ToolTipManager.sharedInstance().setDismissDelay(10000);
149 this.setLayout(null);
150 addMouseListener(this);
151 addMouseMotionListener(this);
152 ap.annotationScroller.getVerticalScrollBar()
153 .addAdjustmentListener(this);
154 // save any wheel listeners on the scroller, so we can propagate scroll
156 _mwl = ap.annotationScroller.getMouseWheelListeners();
157 // and then set our own listener to consume all mousewheel events
158 ap.annotationScroller.addMouseWheelListener(this);
159 renderer = new AnnotationRenderer();
162 public AnnotationPanel(AlignViewport av)
165 renderer = new AnnotationRenderer();
169 public void mouseWheelMoved(MouseWheelEvent e)
174 if (e.getWheelRotation() > 0)
176 ap.scrollRight(true);
180 ap.scrollRight(false);
185 // TODO: find the correct way to let the event bubble up to
186 // ap.annotationScroller
187 for (MouseWheelListener mwl : _mwl)
191 mwl.mouseWheelMoved(e);
202 public Dimension getPreferredScrollableViewportSize()
204 return getPreferredSize();
208 public int getScrollableBlockIncrement(Rectangle visibleRect,
209 int orientation, int direction)
215 public boolean getScrollableTracksViewportHeight()
221 public boolean getScrollableTracksViewportWidth()
227 public int getScrollableUnitIncrement(Rectangle visibleRect,
228 int orientation, int direction)
237 * java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event
241 public void adjustmentValueChanged(AdjustmentEvent evt)
243 // update annotation label display
244 ap.getAlabels().setScrollOffset(-evt.getValue());
248 * Calculates the height of the annotation displayed in the annotation panel.
249 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
250 * all annotation associated components are updated correctly.
253 public int adjustPanelHeight()
255 int height = av.calcPanelHeight();
256 this.setPreferredSize(new Dimension(1, height));
259 // revalidate only when the alignment panel is fully constructed
273 public void actionPerformed(ActionEvent evt)
275 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
280 Annotation[] anot = aa[activeRow].annotations;
282 if (anot.length < av.getColumnSelection().getMax())
284 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
285 System.arraycopy(anot, 0, temp, 0, anot.length);
287 aa[activeRow].annotations = anot;
290 if (evt.getActionCommand().equals(REMOVE))
292 for (int sel : av.getColumnSelection().getSelected())
297 else if (evt.getActionCommand().equals(LABEL))
299 String exMesg = collectAnnotVals(anot, LABEL);
300 String label = JOptionPane.showInputDialog(this,
301 MessageManager.getString("label.enter_label"), exMesg);
308 if ((label.length() > 0) && !aa[activeRow].hasText)
310 aa[activeRow].hasText = true;
313 for (int index : av.getColumnSelection().getSelected())
315 if (!av.getColumnSelection().isVisible(index))
320 if (anot[index] == null)
322 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
329 anot[index].displayCharacter = label;
333 else if (evt.getActionCommand().equals(COLOUR))
335 Color col = JColorChooser.showDialog(this,
336 MessageManager.getString("label.select_foreground_colour"),
339 for (int index : av.getColumnSelection().getSelected())
341 if (!av.getColumnSelection().isVisible(index))
346 if (anot[index] == null)
348 anot[index] = new Annotation("", "", ' ', 0);
351 anot[index].colour = col;
358 String symbol = "\u03B1";
360 if (evt.getActionCommand().equals(HELIX))
364 else if (evt.getActionCommand().equals(SHEET))
370 // Added by LML to color stems
371 else if (evt.getActionCommand().equals(STEM))
377 if (!aa[activeRow].hasIcons)
379 aa[activeRow].hasIcons = true;
382 String label = JOptionPane.showInputDialog(MessageManager
383 .getString("label.enter_label_for_the_structure"), symbol);
390 if ((label.length() > 0) && !aa[activeRow].hasText)
392 aa[activeRow].hasText = true;
393 if (evt.getActionCommand().equals(STEM))
395 aa[activeRow].showAllColLabels = true;
398 for (int index : av.getColumnSelection().getSelected())
400 if (!av.getColumnSelection().isVisible(index))
405 if (anot[index] == null)
407 anot[index] = new Annotation(label, "", type, 0);
410 anot[index].secondaryStructure = type != 'S' ? type : label
411 .length() == 0 ? ' ' : label.charAt(0);
412 anot[index].displayCharacter = label;
416 av.getAlignment().validateAnnotation(aa[activeRow]);
417 ap.alignmentChanged();
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 (SwingUtilities.isRightMouseButton(evt) && 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;