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;
34 import java.awt.Color;
35 import java.awt.Dimension;
37 import java.awt.FontMetrics;
38 import java.awt.Graphics;
39 import java.awt.Image;
40 import java.awt.MenuItem;
41 import java.awt.Panel;
42 import java.awt.PopupMenu;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.awt.event.AdjustmentEvent;
46 import java.awt.event.AdjustmentListener;
47 import java.awt.event.InputEvent;
48 import java.awt.event.MouseEvent;
49 import java.awt.event.MouseListener;
50 import java.awt.event.MouseMotionListener;
51 import java.beans.PropertyChangeEvent;
53 public class AnnotationPanel extends Panel implements AwtRenderPanelI,
54 AdjustmentListener, ActionListener, MouseListener,
55 MouseMotionListener, ViewportListenerI
63 final String HELIX = "Helix";
65 final String SHEET = "Sheet";
68 * For RNA secondary structure "stems" aka helices
70 final String STEM = "RNA Helix";
72 final String LABEL = "Label";
74 final String REMOVE = "Remove Annotation";
76 final String COLOUR = "Colour";
78 final Color HELIX_COLOUR = Color.red.darker();
80 final Color SHEET_COLOUR = Color.green.darker().darker();
90 boolean fastPaint = false;
92 // Used For mouse Dragging and resizing graphs
93 int graphStretch = -1;
95 int graphStretchY = -1;
97 boolean mouseDragging = false;
99 public static int GRAPH_HEIGHT = 40;
103 public final AnnotationRenderer renderer;
105 public AnnotationPanel(AlignmentPanel ap)
107 new jalview.util.Platform();
108 MAC = Platform.isAMac();
112 int height = adjustPanelHeight();
113 ap.apvscroll.setValues(0, getSize().height, 0, height);
115 addMouseMotionListener(this);
117 addMouseListener(this);
119 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
120 renderer = new AnnotationRenderer();
123 public AnnotationPanel(AlignViewport av)
126 renderer = new AnnotationRenderer();
127 av.getRanges().addPropertyChangeListener(this);
131 public void adjustmentValueChanged(AdjustmentEvent evt)
142 public void actionPerformed(ActionEvent evt)
144 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
149 Annotation[] anot = aa[activeRow].annotations;
151 if (anot.length < av.getColumnSelection().getMax())
153 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
154 System.arraycopy(anot, 0, temp, 0, anot.length);
156 aa[activeRow].annotations = anot;
160 if (av.getColumnSelection() != null
161 && !av.getColumnSelection().isEmpty()
162 && anot[av.getColumnSelection().getMin()] != null)
164 label = anot[av.getColumnSelection().getMin()].displayCharacter;
167 if (evt.getActionCommand().equals(REMOVE))
169 for (int index : av.getColumnSelection().getSelected())
171 if (av.getAlignment().getHiddenColumns().isVisible(index))
177 else if (evt.getActionCommand().equals(LABEL))
179 label = enterLabel(label, "Enter Label");
186 if ((label.length() > 0) && !aa[activeRow].hasText)
188 aa[activeRow].hasText = true;
191 for (int index : av.getColumnSelection().getSelected())
193 // TODO: JAL-2001 - provide a fast method to list visible selected
195 if (!av.getAlignment().getHiddenColumns().isVisible(index))
200 if (anot[index] == null)
202 anot[index] = new Annotation(label, "", ' ', 0);
205 anot[index].displayCharacter = label;
208 else if (evt.getActionCommand().equals(COLOUR))
210 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
213 Color col = udc.getColor();
215 for (int index : av.getColumnSelection().getSelected())
217 if (!av.getAlignment().getHiddenColumns().isVisible(index))
222 if (anot[index] == null)
224 anot[index] = new Annotation("", "", ' ', 0);
227 anot[index].colour = col;
234 String symbol = "\u03B1";
236 if (evt.getActionCommand().equals(HELIX))
240 else if (evt.getActionCommand().equals(SHEET))
246 // Added by LML to color stems
247 else if (evt.getActionCommand().equals(STEM))
250 int column = av.getColumnSelection().getSelectedRanges().get(0)[0];
251 symbol = aa[activeRow].getDefaultRnaHelixSymbol(column);
254 if (!aa[activeRow].hasIcons)
256 aa[activeRow].hasIcons = true;
259 label = enterLabel(symbol, "Enter Label");
266 if ((label.length() > 0) && !aa[activeRow].hasText)
268 aa[activeRow].hasText = true;
269 if (evt.getActionCommand().equals(STEM))
271 aa[activeRow].showAllColLabels = true;
275 for (int index : av.getColumnSelection().getSelected())
277 if (!av.getAlignment().getHiddenColumns().isVisible(index))
282 if (anot[index] == null)
284 anot[index] = new Annotation(label, "", type, 0);
287 anot[index].secondaryStructure = type != 'S' ? type : label
288 .length() == 0 ? ' ' : label.charAt(0);
289 anot[index].displayCharacter = label;
293 av.getAlignment().validateAnnotation(aa[activeRow]);
295 ap.alignmentChanged();
302 String enterLabel(String text, String label)
304 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
305 ap.alignFrame, "Enter Label", 400, 200, true);
309 return dialog.getName();
318 public void mousePressed(MouseEvent evt)
320 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
326 int height = -scrollOffset;
329 for (int i = 0; i < aa.length; i++)
333 height += aa[i].height;
336 if (evt.getY() < height)
342 else if (aa[i].graph > 0)
346 graphStretchY = evt.getY();
353 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
356 if (av.getColumnSelection() == null
357 || av.getColumnSelection().isEmpty())
362 PopupMenu pop = new PopupMenu(
363 MessageManager.getString("label.structure_type"));
366 if (av.getAlignment().isNucleotide())
368 item = new MenuItem(STEM);
369 item.addActionListener(this);
374 item = new MenuItem(HELIX);
375 item.addActionListener(this);
377 item = new MenuItem(SHEET);
378 item.addActionListener(this);
381 item = new MenuItem(LABEL);
382 item.addActionListener(this);
384 item = new MenuItem(COLOUR);
385 item.addActionListener(this);
387 item = new MenuItem(REMOVE);
388 item.addActionListener(this);
390 ap.alignFrame.add(pop);
391 pop.show(this, evt.getX(), evt.getY());
396 ap.scalePanel.mousePressed(evt);
400 public void mouseReleased(MouseEvent evt)
404 mouseDragging = false;
408 needValidating = false;
410 ap.scalePanel.mouseReleased(evt);
414 public void mouseClicked(MouseEvent evt)
418 boolean needValidating = false;
421 public void mouseDragged(MouseEvent evt)
423 if (graphStretch > -1)
425 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
427 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
429 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
431 graphStretchY = evt.getY();
432 av.calcPanelHeight();
433 needValidating = true;
434 ap.paintAlignment(true);
438 ap.scalePanel.mouseDragged(evt);
443 public void mouseMoved(MouseEvent evt)
445 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
452 int height = -scrollOffset;
453 for (int i = 0; i < aa.length; i++)
458 height += aa[i].height;
461 if (evt.getY() < height)
468 int column = evt.getX() / av.getCharWidth()
469 + av.getRanges().getStartRes();
471 if (av.hasHiddenColumns())
473 column = av.getAlignment().getHiddenColumns()
474 .adjustForHiddenColumns(column);
477 if (row > -1 && column < aa[row].annotations.length
478 && aa[row].annotations[column] != null)
480 StringBuilder text = new StringBuilder();
481 text.append(MessageManager.getString("label.column")).append(" ")
483 String description = aa[row].annotations[column].description;
484 if (description != null && description.length() > 0)
486 text.append(" ").append(description);
490 * if the annotation is sequence-specific, show the sequence number
491 * in the alignment, and (if not a gap) the residue and position
493 SequenceI seqref = aa[row].sequenceRef;
496 int seqIndex = av.getAlignment().findIndex(seqref);
500 .append(MessageManager.getString("label.sequence"))
501 .append(" ").append(seqIndex + 1);
502 char residue = seqref.getCharAt(column);
503 if (!Comparison.isGap(residue))
507 if (av.getAlignment().isNucleotide())
509 name = ResidueProperties.nucleotideName.get(String
511 text.append(" Nucleotide: ").append(
512 name != null ? name : residue);
516 name = 'X' == residue ? "X" : ('*' == residue ? "STOP"
517 : ResidueProperties.aa2Triplet.get(String
519 text.append(" Residue: ").append(
520 name != null ? name : residue);
522 int residuePos = seqref.findPosition(column);
523 text.append(" (").append(residuePos).append(")");
524 // int residuePos = seqref.findPosition(column);
525 // text.append(residue).append(" (")
526 // .append(residuePos).append(")");
531 ap.alignFrame.statusBar.setText(text.toString());
536 public void mouseEntered(MouseEvent evt)
538 ap.scalePanel.mouseEntered(evt);
542 public void mouseExited(MouseEvent evt)
544 ap.scalePanel.mouseExited(evt);
547 public int adjustPanelHeight()
549 return adjustPanelHeight(true);
552 public int adjustPanelHeight(boolean repaint)
554 int height = av.calcPanelHeight();
555 this.setSize(new Dimension(getSize().width, height));
564 * calculate the height for visible annotation, revalidating bounds where
565 * necessary ABSTRACT GUI METHOD
567 * @return total height of annotation
570 public void addEditableColumn(int i)
574 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
580 for (int j = 0; j < aa.length; j++)
592 public void update(Graphics g)
598 public void paint(Graphics g)
600 Dimension d = getSize();
602 // (av.endRes - av.startRes + 1) * av.charWidth;
603 if (imgWidth < 1 || d.height < 1)
607 if (image == null || imgWidth != image.getWidth(this)
608 || d.height != image.getHeight(this))
610 image = createImage(imgWidth, d.height);
611 gg = image.getGraphics();
612 gg.setFont(av.getFont());
613 fm = gg.getFontMetrics();
619 g.drawImage(image, 0, 0, this);
624 gg.setColor(Color.white);
625 gg.fillRect(0, 0, getSize().width, getSize().height);
626 drawComponent(gg, av.getRanges().getStartRes(), av.getRanges()
629 g.drawImage(image, 0, 0, this);
632 public void fastPaint(int horizontal)
635 || av.getAlignment().getAlignmentAnnotation() == null
636 || av.getAlignment().getAlignmentAnnotation().length < 1)
642 gg.copyArea(0, 0, imgWidth, getSize().height,
643 -horizontal * av.getCharWidth(), 0);
644 int sr = av.getRanges().getStartRes(), er = av.getRanges().getEndRes() + 1, transX = 0;
646 if (horizontal > 0) // scrollbar pulled right, image to the left
648 transX = (er - sr - horizontal) * av.getCharWidth();
649 sr = er - horizontal;
651 else if (horizontal < 0)
653 er = sr - horizontal;
656 gg.translate(transX, 0);
658 drawComponent(gg, sr, er);
660 gg.translate(-transX, 0);
676 public void drawComponent(Graphics g, int startRes, int endRes)
678 Font ofont = av.getFont();
681 g.setColor(Color.white);
682 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(),
687 fm = g.getFontMetrics();
690 if ((av.getAlignment().getAlignmentAnnotation() == null)
691 || (av.getAlignment().getAlignmentAnnotation().length < 1))
693 g.setColor(Color.white);
694 g.fillRect(0, 0, getSize().width, getSize().height);
695 g.setColor(Color.black);
696 if (av.validCharWidth)
698 g.drawString(MessageManager
699 .getString("label.alignment_has_no_annotations"), 20, 15);
704 g.translate(0, -scrollOffset);
705 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
706 g.translate(0, +scrollOffset);
709 int scrollOffset = 0;
711 public void setScrollOffset(int value, boolean repaint)
713 scrollOffset = value;
721 public FontMetrics getFontMetrics()
727 public Image getFadedImage()
733 public int getFadedImageWidth()
738 private int[] bounds = new int[2];
741 public int[] getVisibleVRange()
743 if (ap != null && ap.alabels != null)
745 int sOffset = -ap.alabels.scrollOffset;
746 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
748 bounds[1] = visHeight;
758 public void propertyChange(PropertyChangeEvent evt)
760 // Respond to viewport range changes (e.g. alignment panel was scrolled)
761 if (evt.getPropertyName().equals("startres")
762 || evt.getPropertyName().equals("endres"))
764 fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());