2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.*;
24 import java.awt.font.LineMetrics;
25 import java.awt.geom.AffineTransform;
27 import jalview.analysis.AAFrequency;
28 import jalview.datamodel.*;
29 import jalview.renderer.AnnotationRenderer;
30 import jalview.renderer.AwtRenderPanelI;
31 import jalview.schemes.ColourSchemeI;
33 public class AnnotationPanel extends Panel implements AwtRenderPanelI, AdjustmentListener,
34 ActionListener, MouseListener, MouseMotionListener
44 final String HELIX = "Helix";
46 final String SHEET = "Sheet";
49 * For RNA secondary structure "stems" aka helices
51 final String STEM = "RNA Helix";
53 final String LABEL = "Label";
55 final String REMOVE = "Remove Annotation";
57 final String COLOUR = "Colour";
59 final Color HELIX_COLOUR = Color.red.darker();
61 final Color SHEET_COLOUR = Color.green.darker().darker();
71 boolean fastPaint = false;
73 // Used For mouse Dragging and resizing graphs
74 int graphStretch = -1;
76 int graphStretchY = -1;
78 boolean mouseDragging = false;
80 public static int GRAPH_HEIGHT = 40;
84 public final AnnotationRenderer renderer;
86 public AnnotationPanel(AlignmentPanel ap)
88 MAC = new jalview.util.Platform().isAMac();
92 int height = adjustPanelHeight();
93 ap.apvscroll.setValues(0, getSize().height, 0, height);
95 addMouseMotionListener(this);
97 addMouseListener(this);
99 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
100 renderer = new AnnotationRenderer();
103 public AnnotationPanel(AlignViewport av)
106 renderer = new AnnotationRenderer();
109 public void adjustmentValueChanged(AdjustmentEvent evt)
119 public void actionPerformed(ActionEvent evt)
121 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
126 Annotation[] anot = aa[activeRow].annotations;
128 if (anot.length < av.getColumnSelection().getMax())
130 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
131 System.arraycopy(anot, 0, temp, 0, anot.length);
133 aa[activeRow].annotations = anot;
137 if (av.getColumnSelection() != null && 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 aa[activeRow].validateRangeAndDisplay();
264 String enterLabel(String text, String label)
266 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
267 ap.alignFrame, "Enter Label", 400, 200, true);
270 return dialog.getName();
275 public void mousePressed(MouseEvent evt)
277 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
283 int height = -scrollOffset;
286 for (int i = 0; i < aa.length; i++)
290 height += aa[i].height;
293 if (evt.getY() < height)
299 else if (aa[i].graph > 0)
303 graphStretchY = evt.getY();
310 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
313 if (av.getColumnSelection() == null)
318 PopupMenu pop = new PopupMenu("Structure type");
321 * Just display the needed structure options
323 if (av.getAlignment().isNucleotide() == true)
325 item = new MenuItem(STEM);
326 item.addActionListener(this);
329 item = new MenuItem(HELIX);
330 item.addActionListener(this);
332 item = new MenuItem(SHEET);
333 item.addActionListener(this);
336 item = new MenuItem(LABEL);
337 item.addActionListener(this);
339 item = new MenuItem(COLOUR);
340 item.addActionListener(this);
342 item = new MenuItem(REMOVE);
343 item.addActionListener(this);
345 ap.alignFrame.add(pop);
346 pop.show(this, evt.getX(), evt.getY());
356 ap.scalePanel.mousePressed(evt);
359 public void mouseReleased(MouseEvent evt)
363 mouseDragging = false;
367 needValidating = false;
369 ap.scalePanel.mouseReleased(evt);
372 public void mouseClicked(MouseEvent evt)
376 boolean needValidating = false;
378 public void mouseDragged(MouseEvent evt)
380 if (graphStretch > -1)
382 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
384 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
386 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
388 graphStretchY = evt.getY();
390 needValidating = true;
391 ap.paintAlignment(true);
395 ap.scalePanel.mouseDragged(evt);
399 public void mouseMoved(MouseEvent evt)
401 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
408 int height = -scrollOffset;
409 for (int i = 0; i < aa.length; i++)
414 height += aa[i].height;
417 if (evt.getY() < height)
424 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
426 if (av.hasHiddenColumns())
428 res = av.getColumnSelection().adjustForHiddenColumns(res);
431 if (row > -1 && res < aa[row].annotations.length
432 && aa[row].annotations[res] != null)
434 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
435 if (aa[row].annotations[res].description != null)
437 text.append(" " + aa[row].annotations[res].description);
439 ap.alignFrame.statusBar.setText(text.toString());
443 public void mouseEntered(MouseEvent evt)
445 ap.scalePanel.mouseEntered(evt);
448 public void mouseExited(MouseEvent evt)
450 ap.scalePanel.mouseExited(evt);
453 public int adjustPanelHeight()
455 return adjustPanelHeight(true);
458 public int adjustPanelHeight(boolean repaint)
460 int height = calcPanelHeight();
461 this.setSize(new Dimension(getSize().width, height));
469 * calculate the height for visible annotation, revalidating bounds where necessary
470 * ABSTRACT GUI METHOD
471 * @return total height of annotation
473 public int calcPanelHeight()
475 // setHeight of panels
476 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
481 for (int i = 0; i < aa.length; i++)
492 aa[i].height += av.charHeight;
502 aa[i].height += aa[i].graphHeight;
505 if (aa[i].height == 0)
510 height += aa[i].height;
522 public void addEditableColumn(int i)
526 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
532 for (int j = 0; j < aa.length; j++)
542 if (activeRes == null)
544 activeRes = new Vector();
545 activeRes.addElement(String.valueOf(i));
549 activeRes.addElement(String.valueOf(i));
552 public void update(Graphics g)
557 public void paint(Graphics g)
559 Dimension d = getSize();
561 // (av.endRes - av.startRes + 1) * av.charWidth;
562 if (imgWidth<1 || d.height<1)
566 if (image == null || imgWidth != image.getWidth(this) || d.height != image.getHeight(this))
568 image = createImage(imgWidth, d.height);
569 gg = image.getGraphics();
570 gg.setFont(av.getFont());
571 fm = gg.getFontMetrics();
577 g.drawImage(image, 0, 0, this);
582 gg.setColor(Color.white);
583 gg.fillRect(0, 0, getSize().width, getSize().height);
584 drawComponent(gg, av.startRes, av.endRes + 1);
586 g.drawImage(image, 0, 0, this);
589 public void fastPaint(int horizontal)
591 if (horizontal == 0 || av.getAlignment().getAlignmentAnnotation() == null
592 || av.getAlignment().getAlignmentAnnotation().length < 1)
598 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
600 int sr = av.startRes, er = av.endRes + 1, transX = 0;
602 if (horizontal > 0) // scrollbar pulled right, image to the left
604 transX = (er - sr - horizontal) * av.charWidth;
605 sr = er - horizontal;
607 else if (horizontal < 0)
609 er = sr - horizontal;
612 gg.translate(transX, 0);
614 drawComponent(gg, sr, er);
616 gg.translate(-transX, 0);
632 public void drawComponent(Graphics g, int startRes, int endRes)
634 Font ofont = av.getFont();
637 g.setColor(Color.white);
638 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
642 fm = g.getFontMetrics();
645 if ((av.getAlignment().getAlignmentAnnotation() == null)
646 || (av.getAlignment().getAlignmentAnnotation().length < 1))
648 g.setColor(Color.white);
649 g.fillRect(0, 0, getSize().width, getSize().height);
650 g.setColor(Color.black);
651 if (av.validCharWidth)
653 g.drawString("Alignment has no annotations", 20, 15);
658 g.translate(0, -scrollOffset);
659 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
660 g.translate(0, +scrollOffset);
663 int scrollOffset = 0;
665 public void setScrollOffset(int value)
667 scrollOffset = value;
672 public FontMetrics getFontMetrics()
678 public Image getFadedImage()
684 public int getFadedImageWidth()