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 volatile 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();
366 ap.alignmentChanged();
372 private String collectAnnotVals(Annotation[] anot,
373 ColumnSelection columnSelection, String label2)
375 String collatedInput = "";
377 ColumnSelection viscols=av.getColumnSelection();
378 // TODO: refactor and save av.getColumnSelection for efficiency
379 for (int i = 0; i < columnSelection.size(); i++)
381 int index = columnSelection.columnAt(i);
382 // always check for current display state - just in case
383 if (!viscols.isVisible(index))
385 String tlabel = null;
386 if (anot[index] != null)
387 { // LML added stem code
388 if (label2.equals(HELIX) || label2.equals(SHEET)
389 || label2.equals(STEM) || label2.equals(LABEL))
391 tlabel = anot[index].description;
392 if (tlabel == null || tlabel.length() < 1)
394 if (label2.equals(HELIX) || label2.equals(SHEET)
395 || label2.equals(STEM))
397 tlabel = "" + anot[index].secondaryStructure;
401 tlabel = "" + anot[index].displayCharacter;
405 if (tlabel != null && !tlabel.equals(last))
407 if (last.length() > 0)
409 collatedInput += " ";
411 collatedInput += tlabel;
415 return collatedInput;
425 public void mousePressed(MouseEvent evt)
428 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
437 for (int i = 0; i < aa.length; i++)
441 height += aa[i].height;
444 if (evt.getY() < height)
450 else if (aa[i].graph > 0)
454 graphStretchY = evt.getY();
461 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
463 if (av.getColumnSelection() == null)
468 JPopupMenu pop = new JPopupMenu("Structure type");
471 * Just display the needed structure options
473 if (av.getAlignment().isNucleotide() == true)
475 item = new JMenuItem(STEM);
476 item.addActionListener(this);
481 item = new JMenuItem(HELIX);
482 item.addActionListener(this);
484 item = new JMenuItem(SHEET);
485 item.addActionListener(this);
488 item = new JMenuItem(LABEL);
489 item.addActionListener(this);
491 item = new JMenuItem(COLOUR);
492 item.addActionListener(this);
494 item = new JMenuItem(REMOVE);
495 item.addActionListener(this);
497 pop.show(this, evt.getX(), evt.getY());
507 ap.scalePanel.mousePressed(evt);
518 public void mouseReleased(MouseEvent evt)
522 mouseDragging = false;
523 ap.scalePanel.mouseReleased(evt);
533 public void mouseEntered(MouseEvent evt)
535 ap.scalePanel.mouseEntered(evt);
545 public void mouseExited(MouseEvent evt)
547 ap.scalePanel.mouseExited(evt);
557 public void mouseDragged(MouseEvent evt)
559 if (graphStretch > -1)
561 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
563 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
565 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
567 graphStretchY = evt.getY();
569 ap.paintAlignment(true);
573 ap.scalePanel.mouseDragged(evt);
584 public void mouseMoved(MouseEvent evt)
586 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
590 this.setToolTipText(null);
597 for (int i = 0; i < aa.length; i++)
601 height += aa[i].height;
604 if (evt.getY() < height)
614 this.setToolTipText(null);
618 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
620 if (av.hasHiddenColumns())
622 res = av.getColumnSelection().adjustForHiddenColumns(res);
625 if (row > -1 && aa[row].annotations != null
626 && res < aa[row].annotations.length)
628 if (aa[row].graphGroup > -1)
630 StringBuffer tip = new StringBuffer("<html>");
631 for (int gg = 0; gg < aa.length; gg++)
633 if (aa[gg].graphGroup == aa[row].graphGroup
634 && aa[gg].annotations[res] != null)
636 tip.append(aa[gg].label + " "
637 + aa[gg].annotations[res].description + "<br>");
640 if (tip.length() != 6)
642 tip.setLength(tip.length() - 4);
643 this.setToolTipText(tip.toString() + "</html>");
646 else if (aa[row].annotations[res] != null
647 && aa[row].annotations[res].description != null
648 && aa[row].annotations[res].description.length() > 0)
650 this.setToolTipText(aa[row].annotations[res].description);
654 // clear the tooltip.
655 this.setToolTipText(null);
658 if (aa[row].annotations[res] != null)
660 StringBuffer text = new StringBuffer("Sequence position "
663 if (aa[row].annotations[res].description != null)
665 text.append(" " + aa[row].annotations[res].description);
668 ap.alignFrame.statusBar.setText(text.toString());
673 this.setToolTipText(null);
684 public void mouseClicked(MouseEvent evt)
688 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
689 AlignmentAnnotation anot = aa[activeRow];
691 if (anot.description.equals("secondary structure"))
693 // System.out.println(anot.description+" "+anot.getRNAStruc());
698 // TODO mouseClicked-content and drawCursor are quite experimental!
699 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
702 int pady = av.charHeight / 5;
704 graphics.setColor(Color.black);
705 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
707 if (av.validCharWidth)
709 graphics.setColor(Color.white);
711 char s = seq.getCharAt(res);
713 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
714 graphics.drawString(String.valueOf(s), charOffset + x1,
715 (y1 + av.charHeight) - pady);
719 private volatile boolean imageFresh=false;
727 public void paintComponent(Graphics g)
729 g.setColor(Color.white);
730 g.fillRect(0, 0, getWidth(), getHeight());
734 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
735 || (getVisibleRect().height != g.getClipBounds().height))
737 g.drawImage(image, 0, 0, this);
742 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
745 if (image == null || imgWidth != image.getWidth()
746 || image.getHeight(this) != getHeight())
748 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
749 BufferedImage.TYPE_INT_RGB);
750 gg = (Graphics2D) image.getGraphics();
754 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
755 RenderingHints.VALUE_ANTIALIAS_ON);
758 gg.setFont(av.getFont());
759 fm = gg.getFontMetrics();
760 gg.setColor(Color.white);
761 gg.fillRect(0, 0, imgWidth, image.getHeight());
765 drawComponent(gg, av.startRes, av.endRes + 1);
767 g.drawImage(image, 0, 0, this);
771 * non-Thread safe repaint
774 * repaint with horizontal shift in alignment
776 public void fastPaint(int horizontal)
779 if ((horizontal == 0) || gg == null
780 || av.getAlignment().getAlignmentAnnotation() == null
781 || av.getAlignment().getAlignmentAnnotation().length < 1
782 || av.isCalcInProgress())
787 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
789 int sr = av.startRes;
790 int er = av.endRes + 1;
793 if (horizontal > 0) // scrollbar pulled right, image to the left
795 transX = (er - sr - horizontal) * av.charWidth;
796 sr = er - horizontal;
798 else if (horizontal < 0)
800 er = sr - horizontal;
803 gg.translate(transX, 0);
805 drawComponent(gg, sr, er);
807 gg.translate(-transX, 0);
813 private volatile boolean lastImageGood=false;
824 public void drawComponent(Graphics g, int startRes, int endRes)
826 BufferedImage oldFaded=fadedImage;
827 if (av.isCalcInProgress())
834 // We'll keep a record of the old image,
835 // and draw a faded image until the calculation
837 if (lastImageGood && (fadedImage == null || fadedImage.getWidth() != imgWidth
838 || fadedImage.getHeight() != image.getHeight()))
840 // System.err.println("redraw faded image ("+(fadedImage==null ? "null image" : "") + " lastGood="+lastImageGood+")");
841 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
842 BufferedImage.TYPE_INT_RGB);
844 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
846 fadedG.setColor(Color.white);
847 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
849 fadedG.setComposite(AlphaComposite.getInstance(
850 AlphaComposite.SRC_OVER, .3f));
851 fadedG.drawImage(image, 0, 0, this);
854 // make sure we don't overwrite the last good faded image until all calculations have finished
860 if (fadedImage!=null)
867 g.setColor(Color.white);
868 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
870 g.setFont(av.getFont());
873 fm = g.getFontMetrics();
876 if ((av.getAlignment().getAlignmentAnnotation() == null)
877 || (av.getAlignment().getAlignmentAnnotation().length < 1))
879 g.setColor(Color.white);
880 g.fillRect(0, 0, getWidth(), getHeight());
881 g.setColor(Color.black);
882 if (av.validCharWidth)
884 g.drawString("Alignment has no annotations", 20, 15);
889 lastImageGood = renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
890 if (!lastImageGood && fadedImage==null)
897 public FontMetrics getFontMetrics()
903 public Image getFadedImage()
909 public int getFadedImageWidth()