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(
329 MessageManager.getString("label.structure_type"));
332 * Just display the needed structure options
334 if (av.getAlignment().isNucleotide() == true)
336 item = new MenuItem(STEM);
337 item.addActionListener(this);
342 item = new MenuItem(HELIX);
343 item.addActionListener(this);
345 item = new MenuItem(SHEET);
346 item.addActionListener(this);
349 item = new MenuItem(LABEL);
350 item.addActionListener(this);
352 item = new MenuItem(COLOUR);
353 item.addActionListener(this);
355 item = new MenuItem(REMOVE);
356 item.addActionListener(this);
358 ap.alignFrame.add(pop);
359 pop.show(this, evt.getX(), evt.getY());
369 ap.scalePanel.mousePressed(evt);
373 public void mouseReleased(MouseEvent evt)
377 mouseDragging = false;
381 needValidating = false;
383 ap.scalePanel.mouseReleased(evt);
387 public void mouseClicked(MouseEvent evt)
391 boolean needValidating = false;
394 public void mouseDragged(MouseEvent evt)
396 if (graphStretch > -1)
398 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
400 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
402 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
404 graphStretchY = evt.getY();
405 av.calcPanelHeight();
406 needValidating = true;
407 ap.paintAlignment(true);
411 ap.scalePanel.mouseDragged(evt);
416 public void mouseMoved(MouseEvent evt)
418 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
425 int height = -scrollOffset;
426 for (int i = 0; i < aa.length; i++)
431 height += aa[i].height;
434 if (evt.getY() < height)
441 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
443 if (av.hasHiddenColumns())
445 res = av.getColumnSelection().adjustForHiddenColumns(res);
448 if (row > -1 && res < aa[row].annotations.length
449 && aa[row].annotations[res] != null)
451 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
452 if (aa[row].annotations[res].description != null)
454 text.append(" " + aa[row].annotations[res].description);
456 ap.alignFrame.statusBar.setText(text.toString());
461 public void mouseEntered(MouseEvent evt)
463 ap.scalePanel.mouseEntered(evt);
467 public void mouseExited(MouseEvent evt)
469 ap.scalePanel.mouseExited(evt);
472 public int adjustPanelHeight()
474 return adjustPanelHeight(true);
477 public int adjustPanelHeight(boolean repaint)
479 int height = av.calcPanelHeight();
480 this.setSize(new Dimension(getSize().width, height));
489 * calculate the height for visible annotation, revalidating bounds where
490 * necessary ABSTRACT GUI METHOD
492 * @return total height of annotation
495 public void addEditableColumn(int i)
499 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
505 for (int j = 0; j < aa.length; j++)
515 if (activeRes == null)
517 activeRes = new Vector();
518 activeRes.addElement(String.valueOf(i));
522 activeRes.addElement(String.valueOf(i));
526 public void update(Graphics g)
532 public void paint(Graphics g)
534 Dimension d = getSize();
536 // (av.endRes - av.startRes + 1) * av.charWidth;
537 if (imgWidth < 1 || d.height < 1)
541 if (image == null || imgWidth != image.getWidth(this)
542 || d.height != image.getHeight(this))
544 image = createImage(imgWidth, d.height);
545 gg = image.getGraphics();
546 gg.setFont(av.getFont());
547 fm = gg.getFontMetrics();
553 g.drawImage(image, 0, 0, this);
558 gg.setColor(Color.white);
559 gg.fillRect(0, 0, getSize().width, getSize().height);
560 drawComponent(gg, av.startRes, av.endRes + 1);
562 g.drawImage(image, 0, 0, this);
565 public void fastPaint(int horizontal)
568 || av.getAlignment().getAlignmentAnnotation() == null
569 || av.getAlignment().getAlignmentAnnotation().length < 1)
575 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
577 int sr = av.startRes, er = av.endRes + 1, transX = 0;
579 if (horizontal > 0) // scrollbar pulled right, image to the left
581 transX = (er - sr - horizontal) * av.charWidth;
582 sr = er - horizontal;
584 else if (horizontal < 0)
586 er = sr - horizontal;
589 gg.translate(transX, 0);
591 drawComponent(gg, sr, er);
593 gg.translate(-transX, 0);
609 public void drawComponent(Graphics g, int startRes, int endRes)
611 Font ofont = av.getFont();
614 g.setColor(Color.white);
615 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
619 fm = g.getFontMetrics();
622 if ((av.getAlignment().getAlignmentAnnotation() == null)
623 || (av.getAlignment().getAlignmentAnnotation().length < 1))
625 g.setColor(Color.white);
626 g.fillRect(0, 0, getSize().width, getSize().height);
627 g.setColor(Color.black);
628 if (av.validCharWidth)
630 g.drawString(MessageManager
631 .getString("label.alignment_has_no_annotations"), 20, 15);
636 g.translate(0, -scrollOffset);
637 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
638 g.translate(0, +scrollOffset);
641 int scrollOffset = 0;
643 public void setScrollOffset(int value, boolean repaint)
645 scrollOffset = value;
653 public FontMetrics getFontMetrics()
659 public Image getFadedImage()
665 public int getFadedImageWidth()
670 private int[] bounds = new int[2];
673 public int[] getVisibleVRange()
675 if (ap != null && ap.alabels != null)
677 int sOffset = -ap.alabels.scrollOffset;
678 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
680 bounds[1] = visHeight;