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 implements AwtRenderPanelI,
55 AdjustmentListener, ActionListener, MouseListener,
56 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;
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() + 2];
157 System.arraycopy(anot, 0, temp, 0, anot.length);
159 aa[activeRow].annotations = anot;
163 if (av.getColumnSelection() != null
164 && !av.getColumnSelection().isEmpty()
165 && anot[av.getColumnSelection().getMin()] != null)
167 label = anot[av.getColumnSelection().getMin()].displayCharacter;
170 if (evt.getActionCommand().equals(REMOVE))
172 for (int index : av.getColumnSelection().getSelected())
174 if (av.getAlignment().getHiddenColumns().isVisible(index))
180 else if (evt.getActionCommand().equals(LABEL))
182 label = enterLabel(label, "Enter Label");
189 if ((label.length() > 0) && !aa[activeRow].hasText)
191 aa[activeRow].hasText = true;
194 for (int index : av.getColumnSelection().getSelected())
196 // TODO: JAL-2001 - provide a fast method to list visible selected
198 if (!av.getAlignment().getHiddenColumns().isVisible(index))
203 if (anot[index] == null)
205 anot[index] = new Annotation(label, "", ' ', 0);
208 anot[index].displayCharacter = label;
211 else if (evt.getActionCommand().equals(COLOUR))
213 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
216 Color col = udc.getColor();
218 for (int index : av.getColumnSelection().getSelected())
220 if (!av.getAlignment().getHiddenColumns().isVisible(index))
225 if (anot[index] == null)
227 anot[index] = new Annotation("", "", ' ', 0);
230 anot[index].colour = col;
237 String symbol = "\u03B1";
239 if (evt.getActionCommand().equals(HELIX))
243 else if (evt.getActionCommand().equals(SHEET))
249 // Added by LML to color stems
250 else if (evt.getActionCommand().equals(STEM))
253 int column = av.getColumnSelection().getSelectedRanges().get(0)[0];
254 symbol = aa[activeRow].getDefaultRnaHelixSymbol(column);
257 if (!aa[activeRow].hasIcons)
259 aa[activeRow].hasIcons = true;
262 label = enterLabel(symbol, "Enter Label");
269 if ((label.length() > 0) && !aa[activeRow].hasText)
271 aa[activeRow].hasText = true;
272 if (evt.getActionCommand().equals(STEM))
274 aa[activeRow].showAllColLabels = true;
278 for (int index : av.getColumnSelection().getSelected())
280 if (!av.getAlignment().getHiddenColumns().isVisible(index))
285 if (anot[index] == null)
287 anot[index] = new Annotation(label, "", type, 0);
290 anot[index].secondaryStructure = type != 'S' ? type : label
291 .length() == 0 ? ' ' : label.charAt(0);
292 anot[index].displayCharacter = label;
296 av.getAlignment().validateAnnotation(aa[activeRow]);
298 ap.alignmentChanged();
305 String enterLabel(String text, String label)
307 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
308 ap.alignFrame, "Enter Label", 400, 200, true);
312 return dialog.getName();
321 public void mousePressed(MouseEvent evt)
323 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
329 int height = -scrollOffset;
332 for (int i = 0; i < aa.length; i++)
336 height += aa[i].height;
339 if (evt.getY() < height)
345 else if (aa[i].graph > 0)
349 graphStretchY = evt.getY();
356 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
359 if (av.getColumnSelection() == null
360 || av.getColumnSelection().isEmpty())
365 PopupMenu pop = new PopupMenu(
366 MessageManager.getString("label.structure_type"));
369 if (av.getAlignment().isNucleotide())
371 item = new MenuItem(STEM);
372 item.addActionListener(this);
377 item = new MenuItem(HELIX);
378 item.addActionListener(this);
380 item = new MenuItem(SHEET);
381 item.addActionListener(this);
384 item = new MenuItem(LABEL);
385 item.addActionListener(this);
387 item = new MenuItem(COLOUR);
388 item.addActionListener(this);
390 item = new MenuItem(REMOVE);
391 item.addActionListener(this);
393 ap.alignFrame.add(pop);
394 pop.show(this, evt.getX(), evt.getY());
399 ap.scalePanel.mousePressed(evt);
403 public void mouseReleased(MouseEvent evt)
407 mouseDragging = false;
411 needValidating = false;
413 ap.scalePanel.mouseReleased(evt);
417 public void mouseClicked(MouseEvent evt)
421 boolean needValidating = false;
424 public void mouseDragged(MouseEvent evt)
426 if (graphStretch > -1)
428 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
430 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
432 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
434 graphStretchY = evt.getY();
435 av.calcPanelHeight();
436 needValidating = true;
437 ap.paintAlignment(true);
441 ap.scalePanel.mouseDragged(evt);
446 public void mouseMoved(MouseEvent evt)
448 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
455 int height = -scrollOffset;
456 for (int i = 0; i < aa.length; i++)
461 height += aa[i].height;
464 if (evt.getY() < height)
471 int column = evt.getX() / av.getCharWidth()
472 + av.getRanges().getStartRes();
474 if (av.hasHiddenColumns())
476 column = av.getAlignment().getHiddenColumns()
477 .adjustForHiddenColumns(column);
480 if (row > -1 && column < aa[row].annotations.length
481 && aa[row].annotations[column] != null)
483 StringBuilder text = new StringBuilder();
484 text.append(MessageManager.getString("label.column")).append(" ")
486 String description = aa[row].annotations[column].description;
487 if (description != null && description.length() > 0)
489 text.append(" ").append(description);
493 * if the annotation is sequence-specific, show the sequence number
494 * in the alignment, and (if not a gap) the residue and position
496 SequenceI seqref = aa[row].sequenceRef;
499 int seqIndex = av.getAlignment().findIndex(seqref);
503 .append(MessageManager.getString("label.sequence"))
504 .append(" ").append(seqIndex + 1);
505 char residue = seqref.getCharAt(column);
506 if (!Comparison.isGap(residue))
510 if (av.getAlignment().isNucleotide())
512 name = ResidueProperties.nucleotideName.get(String
514 text.append(" Nucleotide: ").append(
515 name != null ? name : residue);
519 name = 'X' == residue ? "X" : ('*' == residue ? "STOP"
520 : ResidueProperties.aa2Triplet.get(String
522 text.append(" Residue: ").append(
523 name != null ? name : residue);
525 int residuePos = seqref.findPosition(column);
526 text.append(" (").append(residuePos).append(")");
527 // int residuePos = seqref.findPosition(column);
528 // text.append(residue).append(" (")
529 // .append(residuePos).append(")");
534 ap.alignFrame.statusBar.setText(text.toString());
539 public void mouseEntered(MouseEvent evt)
541 ap.scalePanel.mouseEntered(evt);
545 public void mouseExited(MouseEvent evt)
547 ap.scalePanel.mouseExited(evt);
550 public int adjustPanelHeight()
552 return adjustPanelHeight(true);
555 public int adjustPanelHeight(boolean repaint)
557 int height = av.calcPanelHeight();
558 this.setSize(new Dimension(getSize().width, height));
567 * calculate the height for visible annotation, revalidating bounds where
568 * necessary ABSTRACT GUI METHOD
570 * @return total height of annotation
573 public void addEditableColumn(int i)
577 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
583 for (int j = 0; j < aa.length; j++)
595 public void update(Graphics g)
601 public void paint(Graphics g)
603 Dimension d = getSize();
605 // (av.endRes - av.startRes + 1) * av.charWidth;
606 if (imgWidth < 1 || d.height < 1)
610 if (image == null || imgWidth != image.getWidth(this)
611 || d.height != image.getHeight(this))
613 image = createImage(imgWidth, d.height);
614 gg = image.getGraphics();
615 gg.setFont(av.getFont());
616 fm = gg.getFontMetrics();
622 g.drawImage(image, 0, 0, this);
627 gg.setColor(Color.white);
628 gg.fillRect(0, 0, getSize().width, getSize().height);
629 drawComponent(gg, av.getRanges().getStartRes(), av.getRanges()
632 g.drawImage(image, 0, 0, this);
635 public void fastPaint(int horizontal)
638 || av.getAlignment().getAlignmentAnnotation() == null
639 || av.getAlignment().getAlignmentAnnotation().length < 1)
645 gg.copyArea(0, 0, imgWidth, getSize().height,
646 -horizontal * av.getCharWidth(), 0);
647 int sr = av.getRanges().getStartRes(), er = av.getRanges().getEndRes() + 1, transX = 0;
649 if (horizontal > 0) // scrollbar pulled right, image to the left
651 transX = (er - sr - horizontal) * av.getCharWidth();
652 sr = er - horizontal;
654 else if (horizontal < 0)
656 er = sr - horizontal;
659 gg.translate(transX, 0);
661 drawComponent(gg, sr, er);
663 gg.translate(-transX, 0);
679 public void drawComponent(Graphics g, int startRes, int endRes)
681 Font ofont = av.getFont();
684 g.setColor(Color.white);
685 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(),
690 fm = g.getFontMetrics();
693 if ((av.getAlignment().getAlignmentAnnotation() == null)
694 || (av.getAlignment().getAlignmentAnnotation().length < 1))
696 g.setColor(Color.white);
697 g.fillRect(0, 0, getSize().width, getSize().height);
698 g.setColor(Color.black);
699 if (av.validCharWidth)
701 g.drawString(MessageManager
702 .getString("label.alignment_has_no_annotations"), 20, 15);
707 g.translate(0, -scrollOffset);
708 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
709 g.translate(0, +scrollOffset);
712 int scrollOffset = 0;
714 public void setScrollOffset(int value, boolean repaint)
716 scrollOffset = value;
724 public FontMetrics getFontMetrics()
730 public Image getFadedImage()
736 public int getFadedImageWidth()
741 private int[] bounds = new int[2];
744 public int[] getVisibleVRange()
746 if (ap != null && ap.alabels != null)
748 int sOffset = -ap.alabels.scrollOffset;
749 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
751 bounds[1] = visHeight;
761 public void propertyChange(PropertyChangeEvent evt)
763 // Respond to viewport range changes (e.g. alignment panel was scrolled)
764 // Both scrolling and resizing change viewport ranges: scrolling changes
765 // both start and end points, but resize only changes end values.
766 // Here we only want to fastpaint on a scroll, with resize using a normal
767 // paint, so scroll events are identified as changes to the horizontal or
768 // vertical start value.
769 if (evt.getPropertyName().equals(ViewportRanges.STARTRES))
771 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());