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 i = 0; i < av.getColumnSelection().size(); i++)
294 anot[av.getColumnSelection().columnAt(i)] = null;
297 else if (evt.getActionCommand().equals(LABEL))
299 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), 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 i = 0; i < av.getColumnSelection().size(); i++)
315 int index = av.getColumnSelection().columnAt(i);
317 if (!av.getColumnSelection().isVisible(index))
322 if (anot[index] == null)
324 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
331 anot[index].displayCharacter = label;
335 else if (evt.getActionCommand().equals(COLOUR))
337 Color col = JColorChooser.showDialog(this,
338 MessageManager.getString("label.select_foreground_colour"), Color.black);
340 for (int i = 0; i < av.getColumnSelection().size(); i++)
342 int index = av.getColumnSelection().columnAt(i);
344 if (!av.getColumnSelection().isVisible(index))
349 if (anot[index] == null)
351 anot[index] = new Annotation("", "", ' ', 0);
354 anot[index].colour = col;
361 String symbol = "\u03B1";
363 if (evt.getActionCommand().equals(HELIX))
367 else if (evt.getActionCommand().equals(SHEET))
373 // Added by LML to color stems
374 else if (evt.getActionCommand().equals(STEM))
380 if (!aa[activeRow].hasIcons)
382 aa[activeRow].hasIcons = true;
385 String label = JOptionPane.showInputDialog(MessageManager
386 .getString("label.enter_label_for_the_structure"), symbol);
393 if ((label.length() > 0) && !aa[activeRow].hasText)
395 aa[activeRow].hasText = true;
396 if (evt.getActionCommand().equals(STEM))
398 aa[activeRow].showAllColLabels = true;
401 for (int i = 0; i < av.getColumnSelection().size(); i++)
403 int index = av.getColumnSelection().columnAt(i);
405 if (!av.getColumnSelection().isVisible(index))
410 if (anot[index] == null)
412 anot[index] = new Annotation(label, "", type, 0);
416 anot[index].secondaryStructure = type != 'S' ? type : label
417 .length() == 0 ? ' ' : label.charAt(0);
418 anot[index].displayCharacter = label;
422 av.getAlignment().validateAnnotation(aa[activeRow]);
423 ap.alignmentChanged();
431 private String collectAnnotVals(Annotation[] anot,
432 ColumnSelection columnSelection, String label2)
434 String collatedInput = "";
436 ColumnSelection viscols = av.getColumnSelection();
437 // TODO: refactor and save av.getColumnSelection for efficiency
438 for (int i = 0; i < columnSelection.size(); i++)
440 int index = columnSelection.columnAt(i);
441 // always check for current display state - just in case
442 if (!viscols.isVisible(index))
446 String tlabel = null;
447 if (anot[index] != null)
448 { // LML added stem code
449 if (label2.equals(HELIX) || label2.equals(SHEET)
450 || label2.equals(STEM) || label2.equals(LABEL))
452 tlabel = anot[index].description;
453 if (tlabel == null || tlabel.length() < 1)
455 if (label2.equals(HELIX) || label2.equals(SHEET)
456 || label2.equals(STEM))
458 tlabel = "" + anot[index].secondaryStructure;
462 tlabel = "" + anot[index].displayCharacter;
466 if (tlabel != null && !tlabel.equals(last))
468 if (last.length() > 0)
470 collatedInput += " ";
472 collatedInput += tlabel;
476 return collatedInput;
486 public void mousePressed(MouseEvent evt)
489 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
498 for (int i = 0; i < aa.length; i++)
502 height += aa[i].height;
505 if (evt.getY() < height)
511 else if (aa[i].graph > 0)
515 graphStretchY = evt.getY();
522 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
524 if (av.getColumnSelection() == null)
529 JPopupMenu pop = new JPopupMenu(
530 MessageManager.getString("label.structure_type"));
533 * Just display the needed structure options
535 if (av.getAlignment().isNucleotide() == true)
537 item = new JMenuItem(STEM);
538 item.addActionListener(this);
543 item = new JMenuItem(HELIX);
544 item.addActionListener(this);
546 item = new JMenuItem(SHEET);
547 item.addActionListener(this);
550 item = new JMenuItem(LABEL);
551 item.addActionListener(this);
553 item = new JMenuItem(COLOUR);
554 item.addActionListener(this);
556 item = new JMenuItem(REMOVE);
557 item.addActionListener(this);
559 pop.show(this, evt.getX(), evt.getY());
569 ap.getScalePanel().mousePressed(evt);
580 public void mouseReleased(MouseEvent evt)
584 mouseDragging = false;
585 ap.getScalePanel().mouseReleased(evt);
595 public void mouseEntered(MouseEvent evt)
597 ap.getScalePanel().mouseEntered(evt);
607 public void mouseExited(MouseEvent evt)
609 ap.getScalePanel().mouseExited(evt);
619 public void mouseDragged(MouseEvent evt)
621 if (graphStretch > -1)
623 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
625 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
627 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
629 graphStretchY = evt.getY();
631 ap.paintAlignment(true);
635 ap.getScalePanel().mouseDragged(evt);
646 public void mouseMoved(MouseEvent evt)
648 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
652 this.setToolTipText(null);
659 for (int i = 0; i < aa.length; i++)
663 height += aa[i].height;
666 if (evt.getY() < height)
676 this.setToolTipText(null);
680 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
682 if (av.hasHiddenColumns())
684 res = av.getColumnSelection().adjustForHiddenColumns(res);
687 if (row > -1 && aa[row].annotations != null
688 && res < aa[row].annotations.length)
690 if (aa[row].graphGroup > -1)
692 StringBuffer tip = new StringBuffer("<html>");
693 for (int gg = 0; gg < aa.length; gg++)
695 if (aa[gg].graphGroup == aa[row].graphGroup
696 && aa[gg].annotations[res] != null)
698 tip.append(aa[gg].label + " "
699 + aa[gg].annotations[res].description + "<br>");
702 if (tip.length() != 6)
704 tip.setLength(tip.length() - 4);
705 this.setToolTipText(tip.toString() + "</html>");
708 else if (aa[row].annotations[res] != null
709 && aa[row].annotations[res].description != null
710 && aa[row].annotations[res].description.length() > 0)
712 this.setToolTipText(JvSwingUtils
713 .wrapTooltip(true, aa[row].annotations[res].description));
717 // clear the tooltip.
718 this.setToolTipText(null);
721 if (aa[row].annotations[res] != null)
723 StringBuffer text = new StringBuffer("Sequence position "
726 if (aa[row].annotations[res].description != null)
728 text.append(" " + aa[row].annotations[res].description);
731 ap.alignFrame.statusBar.setText(text.toString());
736 this.setToolTipText(null);
747 public void mouseClicked(MouseEvent evt)
749 // if (activeRow != -1)
751 // AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
752 // AlignmentAnnotation anot = aa[activeRow];
756 // TODO mouseClicked-content and drawCursor are quite experimental!
757 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
760 int pady = av.getCharHeight() / 5;
762 graphics.setColor(Color.black);
763 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
765 if (av.validCharWidth)
767 graphics.setColor(Color.white);
769 char s = seq.getCharAt(res);
771 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
772 graphics.drawString(String.valueOf(s), charOffset + x1,
773 (y1 + av.getCharHeight()) - pady);
778 private volatile boolean imageFresh = false;
787 public void paintComponent(Graphics g)
789 g.setColor(Color.white);
790 g.fillRect(0, 0, getWidth(), getHeight());
794 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
795 || (getVisibleRect().height != g.getClipBounds().height))
797 g.drawImage(image, 0, 0, this);
802 imgWidth = (av.endRes - av.startRes + 1) * av.getCharWidth();
807 if (image == null || imgWidth != image.getWidth(this)
808 || image.getHeight(this) != getHeight())
812 image = new BufferedImage(imgWidth, ap.getAnnotationPanel().getHeight(),
813 BufferedImage.TYPE_INT_RGB);
814 } catch (OutOfMemoryError oom)
819 } catch (Exception x)
824 "Couldn't allocate memory to redraw screen. Please restart Jalview",
828 gg = (Graphics2D) image.getGraphics();
832 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
833 RenderingHints.VALUE_ANTIALIAS_ON);
836 gg.setFont(av.getFont());
837 fm = gg.getFontMetrics();
838 gg.setColor(Color.white);
839 gg.fillRect(0, 0, imgWidth, image.getHeight());
843 drawComponent(gg, av.startRes, av.endRes + 1);
845 g.drawImage(image, 0, 0, this);
849 * set true to enable redraw timing debug output on stderr
851 private final boolean debugRedraw = false;
854 * non-Thread safe repaint
857 * repaint with horizontal shift in alignment
859 public void fastPaint(int horizontal)
861 if ((horizontal == 0) || gg == null
862 || av.getAlignment().getAlignmentAnnotation() == null
863 || av.getAlignment().getAlignmentAnnotation().length < 1
864 || av.isCalcInProgress())
869 long stime = System.currentTimeMillis();
870 gg.copyArea(0, 0, imgWidth, getHeight(),
871 -horizontal * av.getCharWidth(), 0);
872 long mtime = System.currentTimeMillis();
873 int sr = av.startRes;
874 int er = av.endRes + 1;
877 if (horizontal > 0) // scrollbar pulled right, image to the left
879 transX = (er - sr - horizontal) * av.getCharWidth();
880 sr = er - horizontal;
882 else if (horizontal < 0)
884 er = sr - horizontal;
887 gg.translate(transX, 0);
889 drawComponent(gg, sr, er);
891 gg.translate(-transX, 0);
892 long dtime = System.currentTimeMillis();
895 long rtime = System.currentTimeMillis();
898 System.err.println("Scroll:\t" + horizontal + "\tCopyArea:\t"
899 + (mtime - stime) + "\tDraw component:\t" + (dtime - mtime)
900 + "\tRepaint call:\t" + (rtime - dtime));
905 private volatile boolean lastImageGood = false;
917 public void drawComponent(Graphics g, int startRes, int endRes)
919 BufferedImage oldFaded = fadedImage;
920 if (av.isCalcInProgress())
924 lastImageGood = false;
927 // We'll keep a record of the old image,
928 // and draw a faded image until the calculation
931 && (fadedImage == null || fadedImage.getWidth() != imgWidth || fadedImage
932 .getHeight() != image.getHeight()))
934 // System.err.println("redraw faded image ("+(fadedImage==null ?
935 // "null image" : "") + " lastGood="+lastImageGood+")");
936 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
937 BufferedImage.TYPE_INT_RGB);
939 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
941 fadedG.setColor(Color.white);
942 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
944 fadedG.setComposite(AlphaComposite.getInstance(
945 AlphaComposite.SRC_OVER, .3f));
946 fadedG.drawImage(image, 0, 0, this);
949 // make sure we don't overwrite the last good faded image until all
950 // calculations have finished
951 lastImageGood = false;
956 if (fadedImage != null)
958 oldFaded = fadedImage;
963 g.setColor(Color.white);
964 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(), getHeight());
966 g.setFont(av.getFont());
969 fm = g.getFontMetrics();
972 if ((av.getAlignment().getAlignmentAnnotation() == null)
973 || (av.getAlignment().getAlignmentAnnotation().length < 1))
975 g.setColor(Color.white);
976 g.fillRect(0, 0, getWidth(), getHeight());
977 g.setColor(Color.black);
978 if (av.validCharWidth)
980 g.drawString(MessageManager
981 .getString("label.alignment_has_no_annotations"), 20, 15);
986 lastImageGood = renderer.drawComponent(this, av, g, activeRow,
988 if (!lastImageGood && fadedImage == null)
990 fadedImage = oldFaded;
995 public FontMetrics getFontMetrics()
1001 public Image getFadedImage()
1007 public int getFadedImageWidth()
1012 private int[] bounds = new int[2];
1015 public int[] getVisibleVRange()
1017 if (ap != null && ap.getAlabels() != null)
1019 int sOffset = -ap.getAlabels().getScrollOffset();
1020 int visHeight = sOffset + ap.annotationSpaceFillerHolder.getHeight();
1021 bounds[0] = sOffset;
1022 bounds[1] = visHeight;