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.renderer.AnnotationRenderer;
26 import jalview.renderer.AwtRenderPanelI;
27 import jalview.util.MessageManager;
29 import java.awt.Color;
30 import java.awt.Dimension;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
34 import java.awt.Image;
35 import java.awt.MenuItem;
36 import java.awt.Panel;
37 import java.awt.PopupMenu;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.AdjustmentEvent;
41 import java.awt.event.AdjustmentListener;
42 import java.awt.event.InputEvent;
43 import java.awt.event.MouseEvent;
44 import java.awt.event.MouseListener;
45 import java.awt.event.MouseMotionListener;
47 public class AnnotationPanel extends Panel implements AwtRenderPanelI,
48 AdjustmentListener, ActionListener, MouseListener,
57 final String HELIX = "Helix";
59 final String SHEET = "Sheet";
62 * For RNA secondary structure "stems" aka helices
64 final String STEM = "RNA Helix";
66 final String LABEL = "Label";
68 final String REMOVE = "Remove Annotation";
70 final String COLOUR = "Colour";
72 final Color HELIX_COLOUR = Color.red.darker();
74 final Color SHEET_COLOUR = Color.green.darker().darker();
84 boolean fastPaint = false;
86 // Used For mouse Dragging and resizing graphs
87 int graphStretch = -1;
89 int graphStretchY = -1;
91 boolean mouseDragging = false;
93 public static int GRAPH_HEIGHT = 40;
97 public final AnnotationRenderer renderer;
99 public AnnotationPanel(AlignmentPanel ap)
101 MAC = new jalview.util.Platform().isAMac();
105 int height = adjustPanelHeight();
106 ap.apvscroll.setValues(0, getSize().height, 0, height);
108 addMouseMotionListener(this);
110 addMouseListener(this);
112 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
113 renderer = new AnnotationRenderer();
116 public AnnotationPanel(AlignViewport av)
119 renderer = new AnnotationRenderer();
123 public void adjustmentValueChanged(AdjustmentEvent evt)
134 public void actionPerformed(ActionEvent evt)
136 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
141 Annotation[] anot = aa[activeRow].annotations;
143 if (anot.length < av.getColumnSelection().getMax())
145 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
146 System.arraycopy(anot, 0, temp, 0, anot.length);
148 aa[activeRow].annotations = anot;
152 if (av.getColumnSelection() != null
153 && !av.getColumnSelection().isEmpty()
154 && anot[av.getColumnSelection().getMin()] != null)
156 label = anot[av.getColumnSelection().getMin()].displayCharacter;
159 if (evt.getActionCommand().equals(REMOVE))
161 for (int sel : av.getColumnSelection().getSelected())
166 else if (evt.getActionCommand().equals(LABEL))
168 label = enterLabel(label, "Enter Label");
175 if ((label.length() > 0) && !aa[activeRow].hasText)
177 aa[activeRow].hasText = true;
180 for (int index : av.getColumnSelection().getSelected())
182 if (!av.getColumnSelection().isVisible(index))
187 if (anot[index] == null)
189 anot[index] = new Annotation(label, "", ' ', 0);
192 anot[index].displayCharacter = label;
195 else if (evt.getActionCommand().equals(COLOUR))
197 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
200 Color col = udc.getColor();
202 for (int index : av.getColumnSelection().getSelected())
204 if (!av.getColumnSelection().isVisible(index))
209 if (anot[index] == null)
211 anot[index] = new Annotation("", "", ' ', 0);
214 anot[index].colour = col;
221 String symbol = "\u03B1";
223 if (evt.getActionCommand().equals(HELIX))
227 else if (evt.getActionCommand().equals(SHEET))
233 // Added by LML to color stems
234 else if (evt.getActionCommand().equals(STEM))
240 if (!aa[activeRow].hasIcons)
242 aa[activeRow].hasIcons = true;
245 label = enterLabel(symbol, "Enter Label");
252 if ((label.length() > 0) && !aa[activeRow].hasText)
254 aa[activeRow].hasText = true;
255 if (evt.getActionCommand().equals(STEM))
257 aa[activeRow].showAllColLabels = true;
261 for (int index : av.getColumnSelection().getSelected())
263 if (!av.getColumnSelection().isVisible(index))
268 if (anot[index] == null)
270 anot[index] = new Annotation(label, "", type, 0);
273 anot[index].secondaryStructure = type != 'S' ? type : label
274 .length() == 0 ? ' ' : label.charAt(0);
275 anot[index].displayCharacter = label;
279 av.getAlignment().validateAnnotation(aa[activeRow]);
281 ap.alignmentChanged();
288 String enterLabel(String text, String label)
290 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
291 ap.alignFrame, "Enter Label", 400, 200, true);
295 return dialog.getName();
304 public void mousePressed(MouseEvent evt)
306 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
312 int height = -scrollOffset;
315 for (int i = 0; i < aa.length; i++)
319 height += aa[i].height;
322 if (evt.getY() < height)
328 else if (aa[i].graph > 0)
332 graphStretchY = evt.getY();
339 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
342 if (av.getColumnSelection() == null)
347 PopupMenu pop = new PopupMenu(
348 MessageManager.getString("label.structure_type"));
351 * Just display the needed structure options
353 if (av.getAlignment().isNucleotide() == true)
355 item = new MenuItem(STEM);
356 item.addActionListener(this);
361 item = new MenuItem(HELIX);
362 item.addActionListener(this);
364 item = new MenuItem(SHEET);
365 item.addActionListener(this);
368 item = new MenuItem(LABEL);
369 item.addActionListener(this);
371 item = new MenuItem(COLOUR);
372 item.addActionListener(this);
374 item = new MenuItem(REMOVE);
375 item.addActionListener(this);
377 ap.alignFrame.add(pop);
378 pop.show(this, evt.getX(), evt.getY());
383 ap.scalePanel.mousePressed(evt);
387 public void mouseReleased(MouseEvent evt)
391 mouseDragging = false;
395 needValidating = false;
397 ap.scalePanel.mouseReleased(evt);
401 public void mouseClicked(MouseEvent evt)
405 boolean needValidating = false;
408 public void mouseDragged(MouseEvent evt)
410 if (graphStretch > -1)
412 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
414 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
416 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
418 graphStretchY = evt.getY();
419 av.calcPanelHeight();
420 needValidating = true;
421 ap.paintAlignment(true);
425 ap.scalePanel.mouseDragged(evt);
430 public void mouseMoved(MouseEvent evt)
432 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
439 int height = -scrollOffset;
440 for (int i = 0; i < aa.length; i++)
445 height += aa[i].height;
448 if (evt.getY() < height)
455 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
457 if (av.hasHiddenColumns())
459 res = av.getColumnSelection().adjustForHiddenColumns(res);
462 if (row > -1 && res < aa[row].annotations.length
463 && aa[row].annotations[res] != null)
465 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
466 if (aa[row].annotations[res].description != null)
468 text.append(" " + aa[row].annotations[res].description);
470 ap.alignFrame.statusBar.setText(text.toString());
475 public void mouseEntered(MouseEvent evt)
477 ap.scalePanel.mouseEntered(evt);
481 public void mouseExited(MouseEvent evt)
483 ap.scalePanel.mouseExited(evt);
486 public int adjustPanelHeight()
488 return adjustPanelHeight(true);
491 public int adjustPanelHeight(boolean repaint)
493 int height = av.calcPanelHeight();
494 this.setSize(new Dimension(getSize().width, height));
503 * calculate the height for visible annotation, revalidating bounds where
504 * necessary ABSTRACT GUI METHOD
506 * @return total height of annotation
509 public void addEditableColumn(int i)
513 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
519 for (int j = 0; j < aa.length; j++)
531 public void update(Graphics g)
537 public void paint(Graphics g)
539 Dimension d = getSize();
541 // (av.endRes - av.startRes + 1) * av.charWidth;
542 if (imgWidth < 1 || d.height < 1)
546 if (image == null || imgWidth != image.getWidth(this)
547 || d.height != image.getHeight(this))
549 image = createImage(imgWidth, d.height);
550 gg = image.getGraphics();
551 gg.setFont(av.getFont());
552 fm = gg.getFontMetrics();
558 g.drawImage(image, 0, 0, this);
563 gg.setColor(Color.white);
564 gg.fillRect(0, 0, getSize().width, getSize().height);
565 drawComponent(gg, av.startRes, av.endRes + 1);
567 g.drawImage(image, 0, 0, this);
570 public void fastPaint(int horizontal)
573 || av.getAlignment().getAlignmentAnnotation() == null
574 || av.getAlignment().getAlignmentAnnotation().length < 1)
580 gg.copyArea(0, 0, imgWidth, getSize().height,
581 -horizontal * av.getCharWidth(), 0);
582 int sr = av.startRes, er = av.endRes + 1, transX = 0;
584 if (horizontal > 0) // scrollbar pulled right, image to the left
586 transX = (er - sr - horizontal) * av.getCharWidth();
587 sr = er - horizontal;
589 else if (horizontal < 0)
591 er = sr - horizontal;
594 gg.translate(transX, 0);
596 drawComponent(gg, sr, er);
598 gg.translate(-transX, 0);
614 public void drawComponent(Graphics g, int startRes, int endRes)
616 Font ofont = av.getFont();
619 g.setColor(Color.white);
620 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(),
625 fm = g.getFontMetrics();
628 if ((av.getAlignment().getAlignmentAnnotation() == null)
629 || (av.getAlignment().getAlignmentAnnotation().length < 1))
631 g.setColor(Color.white);
632 g.fillRect(0, 0, getSize().width, getSize().height);
633 g.setColor(Color.black);
634 if (av.validCharWidth)
636 g.drawString(MessageManager
637 .getString("label.alignment_has_no_annotations"), 20, 15);
642 g.translate(0, -scrollOffset);
643 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
644 g.translate(0, +scrollOffset);
647 int scrollOffset = 0;
649 public void setScrollOffset(int value, boolean repaint)
651 scrollOffset = value;
659 public FontMetrics getFontMetrics()
665 public Image getFadedImage()
671 public int getFadedImageWidth()
676 private int[] bounds = new int[2];
679 public int[] getVisibleVRange()
681 if (ap != null && ap.alabels != null)
683 int sOffset = -ap.alabels.scrollOffset;
684 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
686 bounds[1] = visHeight;