2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.datamodel.*;
27 import jalview.renderer.AnnotationRenderer;
28 import jalview.renderer.AwtRenderPanelI;
29 import jalview.util.MessageManager;
31 public class AnnotationPanel extends Panel implements AwtRenderPanelI,
32 AdjustmentListener, ActionListener, MouseListener,
43 final String HELIX = "Helix";
45 final String SHEET = "Sheet";
48 * For RNA secondary structure "stems" aka helices
50 final String STEM = "RNA Helix";
52 final String LABEL = "Label";
54 final String REMOVE = "Remove Annotation";
56 final String COLOUR = "Colour";
58 final Color HELIX_COLOUR = Color.red.darker();
60 final Color SHEET_COLOUR = Color.green.darker().darker();
70 boolean fastPaint = false;
72 // Used For mouse Dragging and resizing graphs
73 int graphStretch = -1;
75 int graphStretchY = -1;
77 boolean mouseDragging = false;
79 public static int GRAPH_HEIGHT = 40;
83 public final AnnotationRenderer renderer;
85 public AnnotationPanel(AlignmentPanel ap)
87 MAC = new jalview.util.Platform().isAMac();
91 int height = adjustPanelHeight();
92 ap.apvscroll.setValues(0, getSize().height, 0, height);
94 addMouseMotionListener(this);
96 addMouseListener(this);
98 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
99 renderer = new AnnotationRenderer();
102 public AnnotationPanel(AlignViewport av)
105 renderer = new AnnotationRenderer();
109 public void adjustmentValueChanged(AdjustmentEvent evt)
120 public void actionPerformed(ActionEvent evt)
122 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
127 Annotation[] anot = aa[activeRow].annotations;
129 if (anot.length < av.getColumnSelection().getMax())
131 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
132 System.arraycopy(anot, 0, temp, 0, anot.length);
134 aa[activeRow].annotations = anot;
138 if (av.getColumnSelection() != null
139 && av.getColumnSelection().size() > 0
140 && anot[av.getColumnSelection().getMin()] != null)
141 label = anot[av.getColumnSelection().getMin()].displayCharacter;
143 if (evt.getActionCommand().equals(REMOVE))
145 for (int i = 0; i < av.getColumnSelection().size(); i++)
147 anot[av.getColumnSelection().columnAt(i)] = null;
150 else if (evt.getActionCommand().equals(LABEL))
152 label = enterLabel(label, "Enter Label");
159 if ((label.length() > 0) && !aa[activeRow].hasText)
161 aa[activeRow].hasText = true;
164 for (int i = 0; i < av.getColumnSelection().size(); i++)
166 int index = av.getColumnSelection().columnAt(i);
168 if (!av.getColumnSelection().isVisible(index))
171 if (anot[index] == null)
173 anot[index] = new Annotation(label, "", ' ', 0);
176 anot[index].displayCharacter = label;
179 else if (evt.getActionCommand().equals(COLOUR))
181 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
184 Color col = udc.getColor();
186 for (int i = 0; i < av.getColumnSelection().size(); i++)
188 int index = av.getColumnSelection().columnAt(i);
190 if (!av.getColumnSelection().isVisible(index))
193 if (anot[index] == null)
195 anot[index] = new Annotation("", "", ' ', 0);
198 anot[index].colour = col;
205 String symbol = "\u03B1";
207 if (evt.getActionCommand().equals(HELIX))
211 else if (evt.getActionCommand().equals(SHEET))
217 // Added by LML to color stems
218 else if (evt.getActionCommand().equals(STEM))
224 if (!aa[activeRow].hasIcons)
226 aa[activeRow].hasIcons = true;
229 label = enterLabel(symbol, "Enter Label");
236 if ((label.length() > 0) && !aa[activeRow].hasText)
238 aa[activeRow].hasText = true;
241 for (int i = 0; i < av.getColumnSelection().size(); i++)
243 int index = av.getColumnSelection().columnAt(i);
245 if (!av.getColumnSelection().isVisible(index))
248 if (anot[index] == null)
250 anot[index] = new Annotation(label, "", type, 0);
253 anot[index].secondaryStructure = type;
254 anot[index].displayCharacter = label;
258 av.getAlignment().validateAnnotation(aa[activeRow]);
260 ap.alignmentChanged();
267 String enterLabel(String text, String label)
269 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
270 ap.alignFrame, "Enter Label", 400, 200, true);
273 return dialog.getName();
279 public void mousePressed(MouseEvent evt)
281 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
287 int height = -scrollOffset;
290 for (int i = 0; i < aa.length; i++)
294 height += aa[i].height;
297 if (evt.getY() < height)
303 else if (aa[i].graph > 0)
307 graphStretchY = evt.getY();
314 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
317 if (av.getColumnSelection() == null)
322 PopupMenu pop = new PopupMenu(MessageManager.getString("label.structure_type"));
325 * Just display the needed structure options
327 if (av.getAlignment().isNucleotide() == true)
329 item = new MenuItem(STEM);
330 item.addActionListener(this);
335 item = new MenuItem(HELIX);
336 item.addActionListener(this);
338 item = new MenuItem(SHEET);
339 item.addActionListener(this);
342 item = new MenuItem(LABEL);
343 item.addActionListener(this);
345 item = new MenuItem(COLOUR);
346 item.addActionListener(this);
348 item = new MenuItem(REMOVE);
349 item.addActionListener(this);
351 ap.alignFrame.add(pop);
352 pop.show(this, evt.getX(), evt.getY());
362 ap.scalePanel.mousePressed(evt);
366 public void mouseReleased(MouseEvent evt)
370 mouseDragging = false;
374 needValidating = false;
376 ap.scalePanel.mouseReleased(evt);
380 public void mouseClicked(MouseEvent evt)
384 boolean needValidating = false;
387 public void mouseDragged(MouseEvent evt)
389 if (graphStretch > -1)
391 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
393 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
395 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
397 graphStretchY = evt.getY();
398 av.calcPanelHeight();
399 needValidating = true;
400 ap.paintAlignment(true);
404 ap.scalePanel.mouseDragged(evt);
409 public void mouseMoved(MouseEvent evt)
411 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
418 int height = -scrollOffset;
419 for (int i = 0; i < aa.length; i++)
424 height += aa[i].height;
427 if (evt.getY() < height)
434 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
436 if (av.hasHiddenColumns())
438 res = av.getColumnSelection().adjustForHiddenColumns(res);
441 if (row > -1 && res < aa[row].annotations.length
442 && aa[row].annotations[res] != null)
444 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
445 if (aa[row].annotations[res].description != null)
447 text.append(" " + aa[row].annotations[res].description);
449 ap.alignFrame.statusBar.setText(text.toString());
454 public void mouseEntered(MouseEvent evt)
456 ap.scalePanel.mouseEntered(evt);
460 public void mouseExited(MouseEvent evt)
462 ap.scalePanel.mouseExited(evt);
465 public int adjustPanelHeight()
467 return adjustPanelHeight(true);
470 public int adjustPanelHeight(boolean repaint)
472 int height = av.calcPanelHeight();
473 this.setSize(new Dimension(getSize().width, height));
482 * calculate the height for visible annotation, revalidating bounds where
483 * necessary ABSTRACT GUI METHOD
485 * @return total height of annotation
488 public void addEditableColumn(int i)
492 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
498 for (int j = 0; j < aa.length; j++)
508 if (activeRes == null)
510 activeRes = new Vector();
511 activeRes.addElement(String.valueOf(i));
515 activeRes.addElement(String.valueOf(i));
519 public void update(Graphics g)
525 public void paint(Graphics g)
527 Dimension d = getSize();
529 // (av.endRes - av.startRes + 1) * av.charWidth;
530 if (imgWidth < 1 || d.height < 1)
534 if (image == null || imgWidth != image.getWidth(this)
535 || d.height != image.getHeight(this))
537 image = createImage(imgWidth, d.height);
538 gg = image.getGraphics();
539 gg.setFont(av.getFont());
540 fm = gg.getFontMetrics();
546 g.drawImage(image, 0, 0, this);
551 gg.setColor(Color.white);
552 gg.fillRect(0, 0, getSize().width, getSize().height);
553 drawComponent(gg, av.startRes, av.endRes + 1);
555 g.drawImage(image, 0, 0, this);
558 public void fastPaint(int horizontal)
561 || av.getAlignment().getAlignmentAnnotation() == null
562 || av.getAlignment().getAlignmentAnnotation().length < 1)
568 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
570 int sr = av.startRes, er = av.endRes + 1, transX = 0;
572 if (horizontal > 0) // scrollbar pulled right, image to the left
574 transX = (er - sr - horizontal) * av.charWidth;
575 sr = er - horizontal;
577 else if (horizontal < 0)
579 er = sr - horizontal;
582 gg.translate(transX, 0);
584 drawComponent(gg, sr, er);
586 gg.translate(-transX, 0);
602 public void drawComponent(Graphics g, int startRes, int endRes)
604 Font ofont = av.getFont();
607 g.setColor(Color.white);
608 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
612 fm = g.getFontMetrics();
615 if ((av.getAlignment().getAlignmentAnnotation() == null)
616 || (av.getAlignment().getAlignmentAnnotation().length < 1))
618 g.setColor(Color.white);
619 g.fillRect(0, 0, getSize().width, getSize().height);
620 g.setColor(Color.black);
621 if (av.validCharWidth)
623 g.drawString(MessageManager.getString("label.alignment_has_no_annotations"), 20, 15);
628 g.translate(0, -scrollOffset);
629 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
630 g.translate(0, +scrollOffset);
633 int scrollOffset = 0;
635 public void setScrollOffset(int value)
637 scrollOffset = value;
642 public FontMetrics getFontMetrics()
648 public Image getFadedImage()
654 public int getFadedImageWidth()
658 private int[] bounds = new int[2];
660 public int[] getVisibleVRange()
662 if (ap!=null && ap.alabels!=null)
664 int sOffset=-ap.alabels.scrollOffset;
665 int visHeight = sOffset+ap.annotationPanelHolder.getHeight();
666 bounds[0] = sOffset; bounds[1]=visHeight;