2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.renderer.AnnotationRenderer;
26 import jalview.renderer.AwtRenderPanelI;
27 import jalview.util.MessageManager;
28 import jalview.util.Platform;
30 import java.awt.Color;
31 import java.awt.Dimension;
33 import java.awt.FontMetrics;
34 import java.awt.Graphics;
35 import java.awt.Image;
36 import awt2swing.MenuItem;
37 import awt2swing.Panel;
38 import awt2swing.PopupMenu;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.AdjustmentEvent;
42 import java.awt.event.AdjustmentListener;
43 import java.awt.event.InputEvent;
44 import java.awt.event.MouseEvent;
45 import java.awt.event.MouseListener;
46 import java.awt.event.MouseMotionListener;
48 public class AnnotationPanel extends Panel implements AwtRenderPanelI,
49 AdjustmentListener, ActionListener, MouseListener,
58 final String HELIX = "Helix";
60 final String SHEET = "Sheet";
63 * For RNA secondary structure "stems" aka helices
65 final String STEM = "RNA Helix";
67 final String LABEL = "Label";
69 final String REMOVE = "Remove Annotation";
71 final String COLOUR = "Colour";
73 final Color HELIX_COLOUR = Color.red.darker();
75 final Color SHEET_COLOUR = Color.green.darker().darker();
85 boolean fastPaint = false;
87 // Used For mouse Dragging and resizing graphs
88 int graphStretch = -1;
90 int graphStretchY = -1;
92 boolean mouseDragging = false;
94 public static int GRAPH_HEIGHT = 40;
98 public final AnnotationRenderer renderer;
100 public AnnotationPanel(AlignmentPanel ap)
102 MAC = Platform.isAMac();
106 int height = adjustPanelHeight();
107 ap.apvscroll.setValues(0, getSize().height, 0, height);
109 addMouseMotionListener(this);
111 addMouseListener(this);
113 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
114 renderer = new AnnotationRenderer();
117 public AnnotationPanel(AlignViewport av)
120 renderer = new AnnotationRenderer();
124 public void adjustmentValueChanged(AdjustmentEvent evt)
135 public void actionPerformed(ActionEvent evt)
137 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
142 Annotation[] anot = aa[activeRow].annotations;
144 if (anot.length < av.getColumnSelection().getMax())
146 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
147 System.arraycopy(anot, 0, temp, 0, anot.length);
149 aa[activeRow].annotations = anot;
153 if (av.getColumnSelection() != null
154 && av.getColumnSelection().size() > 0
155 && anot[av.getColumnSelection().getMin()] != null)
157 label = anot[av.getColumnSelection().getMin()].displayCharacter;
160 if (evt.getActionCommand().equals(REMOVE))
162 for (int i = 0; i < av.getColumnSelection().size(); i++)
164 anot[av.getColumnSelection().columnAt(i)] = null;
167 else if (evt.getActionCommand().equals(LABEL))
169 label = enterLabel(label, "Enter Label");
176 if ((label.length() > 0) && !aa[activeRow].hasText)
178 aa[activeRow].hasText = true;
181 for (int i = 0; i < av.getColumnSelection().size(); i++)
183 int index = av.getColumnSelection().columnAt(i);
185 if (!av.getColumnSelection().isVisible(index))
190 if (anot[index] == null)
192 anot[index] = new Annotation(label, "", ' ', 0);
195 anot[index].displayCharacter = label;
198 else if (evt.getActionCommand().equals(COLOUR))
200 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
203 Color col = udc.getColor();
205 for (int i = 0; i < av.getColumnSelection().size(); i++)
207 int index = av.getColumnSelection().columnAt(i);
209 if (!av.getColumnSelection().isVisible(index))
214 if (anot[index] == null)
216 anot[index] = new Annotation("", "", ' ', 0);
219 anot[index].colour = col;
226 String symbol = "\u03B1";
228 if (evt.getActionCommand().equals(HELIX))
232 else if (evt.getActionCommand().equals(SHEET))
238 // Added by LML to color stems
239 else if (evt.getActionCommand().equals(STEM))
245 if (!aa[activeRow].hasIcons)
247 aa[activeRow].hasIcons = true;
250 label = enterLabel(symbol, "Enter Label");
257 if ((label.length() > 0) && !aa[activeRow].hasText)
259 aa[activeRow].hasText = true;
260 if (evt.getActionCommand().equals(STEM))
262 aa[activeRow].showAllColLabels = true;
266 for (int i = 0; i < av.getColumnSelection().size(); i++)
268 int index = av.getColumnSelection().columnAt(i);
270 if (!av.getColumnSelection().isVisible(index))
275 if (anot[index] == null)
277 anot[index] = new Annotation(label, "", type, 0);
280 anot[index].secondaryStructure = type != 'S' ? type : label
281 .length() == 0 ? ' ' : label.charAt(0);
282 anot[index].displayCharacter = label;
286 av.getAlignment().validateAnnotation(aa[activeRow]);
288 ap.alignmentChanged();
295 String enterLabel(String text, String label)
297 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
298 ap.alignFrame, "Enter Label", 400, 200, true);
302 return dialog.getName();
311 public void mousePressed(MouseEvent evt)
313 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
319 int height = -scrollOffset;
322 for (int i = 0; i < aa.length; i++)
326 height += aa[i].height;
329 if (evt.getY() < height)
335 else if (aa[i].graph > 0)
339 graphStretchY = evt.getY();
346 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
349 if (av.getColumnSelection() == null)
354 PopupMenu pop = new PopupMenu(
355 MessageManager.getString("label.structure_type"));
358 * Just display the needed structure options
360 if (av.getAlignment().isNucleotide() == true)
362 item = new MenuItem(STEM);
363 item.addActionListener(this);
368 item = new MenuItem(HELIX);
369 item.addActionListener(this);
371 item = new MenuItem(SHEET);
372 item.addActionListener(this);
375 item = new MenuItem(LABEL);
376 item.addActionListener(this);
378 item = new MenuItem(COLOUR);
379 item.addActionListener(this);
381 item = new MenuItem(REMOVE);
382 item.addActionListener(this);
384 ap.alignFrame.add(pop);
385 pop.show(this, evt.getX(), evt.getY());
390 ap.scalePanel.mousePressed(evt);
394 public void mouseReleased(MouseEvent evt)
398 mouseDragging = false;
402 needValidating = false;
404 ap.scalePanel.mouseReleased(evt);
408 public void mouseClicked(MouseEvent evt)
412 boolean needValidating = false;
415 public void mouseDragged(MouseEvent evt)
417 if (graphStretch > -1)
419 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
421 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
423 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
425 graphStretchY = evt.getY();
426 av.calcPanelHeight();
427 needValidating = true;
428 ap.paintAlignment(true);
432 ap.scalePanel.mouseDragged(evt);
437 public void mouseMoved(MouseEvent evt)
439 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
446 int height = -scrollOffset;
447 for (int i = 0; i < aa.length; i++)
452 height += aa[i].height;
455 if (evt.getY() < height)
462 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
464 if (av.hasHiddenColumns())
466 res = av.getColumnSelection().adjustForHiddenColumns(res);
469 if (row > -1 && res < aa[row].annotations.length
470 && aa[row].annotations[res] != null)
472 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
473 if (aa[row].annotations[res].description != null)
475 text.append(" " + aa[row].annotations[res].description);
477 ap.alignFrame.setStatus(text.toString());
482 public void mouseEntered(MouseEvent evt)
484 ap.scalePanel.mouseEntered(evt);
488 public void mouseExited(MouseEvent evt)
490 ap.scalePanel.mouseExited(evt);
493 public int adjustPanelHeight()
495 return adjustPanelHeight(true);
498 public int adjustPanelHeight(boolean repaint)
500 int height = av.calcPanelHeight();
501 this.setSize(new Dimension(getSize().width, height));
510 * calculate the height for visible annotation, revalidating bounds where
511 * necessary ABSTRACT GUI METHOD
513 * @return total height of annotation
516 public void addEditableColumn(int i)
520 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
526 for (int j = 0; j < aa.length; j++)
538 public void paintComponent(Graphics g)
540 if (av.getWrapAlignment() || !av.isShowAnnotation())
543 Dimension d = getSize();
545 // (av.endRes - av.startRes + 1) * av.charWidth;
546 if (imgWidth < 1 || d.height < 1)
550 if (image == null || imgWidth != image.getWidth(this)
551 || d.height != image.getHeight(this))
553 image = createImage(imgWidth, d.height);
554 gg = image.getGraphics();
555 gg.setFont(av.getFont());
556 fm = gg.getFontMetrics();
562 g.drawImage(image, 0, 0, this);
567 gg.setColor(Color.white);
568 gg.fillRect(0, 0, getSize().width, getSize().height);
569 drawComponent(gg, av.startRes, av.endRes + 1);
571 g.drawImage(image, 0, 0, this);
574 public void fastPaint(int horizontal)
577 || av.getAlignment().getAlignmentAnnotation() == null
578 || av.getAlignment().getAlignmentAnnotation().length < 1)
584 gg.copyArea(0, 0, imgWidth, getSize().height,
585 -horizontal * av.getCharWidth(), 0);
586 int sr = av.startRes, er = av.endRes + 1, transX = 0;
588 if (horizontal > 0) // scrollbar pulled right, image to the left
590 transX = (er - sr - horizontal) * av.getCharWidth();
591 sr = er - horizontal;
593 else if (horizontal < 0)
595 er = sr - horizontal;
598 gg.translate(transX, 0);
600 drawComponent(gg, sr, er);
602 gg.translate(-transX, 0);
618 public void drawComponent(Graphics g, int startRes, int endRes)
620 Font ofont = av.getFont();
623 g.setColor(Color.white);
624 g.fillRect(0, 0, (endRes - startRes) * av.getCharWidth(),
629 fm = g.getFontMetrics();
632 if ((av.getAlignment().getAlignmentAnnotation() == null)
633 || (av.getAlignment().getAlignmentAnnotation().length < 1))
635 g.setColor(Color.white);
636 g.fillRect(0, 0, getSize().width, getSize().height);
637 g.setColor(Color.black);
638 if (av.validCharWidth)
640 g.drawString(MessageManager
641 .getString("label.alignment_has_no_annotations"), 20, 15);
646 g.translate(0, -scrollOffset);
647 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
648 g.translate(0, +scrollOffset);
651 int scrollOffset = 0;
653 public void setScrollOffset(int value, boolean repaint)
655 scrollOffset = value;
663 public FontMetrics getFontMetrics()
669 public Image getFadedImage()
675 public int getFadedImageWidth()
680 private int[] bounds = new int[2];
683 public int[] getVisibleVRange()
685 if (ap != null && ap.alabels != null)
687 int sOffset = -ap.alabels.scrollOffset;
688 int visHeight = sOffset + ap.annotationPanelHolder.getHeight();
690 bounds[1] = visHeight;