2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.SequenceI;
26 import jalview.renderer.AnnotationRenderer;
27 import jalview.renderer.AwtRenderPanelI;
28 import jalview.schemes.ResidueProperties;
29 import jalview.util.Comparison;
30 import jalview.util.MessageManager;
31 import jalview.util.Platform;
32 import jalview.viewmodel.ViewportListenerI;
33 import jalview.viewmodel.ViewportRanges;
35 import java.awt.Color;
36 import java.awt.Dimension;
38 import java.awt.FontMetrics;
39 import java.awt.Graphics;
40 import java.awt.Image;
41 import java.awt.MenuItem;
42 import java.awt.Panel;
43 import java.awt.PopupMenu;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.AdjustmentEvent;
47 import java.awt.event.AdjustmentListener;
48 import java.awt.event.InputEvent;
49 import java.awt.event.MouseEvent;
50 import java.awt.event.MouseListener;
51 import java.awt.event.MouseMotionListener;
52 import java.beans.PropertyChangeEvent;
54 public class AnnotationPanel extends Panel
55 implements AwtRenderPanelI, AdjustmentListener, ActionListener,
56 MouseListener, MouseMotionListener, ViewportListenerI
64 final String HELIX = "Helix";
66 final String SHEET = "Sheet";
69 * For RNA secondary structure "stems" aka helices
71 final String STEM = "RNA Helix";
73 final String LABEL = "Label";
75 final String REMOVE = "Remove Annotation";
77 final String COLOUR = "Colour";
79 final Color HELIX_COLOUR = Color.red.darker();
81 final Color SHEET_COLOUR = Color.green.darker().darker();
91 boolean fastPaint = false;
93 // Used For mouse Dragging and resizing graphs
94 int graphStretch = -1;
96 int graphStretchY = -1;
98 boolean mouseDragging = false;
100 public static int GRAPH_HEIGHT = 40;
102 // boolean MAC = false;
104 public final AnnotationRenderer renderer;
106 public AnnotationPanel(AlignmentPanel ap)
108 new jalview.util.Platform();
109 // MAC = Platform.isAMac();
113 int height = adjustPanelHeight();
114 ap.apvscroll.setValues(0, getSize().height, 0, height);
116 addMouseMotionListener(this);
118 addMouseListener(this);
120 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
121 renderer = new AnnotationRenderer();
123 av.getRanges().addPropertyChangeListener(this);
126 public AnnotationPanel(AlignViewport av)
129 renderer = new AnnotationRenderer();
134 public void adjustmentValueChanged(AdjustmentEvent evt)
145 public void actionPerformed(ActionEvent evt)
147 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
152 Annotation[] anot = aa[activeRow].annotations;
154 if (anot.length < av.getColumnSelection().getMax())
156 Annotation[] temp = new Annotation[av.getColumnSelection().getMax()
158 System.arraycopy(anot, 0, temp, 0, anot.length);
160 aa[activeRow].annotations = anot;
164 if (av.getColumnSelection() != null
165 && !av.getColumnSelection().isEmpty()
166 && anot[av.getColumnSelection().getMin()] != null)
168 label = anot[av.getColumnSelection().getMin()].displayCharacter;
171 if (evt.getActionCommand().equals(REMOVE))
173 for (int index : av.getColumnSelection().getSelected())
175 if (av.getAlignment().getHiddenColumns().isVisible(index))
181 else if (evt.getActionCommand().equals(LABEL))
183 label = enterLabel(label, "Enter Label");
190 if ((label.length() > 0) && !aa[activeRow].hasText)
192 aa[activeRow].hasText = true;
195 for (int index : av.getColumnSelection().getSelected())
197 // TODO: JAL-2001 - provide a fast method to list visible selected
199 if (!av.getAlignment().getHiddenColumns().isVisible(index))
204 if (anot[index] == null)
206 anot[index] = new Annotation(label, "", ' ', 0);
209 anot[index].displayCharacter = label;
212 else if (evt.getActionCommand().equals(COLOUR))
214 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
217 Color col = udc.getColor();
219 for (int index : av.getColumnSelection().getSelected())
221 if (!av.getAlignment().getHiddenColumns().isVisible(index))
226 if (anot[index] == null)
228 anot[index] = new Annotation("", "", ' ', 0);
231 anot[index].colour = col;
238 String symbol = "\u03B1";
240 if (evt.getActionCommand().equals(HELIX))
244 else if (evt.getActionCommand().equals(SHEET))
250 // Added by LML to color stems
251 else if (evt.getActionCommand().equals(STEM))
254 int column = av.getColumnSelection().getSelectedRanges().get(0)[0];
255 symbol = aa[activeRow].getDefaultRnaHelixSymbol(column);
258 if (!aa[activeRow].hasIcons)
260 aa[activeRow].hasIcons = true;
263 label = enterLabel(symbol, "Enter Label");
270 if ((label.length() > 0) && !aa[activeRow].hasText)
272 aa[activeRow].hasText = true;
273 if (evt.getActionCommand().equals(STEM))
275 aa[activeRow].showAllColLabels = true;
279 for (int index : av.getColumnSelection().getSelected())
281 if (!av.getAlignment().getHiddenColumns().isVisible(index))
286 if (anot[index] == null)
288 anot[index] = new Annotation(label, "", type, 0);
291 anot[index].secondaryStructure = type != 'S' ? type
292 : label.length() == 0 ? ' ' : label.charAt(0);
293 anot[index].displayCharacter = label;
297 av.getAlignment().validateAnnotation(aa[activeRow]);
299 ap.alignmentChanged();
306 String enterLabel(String text, String label)
308 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
309 ap.alignFrame, "Enter Label", 400, 200, true);
313 return dialog.getName();
322 public void mousePressed(MouseEvent evt)
324 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
330 int height = -scrollOffset;
333 for (int i = 0; i < aa.length; i++)
337 height += aa[i].height;
340 if (evt.getY() < height)
346 else if (aa[i].graph > 0)
350 graphStretchY = evt.getY();
357 if ((evt.getModifiersEx()
358 & InputEvent.BUTTON3_DOWN_MASK) == InputEvent.BUTTON3_DOWN_MASK
361 if (av.getColumnSelection() == null
362 || av.getColumnSelection().isEmpty())
367 PopupMenu pop = new PopupMenu(
368 MessageManager.getString("label.structure_type"));
371 if (av.getAlignment().isNucleotide())
373 item = new MenuItem(STEM);
374 item.addActionListener(this);
379 item = new MenuItem(HELIX);
380 item.addActionListener(this);
382 item = new MenuItem(SHEET);
383 item.addActionListener(this);
386 item = new MenuItem(LABEL);
387 item.addActionListener(this);
389 item = new MenuItem(COLOUR);
390 item.addActionListener(this);
392 item = new MenuItem(REMOVE);
393 item.addActionListener(this);
395 ap.alignFrame.add(pop);
396 pop.show(this, evt.getX(), evt.getY());
401 ap.scalePanel.mousePressed(evt);
405 public void mouseReleased(MouseEvent evt)
409 mouseDragging = false;
413 needValidating = false;
415 ap.scalePanel.mouseReleased(evt);
419 public void mouseClicked(MouseEvent evt)
423 boolean needValidating = false;
426 public void mouseDragged(MouseEvent evt)
428 if (graphStretch > -1)
431 .getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
433 if (av.getAlignment()
434 .getAlignmentAnnotation()[graphStretch].graphHeight < 0)
437 .getAlignmentAnnotation()[graphStretch].graphHeight = 0;
439 graphStretchY = evt.getY();
440 av.calcPanelHeight();
441 needValidating = true;
442 // TODO: only update overview visible geometry
443 ap.paintAlignment(true, false);
447 ap.scalePanel.mouseDragged(evt);
452 public void mouseMoved(MouseEvent evt)
454 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
461 int height = -scrollOffset;
462 for (int i = 0; i < aa.length; i++)
467 height += aa[i].height;
470 if (evt.getY() < height)
477 int column = evt.getX() / av.getCharWidth()
478 + av.getRanges().getStartRes();
480 if (av.hasHiddenColumns())
482 column = av.getAlignment().getHiddenColumns()
483 .visibleToAbsoluteColumn(column);
486 if (row > -1 && column < aa[row].annotations.length
487 && aa[row].annotations[column] != null)
489 StringBuilder text = new StringBuilder();
490 text.append(MessageManager.getString("label.column")).append(" ")
492 String description = aa[row].annotations[column].description;
493 if (description != null && description.length() > 0)
495 text.append(" ").append(description);
499 * if the annotation is sequence-specific, show the sequence number
500 * in the alignment, and (if not a gap) the residue and position
502 SequenceI seqref = aa[row].sequenceRef;
505 int seqIndex = av.getAlignment().findIndex(seqref);
509 .append(MessageManager.getString("label.sequence"))
510 .append(" ").append(seqIndex + 1);
511 char residue = seqref.getCharAt(column);
512 if (!Comparison.isGap(residue))
516 if (av.getAlignment().isNucleotide())
518 name = ResidueProperties.nucleotideName
519 .get(String.valueOf(residue));
520 text.append(" Nucleotide: ")
521 .append(name != null ? name : residue);
525 name = 'X' == residue ? "X"
526 : ('*' == residue ? "STOP"
527 : ResidueProperties.aa2Triplet
528 .get(String.valueOf(residue)));
529 text.append(" Residue: ")
530 .append(name != null ? name : residue);
532 int residuePos = seqref.findPosition(column);
533 text.append(" (").append(residuePos).append(")");
534 // int residuePos = seqref.findPosition(column);
535 // text.append(residue).append(" (")
536 // .append(residuePos).append(")");
541 ap.alignFrame.statusBar.setText(text.toString());
546 public void mouseEntered(MouseEvent evt)
548 ap.scalePanel.mouseEntered(evt);
552 public void mouseExited(MouseEvent evt)
554 ap.scalePanel.mouseExited(evt);
557 public int adjustPanelHeight()
559 return adjustPanelHeight(true);
562 public int adjustPanelHeight(boolean repaint)
564 int height = av.calcPanelHeight();
565 this.setSize(new Dimension(getSize().width, height));
574 * calculate the height for visible annotation, revalidating bounds where
575 * necessary ABSTRACT GUI METHOD
577 * @return total height of annotation
580 public void addEditableColumn(int i)
584 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
590 for (int j = 0; j < aa.length; j++)
602 public void update(Graphics g)
608 public void paint(Graphics g)
610 Dimension d = getSize();
612 // (av.endRes - av.startRes + 1) * av.charWidth;
613 if (imgWidth < 1 || d.height < 1)
617 if (image == null || imgWidth != image.getWidth(this)
618 || d.height != image.getHeight(this))
620 image = createImage(imgWidth, d.height);
621 gg = image.getGraphics();
622 gg.setFont(av.getFont());
623 fm = gg.getFontMetrics();
629 g.drawImage(image, 0, 0, this);
634 gg.setColor(Color.white);
635 gg.fillRect(0, 0, getSize().width, getSize().height);
636 drawComponent(gg, av.getRanges().getStartRes(),
637 av.getRanges().getEndRes() + 1);
639 g.drawImage(image, 0, 0, this);
642 public void fastPaint(int horizontal)
644 if (horizontal == 0 || gg == null
645 || av.getAlignment().getAlignmentAnnotation() == null
646 || av.getAlignment().getAlignmentAnnotation().length < 1)
652 gg.copyArea(0, 0, imgWidth, getSize().height,
653 -horizontal * av.getCharWidth(), 0);
654 int sr = av.getRanges().getStartRes(),
655 er = av.getRanges().getEndRes() + 1, transX = 0;
657 if (horizontal > 0) // scrollbar pulled right, image to the left
659 transX = (er - sr - horizontal) * av.getCharWidth();
660 sr = er - horizontal;
662 else if (horizontal < 0)
664 er = sr - horizontal;
667 gg.translate(transX, 0);
669 drawComponent(gg, sr, er);
671 gg.translate(-transX, 0);
687 public void drawComponent(Graphics g, int startRes, int endRes)
689 Font ofont = av.getFont();
692 g.setColor(Color.white);
693 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(),
698 fm = g.getFontMetrics();
701 if ((av.getAlignment().getAlignmentAnnotation() == null)
702 || (av.getAlignment().getAlignmentAnnotation().length < 1))
704 g.setColor(Color.white);
705 g.fillRect(0, 0, getSize().width, getSize().height);
706 g.setColor(Color.black);
707 if (av.validCharWidth)
709 g.drawString(MessageManager
710 .getString("label.alignment_has_no_annotations"), 20, 15);
715 g.translate(0, -scrollOffset);
716 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
717 g.translate(0, +scrollOffset);
720 int scrollOffset = 0;
722 public void setScrollOffset(int value, boolean repaint)
724 scrollOffset = value;
732 public FontMetrics getFontMetrics()
738 public Image getFadedImage()
744 public int getFadedImageWidth()
749 private int[] bounds = new int[2];
752 public int[] getVisibleVRange()
754 if (ap != null && ap.alabels != null)
756 int sOffset = -ap.alabels.scrollOffset;
757 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
759 bounds[1] = visHeight;
769 public void propertyChange(PropertyChangeEvent evt)
771 // Respond to viewport range changes (e.g. alignment panel was scrolled)
772 // Both scrolling and resizing change viewport ranges: scrolling changes
773 // both start and end points, but resize only changes end values.
774 // Here we only want to fastpaint on a scroll, with resize using a normal
775 // paint, so scroll events are identified as changes to the horizontal or
776 // vertical start value.
777 if (evt.getPropertyName().equals(ViewportRanges.STARTRES))
779 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
781 else if (evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ))
783 fastPaint(((int[]) evt.getNewValue())[0]
784 - ((int[]) evt.getOldValue())[0]);
786 else if (evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))