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 = av.calcPanelHeight();
150 this.setPreferredSize(new Dimension(1, height));
153 // revalidate only when the alignment panel is fully constructed
167 public void actionPerformed(ActionEvent evt)
169 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
174 Annotation[] anot = aa[activeRow].annotations;
176 if (anot.length < av.getColumnSelection().getMax())
178 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
179 System.arraycopy(anot, 0, temp, 0, anot.length);
181 aa[activeRow].annotations = anot;
184 if (evt.getActionCommand().equals(REMOVE))
186 for (int i = 0; i < av.getColumnSelection().size(); i++)
188 anot[av.getColumnSelection().columnAt(i)] = null;
191 else if (evt.getActionCommand().equals(LABEL))
193 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
194 String label = JOptionPane.showInputDialog(this, "Enter label",
202 if ((label.length() > 0) && !aa[activeRow].hasText)
204 aa[activeRow].hasText = true;
207 for (int i = 0; i < av.getColumnSelection().size(); i++)
209 int index = av.getColumnSelection().columnAt(i);
211 if (!av.getColumnSelection().isVisible(index))
214 if (anot[index] == null)
216 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
223 anot[index].displayCharacter = label;
227 else if (evt.getActionCommand().equals(COLOUR))
229 Color col = JColorChooser.showDialog(this,
230 "Choose foreground colour", Color.black);
232 for (int i = 0; i < av.getColumnSelection().size(); i++)
234 int index = av.getColumnSelection().columnAt(i);
236 if (!av.getColumnSelection().isVisible(index))
239 if (anot[index] == null)
241 anot[index] = new Annotation("", "", ' ', 0);
244 anot[index].colour = col;
251 String symbol = "\u03B1";
253 if (evt.getActionCommand().equals(HELIX))
257 else if (evt.getActionCommand().equals(SHEET))
263 // Added by LML to color stems
264 else if (evt.getActionCommand().equals(STEM))
270 if (!aa[activeRow].hasIcons)
272 aa[activeRow].hasIcons = true;
275 String label = JOptionPane.showInputDialog(
276 "Enter a label for the structure?", symbol);
283 if ((label.length() > 0) && !aa[activeRow].hasText)
285 aa[activeRow].hasText = true;
288 for (int i = 0; i < av.getColumnSelection().size(); i++)
290 int index = av.getColumnSelection().columnAt(i);
292 if (!av.getColumnSelection().isVisible(index))
295 if (anot[index] == null)
297 anot[index] = new Annotation(label, "", type, 0);
300 anot[index].secondaryStructure = type;
301 anot[index].displayCharacter = label;
304 aa[activeRow].validateRangeAndDisplay();
307 ap.alignmentChanged();
313 private String collectAnnotVals(Annotation[] anot,
314 ColumnSelection columnSelection, String label2)
316 String collatedInput = "";
318 ColumnSelection viscols=av.getColumnSelection();
319 // TODO: refactor and save av.getColumnSelection for efficiency
320 for (int i = 0; i < columnSelection.size(); i++)
322 int index = columnSelection.columnAt(i);
323 // always check for current display state - just in case
324 if (!viscols.isVisible(index))
326 String tlabel = null;
327 if (anot[index] != null)
328 { // LML added stem code
329 if (label2.equals(HELIX) || label2.equals(SHEET)
330 || label2.equals(STEM) || label2.equals(LABEL))
332 tlabel = anot[index].description;
333 if (tlabel == null || tlabel.length() < 1)
335 if (label2.equals(HELIX) || label2.equals(SHEET)
336 || label2.equals(STEM))
338 tlabel = "" + anot[index].secondaryStructure;
342 tlabel = "" + anot[index].displayCharacter;
346 if (tlabel != null && !tlabel.equals(last))
348 if (last.length() > 0)
350 collatedInput += " ";
352 collatedInput += tlabel;
356 return collatedInput;
366 public void mousePressed(MouseEvent evt)
369 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
378 for (int i = 0; i < aa.length; i++)
382 height += aa[i].height;
385 if (evt.getY() < height)
391 else if (aa[i].graph > 0)
395 graphStretchY = evt.getY();
402 if (SwingUtilities.isRightMouseButton(evt) && activeRow != -1)
404 if (av.getColumnSelection() == null)
409 JPopupMenu pop = new JPopupMenu("Structure type");
412 * Just display the needed structure options
414 if (av.getAlignment().isNucleotide() == true)
416 item = new JMenuItem(STEM);
417 item.addActionListener(this);
422 item = new JMenuItem(HELIX);
423 item.addActionListener(this);
425 item = new JMenuItem(SHEET);
426 item.addActionListener(this);
429 item = new JMenuItem(LABEL);
430 item.addActionListener(this);
432 item = new JMenuItem(COLOUR);
433 item.addActionListener(this);
435 item = new JMenuItem(REMOVE);
436 item.addActionListener(this);
438 pop.show(this, evt.getX(), evt.getY());
448 ap.scalePanel.mousePressed(evt);
459 public void mouseReleased(MouseEvent evt)
463 mouseDragging = false;
464 ap.scalePanel.mouseReleased(evt);
474 public void mouseEntered(MouseEvent evt)
476 ap.scalePanel.mouseEntered(evt);
486 public void mouseExited(MouseEvent evt)
488 ap.scalePanel.mouseExited(evt);
498 public void mouseDragged(MouseEvent evt)
500 if (graphStretch > -1)
502 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
504 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
506 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
508 graphStretchY = evt.getY();
510 ap.paintAlignment(true);
514 ap.scalePanel.mouseDragged(evt);
525 public void mouseMoved(MouseEvent evt)
527 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
531 this.setToolTipText(null);
538 for (int i = 0; i < aa.length; i++)
542 height += aa[i].height;
545 if (evt.getY() < height)
555 this.setToolTipText(null);
559 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
561 if (av.hasHiddenColumns())
563 res = av.getColumnSelection().adjustForHiddenColumns(res);
566 if (row > -1 && aa[row].annotations != null
567 && res < aa[row].annotations.length)
569 if (aa[row].graphGroup > -1)
571 StringBuffer tip = new StringBuffer("<html>");
572 for (int gg = 0; gg < aa.length; gg++)
574 if (aa[gg].graphGroup == aa[row].graphGroup
575 && aa[gg].annotations[res] != null)
577 tip.append(aa[gg].label + " "
578 + aa[gg].annotations[res].description + "<br>");
581 if (tip.length() != 6)
583 tip.setLength(tip.length() - 4);
584 this.setToolTipText(tip.toString() + "</html>");
587 else if (aa[row].annotations[res] != null
588 && aa[row].annotations[res].description != null
589 && aa[row].annotations[res].description.length() > 0)
591 this.setToolTipText(aa[row].annotations[res].description);
595 // clear the tooltip.
596 this.setToolTipText(null);
599 if (aa[row].annotations[res] != null)
601 StringBuffer text = new StringBuffer("Sequence position "
604 if (aa[row].annotations[res].description != null)
606 text.append(" " + aa[row].annotations[res].description);
609 ap.alignFrame.statusBar.setText(text.toString());
614 this.setToolTipText(null);
625 public void mouseClicked(MouseEvent evt)
629 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
630 AlignmentAnnotation anot = aa[activeRow];
632 if (anot.description.equals("secondary structure"))
634 // System.out.println(anot.description+" "+anot.getRNAStruc());
639 // TODO mouseClicked-content and drawCursor are quite experimental!
640 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
643 int pady = av.charHeight / 5;
645 graphics.setColor(Color.black);
646 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
648 if (av.validCharWidth)
650 graphics.setColor(Color.white);
652 char s = seq.getCharAt(res);
654 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
655 graphics.drawString(String.valueOf(s), charOffset + x1,
656 (y1 + av.charHeight) - pady);
660 private volatile boolean imageFresh=false;
668 public void paintComponent(Graphics g)
670 g.setColor(Color.white);
671 g.fillRect(0, 0, getWidth(), getHeight());
675 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
676 || (getVisibleRect().height != g.getClipBounds().height))
678 g.drawImage(image, 0, 0, this);
683 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
686 if (image == null || imgWidth != image.getWidth()
687 || image.getHeight(this) != getHeight())
689 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
690 BufferedImage.TYPE_INT_RGB);
691 gg = (Graphics2D) image.getGraphics();
695 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
696 RenderingHints.VALUE_ANTIALIAS_ON);
699 gg.setFont(av.getFont());
700 fm = gg.getFontMetrics();
701 gg.setColor(Color.white);
702 gg.fillRect(0, 0, imgWidth, image.getHeight());
706 drawComponent(gg, av.startRes, av.endRes + 1);
708 g.drawImage(image, 0, 0, this);
712 * non-Thread safe repaint
715 * repaint with horizontal shift in alignment
717 public void fastPaint(int horizontal)
720 if ((horizontal == 0) || gg == null
721 || av.getAlignment().getAlignmentAnnotation() == null
722 || av.getAlignment().getAlignmentAnnotation().length < 1
723 || av.isCalcInProgress())
728 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
730 int sr = av.startRes;
731 int er = av.endRes + 1;
734 if (horizontal > 0) // scrollbar pulled right, image to the left
736 transX = (er - sr - horizontal) * av.charWidth;
737 sr = er - horizontal;
739 else if (horizontal < 0)
741 er = sr - horizontal;
744 gg.translate(transX, 0);
746 drawComponent(gg, sr, er);
748 gg.translate(-transX, 0);
754 private volatile boolean lastImageGood=false;
765 public void drawComponent(Graphics g, int startRes, int endRes)
767 BufferedImage oldFaded=fadedImage;
768 if (av.isCalcInProgress())
775 // We'll keep a record of the old image,
776 // and draw a faded image until the calculation
778 if (lastImageGood && (fadedImage == null || fadedImage.getWidth() != imgWidth
779 || fadedImage.getHeight() != image.getHeight()))
781 // System.err.println("redraw faded image ("+(fadedImage==null ? "null image" : "") + " lastGood="+lastImageGood+")");
782 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
783 BufferedImage.TYPE_INT_RGB);
785 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
787 fadedG.setColor(Color.white);
788 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
790 fadedG.setComposite(AlphaComposite.getInstance(
791 AlphaComposite.SRC_OVER, .3f));
792 fadedG.drawImage(image, 0, 0, this);
795 // make sure we don't overwrite the last good faded image until all calculations have finished
801 if (fadedImage!=null)
808 g.setColor(Color.white);
809 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
811 g.setFont(av.getFont());
814 fm = g.getFontMetrics();
817 if ((av.getAlignment().getAlignmentAnnotation() == null)
818 || (av.getAlignment().getAlignmentAnnotation().length < 1))
820 g.setColor(Color.white);
821 g.fillRect(0, 0, getWidth(), getHeight());
822 g.setColor(Color.black);
823 if (av.validCharWidth)
825 g.drawString("Alignment has no annotations", 20, 15);
830 lastImageGood = renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
831 if (!lastImageGood && fadedImage==null)
838 public FontMetrics getFontMetrics()
844 public Image getFadedImage()
850 public int getFadedImageWidth()