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;
35 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
36 MouseListener, MouseMotionListener, ActionListener,
39 final String HELIX = "Helix";
41 final String SHEET = "Sheet";
44 * For RNA secondary structure "stems" aka helices
46 final String STEM = "RNA Helix";
48 final String LABEL = "Label";
50 final String REMOVE = "Remove Annotation";
52 final String COLOUR = "Colour";
54 public final Color HELIX_COLOUR = Color.red.darker();
56 public final Color SHEET_COLOUR = Color.green.darker().darker();
58 public final Color STEM_COLOUR = Color.blue.darker();
61 public AlignViewport av;
65 public int activeRow = -1;
67 public BufferedImage image;
69 public BufferedImage fadedImage;
73 public FontMetrics fm;
75 public int imgWidth = 0;
77 boolean fastPaint = false;
79 // Used For mouse Dragging and resizing graphs
80 int graphStretch = -1;
82 int graphStretchY = -1;
84 int min; // used by mouseDragged to see if user
86 int max; // used by mouseDragged to see if user
88 boolean mouseDragging = false;
97 public final AnnotationRenderer renderer;
100 * Creates a new AnnotationPanel object.
105 public AnnotationPanel(AlignmentPanel ap)
108 MAC = new jalview.util.Platform().isAMac();
110 ToolTipManager.sharedInstance().registerComponent(this);
111 ToolTipManager.sharedInstance().setInitialDelay(0);
112 ToolTipManager.sharedInstance().setDismissDelay(10000);
115 this.setLayout(null);
116 addMouseListener(this);
117 addMouseMotionListener(this);
118 ap.annotationScroller.getVerticalScrollBar()
119 .addAdjustmentListener(this);
120 renderer = new AnnotationRenderer();
123 public AnnotationPanel(AlignViewport av)
126 renderer = new AnnotationRenderer();
136 public void adjustmentValueChanged(AdjustmentEvent evt)
138 ap.alabels.setScrollOffset(-evt.getValue());
142 * Calculates the height of the annotation displayed in the annotation panel.
143 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
144 * all annotation associated components are updated correctly.
147 public int adjustPanelHeight()
149 int height = calcPanelHeight();
150 this.setPreferredSize(new Dimension(1, height));
153 // revalidate only when the alignment panel is fully constructed
161 * calculate the height for visible annotation, revalidating bounds where
162 * necessary ABSTRACT GUI METHOD
164 * @return total height of annotation
166 public int calcPanelHeight()
168 // setHeight of panels
169 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
174 for (int i = 0; i < aa.length; i++)
178 System.err.println("Null annotation row: ignoring.");
190 aa[i].height += av.charHeight;
200 aa[i].height += aa[i].graphHeight;
203 if (aa[i].height == 0)
208 height += aa[i].height;
226 public void actionPerformed(ActionEvent evt)
228 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
233 Annotation[] anot = aa[activeRow].annotations;
235 if (anot.length < av.getColumnSelection().getMax())
237 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
238 System.arraycopy(anot, 0, temp, 0, anot.length);
240 aa[activeRow].annotations = anot;
243 if (evt.getActionCommand().equals(REMOVE))
245 for (int i = 0; i < av.getColumnSelection().size(); i++)
247 anot[av.getColumnSelection().columnAt(i)] = null;
250 else if (evt.getActionCommand().equals(LABEL))
252 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
253 String label = JOptionPane.showInputDialog(this, "Enter label",
261 if ((label.length() > 0) && !aa[activeRow].hasText)
263 aa[activeRow].hasText = true;
266 for (int i = 0; i < av.getColumnSelection().size(); i++)
268 int index = av.getColumnSelection().columnAt(i);
270 if (!av.getColumnSelection().isVisible(index))
273 if (anot[index] == null)
275 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
282 anot[index].displayCharacter = label;
286 else if (evt.getActionCommand().equals(COLOUR))
288 Color col = JColorChooser.showDialog(this,
289 "Choose foreground colour", Color.black);
291 for (int i = 0; i < av.getColumnSelection().size(); i++)
293 int index = av.getColumnSelection().columnAt(i);
295 if (!av.getColumnSelection().isVisible(index))
298 if (anot[index] == null)
300 anot[index] = new Annotation("", "", ' ', 0);
303 anot[index].colour = col;
310 String symbol = "\u03B1";
312 if (evt.getActionCommand().equals(HELIX))
316 else if (evt.getActionCommand().equals(SHEET))
322 // Added by LML to color stems
323 else if (evt.getActionCommand().equals(STEM))
329 if (!aa[activeRow].hasIcons)
331 aa[activeRow].hasIcons = true;
334 String label = JOptionPane.showInputDialog(
335 "Enter a label for the structure?", symbol);
342 if ((label.length() > 0) && !aa[activeRow].hasText)
344 aa[activeRow].hasText = true;
347 for (int i = 0; i < av.getColumnSelection().size(); i++)
349 int index = av.getColumnSelection().columnAt(i);
351 if (!av.getColumnSelection().isVisible(index))
354 if (anot[index] == null)
356 anot[index] = new Annotation(label, "", type, 0);
359 anot[index].secondaryStructure = type;
360 anot[index].displayCharacter = label;
363 aa[activeRow].validateRangeAndDisplay();
371 private String collectAnnotVals(Annotation[] anot,
372 ColumnSelection columnSelection, String label2)
374 String collatedInput = "";
376 ColumnSelection viscols=av.getColumnSelection();
377 // TODO: refactor and save av.getColumnSelection for efficiency
378 for (int i = 0; i < columnSelection.size(); i++)
380 int index = columnSelection.columnAt(i);
381 // always check for current display state - just in case
382 if (!viscols.isVisible(index))
384 String tlabel = null;
385 if (anot[index] != null)
386 { // LML added stem code
387 if (label2.equals(HELIX) || label2.equals(SHEET)
388 || label2.equals(STEM) || label2.equals(LABEL))
390 tlabel = anot[index].description;
391 if (tlabel == null || tlabel.length() < 1)
393 if (label2.equals(HELIX) || label2.equals(SHEET)
394 || label2.equals(STEM))
396 tlabel = "" + anot[index].secondaryStructure;
400 tlabel = "" + anot[index].displayCharacter;
404 if (tlabel != null && !tlabel.equals(last))
406 if (last.length() > 0)
408 collatedInput += " ";
410 collatedInput += tlabel;
414 return collatedInput;
424 public void mousePressed(MouseEvent evt)
427 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
436 for (int i = 0; i < aa.length; i++)
440 height += aa[i].height;
443 if (evt.getY() < height)
449 else if (aa[i].graph > 0)
453 graphStretchY = evt.getY();
460 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
462 if (av.getColumnSelection() == null)
467 JPopupMenu pop = new JPopupMenu("Structure type");
470 * Just display the needed structure options
472 if (av.getAlignment().isNucleotide() == true)
474 item = new JMenuItem(STEM);
475 item.addActionListener(this);
480 item = new JMenuItem(HELIX);
481 item.addActionListener(this);
483 item = new JMenuItem(SHEET);
484 item.addActionListener(this);
487 item = new JMenuItem(LABEL);
488 item.addActionListener(this);
490 item = new JMenuItem(COLOUR);
491 item.addActionListener(this);
493 item = new JMenuItem(REMOVE);
494 item.addActionListener(this);
496 pop.show(this, evt.getX(), evt.getY());
506 ap.scalePanel.mousePressed(evt);
517 public void mouseReleased(MouseEvent evt)
521 mouseDragging = false;
522 ap.scalePanel.mouseReleased(evt);
532 public void mouseEntered(MouseEvent evt)
534 ap.scalePanel.mouseEntered(evt);
544 public void mouseExited(MouseEvent evt)
546 ap.scalePanel.mouseExited(evt);
556 public void mouseDragged(MouseEvent evt)
558 if (graphStretch > -1)
560 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
562 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
564 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
566 graphStretchY = evt.getY();
568 ap.paintAlignment(true);
572 ap.scalePanel.mouseDragged(evt);
583 public void mouseMoved(MouseEvent evt)
585 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
589 this.setToolTipText(null);
596 for (int i = 0; i < aa.length; i++)
600 height += aa[i].height;
603 if (evt.getY() < height)
613 this.setToolTipText(null);
617 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
619 if (av.hasHiddenColumns())
621 res = av.getColumnSelection().adjustForHiddenColumns(res);
624 if (row > -1 && aa[row].annotations != null
625 && res < aa[row].annotations.length)
627 if (aa[row].graphGroup > -1)
629 StringBuffer tip = new StringBuffer("<html>");
630 for (int gg = 0; gg < aa.length; gg++)
632 if (aa[gg].graphGroup == aa[row].graphGroup
633 && aa[gg].annotations[res] != null)
635 tip.append(aa[gg].label + " "
636 + aa[gg].annotations[res].description + "<br>");
639 if (tip.length() != 6)
641 tip.setLength(tip.length() - 4);
642 this.setToolTipText(tip.toString() + "</html>");
645 else if (aa[row].annotations[res] != null
646 && aa[row].annotations[res].description != null
647 && aa[row].annotations[res].description.length() > 0)
649 this.setToolTipText(aa[row].annotations[res].description);
653 // clear the tooltip.
654 this.setToolTipText(null);
657 if (aa[row].annotations[res] != null)
659 StringBuffer text = new StringBuffer("Sequence position "
662 if (aa[row].annotations[res].description != null)
664 text.append(" " + aa[row].annotations[res].description);
667 ap.alignFrame.statusBar.setText(text.toString());
672 this.setToolTipText(null);
683 public void mouseClicked(MouseEvent evt)
687 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
688 AlignmentAnnotation anot = aa[activeRow];
690 if (anot.description.equals("secondary structure"))
692 // System.out.println(anot.description+" "+anot.getRNAStruc());
697 // TODO mouseClicked-content and drawCursor are quite experimental!
698 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
701 int pady = av.charHeight / 5;
703 graphics.setColor(Color.black);
704 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
706 if (av.validCharWidth)
708 graphics.setColor(Color.white);
710 char s = seq.getCharAt(res);
712 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
713 graphics.drawString(String.valueOf(s), charOffset + x1,
714 (y1 + av.charHeight) - pady);
726 public void paintComponent(Graphics g)
728 g.setColor(Color.white);
729 g.fillRect(0, 0, getWidth(), getHeight());
733 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
734 || (getVisibleRect().height != g.getClipBounds().height))
736 g.drawImage(image, 0, 0, this);
741 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
744 if (image == null || imgWidth != image.getWidth()
745 || image.getHeight(this) != getHeight())
747 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
748 BufferedImage.TYPE_INT_RGB);
749 gg = (Graphics2D) image.getGraphics();
753 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
754 RenderingHints.VALUE_ANTIALIAS_ON);
757 gg.setFont(av.getFont());
758 fm = gg.getFontMetrics();
759 gg.setColor(Color.white);
760 gg.fillRect(0, 0, imgWidth, image.getHeight());
763 drawComponent(gg, av.startRes, av.endRes + 1);
764 g.drawImage(image, 0, 0, this);
768 * non-Thread safe repaint
771 * repaint with horizontal shift in alignment
773 public void fastPaint(int horizontal)
776 if ((horizontal == 0) || gg == null
777 || av.getAlignment().getAlignmentAnnotation() == null
778 || av.getAlignment().getAlignmentAnnotation().length < 1
779 || av.isCalcInProgress())
784 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
786 int sr = av.startRes;
787 int er = av.endRes + 1;
790 if (horizontal > 0) // scrollbar pulled right, image to the left
792 transX = (er - sr - horizontal) * av.charWidth;
793 sr = er - horizontal;
795 else if (horizontal < 0)
797 er = sr - horizontal;
800 gg.translate(transX, 0);
802 drawComponent(gg, sr, er);
804 gg.translate(-transX, 0);
821 public void drawComponent(Graphics g, int startRes, int endRes)
823 if (av.isCalcInProgress())
829 // We'll keep a record of the old image,
830 // and draw a faded image until the calculation
832 if (fadedImage == null || fadedImage.getWidth() != imgWidth
833 || fadedImage.getHeight() != image.getHeight())
835 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
836 BufferedImage.TYPE_INT_RGB);
838 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
840 fadedG.setColor(Color.white);
841 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
843 fadedG.setComposite(AlphaComposite.getInstance(
844 AlphaComposite.SRC_OVER, .3f));
845 fadedG.drawImage(image, 0, 0, this);
855 g.setColor(Color.white);
856 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
858 g.setFont(av.getFont());
861 fm = g.getFontMetrics();
864 if ((av.getAlignment().getAlignmentAnnotation() == null)
865 || (av.getAlignment().getAlignmentAnnotation().length < 1))
867 g.setColor(Color.white);
868 g.fillRect(0, 0, getWidth(), getHeight());
869 g.setColor(Color.black);
870 if (av.validCharWidth)
872 g.drawString("Alignment has no annotations", 20, 15);
877 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
881 public FontMetrics getFontMetrics()
887 public Image getFadedImage()
893 public int getFadedImageWidth()