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;
30 import jalview.util.Platform;
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 new jalview.util.Platform();
105 MAC = Platform.isAMac();
109 int height = adjustPanelHeight();
110 ap.apvscroll.setValues(0, getSize().height, 0, height);
112 addMouseMotionListener(this);
114 addMouseListener(this);
116 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
117 renderer = new AnnotationRenderer();
120 public AnnotationPanel(AlignViewport av)
123 renderer = new AnnotationRenderer();
127 public void adjustmentValueChanged(AdjustmentEvent evt)
138 public void actionPerformed(ActionEvent evt)
140 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
145 Annotation[] anot = aa[activeRow].annotations;
147 if (anot.length < av.getColumnSelection().getMax())
149 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
150 System.arraycopy(anot, 0, temp, 0, anot.length);
152 aa[activeRow].annotations = anot;
156 if (av.getColumnSelection() != null
157 && !av.getColumnSelection().isEmpty()
158 && anot[av.getColumnSelection().getMin()] != null)
160 label = anot[av.getColumnSelection().getMin()].displayCharacter;
163 if (evt.getActionCommand().equals(REMOVE))
165 for (int sel : av.getColumnSelection().getSelected())
167 // TODO: JAL-2001 check if applet has faulty 'REMOVE' selected columns
169 // annotation if selection includes hidden columns
173 else if (evt.getActionCommand().equals(LABEL))
175 label = enterLabel(label, "Enter Label");
182 if ((label.length() > 0) && !aa[activeRow].hasText)
184 aa[activeRow].hasText = true;
187 for (int index : av.getColumnSelection().getSelected())
189 // TODO: JAL-2001 - provide a fast method to list visible selected
191 if (!av.getColumnSelection().isVisible(index))
196 if (anot[index] == null)
198 anot[index] = new Annotation(label, "", ' ', 0);
201 anot[index].displayCharacter = label;
204 else if (evt.getActionCommand().equals(COLOUR))
206 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
209 Color col = udc.getColor();
211 for (int index : av.getColumnSelection().getSelected())
213 if (!av.getColumnSelection().isVisible(index))
218 if (anot[index] == null)
220 anot[index] = new Annotation("", "", ' ', 0);
223 anot[index].colour = col;
230 String symbol = "\u03B1";
232 if (evt.getActionCommand().equals(HELIX))
236 else if (evt.getActionCommand().equals(SHEET))
242 // Added by LML to color stems
243 else if (evt.getActionCommand().equals(STEM))
246 int column = av.getColumnSelection().getSelectedRanges().get(0)[0];
247 symbol = aa[activeRow].getDefaultRnaHelixSymbol(column);
250 if (!aa[activeRow].hasIcons)
252 aa[activeRow].hasIcons = true;
255 label = enterLabel(symbol, "Enter Label");
262 if ((label.length() > 0) && !aa[activeRow].hasText)
264 aa[activeRow].hasText = true;
265 if (evt.getActionCommand().equals(STEM))
267 aa[activeRow].showAllColLabels = true;
271 for (int index : av.getColumnSelection().getSelected())
273 if (!av.getColumnSelection().isVisible(index))
278 if (anot[index] == null)
280 anot[index] = new Annotation(label, "", type, 0);
283 anot[index].secondaryStructure = type != 'S' ? type : label
284 .length() == 0 ? ' ' : label.charAt(0);
285 anot[index].displayCharacter = label;
289 av.getAlignment().validateAnnotation(aa[activeRow]);
291 ap.alignmentChanged();
298 String enterLabel(String text, String label)
300 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
301 ap.alignFrame, "Enter Label", 400, 200, true);
305 return dialog.getName();
314 public void mousePressed(MouseEvent evt)
316 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
322 int height = -scrollOffset;
325 for (int i = 0; i < aa.length; i++)
329 height += aa[i].height;
332 if (evt.getY() < height)
338 else if (aa[i].graph > 0)
342 graphStretchY = evt.getY();
349 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
352 if (av.getColumnSelection() == null
353 || av.getColumnSelection().isEmpty())
358 PopupMenu pop = new PopupMenu(
359 MessageManager.getString("label.structure_type"));
362 if (av.getAlignment().isNucleotide())
364 item = new MenuItem(STEM);
365 item.addActionListener(this);
370 item = new MenuItem(HELIX);
371 item.addActionListener(this);
373 item = new MenuItem(SHEET);
374 item.addActionListener(this);
377 item = new MenuItem(LABEL);
378 item.addActionListener(this);
380 item = new MenuItem(COLOUR);
381 item.addActionListener(this);
383 item = new MenuItem(REMOVE);
384 item.addActionListener(this);
386 ap.alignFrame.add(pop);
387 pop.show(this, evt.getX(), evt.getY());
392 ap.scalePanel.mousePressed(evt);
396 public void mouseReleased(MouseEvent evt)
400 mouseDragging = false;
404 needValidating = false;
406 ap.scalePanel.mouseReleased(evt);
410 public void mouseClicked(MouseEvent evt)
414 boolean needValidating = false;
417 public void mouseDragged(MouseEvent evt)
419 if (graphStretch > -1)
421 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
423 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
425 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
427 graphStretchY = evt.getY();
428 av.calcPanelHeight();
429 needValidating = true;
430 ap.paintAlignment(true);
434 ap.scalePanel.mouseDragged(evt);
439 public void mouseMoved(MouseEvent evt)
441 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
448 int height = -scrollOffset;
449 for (int i = 0; i < aa.length; i++)
454 height += aa[i].height;
457 if (evt.getY() < height)
464 int column = evt.getX() / av.getCharWidth() + av.getStartRes();
466 if (av.hasHiddenColumns())
468 column = av.getColumnSelection().adjustForHiddenColumns(column);
471 if (row > -1 && column < aa[row].annotations.length
472 && aa[row].annotations[column] != null)
474 StringBuilder text = new StringBuilder();
475 text.append(MessageManager.getString("label.column")).append(" ")
477 if (aa[row].annotations[column].description != null)
479 text.append(" ").append(aa[row].annotations[column].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))
498 int residuePos = seqref.findPosition(column);
499 text.append(": ").append(residue).append(" (")
500 .append(residuePos).append(")");
505 ap.alignFrame.statusBar.setText(text.toString());
510 public void mouseEntered(MouseEvent evt)
512 ap.scalePanel.mouseEntered(evt);
516 public void mouseExited(MouseEvent evt)
518 ap.scalePanel.mouseExited(evt);
521 public int adjustPanelHeight()
523 return adjustPanelHeight(true);
526 public int adjustPanelHeight(boolean repaint)
528 int height = av.calcPanelHeight();
529 this.setSize(new Dimension(getSize().width, height));
538 * calculate the height for visible annotation, revalidating bounds where
539 * necessary ABSTRACT GUI METHOD
541 * @return total height of annotation
544 public void addEditableColumn(int i)
548 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
554 for (int j = 0; j < aa.length; j++)
566 public void update(Graphics g)
572 public void paint(Graphics g)
574 Dimension d = getSize();
576 // (av.endRes - av.startRes + 1) * av.charWidth;
577 if (imgWidth < 1 || d.height < 1)
581 if (image == null || imgWidth != image.getWidth(this)
582 || d.height != image.getHeight(this))
584 image = createImage(imgWidth, d.height);
585 gg = image.getGraphics();
586 gg.setFont(av.getFont());
587 fm = gg.getFontMetrics();
593 g.drawImage(image, 0, 0, this);
598 gg.setColor(Color.white);
599 gg.fillRect(0, 0, getSize().width, getSize().height);
600 drawComponent(gg, av.startRes, av.endRes + 1);
602 g.drawImage(image, 0, 0, this);
605 public void fastPaint(int horizontal)
608 || av.getAlignment().getAlignmentAnnotation() == null
609 || av.getAlignment().getAlignmentAnnotation().length < 1)
615 gg.copyArea(0, 0, imgWidth, getSize().height,
616 -horizontal * av.getCharWidth(), 0);
617 int sr = av.startRes, er = av.endRes + 1, transX = 0;
619 if (horizontal > 0) // scrollbar pulled right, image to the left
621 transX = (er - sr - horizontal) * av.getCharWidth();
622 sr = er - horizontal;
624 else if (horizontal < 0)
626 er = sr - horizontal;
629 gg.translate(transX, 0);
631 drawComponent(gg, sr, er);
633 gg.translate(-transX, 0);
649 public void drawComponent(Graphics g, int startRes, int endRes)
651 Font ofont = av.getFont();
654 g.setColor(Color.white);
655 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(),
660 fm = g.getFontMetrics();
663 if ((av.getAlignment().getAlignmentAnnotation() == null)
664 || (av.getAlignment().getAlignmentAnnotation().length < 1))
666 g.setColor(Color.white);
667 g.fillRect(0, 0, getSize().width, getSize().height);
668 g.setColor(Color.black);
669 if (av.validCharWidth)
671 g.drawString(MessageManager
672 .getString("label.alignment_has_no_annotations"), 20, 15);
677 g.translate(0, -scrollOffset);
678 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
679 g.translate(0, +scrollOffset);
682 int scrollOffset = 0;
684 public void setScrollOffset(int value, boolean repaint)
686 scrollOffset = value;
694 public FontMetrics getFontMetrics()
700 public Image getFadedImage()
706 public int getFadedImageWidth()
711 private int[] bounds = new int[2];
714 public int[] getVisibleVRange()
716 if (ap != null && ap.alabels != null)
718 int sOffset = -ap.alabels.scrollOffset;
719 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
721 bounds[1] = visHeight;