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())
163 // TODO: JAL-2001 check if applet has faulty 'REMOVE' selected columns
165 // annotation if selection includes hidden columns
169 else if (evt.getActionCommand().equals(LABEL))
171 label = enterLabel(label, "Enter Label");
178 if ((label.length() > 0) && !aa[activeRow].hasText)
180 aa[activeRow].hasText = true;
183 for (int index : av.getColumnSelection().getSelected())
185 // TODO: JAL-2001 - provide a fast method to list visible selected
187 if (!av.getColumnSelection().isVisible(index))
192 if (anot[index] == null)
194 anot[index] = new Annotation(label, "", ' ', 0);
197 anot[index].displayCharacter = label;
200 else if (evt.getActionCommand().equals(COLOUR))
202 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
205 Color col = udc.getColor();
207 for (int index : av.getColumnSelection().getSelected())
209 if (!av.getColumnSelection().isVisible(index))
214 if (anot[index] == null)
216 anot[index] = new Annotation("", "", ' ', 0);
219 anot[index].colour = col;
226 String symbol = "\u03B1";
228 if (evt.getActionCommand().equals(HELIX))
232 else if (evt.getActionCommand().equals(SHEET))
238 // Added by LML to color stems
239 else if (evt.getActionCommand().equals(STEM))
245 if (!aa[activeRow].hasIcons)
247 aa[activeRow].hasIcons = true;
250 label = enterLabel(symbol, "Enter Label");
257 if ((label.length() > 0) && !aa[activeRow].hasText)
259 aa[activeRow].hasText = true;
260 if (evt.getActionCommand().equals(STEM))
262 aa[activeRow].showAllColLabels = true;
266 for (int index : av.getColumnSelection().getSelected())
268 if (!av.getColumnSelection().isVisible(index))
273 if (anot[index] == null)
275 anot[index] = new Annotation(label, "", type, 0);
278 anot[index].secondaryStructure = type != 'S' ? type : label
279 .length() == 0 ? ' ' : label.charAt(0);
280 anot[index].displayCharacter = label;
284 av.getAlignment().validateAnnotation(aa[activeRow]);
286 ap.alignmentChanged();
293 String enterLabel(String text, String label)
295 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
296 ap.alignFrame, "Enter Label", 400, 200, true);
300 return dialog.getName();
309 public void mousePressed(MouseEvent evt)
311 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
317 int height = -scrollOffset;
320 for (int i = 0; i < aa.length; i++)
324 height += aa[i].height;
327 if (evt.getY() < height)
333 else if (aa[i].graph > 0)
337 graphStretchY = evt.getY();
344 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
347 if (av.getColumnSelection() == null)
352 PopupMenu pop = new PopupMenu(
353 MessageManager.getString("label.structure_type"));
356 * Just display the needed structure options
358 if (av.getAlignment().isNucleotide() == true)
360 item = new MenuItem(STEM);
361 item.addActionListener(this);
366 item = new MenuItem(HELIX);
367 item.addActionListener(this);
369 item = new MenuItem(SHEET);
370 item.addActionListener(this);
373 item = new MenuItem(LABEL);
374 item.addActionListener(this);
376 item = new MenuItem(COLOUR);
377 item.addActionListener(this);
379 item = new MenuItem(REMOVE);
380 item.addActionListener(this);
382 ap.alignFrame.add(pop);
383 pop.show(this, evt.getX(), evt.getY());
388 ap.scalePanel.mousePressed(evt);
392 public void mouseReleased(MouseEvent evt)
396 mouseDragging = false;
400 needValidating = false;
402 ap.scalePanel.mouseReleased(evt);
406 public void mouseClicked(MouseEvent evt)
410 boolean needValidating = false;
413 public void mouseDragged(MouseEvent evt)
415 if (graphStretch > -1)
417 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
419 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
421 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
423 graphStretchY = evt.getY();
424 av.calcPanelHeight();
425 needValidating = true;
426 ap.paintAlignment(true);
430 ap.scalePanel.mouseDragged(evt);
435 public void mouseMoved(MouseEvent evt)
437 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
444 int height = -scrollOffset;
445 for (int i = 0; i < aa.length; i++)
450 height += aa[i].height;
453 if (evt.getY() < height)
460 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
462 if (av.hasHiddenColumns())
464 res = av.getColumnSelection().adjustForHiddenColumns(res);
467 if (row > -1 && res < aa[row].annotations.length
468 && aa[row].annotations[res] != null)
470 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
471 if (aa[row].annotations[res].description != null)
473 text.append(" " + aa[row].annotations[res].description);
475 ap.alignFrame.statusBar.setText(text.toString());
480 public void mouseEntered(MouseEvent evt)
482 ap.scalePanel.mouseEntered(evt);
486 public void mouseExited(MouseEvent evt)
488 ap.scalePanel.mouseExited(evt);
491 public int adjustPanelHeight()
493 return adjustPanelHeight(true);
496 public int adjustPanelHeight(boolean repaint)
498 int height = av.calcPanelHeight();
499 this.setSize(new Dimension(getSize().width, height));
508 * calculate the height for visible annotation, revalidating bounds where
509 * necessary ABSTRACT GUI METHOD
511 * @return total height of annotation
514 public void addEditableColumn(int i)
518 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
524 for (int j = 0; j < aa.length; j++)
536 public void update(Graphics g)
542 public void paint(Graphics g)
544 Dimension d = getSize();
546 // (av.endRes - av.startRes + 1) * av.charWidth;
547 if (imgWidth < 1 || d.height < 1)
551 if (image == null || imgWidth != image.getWidth(this)
552 || d.height != image.getHeight(this))
554 image = createImage(imgWidth, d.height);
555 gg = image.getGraphics();
556 gg.setFont(av.getFont());
557 fm = gg.getFontMetrics();
563 g.drawImage(image, 0, 0, this);
568 gg.setColor(Color.white);
569 gg.fillRect(0, 0, getSize().width, getSize().height);
570 drawComponent(gg, av.startRes, av.endRes + 1);
572 g.drawImage(image, 0, 0, this);
575 public void fastPaint(int horizontal)
578 || av.getAlignment().getAlignmentAnnotation() == null
579 || av.getAlignment().getAlignmentAnnotation().length < 1)
585 gg.copyArea(0, 0, imgWidth, getSize().height,
586 -horizontal * av.getCharWidth(), 0);
587 int sr = av.startRes, er = av.endRes + 1, transX = 0;
589 if (horizontal > 0) // scrollbar pulled right, image to the left
591 transX = (er - sr - horizontal) * av.getCharWidth();
592 sr = er - horizontal;
594 else if (horizontal < 0)
596 er = sr - horizontal;
599 gg.translate(transX, 0);
601 drawComponent(gg, sr, er);
603 gg.translate(-transX, 0);
619 public void drawComponent(Graphics g, int startRes, int endRes)
621 Font ofont = av.getFont();
624 g.setColor(Color.white);
625 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(),
630 fm = g.getFontMetrics();
633 if ((av.getAlignment().getAlignmentAnnotation() == null)
634 || (av.getAlignment().getAlignmentAnnotation().length < 1))
636 g.setColor(Color.white);
637 g.fillRect(0, 0, getSize().width, getSize().height);
638 g.setColor(Color.black);
639 if (av.validCharWidth)
641 g.drawString(MessageManager
642 .getString("label.alignment_has_no_annotations"), 20, 15);
647 g.translate(0, -scrollOffset);
648 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
649 g.translate(0, +scrollOffset);
652 int scrollOffset = 0;
654 public void setScrollOffset(int value, boolean repaint)
656 scrollOffset = value;
664 public FontMetrics getFontMetrics()
670 public Image getFadedImage()
676 public int getFadedImageWidth()
681 private int[] bounds = new int[2];
684 public int[] getVisibleVRange()
686 if (ap != null && ap.alabels != null)
688 int sOffset = -ap.alabels.scrollOffset;
689 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
691 bounds[1] = visHeight;