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.*;
23 import java.util.Hashtable;
27 import jalview.analysis.AAFrequency;
28 import jalview.analysis.StructureFrequency;
29 import jalview.datamodel.*;
30 import jalview.renderer.AnnotationRenderer;
31 import jalview.renderer.AwtRenderPanelI;
39 public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
40 MouseListener, MouseMotionListener, ActionListener,
43 final String HELIX = "Helix";
45 final String SHEET = "Sheet";
48 * For RNA secondary structure "stems" aka helices
50 final String STEM = "RNA Helix";
52 final String LABEL = "Label";
54 final String REMOVE = "Remove Annotation";
56 final String COLOUR = "Colour";
58 public final Color HELIX_COLOUR = Color.red.darker();
60 public final Color SHEET_COLOUR = Color.green.darker().darker();
62 public final Color STEM_COLOUR = Color.blue.darker();
65 public AlignViewport av;
69 public int activeRow = -1;
71 public BufferedImage image;
73 public BufferedImage fadedImage;
77 public FontMetrics fm;
79 public int imgWidth = 0;
81 boolean fastPaint = false;
83 // Used For mouse Dragging and resizing graphs
84 int graphStretch = -1;
86 int graphStretchY = -1;
88 int min; // used by mouseDragged to see if user
90 int max; // used by mouseDragged to see if user
92 boolean mouseDragging = false;
101 public final AnnotationRenderer renderer;
104 * Creates a new AnnotationPanel object.
109 public AnnotationPanel(AlignmentPanel ap)
112 MAC = new jalview.util.Platform().isAMac();
114 ToolTipManager.sharedInstance().registerComponent(this);
115 ToolTipManager.sharedInstance().setInitialDelay(0);
116 ToolTipManager.sharedInstance().setDismissDelay(10000);
119 this.setLayout(null);
120 addMouseListener(this);
121 addMouseMotionListener(this);
122 ap.annotationScroller.getVerticalScrollBar()
123 .addAdjustmentListener(this);
124 renderer = new AnnotationRenderer();
127 public AnnotationPanel(AlignViewport av)
130 renderer = new AnnotationRenderer();
139 public void adjustmentValueChanged(AdjustmentEvent evt)
141 ap.alabels.setScrollOffset(-evt.getValue());
145 * Calculates the height of the annotation displayed in the annotation panel.
146 * Callers should normally call the ap.adjustAnnotationHeight method to ensure
147 * all annotation associated components are updated correctly.
150 public int adjustPanelHeight()
152 int height = calcPanelHeight();
153 this.setPreferredSize(new Dimension(1, height));
156 // revalidate only when the alignment panel is fully constructed
164 * calculate the height for visible annotation, revalidating bounds where
165 * necessary ABSTRACT GUI METHOD
167 * @return total height of annotation
169 public int calcPanelHeight()
171 // setHeight of panels
172 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
177 for (int i = 0; i < aa.length; i++)
181 System.err.println("Null annotation row: ignoring.");
193 aa[i].height += av.charHeight;
203 aa[i].height += aa[i].graphHeight;
206 if (aa[i].height == 0)
211 height += aa[i].height;
228 public void actionPerformed(ActionEvent evt)
230 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
235 Annotation[] anot = aa[activeRow].annotations;
237 if (anot.length < av.getColumnSelection().getMax())
239 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
240 System.arraycopy(anot, 0, temp, 0, anot.length);
242 aa[activeRow].annotations = anot;
245 if (evt.getActionCommand().equals(REMOVE))
247 for (int i = 0; i < av.getColumnSelection().size(); i++)
249 anot[av.getColumnSelection().columnAt(i)] = null;
252 else if (evt.getActionCommand().equals(LABEL))
254 String exMesg = collectAnnotVals(anot, av.getColumnSelection(), LABEL);
255 String label = JOptionPane.showInputDialog(this, "Enter label",
263 if ((label.length() > 0) && !aa[activeRow].hasText)
265 aa[activeRow].hasText = true;
268 for (int i = 0; i < av.getColumnSelection().size(); i++)
270 int index = av.getColumnSelection().columnAt(i);
272 if (!av.getColumnSelection().isVisible(index))
275 if (anot[index] == null)
277 anot[index] = new Annotation(label, "", ' ', 0); // TODO: verify that
284 anot[index].displayCharacter = label;
288 else if (evt.getActionCommand().equals(COLOUR))
290 Color col = JColorChooser.showDialog(this,
291 "Choose foreground colour", Color.black);
293 for (int i = 0; i < av.getColumnSelection().size(); i++)
295 int index = av.getColumnSelection().columnAt(i);
297 if (!av.getColumnSelection().isVisible(index))
300 if (anot[index] == null)
302 anot[index] = new Annotation("", "", ' ', 0);
305 anot[index].colour = col;
312 String symbol = "\u03B1";
314 if (evt.getActionCommand().equals(HELIX))
318 else if (evt.getActionCommand().equals(SHEET))
324 // Added by LML to color stems
325 else if (evt.getActionCommand().equals(STEM))
331 if (!aa[activeRow].hasIcons)
333 aa[activeRow].hasIcons = true;
336 String label = JOptionPane.showInputDialog(
337 "Enter a label for the structure?", symbol);
344 if ((label.length() > 0) && !aa[activeRow].hasText)
346 aa[activeRow].hasText = true;
349 for (int i = 0; i < av.getColumnSelection().size(); i++)
351 int index = av.getColumnSelection().columnAt(i);
353 if (!av.getColumnSelection().isVisible(index))
356 if (anot[index] == null)
358 anot[index] = new Annotation(label, "", type, 0);
361 anot[index].secondaryStructure = type;
362 anot[index].displayCharacter = label;
365 aa[activeRow].validateRangeAndDisplay();
373 private String collectAnnotVals(Annotation[] anot,
374 ColumnSelection columnSelection, String label2)
376 String collatedInput = "";
378 ColumnSelection viscols=av.getColumnSelection();
379 // TODO: refactor and save av.getColumnSelection for efficiency
380 for (int i = 0; i < columnSelection.size(); i++)
382 int index = columnSelection.columnAt(i);
383 // always check for current display state - just in case
384 if (!viscols.isVisible(index))
386 String tlabel = null;
387 if (anot[index] != null)
388 { // LML added stem code
389 if (label2.equals(HELIX) || label2.equals(SHEET)
390 || label2.equals(STEM) || label2.equals(LABEL))
392 tlabel = anot[index].description;
393 if (tlabel == null || tlabel.length() < 1)
395 if (label2.equals(HELIX) || label2.equals(SHEET)
396 || label2.equals(STEM))
398 tlabel = "" + anot[index].secondaryStructure;
402 tlabel = "" + anot[index].displayCharacter;
406 if (tlabel != null && !tlabel.equals(last))
408 if (last.length() > 0)
410 collatedInput += " ";
412 collatedInput += tlabel;
416 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);
517 public void mouseReleased(MouseEvent evt)
521 mouseDragging = false;
522 ap.scalePanel.mouseReleased(evt);
531 public void mouseEntered(MouseEvent evt)
533 ap.scalePanel.mouseEntered(evt);
542 public void mouseExited(MouseEvent evt)
544 ap.scalePanel.mouseExited(evt);
553 public void mouseDragged(MouseEvent evt)
555 if (graphStretch > -1)
557 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
559 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
561 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
563 graphStretchY = evt.getY();
565 ap.paintAlignment(true);
569 ap.scalePanel.mouseDragged(evt);
579 public void mouseMoved(MouseEvent evt)
581 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
585 this.setToolTipText(null);
592 for (int i = 0; i < aa.length; i++)
596 height += aa[i].height;
599 if (evt.getY() < height)
609 this.setToolTipText(null);
613 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
615 if (av.hasHiddenColumns())
617 res = av.getColumnSelection().adjustForHiddenColumns(res);
620 if (row > -1 && aa[row].annotations != null
621 && res < (int) aa[row].annotations.length)
623 if (aa[row].graphGroup > -1)
625 StringBuffer tip = new StringBuffer("<html>");
626 for (int gg = 0; gg < aa.length; gg++)
628 if (aa[gg].graphGroup == aa[row].graphGroup
629 && aa[gg].annotations[res] != null)
631 tip.append(aa[gg].label + " "
632 + aa[gg].annotations[res].description + "<br>");
635 if (tip.length() != 6)
637 tip.setLength(tip.length() - 4);
638 this.setToolTipText(tip.toString() + "</html>");
641 else if (aa[row].annotations[res] != null
642 && aa[row].annotations[res].description != null
643 && aa[row].annotations[res].description.length() > 0)
645 this.setToolTipText(aa[row].annotations[res].description);
649 // clear the tooltip.
650 this.setToolTipText(null);
653 if (aa[row].annotations[res] != null)
655 StringBuffer text = new StringBuffer("Sequence position "
658 if (aa[row].annotations[res].description != null)
660 text.append(" " + aa[row].annotations[res].description);
663 ap.alignFrame.statusBar.setText(text.toString());
668 this.setToolTipText(null);
678 public void mouseClicked(MouseEvent evt)
682 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
683 AlignmentAnnotation anot = aa[activeRow];
685 if (anot.description.equals("secondary structure"))
687 // System.out.println(anot.description+" "+anot.getRNAStruc());
692 // TODO mouseClicked-content and drawCursor are quite experimental!
693 public void drawCursor(Graphics graphics, SequenceI seq, int res, int x1,
696 int pady = av.charHeight / 5;
698 graphics.setColor(Color.black);
699 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
701 if (av.validCharWidth)
703 graphics.setColor(Color.white);
705 char s = seq.getCharAt(res);
707 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
708 graphics.drawString(String.valueOf(s), charOffset + x1,
709 (y1 + av.charHeight) - pady);
720 public void paintComponent(Graphics g)
722 g.setColor(Color.white);
723 g.fillRect(0, 0, getWidth(), getHeight());
727 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
728 || (getVisibleRect().height != g.getClipBounds().height))
730 g.drawImage(image, 0, 0, this);
735 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
738 if (image == null || imgWidth != image.getWidth()
739 || image.getHeight(this) != getHeight())
741 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
742 BufferedImage.TYPE_INT_RGB);
743 gg = (Graphics2D) image.getGraphics();
747 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
748 RenderingHints.VALUE_ANTIALIAS_ON);
751 gg.setFont(av.getFont());
752 fm = gg.getFontMetrics();
753 gg.setColor(Color.white);
754 gg.fillRect(0, 0, imgWidth, image.getHeight());
757 drawComponent(gg, av.startRes, av.endRes + 1);
758 g.drawImage(image, 0, 0, this);
762 * non-Thread safe repaint
765 * repaint with horizontal shift in alignment
767 public void fastPaint(int horizontal)
770 if ((horizontal == 0) || gg == null
771 || av.getAlignment().getAlignmentAnnotation() == null
772 || av.getAlignment().getAlignmentAnnotation().length < 1
773 || av.isCalcInProgress())
778 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
780 int sr = av.startRes;
781 int er = av.endRes + 1;
784 if (horizontal > 0) // scrollbar pulled right, image to the left
786 transX = (er - sr - horizontal) * av.charWidth;
787 sr = er - horizontal;
789 else if (horizontal < 0)
791 er = sr - horizontal;
794 gg.translate(transX, 0);
796 drawComponent(gg, sr, er);
798 gg.translate(-transX, 0);
815 public void drawComponent(Graphics g, int startRes, int endRes)
817 if (av.isCalcInProgress())
823 // We'll keep a record of the old image,
824 // and draw a faded image until the calculation
826 if (fadedImage == null || fadedImage.getWidth() != imgWidth
827 || fadedImage.getHeight() != image.getHeight())
829 fadedImage = new BufferedImage(imgWidth, image.getHeight(),
830 BufferedImage.TYPE_INT_RGB);
832 Graphics2D fadedG = (Graphics2D) fadedImage.getGraphics();
834 fadedG.setColor(Color.white);
835 fadedG.fillRect(0, 0, imgWidth, image.getHeight());
837 fadedG.setComposite(AlphaComposite.getInstance(
838 AlphaComposite.SRC_OVER, .3f));
839 fadedG.drawImage(image, 0, 0, this);
849 g.setColor(Color.white);
850 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
852 g.setFont(av.getFont());
855 fm = g.getFontMetrics();
858 if ((av.getAlignment().getAlignmentAnnotation() == null)
859 || (av.getAlignment().getAlignmentAnnotation().length < 1))
861 g.setColor(Color.white);
862 g.fillRect(0, 0, getWidth(), getHeight());
863 g.setColor(Color.black);
864 if (av.validCharWidth)
866 g.drawString("Alignment has no annotations", 20, 15);
871 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
875 public FontMetrics getFontMetrics()
881 public Image getFadedImage()
887 public int getFadedImageWidth()