2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
23 import java.awt.event.*;
25 import jalview.datamodel.*;
26 import jalview.renderer.AnnotationRenderer;
27 import jalview.renderer.AwtRenderPanelI;
29 public class AnnotationPanel extends Panel implements AwtRenderPanelI,
30 AdjustmentListener, ActionListener, MouseListener,
41 final String HELIX = "Helix";
43 final String SHEET = "Sheet";
46 * For RNA secondary structure "stems" aka helices
48 final String STEM = "RNA Helix";
50 final String LABEL = "Label";
52 final String REMOVE = "Remove Annotation";
54 final String COLOUR = "Colour";
56 final Color HELIX_COLOUR = Color.red.darker();
58 final Color SHEET_COLOUR = Color.green.darker().darker();
68 boolean fastPaint = false;
70 // Used For mouse Dragging and resizing graphs
71 int graphStretch = -1;
73 int graphStretchY = -1;
75 boolean mouseDragging = false;
77 public static int GRAPH_HEIGHT = 40;
81 public final AnnotationRenderer renderer;
83 public AnnotationPanel(AlignmentPanel ap)
85 MAC = new jalview.util.Platform().isAMac();
89 int height = adjustPanelHeight();
90 ap.apvscroll.setValues(0, getSize().height, 0, height);
92 addMouseMotionListener(this);
94 addMouseListener(this);
96 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
97 renderer = new AnnotationRenderer();
100 public AnnotationPanel(AlignViewport av)
103 renderer = new AnnotationRenderer();
107 public void adjustmentValueChanged(AdjustmentEvent evt)
118 public void actionPerformed(ActionEvent evt)
120 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
125 Annotation[] anot = aa[activeRow].annotations;
127 if (anot.length < av.getColumnSelection().getMax())
129 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
130 System.arraycopy(anot, 0, temp, 0, anot.length);
132 aa[activeRow].annotations = anot;
136 if (av.getColumnSelection() != null
137 && av.getColumnSelection().size() > 0
138 && anot[av.getColumnSelection().getMin()] != null)
139 label = anot[av.getColumnSelection().getMin()].displayCharacter;
141 if (evt.getActionCommand().equals(REMOVE))
143 for (int i = 0; i < av.getColumnSelection().size(); i++)
145 anot[av.getColumnSelection().columnAt(i)] = null;
148 else if (evt.getActionCommand().equals(LABEL))
150 label = enterLabel(label, "Enter Label");
157 if ((label.length() > 0) && !aa[activeRow].hasText)
159 aa[activeRow].hasText = true;
162 for (int i = 0; i < av.getColumnSelection().size(); i++)
164 int index = av.getColumnSelection().columnAt(i);
166 if (!av.getColumnSelection().isVisible(index))
169 if (anot[index] == null)
171 anot[index] = new Annotation(label, "", ' ', 0);
174 anot[index].displayCharacter = label;
177 else if (evt.getActionCommand().equals(COLOUR))
179 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
182 Color col = udc.getColor();
184 for (int i = 0; i < av.getColumnSelection().size(); i++)
186 int index = av.getColumnSelection().columnAt(i);
188 if (!av.getColumnSelection().isVisible(index))
191 if (anot[index] == null)
193 anot[index] = new Annotation("", "", ' ', 0);
196 anot[index].colour = col;
203 String symbol = "\u03B1";
205 if (evt.getActionCommand().equals(HELIX))
209 else if (evt.getActionCommand().equals(SHEET))
215 // Added by LML to color stems
216 else if (evt.getActionCommand().equals(STEM))
222 if (!aa[activeRow].hasIcons)
224 aa[activeRow].hasIcons = true;
227 label = enterLabel(symbol, "Enter Label");
234 if ((label.length() > 0) && !aa[activeRow].hasText)
236 aa[activeRow].hasText = true;
239 for (int i = 0; i < av.getColumnSelection().size(); i++)
241 int index = av.getColumnSelection().columnAt(i);
243 if (!av.getColumnSelection().isVisible(index))
246 if (anot[index] == null)
248 anot[index] = new Annotation(label, "", type, 0);
251 anot[index].secondaryStructure = type;
252 anot[index].displayCharacter = label;
256 av.getAlignment().validateAnnotation(aa[activeRow]);
258 ap.alignmentChanged();
265 String enterLabel(String text, String label)
267 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
268 ap.alignFrame, "Enter Label", 400, 200, true);
271 return dialog.getName();
277 public void mousePressed(MouseEvent evt)
279 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
285 int height = -scrollOffset;
288 for (int i = 0; i < aa.length; i++)
292 height += aa[i].height;
295 if (evt.getY() < height)
301 else if (aa[i].graph > 0)
305 graphStretchY = evt.getY();
312 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
315 if (av.getColumnSelection() == null)
320 PopupMenu pop = new PopupMenu("Structure type");
323 * Just display the needed structure options
325 if (av.getAlignment().isNucleotide() == true)
327 item = new MenuItem(STEM);
328 item.addActionListener(this);
333 item = new MenuItem(HELIX);
334 item.addActionListener(this);
336 item = new MenuItem(SHEET);
337 item.addActionListener(this);
340 item = new MenuItem(LABEL);
341 item.addActionListener(this);
343 item = new MenuItem(COLOUR);
344 item.addActionListener(this);
346 item = new MenuItem(REMOVE);
347 item.addActionListener(this);
349 ap.alignFrame.add(pop);
350 pop.show(this, evt.getX(), evt.getY());
360 ap.scalePanel.mousePressed(evt);
364 public void mouseReleased(MouseEvent evt)
368 mouseDragging = false;
372 needValidating = false;
374 ap.scalePanel.mouseReleased(evt);
378 public void mouseClicked(MouseEvent evt)
382 boolean needValidating = false;
385 public void mouseDragged(MouseEvent evt)
387 if (graphStretch > -1)
389 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
391 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
393 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
395 graphStretchY = evt.getY();
396 av.calcPanelHeight();
397 needValidating = true;
398 ap.paintAlignment(true);
402 ap.scalePanel.mouseDragged(evt);
407 public void mouseMoved(MouseEvent evt)
409 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
416 int height = -scrollOffset;
417 for (int i = 0; i < aa.length; i++)
422 height += aa[i].height;
425 if (evt.getY() < height)
432 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
434 if (av.hasHiddenColumns())
436 res = av.getColumnSelection().adjustForHiddenColumns(res);
439 if (row > -1 && res < aa[row].annotations.length
440 && aa[row].annotations[res] != null)
442 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
443 if (aa[row].annotations[res].description != null)
445 text.append(" " + aa[row].annotations[res].description);
447 ap.alignFrame.statusBar.setText(text.toString());
452 public void mouseEntered(MouseEvent evt)
454 ap.scalePanel.mouseEntered(evt);
458 public void mouseExited(MouseEvent evt)
460 ap.scalePanel.mouseExited(evt);
463 public int adjustPanelHeight()
465 return adjustPanelHeight(true);
468 public int adjustPanelHeight(boolean repaint)
470 int height = av.calcPanelHeight();
471 this.setSize(new Dimension(getSize().width, height));
480 * calculate the height for visible annotation, revalidating bounds where
481 * necessary ABSTRACT GUI METHOD
483 * @return total height of annotation
486 public void addEditableColumn(int i)
490 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
496 for (int j = 0; j < aa.length; j++)
506 if (activeRes == null)
508 activeRes = new Vector();
509 activeRes.addElement(String.valueOf(i));
513 activeRes.addElement(String.valueOf(i));
517 public void update(Graphics g)
523 public void paint(Graphics g)
525 Dimension d = getSize();
527 // (av.endRes - av.startRes + 1) * av.charWidth;
528 if (imgWidth < 1 || d.height < 1)
532 if (image == null || imgWidth != image.getWidth(this)
533 || d.height != image.getHeight(this))
535 image = createImage(imgWidth, d.height);
536 gg = image.getGraphics();
537 gg.setFont(av.getFont());
538 fm = gg.getFontMetrics();
544 g.drawImage(image, 0, 0, this);
549 gg.setColor(Color.white);
550 gg.fillRect(0, 0, getSize().width, getSize().height);
551 drawComponent(gg, av.startRes, av.endRes + 1);
553 g.drawImage(image, 0, 0, this);
556 public void fastPaint(int horizontal)
559 || av.getAlignment().getAlignmentAnnotation() == null
560 || av.getAlignment().getAlignmentAnnotation().length < 1)
566 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
568 int sr = av.startRes, er = av.endRes + 1, transX = 0;
570 if (horizontal > 0) // scrollbar pulled right, image to the left
572 transX = (er - sr - horizontal) * av.charWidth;
573 sr = er - horizontal;
575 else if (horizontal < 0)
577 er = sr - horizontal;
580 gg.translate(transX, 0);
582 drawComponent(gg, sr, er);
584 gg.translate(-transX, 0);
600 public void drawComponent(Graphics g, int startRes, int endRes)
602 Font ofont = av.getFont();
605 g.setColor(Color.white);
606 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
610 fm = g.getFontMetrics();
613 if ((av.getAlignment().getAlignmentAnnotation() == null)
614 || (av.getAlignment().getAlignmentAnnotation().length < 1))
616 g.setColor(Color.white);
617 g.fillRect(0, 0, getSize().width, getSize().height);
618 g.setColor(Color.black);
619 if (av.validCharWidth)
621 g.drawString("Alignment has no annotations", 20, 15);
626 g.translate(0, -scrollOffset);
627 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
628 g.translate(0, +scrollOffset);
631 int scrollOffset = 0;
633 public void setScrollOffset(int value)
635 scrollOffset = value;
640 public FontMetrics getFontMetrics()
646 public Image getFadedImage()
652 public int getFadedImageWidth()
656 private int[] bounds = new int[2];
658 public int[] getVisibleVRange()
660 if (ap!=null && ap.alabels!=null)
662 int sOffset=-ap.alabels.scrollOffset;
663 int visHeight = sOffset+ap.annotationPanelHolder.getHeight();
664 bounds[0] = sOffset; bounds[1]=visHeight;