2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 import java.awt.event.*;
22 import java.awt.image.*;
25 import jalview.datamodel.*;
26 import jalview.renderer.AnnotationRenderer;
27 import jalview.renderer.AwtRenderPanelI;
30 * AnnotationPanel displays visible portion of annotation rows below unwrapped alignment
34 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
35 MouseListener, MouseMotionListener, ActionListener,
38 final String HELIX = "Helix";
40 final String SHEET = "Sheet";
43 * For RNA secondary structure "stems" aka helices
45 final String STEM = "RNA Helix";
47 final String LABEL = "Label";
49 final String REMOVE = "Remove Annotation";
51 final String COLOUR = "Colour";
53 public final Color HELIX_COLOUR = Color.red.darker();
55 public final Color SHEET_COLOUR = Color.green.darker().darker();
57 public final Color STEM_COLOUR = Color.blue.darker();
60 public AlignViewport av;
64 public int activeRow = -1;
66 public BufferedImage image;
68 public volatile BufferedImage fadedImage;
72 public FontMetrics fm;
74 public int imgWidth = 0;
76 boolean fastPaint = false;
78 // Used For mouse Dragging and resizing graphs
79 int graphStretch = -1;
81 int graphStretchY = -1;
83 int min; // used by mouseDragged to see if user
85 int max; // used by mouseDragged to see if user
87 boolean mouseDragging = false;
96 public final AnnotationRenderer renderer;
99 * Creates a new AnnotationPanel object.
104 public AnnotationPanel(AlignmentPanel ap)
107 MAC = new jalview.util.Platform().isAMac();
109 ToolTipManager.sharedInstance().registerComponent(this);
110 ToolTipManager.sharedInstance().setInitialDelay(0);
111 ToolTipManager.sharedInstance().setDismissDelay(10000);
114 this.setLayout(null);
115 addMouseListener(this);
116 addMouseMotionListener(this);
117 ap.annotationScroller.getVerticalScrollBar()
118 .addAdjustmentListener(this);
119 renderer = new AnnotationRenderer();
122 public AnnotationPanel(AlignViewport av)
125 renderer = new AnnotationRenderer();
135 public void adjustmentValueChanged(AdjustmentEvent evt)
137 ap.alabels.setScrollOffset(-evt.getValue());
141 * Calculates the height of the annotation displayed in the annotation panel.
142 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
143 * all annotation associated components are updated correctly.
146 public int adjustPanelHeight()
148 int height = av.calcPanelHeight();
149 this.setPreferredSize(new Dimension(1, height));
152 // revalidate only when the alignment panel is fully constructed
166 public void actionPerformed(ActionEvent evt)
168 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
173 Annotation[] anot = aa[activeRow].annotations;
175 if (anot.length < av.getColumnSelection().getMax())
177 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
178 System.arraycopy(anot, 0, temp, 0, anot.length);
180 aa[activeRow].annotations = anot;
183 if (evt.getActionCommand().equals(REMOVE))
185 for (int i = 0; i < av.getColumnSelection().size(); i++)
187 anot[av.getColumnSelection().columnAt(i)] = null;
190 else if (evt.getActionCommand().equals(LABEL))
192 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
193 String label = JOptionPane.showInputDialog(this, "Enter label",
201 if ((label.length() > 0) && !aa[activeRow].hasText)
203 aa[activeRow].hasText = true;
206 for (int i = 0; i < av.getColumnSelection().size(); i++)
208 int index = av.getColumnSelection().columnAt(i);
210 if (!av.getColumnSelection().isVisible(index))
213 if (anot[index] == null)
215 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
222 anot[index].displayCharacter = label;
226 else if (evt.getActionCommand().equals(COLOUR))
228 Color col = JColorChooser.showDialog(this,
229 "Choose foreground colour", Color.black);
231 for (int i = 0; i < av.getColumnSelection().size(); i++)
233 int index = av.getColumnSelection().columnAt(i);
235 if (!av.getColumnSelection().isVisible(index))
238 if (anot[index] == null)
240 anot[index] = new Annotation("", "", ' ', 0);
243 anot[index].colour = col;
250 String symbol = "\u03B1";
252 if (evt.getActionCommand().equals(HELIX))
256 else if (evt.getActionCommand().equals(SHEET))
262 // Added by LML to color stems
263 else if (evt.getActionCommand().equals(STEM))
269 if (!aa[activeRow].hasIcons)
271 aa[activeRow].hasIcons = true;
274 String label = JOptionPane.showInputDialog(
275 "Enter a label for the structure?", symbol);
282 if ((label.length() > 0) && !aa[activeRow].hasText)
284 aa[activeRow].hasText = true;
287 for (int i = 0; i < av.getColumnSelection().size(); i++)
289 int index = av.getColumnSelection().columnAt(i);
291 if (!av.getColumnSelection().isVisible(index))
294 if (anot[index] == null)
296 anot[index] = new Annotation(label, "", type, 0);
299 anot[index].secondaryStructure = type;
300 anot[index].displayCharacter = label;
303 aa[activeRow].validateRangeAndDisplay();
306 ap.alignmentChanged();
312 private String collectAnnotVals(Annotation[] anot,
313 ColumnSelection columnSelection, String label2)
315 String collatedInput = "";
317 ColumnSelection viscols=av.getColumnSelection();
318 // TODO: refactor and save av.getColumnSelection for efficiency
319 for (int i = 0; i < columnSelection.size(); i++)
321 int index = columnSelection.columnAt(i);
322 // always check for current display state - just in case
323 if (!viscols.isVisible(index))
325 String tlabel = null;
326 if (anot[index] != null)
327 { // LML added stem code
328 if (label2.equals(HELIX) || label2.equals(SHEET)
329 || label2.equals(STEM) || label2.equals(LABEL))
331 tlabel = anot[index].description;
332 if (tlabel == null || tlabel.length() < 1)
334 if (label2.equals(HELIX) || label2.equals(SHEET)
335 || label2.equals(STEM))
337 tlabel = "" + anot[index].secondaryStructure;
341 tlabel = "" + anot[index].displayCharacter;
345 if (tlabel != null && !tlabel.equals(last))
347 if (last.length() > 0)
349 collatedInput += " ";
351 collatedInput += tlabel;
355 return collatedInput;
365 public void mousePressed(MouseEvent evt)
368 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
377 for (int i = 0; i < aa.length; i++)
381 height += aa[i].height;
384 if (evt.getY() < height)
390 else if (aa[i].graph > 0)
394 graphStretchY = evt.getY();
401 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
403 if (av.getColumnSelection() == null)
408 JPopupMenu pop = new JPopupMenu("Structure type");
411 * Just display the needed structure options
413 if (av.getAlignment().isNucleotide() == true)
415 item = new JMenuItem(STEM);
416 item.addActionListener(this);
421 item = new JMenuItem(HELIX);
422 item.addActionListener(this);
424 item = new JMenuItem(SHEET);
425 item.addActionListener(this);
428 item = new JMenuItem(LABEL);
429 item.addActionListener(this);
431 item = new JMenuItem(COLOUR);
432 item.addActionListener(this);
434 item = new JMenuItem(REMOVE);
435 item.addActionListener(this);
437 pop.show(this, evt.getX(), evt.getY());
447 ap.scalePanel.mousePressed(evt);
458 public void mouseReleased(MouseEvent evt)
462 mouseDragging = false;
463 ap.scalePanel.mouseReleased(evt);
473 public void mouseEntered(MouseEvent evt)
475 ap.scalePanel.mouseEntered(evt);
485 public void mouseExited(MouseEvent evt)
487 ap.scalePanel.mouseExited(evt);
497 public void mouseDragged(MouseEvent evt)
499 if (graphStretch > -1)
501 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
503 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
505 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
507 graphStretchY = evt.getY();
509 ap.paintAlignment(true);
513 ap.scalePanel.mouseDragged(evt);
524 public void mouseMoved(MouseEvent evt)
526 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
530 this.setToolTipText(null);
537 for (int i = 0; i < aa.length; i++)
541 height += aa[i].height;
544 if (evt.getY() < height)
554 this.setToolTipText(null);
558 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
560 if (av.hasHiddenColumns())
562 res = av.getColumnSelection().adjustForHiddenColumns(res);
565 if (row > -1 && aa[row].annotations != null
566 && res < aa[row].annotations.length)
568 if (aa[row].graphGroup > -1)
570 StringBuffer tip = new StringBuffer("<html>");
571 for (int gg = 0; gg < aa.length; gg++)
573 if (aa[gg].graphGroup == aa[row].graphGroup
574 && aa[gg].annotations[res] != null)
576 tip.append(aa[gg].label + " "
577 + aa[gg].annotations[res].description + "<br>");
580 if (tip.length() != 6)
582 tip.setLength(tip.length() - 4);
583 this.setToolTipText(tip.toString() + "</html>");
586 else if (aa[row].annotations[res] != null
587 && aa[row].annotations[res].description != null
588 && aa[row].annotations[res].description.length() > 0)
590 this.setToolTipText(aa[row].annotations[res].description);
594 // clear the tooltip.
595 this.setToolTipText(null);
598 if (aa[row].annotations[res] != null)
600 StringBuffer text = new StringBuffer("Sequence position "
603 if (aa[row].annotations[res].description != null)
605 text.append(" " + aa[row].annotations[res].description);
608 ap.alignFrame.statusBar.setText(text.toString());
613 this.setToolTipText(null);
624 public void mouseClicked(MouseEvent evt)
628 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
629 AlignmentAnnotation anot = aa[activeRow];
631 if (anot.description.equals("secondary structure"))
633 // System.out.println(anot.description+" "+anot.getRNAStruc());
638 // TODO mouseClicked-content and drawCursor are quite experimental!
639 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
642 int pady = av.charHeight / 5;
644 graphics.setColor(Color.black);
645 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
647 if (av.validCharWidth)
649 graphics.setColor(Color.white);
651 char s = seq.getCharAt(res);
653 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
654 graphics.drawString(String.valueOf(s), charOffset + x1,
655 (y1 + av.charHeight) - pady);
659 private volatile boolean imageFresh=false;
667 public void paintComponent(Graphics g)
669 g.setColor(Color.white);
670 g.fillRect(0, 0, getWidth(), getHeight());
674 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
675 || (getVisibleRect().height != g.getClipBounds().height))
677 g.drawImage(image, 0, 0, this);
682 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
685 if (image == null || imgWidth != image.getWidth()
686 || image.getHeight(this) != getHeight())
688 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
689 BufferedImage.TYPE_INT_RGB);
690 gg = (Graphics2D) image.getGraphics();
694 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
695 RenderingHints.VALUE_ANTIALIAS_ON);
698 gg.setFont(av.getFont());
699 fm = gg.getFontMetrics();
700 gg.setColor(Color.white);
701 gg.fillRect(0, 0, imgWidth, image.getHeight());
705 drawComponent(gg, av.startRes, av.endRes + 1);
707 g.drawImage(image, 0, 0, this);
711 * non-Thread safe repaint
714 * repaint with horizontal shift in alignment
716 public void fastPaint(int horizontal)
719 if ((horizontal == 0) || gg == null
720 || av.getAlignment().getAlignmentAnnotation() == null
721 || av.getAlignment().getAlignmentAnnotation().length < 1
722 || av.isCalcInProgress())
727 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
729 int sr = av.startRes;
730 int er = av.endRes + 1;
733 if (horizontal > 0) // scrollbar pulled right, image to the left
735 transX = (er - sr - horizontal) * av.charWidth;
736 sr = er - horizontal;
738 else if (horizontal < 0)
740 er = sr - horizontal;
743 gg.translate(transX, 0);
745 drawComponent(gg, sr, er);
747 gg.translate(-transX, 0);
753 private volatile boolean lastImageGood=false;
764 public void drawComponent(Graphics g, int startRes, int endRes)
766 BufferedImage oldFaded=fadedImage;
767 if (av.isCalcInProgress())
774 // We'll keep a record of the old image,
775 // and draw a faded image until the calculation
777 if (lastImageGood && (fadedImage == null || fadedImage.getWidth() != imgWidth
778 || fadedImage.getHeight() != image.getHeight()))
780 // System.err.println("redraw faded image ("+(fadedImage==null ? "null image" : "") + " lastGood="+lastImageGood+")");
781 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
782 BufferedImage.TYPE_INT_RGB);
784 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
786 fadedG.setColor(Color.white);
787 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
789 fadedG.setComposite(AlphaComposite.getInstance(
790 AlphaComposite.SRC_OVER, .3f));
791 fadedG.drawImage(image, 0, 0, this);
794 // make sure we don't overwrite the last good faded image until all calculations have finished
800 if (fadedImage!=null)
807 g.setColor(Color.white);
808 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
810 g.setFont(av.getFont());
813 fm = g.getFontMetrics();
816 if ((av.getAlignment().getAlignmentAnnotation() == null)
817 || (av.getAlignment().getAlignmentAnnotation().length < 1))
819 g.setColor(Color.white);
820 g.fillRect(0, 0, getWidth(), getHeight());
821 g.setColor(Color.black);
822 if (av.validCharWidth)
824 g.drawString("Alignment has no annotations", 20, 15);
829 lastImageGood = renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
830 if (!lastImageGood && fadedImage==null)
837 public FontMetrics getFontMetrics()
843 public Image getFadedImage()
849 public int getFadedImageWidth()