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.util.Comparison;
29 import jalview.util.MessageManager;
31 import java.awt.Color;
32 import java.awt.Dimension;
34 import java.awt.FontMetrics;
35 import java.awt.Graphics;
36 import java.awt.Image;
37 import java.awt.MenuItem;
38 import java.awt.Panel;
39 import java.awt.PopupMenu;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.event.AdjustmentEvent;
43 import java.awt.event.AdjustmentListener;
44 import java.awt.event.InputEvent;
45 import java.awt.event.MouseEvent;
46 import java.awt.event.MouseListener;
47 import java.awt.event.MouseMotionListener;
49 public class AnnotationPanel extends Panel implements AwtRenderPanelI,
50 AdjustmentListener, ActionListener, MouseListener,
59 final String HELIX = "Helix";
61 final String SHEET = "Sheet";
64 * For RNA secondary structure "stems" aka helices
66 final String STEM = "RNA Helix";
68 final String LABEL = "Label";
70 final String REMOVE = "Remove Annotation";
72 final String COLOUR = "Colour";
74 final Color HELIX_COLOUR = Color.red.darker();
76 final Color SHEET_COLOUR = Color.green.darker().darker();
86 boolean fastPaint = false;
88 // Used For mouse Dragging and resizing graphs
89 int graphStretch = -1;
91 int graphStretchY = -1;
93 boolean mouseDragging = false;
95 public static int GRAPH_HEIGHT = 40;
99 public final AnnotationRenderer renderer;
101 public AnnotationPanel(AlignmentPanel ap)
103 MAC = new jalview.util.Platform().isAMac();
107 int height = adjustPanelHeight();
108 ap.apvscroll.setValues(0, getSize().height, 0, height);
110 addMouseMotionListener(this);
112 addMouseListener(this);
114 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
115 renderer = new AnnotationRenderer();
118 public AnnotationPanel(AlignViewport av)
121 renderer = new AnnotationRenderer();
125 public void adjustmentValueChanged(AdjustmentEvent evt)
136 public void actionPerformed(ActionEvent evt)
138 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
143 Annotation[] anot = aa[activeRow].annotations;
145 if (anot.length < av.getColumnSelection().getMax())
147 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
148 System.arraycopy(anot, 0, temp, 0, anot.length);
150 aa[activeRow].annotations = anot;
154 if (av.getColumnSelection() != null
155 && !av.getColumnSelection().isEmpty()
156 && anot[av.getColumnSelection().getMin()] != null)
158 label = anot[av.getColumnSelection().getMin()].displayCharacter;
161 if (evt.getActionCommand().equals(REMOVE))
163 for (int sel : av.getColumnSelection().getSelected())
165 // TODO: JAL-2001 check if applet has faulty 'REMOVE' selected columns
167 // annotation if selection includes hidden columns
171 else if (evt.getActionCommand().equals(LABEL))
173 label = enterLabel(label, "Enter Label");
180 if ((label.length() > 0) && !aa[activeRow].hasText)
182 aa[activeRow].hasText = true;
185 for (int index : av.getColumnSelection().getSelected())
187 // TODO: JAL-2001 - provide a fast method to list visible selected
189 if (!av.getColumnSelection().isVisible(index))
194 if (anot[index] == null)
196 anot[index] = new Annotation(label, "", ' ', 0);
199 anot[index].displayCharacter = label;
202 else if (evt.getActionCommand().equals(COLOUR))
204 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
207 Color col = udc.getColor();
209 for (int index : av.getColumnSelection().getSelected())
211 if (!av.getColumnSelection().isVisible(index))
216 if (anot[index] == null)
218 anot[index] = new Annotation("", "", ' ', 0);
221 anot[index].colour = col;
228 String symbol = "\u03B1";
230 if (evt.getActionCommand().equals(HELIX))
234 else if (evt.getActionCommand().equals(SHEET))
240 // Added by LML to color stems
241 else if (evt.getActionCommand().equals(STEM))
247 if (!aa[activeRow].hasIcons)
249 aa[activeRow].hasIcons = true;
252 label = enterLabel(symbol, "Enter Label");
259 if ((label.length() > 0) && !aa[activeRow].hasText)
261 aa[activeRow].hasText = true;
262 if (evt.getActionCommand().equals(STEM))
264 aa[activeRow].showAllColLabels = true;
268 for (int index : av.getColumnSelection().getSelected())
270 if (!av.getColumnSelection().isVisible(index))
275 if (anot[index] == null)
277 anot[index] = new Annotation(label, "", type, 0);
280 anot[index].secondaryStructure = type != 'S' ? type : label
281 .length() == 0 ? ' ' : label.charAt(0);
282 anot[index].displayCharacter = label;
286 av.getAlignment().validateAnnotation(aa[activeRow]);
288 ap.alignmentChanged();
295 String enterLabel(String text, String label)
297 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
298 ap.alignFrame, "Enter Label", 400, 200, true);
302 return dialog.getName();
311 public void mousePressed(MouseEvent evt)
313 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
319 int height = -scrollOffset;
322 for (int i = 0; i < aa.length; i++)
326 height += aa[i].height;
329 if (evt.getY() < height)
335 else if (aa[i].graph > 0)
339 graphStretchY = evt.getY();
346 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
349 if (av.getColumnSelection() == null)
354 PopupMenu pop = new PopupMenu(
355 MessageManager.getString("label.structure_type"));
358 * Just display the needed structure options
360 if (av.getAlignment().isNucleotide() == true)
362 item = new MenuItem(STEM);
363 item.addActionListener(this);
368 item = new MenuItem(HELIX);
369 item.addActionListener(this);
371 item = new MenuItem(SHEET);
372 item.addActionListener(this);
375 item = new MenuItem(LABEL);
376 item.addActionListener(this);
378 item = new MenuItem(COLOUR);
379 item.addActionListener(this);
381 item = new MenuItem(REMOVE);
382 item.addActionListener(this);
384 ap.alignFrame.add(pop);
385 pop.show(this, evt.getX(), evt.getY());
390 ap.scalePanel.mousePressed(evt);
394 public void mouseReleased(MouseEvent evt)
398 mouseDragging = false;
402 needValidating = false;
404 ap.scalePanel.mouseReleased(evt);
408 public void mouseClicked(MouseEvent evt)
412 boolean needValidating = false;
415 public void mouseDragged(MouseEvent evt)
417 if (graphStretch > -1)
419 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
421 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
423 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
425 graphStretchY = evt.getY();
426 av.calcPanelHeight();
427 needValidating = true;
428 ap.paintAlignment(true);
432 ap.scalePanel.mouseDragged(evt);
437 public void mouseMoved(MouseEvent evt)
439 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
446 int height = -scrollOffset;
447 for (int i = 0; i < aa.length; i++)
452 height += aa[i].height;
455 if (evt.getY() < height)
462 int column = evt.getX() / av.getCharWidth() + av.getStartRes();
464 if (av.hasHiddenColumns())
466 column = av.getColumnSelection().adjustForHiddenColumns(column);
469 if (row > -1 && column < aa[row].annotations.length
470 && aa[row].annotations[column] != null)
472 StringBuilder text = new StringBuilder();
473 text.append(MessageManager.getString("label.column")).append(" ")
475 if (aa[row].annotations[column].description != null)
477 text.append(" ").append(aa[row].annotations[column].description);
481 * if the annotation is sequence-specific, show the sequence number
482 * in the alignment, and (if not a gap) the residue and position
484 SequenceI seqref = aa[row].sequenceRef;
487 int seqIndex = av.getAlignment().findIndex(seqref);
491 .append(MessageManager.getString("label.sequence"))
492 .append(" ").append(seqIndex + 1);
493 char residue = seqref.getCharAt(column);
494 if (!Comparison.isGap(residue))
496 int residuePos = seqref.findPosition(column);
497 text.append(": ").append(residue).append(" (")
498 .append(residuePos).append(")");
503 ap.alignFrame.statusBar.setText(text.toString());
508 public void mouseEntered(MouseEvent evt)
510 ap.scalePanel.mouseEntered(evt);
514 public void mouseExited(MouseEvent evt)
516 ap.scalePanel.mouseExited(evt);
519 public int adjustPanelHeight()
521 return adjustPanelHeight(true);
524 public int adjustPanelHeight(boolean repaint)
526 int height = av.calcPanelHeight();
527 this.setSize(new Dimension(getSize().width, height));
536 * calculate the height for visible annotation, revalidating bounds where
537 * necessary ABSTRACT GUI METHOD
539 * @return total height of annotation
542 public void addEditableColumn(int i)
546 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
552 for (int j = 0; j < aa.length; j++)
564 public void update(Graphics g)
570 public void paint(Graphics g)
572 Dimension d = getSize();
574 // (av.endRes - av.startRes + 1) * av.charWidth;
575 if (imgWidth < 1 || d.height < 1)
579 if (image == null || imgWidth != image.getWidth(this)
580 || d.height != image.getHeight(this))
582 image = createImage(imgWidth, d.height);
583 gg = image.getGraphics();
584 gg.setFont(av.getFont());
585 fm = gg.getFontMetrics();
591 g.drawImage(image, 0, 0, this);
596 gg.setColor(Color.white);
597 gg.fillRect(0, 0, getSize().width, getSize().height);
598 drawComponent(gg, av.startRes, av.endRes + 1);
600 g.drawImage(image, 0, 0, this);
603 public void fastPaint(int horizontal)
606 || av.getAlignment().getAlignmentAnnotation() == null
607 || av.getAlignment().getAlignmentAnnotation().length < 1)
613 gg.copyArea(0, 0, imgWidth, getSize().height,
614 -horizontal * av.getCharWidth(), 0);
615 int sr = av.startRes, er = av.endRes + 1, transX = 0;
617 if (horizontal > 0) // scrollbar pulled right, image to the left
619 transX = (er - sr - horizontal) * av.getCharWidth();
620 sr = er - horizontal;
622 else if (horizontal < 0)
624 er = sr - horizontal;
627 gg.translate(transX, 0);
629 drawComponent(gg, sr, er);
631 gg.translate(-transX, 0);
647 public void drawComponent(Graphics g, int startRes, int endRes)
649 Font ofont = av.getFont();
652 g.setColor(Color.white);
653 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(),
658 fm = g.getFontMetrics();
661 if ((av.getAlignment().getAlignmentAnnotation() == null)
662 || (av.getAlignment().getAlignmentAnnotation().length < 1))
664 g.setColor(Color.white);
665 g.fillRect(0, 0, getSize().width, getSize().height);
666 g.setColor(Color.black);
667 if (av.validCharWidth)
669 g.drawString(MessageManager
670 .getString("label.alignment_has_no_annotations"), 20, 15);
675 g.translate(0, -scrollOffset);
676 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
677 g.translate(0, +scrollOffset);
680 int scrollOffset = 0;
682 public void setScrollOffset(int value, boolean repaint)
684 scrollOffset = value;
692 public FontMetrics getFontMetrics()
698 public Image getFadedImage()
704 public int getFadedImageWidth()
709 private int[] bounds = new int[2];
712 public int[] getVisibleVRange()
714 if (ap != null && ap.alabels != null)
716 int sOffset = -ap.alabels.scrollOffset;
717 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
719 bounds[1] = visHeight;