2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
26 import java.awt.event.*;
28 import jalview.datamodel.*;
29 import jalview.renderer.AnnotationRenderer;
30 import jalview.renderer.AwtRenderPanelI;
31 import jalview.util.MessageManager;
33 public class AnnotationPanel extends Panel implements AwtRenderPanelI,
34 AdjustmentListener, ActionListener, MouseListener,
45 final String HELIX = "Helix";
47 final String SHEET = "Sheet";
50 * For RNA secondary structure "stems" aka helices
52 final String STEM = "RNA Helix";
54 final String LABEL = "Label";
56 final String REMOVE = "Remove Annotation";
58 final String COLOUR = "Colour";
60 final Color HELIX_COLOUR = Color.red.darker();
62 final Color SHEET_COLOUR = Color.green.darker().darker();
72 boolean fastPaint = false;
74 // Used For mouse Dragging and resizing graphs
75 int graphStretch = -1;
77 int graphStretchY = -1;
79 boolean mouseDragging = false;
81 public static int GRAPH_HEIGHT = 40;
85 public final AnnotationRenderer renderer;
87 public AnnotationPanel(AlignmentPanel ap)
89 MAC = new jalview.util.Platform().isAMac();
93 int height = adjustPanelHeight();
94 ap.apvscroll.setValues(0, getSize().height, 0, height);
96 addMouseMotionListener(this);
98 addMouseListener(this);
100 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
101 renderer = new AnnotationRenderer();
104 public AnnotationPanel(AlignViewport av)
107 renderer = new AnnotationRenderer();
111 public void adjustmentValueChanged(AdjustmentEvent evt)
122 public void actionPerformed(ActionEvent evt)
124 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
129 Annotation[] anot = aa[activeRow].annotations;
131 if (anot.length < av.getColumnSelection().getMax())
133 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
134 System.arraycopy(anot, 0, temp, 0, anot.length);
136 aa[activeRow].annotations = anot;
140 if (av.getColumnSelection() != null
141 && av.getColumnSelection().size() > 0
142 && anot[av.getColumnSelection().getMin()] != null)
143 label = anot[av.getColumnSelection().getMin()].displayCharacter;
145 if (evt.getActionCommand().equals(REMOVE))
147 for (int i = 0; i < av.getColumnSelection().size(); i++)
149 anot[av.getColumnSelection().columnAt(i)] = null;
152 else if (evt.getActionCommand().equals(LABEL))
154 label = enterLabel(label, "Enter Label");
161 if ((label.length() > 0) && !aa[activeRow].hasText)
163 aa[activeRow].hasText = true;
166 for (int i = 0; i < av.getColumnSelection().size(); i++)
168 int index = av.getColumnSelection().columnAt(i);
170 if (!av.getColumnSelection().isVisible(index))
173 if (anot[index] == null)
175 anot[index] = new Annotation(label, "", ' ', 0);
178 anot[index].displayCharacter = label;
181 else if (evt.getActionCommand().equals(COLOUR))
183 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
186 Color col = udc.getColor();
188 for (int i = 0; i < av.getColumnSelection().size(); i++)
190 int index = av.getColumnSelection().columnAt(i);
192 if (!av.getColumnSelection().isVisible(index))
195 if (anot[index] == null)
197 anot[index] = new Annotation("", "", ' ', 0);
200 anot[index].colour = col;
207 String symbol = "\u03B1";
209 if (evt.getActionCommand().equals(HELIX))
213 else if (evt.getActionCommand().equals(SHEET))
219 // Added by LML to color stems
220 else if (evt.getActionCommand().equals(STEM))
226 if (!aa[activeRow].hasIcons)
228 aa[activeRow].hasIcons = true;
231 label = enterLabel(symbol, "Enter Label");
238 if ((label.length() > 0) && !aa[activeRow].hasText)
240 aa[activeRow].hasText = true;
241 if (evt.getActionCommand().equals(STEM))
243 aa[activeRow].showAllColLabels=true;
247 for (int i = 0; i < av.getColumnSelection().size(); i++)
249 int index = av.getColumnSelection().columnAt(i);
251 if (!av.getColumnSelection().isVisible(index))
254 if (anot[index] == null)
256 anot[index] = new Annotation(label, "", type, 0);
259 anot[index].secondaryStructure = type;
260 anot[index].displayCharacter = label;
264 av.getAlignment().validateAnnotation(aa[activeRow]);
266 ap.alignmentChanged();
273 String enterLabel(String text, String label)
275 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
276 ap.alignFrame, "Enter Label", 400, 200, true);
279 return dialog.getName();
285 public void mousePressed(MouseEvent evt)
287 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
293 int height = -scrollOffset;
296 for (int i = 0; i < aa.length; i++)
300 height += aa[i].height;
303 if (evt.getY() < height)
309 else if (aa[i].graph > 0)
313 graphStretchY = evt.getY();
320 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
323 if (av.getColumnSelection() == null)
328 PopupMenu pop = new PopupMenu(MessageManager.getString("label.structure_type"));
331 * Just display the needed structure options
333 if (av.getAlignment().isNucleotide() == true)
335 item = new MenuItem(STEM);
336 item.addActionListener(this);
341 item = new MenuItem(HELIX);
342 item.addActionListener(this);
344 item = new MenuItem(SHEET);
345 item.addActionListener(this);
348 item = new MenuItem(LABEL);
349 item.addActionListener(this);
351 item = new MenuItem(COLOUR);
352 item.addActionListener(this);
354 item = new MenuItem(REMOVE);
355 item.addActionListener(this);
357 ap.alignFrame.add(pop);
358 pop.show(this, evt.getX(), evt.getY());
368 ap.scalePanel.mousePressed(evt);
372 public void mouseReleased(MouseEvent evt)
376 mouseDragging = false;
380 needValidating = false;
382 ap.scalePanel.mouseReleased(evt);
386 public void mouseClicked(MouseEvent evt)
390 boolean needValidating = false;
393 public void mouseDragged(MouseEvent evt)
395 if (graphStretch > -1)
397 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
399 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
401 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
403 graphStretchY = evt.getY();
404 av.calcPanelHeight();
405 needValidating = true;
406 ap.paintAlignment(true);
410 ap.scalePanel.mouseDragged(evt);
415 public void mouseMoved(MouseEvent evt)
417 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
424 int height = -scrollOffset;
425 for (int i = 0; i < aa.length; i++)
430 height += aa[i].height;
433 if (evt.getY() < height)
440 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
442 if (av.hasHiddenColumns())
444 res = av.getColumnSelection().adjustForHiddenColumns(res);
447 if (row > -1 && res < aa[row].annotations.length
448 && aa[row].annotations[res] != null)
450 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
451 if (aa[row].annotations[res].description != null)
453 text.append(" " + aa[row].annotations[res].description);
455 ap.alignFrame.statusBar.setText(text.toString());
460 public void mouseEntered(MouseEvent evt)
462 ap.scalePanel.mouseEntered(evt);
466 public void mouseExited(MouseEvent evt)
468 ap.scalePanel.mouseExited(evt);
471 public int adjustPanelHeight()
473 return adjustPanelHeight(true);
476 public int adjustPanelHeight(boolean repaint)
478 int height = av.calcPanelHeight();
479 this.setSize(new Dimension(getSize().width, height));
488 * calculate the height for visible annotation, revalidating bounds where
489 * necessary ABSTRACT GUI METHOD
491 * @return total height of annotation
494 public void addEditableColumn(int i)
498 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
504 for (int j = 0; j < aa.length; j++)
514 if (activeRes == null)
516 activeRes = new Vector();
517 activeRes.addElement(String.valueOf(i));
521 activeRes.addElement(String.valueOf(i));
525 public void update(Graphics g)
531 public void paint(Graphics g)
533 Dimension d = getSize();
535 // (av.endRes - av.startRes + 1) * av.charWidth;
536 if (imgWidth < 1 || d.height < 1)
540 if (image == null || imgWidth != image.getWidth(this)
541 || d.height != image.getHeight(this))
543 image = createImage(imgWidth, d.height);
544 gg = image.getGraphics();
545 gg.setFont(av.getFont());
546 fm = gg.getFontMetrics();
552 g.drawImage(image, 0, 0, this);
557 gg.setColor(Color.white);
558 gg.fillRect(0, 0, getSize().width, getSize().height);
559 drawComponent(gg, av.startRes, av.endRes + 1);
561 g.drawImage(image, 0, 0, this);
564 public void fastPaint(int horizontal)
567 || av.getAlignment().getAlignmentAnnotation() == null
568 || av.getAlignment().getAlignmentAnnotation().length < 1)
574 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
576 int sr = av.startRes, er = av.endRes + 1, transX = 0;
578 if (horizontal > 0) // scrollbar pulled right, image to the left
580 transX = (er - sr - horizontal) * av.charWidth;
581 sr = er - horizontal;
583 else if (horizontal < 0)
585 er = sr - horizontal;
588 gg.translate(transX, 0);
590 drawComponent(gg, sr, er);
592 gg.translate(-transX, 0);
608 public void drawComponent(Graphics g, int startRes, int endRes)
610 Font ofont = av.getFont();
613 g.setColor(Color.white);
614 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
618 fm = g.getFontMetrics();
621 if ((av.getAlignment().getAlignmentAnnotation() == null)
622 || (av.getAlignment().getAlignmentAnnotation().length < 1))
624 g.setColor(Color.white);
625 g.fillRect(0, 0, getSize().width, getSize().height);
626 g.setColor(Color.black);
627 if (av.validCharWidth)
629 g.drawString(MessageManager.getString("label.alignment_has_no_annotations"), 20, 15);
634 g.translate(0, -scrollOffset);
635 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
636 g.translate(0, +scrollOffset);
639 int scrollOffset = 0;
641 public void setScrollOffset(int value, boolean repaint)
643 scrollOffset = value;
651 public FontMetrics getFontMetrics()
657 public Image getFadedImage()
663 public int getFadedImageWidth()
667 private int[] bounds = new int[2];
669 public int[] getVisibleVRange()
671 if (ap!=null && ap.alabels!=null)
673 int sOffset=-ap.alabels.scrollOffset;
674 int visHeight = sOffset+ap.annotationPanelHolder.getHeight();
675 bounds[0] = sOffset; bounds[1]=visHeight;