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;
32 import java.awt.Color;
33 import java.awt.Dimension;
35 import java.awt.FontMetrics;
36 import java.awt.Graphics;
37 import java.awt.Image;
38 import java.awt.MenuItem;
39 import java.awt.Panel;
40 import java.awt.PopupMenu;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.AdjustmentEvent;
44 import java.awt.event.AdjustmentListener;
45 import java.awt.event.InputEvent;
46 import java.awt.event.MouseEvent;
47 import java.awt.event.MouseListener;
48 import java.awt.event.MouseMotionListener;
50 public class AnnotationPanel extends Panel implements AwtRenderPanelI,
51 AdjustmentListener, ActionListener, MouseListener,
60 final String HELIX = "Helix";
62 final String SHEET = "Sheet";
65 * For RNA secondary structure "stems" aka helices
67 final String STEM = "RNA Helix";
69 final String LABEL = "Label";
71 final String REMOVE = "Remove Annotation";
73 final String COLOUR = "Colour";
75 final Color HELIX_COLOUR = Color.red.darker();
77 final Color SHEET_COLOUR = Color.green.darker().darker();
87 boolean fastPaint = false;
89 // Used For mouse Dragging and resizing graphs
90 int graphStretch = -1;
92 int graphStretchY = -1;
94 boolean mouseDragging = false;
96 public static int GRAPH_HEIGHT = 40;
100 public final AnnotationRenderer renderer;
102 public AnnotationPanel(AlignmentPanel ap)
104 MAC = new jalview.util.Platform().isAMac();
108 int height = adjustPanelHeight();
109 ap.apvscroll.setValues(0, getSize().height, 0, height);
111 addMouseMotionListener(this);
113 addMouseListener(this);
115 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
116 renderer = new AnnotationRenderer();
119 public AnnotationPanel(AlignViewport av)
122 renderer = new AnnotationRenderer();
126 public void adjustmentValueChanged(AdjustmentEvent evt)
137 public void actionPerformed(ActionEvent evt)
139 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
144 Annotation[] anot = aa[activeRow].annotations;
146 if (anot.length < av.getColumnSelection().getMax())
148 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
149 System.arraycopy(anot, 0, temp, 0, anot.length);
151 aa[activeRow].annotations = anot;
155 if (av.getColumnSelection() != null
156 && !av.getColumnSelection().isEmpty()
157 && anot[av.getColumnSelection().getMin()] != null)
159 label = anot[av.getColumnSelection().getMin()].displayCharacter;
162 if (evt.getActionCommand().equals(REMOVE))
164 for (int index : av.getColumnSelection().getSelected())
166 if (av.getColumnSelection().isVisible(index))
172 else if (evt.getActionCommand().equals(LABEL))
174 label = enterLabel(label, "Enter Label");
181 if ((label.length() > 0) && !aa[activeRow].hasText)
183 aa[activeRow].hasText = true;
186 for (int index : av.getColumnSelection().getSelected())
188 // TODO: JAL-2001 - provide a fast method to list visible selected
190 if (!av.getColumnSelection().isVisible(index))
195 if (anot[index] == null)
197 anot[index] = new Annotation(label, "", ' ', 0);
200 anot[index].displayCharacter = label;
203 else if (evt.getActionCommand().equals(COLOUR))
205 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
208 Color col = udc.getColor();
210 for (int index : av.getColumnSelection().getSelected())
212 if (!av.getColumnSelection().isVisible(index))
217 if (anot[index] == null)
219 anot[index] = new Annotation("", "", ' ', 0);
222 anot[index].colour = col;
229 String symbol = "\u03B1";
231 if (evt.getActionCommand().equals(HELIX))
235 else if (evt.getActionCommand().equals(SHEET))
241 // Added by LML to color stems
242 else if (evt.getActionCommand().equals(STEM))
248 if (!aa[activeRow].hasIcons)
250 aa[activeRow].hasIcons = true;
253 label = enterLabel(symbol, "Enter Label");
260 if ((label.length() > 0) && !aa[activeRow].hasText)
262 aa[activeRow].hasText = true;
263 if (evt.getActionCommand().equals(STEM))
265 aa[activeRow].showAllColLabels = true;
269 for (int index : av.getColumnSelection().getSelected())
271 if (!av.getColumnSelection().isVisible(index))
276 if (anot[index] == null)
278 anot[index] = new Annotation(label, "", type, 0);
281 anot[index].secondaryStructure = type != 'S' ? type : label
282 .length() == 0 ? ' ' : label.charAt(0);
283 anot[index].displayCharacter = label;
287 av.getAlignment().validateAnnotation(aa[activeRow]);
289 ap.alignmentChanged();
296 String enterLabel(String text, String label)
298 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
299 ap.alignFrame, "Enter Label", 400, 200, true);
303 return dialog.getName();
312 public void mousePressed(MouseEvent evt)
314 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
320 int height = -scrollOffset;
323 for (int i = 0; i < aa.length; i++)
327 height += aa[i].height;
330 if (evt.getY() < height)
336 else if (aa[i].graph > 0)
340 graphStretchY = evt.getY();
347 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
350 if (av.getColumnSelection() == null)
355 PopupMenu pop = new PopupMenu(
356 MessageManager.getString("label.structure_type"));
359 * Just display the needed structure options
361 if (av.getAlignment().isNucleotide() == true)
363 item = new MenuItem(STEM);
364 item.addActionListener(this);
369 item = new MenuItem(HELIX);
370 item.addActionListener(this);
372 item = new MenuItem(SHEET);
373 item.addActionListener(this);
376 item = new MenuItem(LABEL);
377 item.addActionListener(this);
379 item = new MenuItem(COLOUR);
380 item.addActionListener(this);
382 item = new MenuItem(REMOVE);
383 item.addActionListener(this);
385 ap.alignFrame.add(pop);
386 pop.show(this, evt.getX(), evt.getY());
391 ap.scalePanel.mousePressed(evt);
395 public void mouseReleased(MouseEvent evt)
399 mouseDragging = false;
403 needValidating = false;
405 ap.scalePanel.mouseReleased(evt);
409 public void mouseClicked(MouseEvent evt)
413 boolean needValidating = false;
416 public void mouseDragged(MouseEvent evt)
418 if (graphStretch > -1)
420 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
422 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
424 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
426 graphStretchY = evt.getY();
427 av.calcPanelHeight();
428 needValidating = true;
429 ap.paintAlignment(true);
433 ap.scalePanel.mouseDragged(evt);
438 public void mouseMoved(MouseEvent evt)
440 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
447 int height = -scrollOffset;
448 for (int i = 0; i < aa.length; i++)
453 height += aa[i].height;
456 if (evt.getY() < height)
463 int column = evt.getX() / av.getCharWidth() + av.getStartRes();
465 if (av.hasHiddenColumns())
467 column = av.getColumnSelection().adjustForHiddenColumns(column);
470 if (row > -1 && column < aa[row].annotations.length
471 && aa[row].annotations[column] != null)
473 StringBuilder text = new StringBuilder();
474 text.append(MessageManager.getString("label.column")).append(" ")
476 String description = aa[row].annotations[column].description;
477 if (description != null && description.length() > 0)
479 text.append(" ").append(description);
483 * if the annotation is sequence-specific, show the sequence number
484 * in the alignment, and (if not a gap) the residue and position
486 SequenceI seqref = aa[row].sequenceRef;
489 int seqIndex = av.getAlignment().findIndex(seqref);
493 .append(MessageManager.getString("label.sequence"))
494 .append(" ").append(seqIndex + 1);
495 char residue = seqref.getCharAt(column);
496 if (!Comparison.isGap(residue))
500 if (av.getAlignment().isNucleotide())
502 name = ResidueProperties.nucleotideName.get(String
504 text.append(" Nucleotide: ").append(
505 name != null ? name : residue);
509 name = 'X' == residue ? "X" : ('*' == residue ? "STOP"
510 : ResidueProperties.aa2Triplet.get(String
512 text.append(" Residue: ").append(
513 name != null ? name : residue);
515 int residuePos = seqref.findPosition(column);
516 text.append(" (").append(residuePos).append(")");
517 // int residuePos = seqref.findPosition(column);
518 // text.append(residue).append(" (")
519 // .append(residuePos).append(")");
524 ap.alignFrame.statusBar.setText(text.toString());
529 public void mouseEntered(MouseEvent evt)
531 ap.scalePanel.mouseEntered(evt);
535 public void mouseExited(MouseEvent evt)
537 ap.scalePanel.mouseExited(evt);
540 public int adjustPanelHeight()
542 return adjustPanelHeight(true);
545 public int adjustPanelHeight(boolean repaint)
547 int height = av.calcPanelHeight();
548 this.setSize(new Dimension(getSize().width, height));
557 * calculate the height for visible annotation, revalidating bounds where
558 * necessary ABSTRACT GUI METHOD
560 * @return total height of annotation
563 public void addEditableColumn(int i)
567 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
573 for (int j = 0; j < aa.length; j++)
585 public void update(Graphics g)
591 public void paint(Graphics g)
593 Dimension d = getSize();
595 // (av.endRes - av.startRes + 1) * av.charWidth;
596 if (imgWidth < 1 || d.height < 1)
600 if (image == null || imgWidth != image.getWidth(this)
601 || d.height != image.getHeight(this))
603 image = createImage(imgWidth, d.height);
604 gg = image.getGraphics();
605 gg.setFont(av.getFont());
606 fm = gg.getFontMetrics();
612 g.drawImage(image, 0, 0, this);
617 gg.setColor(Color.white);
618 gg.fillRect(0, 0, getSize().width, getSize().height);
619 drawComponent(gg, av.startRes, av.endRes + 1);
621 g.drawImage(image, 0, 0, this);
624 public void fastPaint(int horizontal)
627 || av.getAlignment().getAlignmentAnnotation() == null
628 || av.getAlignment().getAlignmentAnnotation().length < 1)
634 gg.copyArea(0, 0, imgWidth, getSize().height,
635 -horizontal * av.getCharWidth(), 0);
636 int sr = av.startRes, er = av.endRes + 1, transX = 0;
638 if (horizontal > 0) // scrollbar pulled right, image to the left
640 transX = (er - sr - horizontal) * av.getCharWidth();
641 sr = er - horizontal;
643 else if (horizontal < 0)
645 er = sr - horizontal;
648 gg.translate(transX, 0);
650 drawComponent(gg, sr, er);
652 gg.translate(-transX, 0);
668 public void drawComponent(Graphics g, int startRes, int endRes)
670 Font ofont = av.getFont();
673 g.setColor(Color.white);
674 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(),
679 fm = g.getFontMetrics();
682 if ((av.getAlignment().getAlignmentAnnotation() == null)
683 || (av.getAlignment().getAlignmentAnnotation().length < 1))
685 g.setColor(Color.white);
686 g.fillRect(0, 0, getSize().width, getSize().height);
687 g.setColor(Color.black);
688 if (av.validCharWidth)
690 g.drawString(MessageManager
691 .getString("label.alignment_has_no_annotations"), 20, 15);
696 g.translate(0, -scrollOffset);
697 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
698 g.translate(0, +scrollOffset);
701 int scrollOffset = 0;
703 public void setScrollOffset(int value, boolean repaint)
705 scrollOffset = value;
713 public FontMetrics getFontMetrics()
719 public Image getFadedImage()
725 public int getFadedImageWidth()
730 private int[] bounds = new int[2];
733 public int[] getVisibleVRange()
735 if (ap != null && ap.alabels != null)
737 int sOffset = -ap.alabels.scrollOffset;
738 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
740 bounds[1] = visHeight;