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.image.BufferedImage;
26 import jalview.datamodel.*;
27 import jalview.renderer.AnnotationRenderer;
28 import jalview.renderer.AwtRenderPanelI;
30 public class AnnotationPanel extends Panel implements AwtRenderPanelI, AdjustmentListener,
31 ActionListener, MouseListener, MouseMotionListener
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 && av.getColumnSelection().size() > 0
137 && anot[av.getColumnSelection().getMin()] != null)
138 label = anot[av.getColumnSelection().getMin()].displayCharacter;
140 if (evt.getActionCommand().equals(REMOVE))
142 for (int i = 0; i < av.getColumnSelection().size(); i++)
144 anot[av.getColumnSelection().columnAt(i)] = null;
147 else if (evt.getActionCommand().equals(LABEL))
149 label = enterLabel(label, "Enter Label");
156 if ((label.length() > 0) && !aa[activeRow].hasText)
158 aa[activeRow].hasText = true;
161 for (int i = 0; i < av.getColumnSelection().size(); i++)
163 int index = av.getColumnSelection().columnAt(i);
165 if (!av.getColumnSelection().isVisible(index))
168 if (anot[index] == null)
170 anot[index] = new Annotation(label, "", ' ', 0);
173 anot[index].displayCharacter = label;
176 else if (evt.getActionCommand().equals(COLOUR))
178 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
181 Color col = udc.getColor();
183 for (int i = 0; i < av.getColumnSelection().size(); i++)
185 int index = av.getColumnSelection().columnAt(i);
187 if (!av.getColumnSelection().isVisible(index))
190 if (anot[index] == null)
192 anot[index] = new Annotation("", "", ' ', 0);
195 anot[index].colour = col;
202 String symbol = "\u03B1";
204 if (evt.getActionCommand().equals(HELIX))
208 else if (evt.getActionCommand().equals(SHEET))
214 // Added by LML to color stems
215 else if (evt.getActionCommand().equals(STEM))
221 if (!aa[activeRow].hasIcons)
223 aa[activeRow].hasIcons = true;
226 label = enterLabel(symbol, "Enter Label");
233 if ((label.length() > 0) && !aa[activeRow].hasText)
235 aa[activeRow].hasText = true;
238 for (int i = 0; i < av.getColumnSelection().size(); i++)
240 int index = av.getColumnSelection().columnAt(i);
242 if (!av.getColumnSelection().isVisible(index))
245 if (anot[index] == null)
247 anot[index] = new Annotation(label, "", type, 0);
250 anot[index].secondaryStructure = type;
251 anot[index].displayCharacter = label;
255 aa[activeRow].validateRangeAndDisplay();
263 String enterLabel(String text, String label)
265 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
266 ap.alignFrame, "Enter Label", 400, 200, true);
269 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);
360 public void mouseReleased(MouseEvent evt)
364 mouseDragging = false;
368 needValidating = false;
370 ap.scalePanel.mouseReleased(evt);
374 public void mouseClicked(MouseEvent evt)
378 boolean needValidating = false;
381 public void mouseDragged(MouseEvent evt)
383 if (graphStretch > -1)
385 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
387 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
389 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
391 graphStretchY = evt.getY();
393 needValidating = true;
394 ap.paintAlignment(true);
398 ap.scalePanel.mouseDragged(evt);
403 public void mouseMoved(MouseEvent evt)
405 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
412 int height = -scrollOffset;
413 for (int i = 0; i < aa.length; i++)
418 height += aa[i].height;
421 if (evt.getY() < height)
428 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
430 if (av.hasHiddenColumns())
432 res = av.getColumnSelection().adjustForHiddenColumns(res);
435 if (row > -1 && res < aa[row].annotations.length
436 && aa[row].annotations[res] != null)
438 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
439 if (aa[row].annotations[res].description != null)
441 text.append(" " + aa[row].annotations[res].description);
443 ap.alignFrame.statusBar.setText(text.toString());
448 public void mouseEntered(MouseEvent evt)
450 ap.scalePanel.mouseEntered(evt);
454 public void mouseExited(MouseEvent evt)
456 ap.scalePanel.mouseExited(evt);
459 public int adjustPanelHeight()
461 return adjustPanelHeight(true);
464 public int adjustPanelHeight(boolean repaint)
466 int height = calcPanelHeight();
467 this.setSize(new Dimension(getSize().width, height));
475 * calculate the height for visible annotation, revalidating bounds where necessary
476 * ABSTRACT GUI METHOD
477 * @return total height of annotation
479 public int calcPanelHeight()
481 // setHeight of panels
482 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
487 for (int i = 0; i < aa.length; i++)
498 aa[i].height += av.charHeight;
508 aa[i].height += aa[i].graphHeight;
511 if (aa[i].height == 0)
516 height += aa[i].height;
528 public void addEditableColumn(int i)
532 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
538 for (int j = 0; j < aa.length; j++)
548 if (activeRes == null)
550 activeRes = new Vector();
551 activeRes.addElement(String.valueOf(i));
555 activeRes.addElement(String.valueOf(i));
559 public void update(Graphics g)
565 public void paint(Graphics g)
567 Dimension d = getSize();
569 // (av.endRes - av.startRes + 1) * av.charWidth;
570 if (imgWidth<1 || d.height<1)
574 if (image == null || imgWidth != image.getWidth(this) || d.height != image.getHeight(this))
576 image = createImage(imgWidth, d.height);
577 gg = image.getGraphics();
578 gg.setFont(av.getFont());
579 fm = gg.getFontMetrics();
585 g.drawImage(image, 0, 0, this);
590 gg.setColor(Color.white);
591 gg.fillRect(0, 0, getSize().width, getSize().height);
592 drawComponent(gg, av.startRes, av.endRes + 1);
594 g.drawImage(image, 0, 0, this);
597 public void fastPaint(int horizontal)
599 if (horizontal == 0 || av.getAlignment().getAlignmentAnnotation() == null
600 || av.getAlignment().getAlignmentAnnotation().length < 1)
606 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
608 int sr = av.startRes, er = av.endRes + 1, transX = 0;
610 if (horizontal > 0) // scrollbar pulled right, image to the left
612 transX = (er - sr - horizontal) * av.charWidth;
613 sr = er - horizontal;
615 else if (horizontal < 0)
617 er = sr - horizontal;
620 gg.translate(transX, 0);
622 drawComponent(gg, sr, er);
624 gg.translate(-transX, 0);
640 public void drawComponent(Graphics g, int startRes, int endRes)
642 Font ofont = av.getFont();
645 g.setColor(Color.white);
646 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
650 fm = g.getFontMetrics();
653 if ((av.getAlignment().getAlignmentAnnotation() == null)
654 || (av.getAlignment().getAlignmentAnnotation().length < 1))
656 g.setColor(Color.white);
657 g.fillRect(0, 0, getSize().width, getSize().height);
658 g.setColor(Color.black);
659 if (av.validCharWidth)
661 g.drawString("Alignment has no annotations", 20, 15);
666 g.translate(0, -scrollOffset);
667 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
668 g.translate(0, +scrollOffset);
671 int scrollOffset = 0;
673 public void setScrollOffset(int value)
675 scrollOffset = value;
680 public FontMetrics getFontMetrics()
686 public Image getFadedImage()
692 public int getFadedImageWidth()