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 static String HELIX = "Helix";
46 static String SHEET = "Sheet";
48 static String LABEL = "Label";
50 static String REMOVE = "Remove Annotation";
52 static String COLOUR = "Colour";
54 static Color HELIX_COLOUR = Color.red.darker();
56 static Color SHEET_COLOUR = Color.green.darker().darker();
66 boolean fastPaint = false;
68 // Used For mouse Dragging and resizing graphs
69 int graphStretch = -1;
71 int graphStretchY = -1;
73 boolean mouseDragging = false;
75 public static int GRAPH_HEIGHT = 40;
79 public final AnnotationRenderer renderer;
81 public AnnotationPanel(AlignmentPanel ap)
83 MAC = new jalview.util.Platform().isAMac();
87 int height = adjustPanelHeight();
88 ap.apvscroll.setValues(0, getSize().height, 0, height);
90 addMouseMotionListener(this);
92 addMouseListener(this);
94 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
95 renderer = new AnnotationRenderer();
98 public AnnotationPanel(AlignViewport av)
101 renderer = new AnnotationRenderer();
104 public void adjustmentValueChanged(AdjustmentEvent evt)
114 public void actionPerformed(ActionEvent evt)
116 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
121 Annotation[] anot = aa[activeRow].annotations;
123 if (anot.length < av.getColumnSelection().getMax())
125 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
126 System.arraycopy(anot, 0, temp, 0, anot.length);
128 aa[activeRow].annotations = anot;
132 if (av.getColumnSelection() != null && av.getColumnSelection().size() > 0
133 && anot[av.getColumnSelection().getMin()] != null)
134 label = anot[av.getColumnSelection().getMin()].displayCharacter;
136 if (evt.getActionCommand().equals(REMOVE))
138 for (int i = 0; i < av.getColumnSelection().size(); i++)
140 anot[av.getColumnSelection().columnAt(i)] = null;
143 else if (evt.getActionCommand().equals(LABEL))
145 label = enterLabel(label, "Enter Label");
152 if ((label.length() > 0) && !aa[activeRow].hasText)
154 aa[activeRow].hasText = true;
157 for (int i = 0; i < av.getColumnSelection().size(); i++)
159 int index = av.getColumnSelection().columnAt(i);
161 if (!av.getColumnSelection().isVisible(index))
164 if (anot[index] == null)
166 anot[index] = new Annotation(label, "", ' ', 0);
169 anot[index].displayCharacter = label;
172 else if (evt.getActionCommand().equals(COLOUR))
174 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
177 Color col = udc.getColor();
179 for (int i = 0; i < av.getColumnSelection().size(); i++)
181 int index = av.getColumnSelection().columnAt(i);
183 if (!av.getColumnSelection().isVisible(index))
186 if (anot[index] == null)
188 anot[index] = new Annotation("", "", ' ', 0);
191 anot[index].colour = col;
198 String symbol = "\u03B1";
200 if (evt.getActionCommand().equals(HELIX))
204 else if (evt.getActionCommand().equals(SHEET))
210 if (!aa[activeRow].hasIcons)
212 aa[activeRow].hasIcons = true;
215 label = enterLabel(symbol, "Enter Label");
222 if ((label.length() > 0) && !aa[activeRow].hasText)
224 aa[activeRow].hasText = true;
227 for (int i = 0; i < av.getColumnSelection().size(); i++)
229 int index = av.getColumnSelection().columnAt(i);
231 if (!av.getColumnSelection().isVisible(index))
234 if (anot[index] == null)
236 anot[index] = new Annotation(label, "", type, 0);
239 anot[index].secondaryStructure = type;
240 anot[index].displayCharacter = label;
244 aa[activeRow].validateRangeAndDisplay();
252 String enterLabel(String text, String label)
254 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
255 ap.alignFrame, "Enter Label", 400, 200, true);
258 return dialog.getName();
263 public void mousePressed(MouseEvent evt)
265 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
271 int height = -scrollOffset;
274 for (int i = 0; i < aa.length; i++)
278 height += aa[i].height;
281 if (evt.getY() < height)
287 else if (aa[i].graph > 0)
291 graphStretchY = evt.getY();
298 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
301 if (av.getColumnSelection() == null)
306 PopupMenu pop = new PopupMenu("Structure type");
307 MenuItem item = new MenuItem(HELIX);
308 item.addActionListener(this);
310 item = new MenuItem(SHEET);
311 item.addActionListener(this);
313 item = new MenuItem(LABEL);
314 item.addActionListener(this);
316 item = new MenuItem(COLOUR);
317 item.addActionListener(this);
319 item = new MenuItem(REMOVE);
320 item.addActionListener(this);
322 ap.alignFrame.add(pop);
323 pop.show(this, evt.getX(), evt.getY());
333 ap.scalePanel.mousePressed(evt);
336 public void mouseReleased(MouseEvent evt)
340 mouseDragging = false;
344 needValidating = false;
346 ap.scalePanel.mouseReleased(evt);
349 public void mouseClicked(MouseEvent evt)
353 boolean needValidating = false;
355 public void mouseDragged(MouseEvent evt)
357 if (graphStretch > -1)
359 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
361 if (av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight < 0)
363 av.getAlignment().getAlignmentAnnotation()[graphStretch].graphHeight = 0;
365 graphStretchY = evt.getY();
367 needValidating = true;
368 ap.paintAlignment(true);
372 ap.scalePanel.mouseDragged(evt);
376 public void mouseMoved(MouseEvent evt)
378 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
385 int height = -scrollOffset;
386 for (int i = 0; i < aa.length; i++)
391 height += aa[i].height;
394 if (evt.getY() < height)
401 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
403 if (av.hasHiddenColumns())
405 res = av.getColumnSelection().adjustForHiddenColumns(res);
408 if (row > -1 && res < aa[row].annotations.length
409 && aa[row].annotations[res] != null)
411 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
412 if (aa[row].annotations[res].description != null)
414 text.append(" " + aa[row].annotations[res].description);
416 ap.alignFrame.statusBar.setText(text.toString());
420 public void mouseEntered(MouseEvent evt)
422 ap.scalePanel.mouseEntered(evt);
425 public void mouseExited(MouseEvent evt)
427 ap.scalePanel.mouseExited(evt);
430 public int adjustPanelHeight()
432 return adjustPanelHeight(true);
435 public int adjustPanelHeight(boolean repaint)
437 int height = calcPanelHeight();
438 this.setSize(new Dimension(getSize().width, height));
446 * calculate the height for visible annotation, revalidating bounds where necessary
447 * ABSTRACT GUI METHOD
448 * @return total height of annotation
450 public int calcPanelHeight()
452 // setHeight of panels
453 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
458 for (int i = 0; i < aa.length; i++)
469 aa[i].height += av.charHeight;
479 aa[i].height += aa[i].graphHeight;
482 if (aa[i].height == 0)
487 height += aa[i].height;
499 public void addEditableColumn(int i)
503 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
509 for (int j = 0; j < aa.length; j++)
519 if (activeRes == null)
521 activeRes = new Vector();
522 activeRes.addElement(String.valueOf(i));
526 activeRes.addElement(String.valueOf(i));
529 public void update(Graphics g)
534 public void paint(Graphics g)
536 Dimension d = getSize();
538 // (av.endRes - av.startRes + 1) * av.charWidth;
539 if (imgWidth<1 || d.height<1)
543 if (image == null || imgWidth != image.getWidth(this) || d.height != image.getHeight(this))
545 image = createImage(imgWidth, d.height);
546 gg = image.getGraphics();
547 gg.setFont(av.getFont());
548 fm = gg.getFontMetrics();
554 g.drawImage(image, 0, 0, this);
559 gg.setColor(Color.white);
560 gg.fillRect(0, 0, getSize().width, getSize().height);
561 drawComponent(gg, av.startRes, av.endRes + 1);
563 g.drawImage(image, 0, 0, this);
566 public void fastPaint(int horizontal)
568 if (horizontal == 0 || av.getAlignment().getAlignmentAnnotation() == null
569 || av.getAlignment().getAlignmentAnnotation().length < 1)
575 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
577 int sr = av.startRes, er = av.endRes + 1, transX = 0;
579 if (horizontal > 0) // scrollbar pulled right, image to the left
581 transX = (er - sr - horizontal) * av.charWidth;
582 sr = er - horizontal;
584 else if (horizontal < 0)
586 er = sr - horizontal;
589 gg.translate(transX, 0);
591 drawComponent(gg, sr, er);
593 gg.translate(-transX, 0);
609 public void drawComponent(Graphics g, int startRes, int endRes)
611 Font ofont = av.getFont();
614 g.setColor(Color.white);
615 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
619 fm = g.getFontMetrics();
622 if ((av.getAlignment().getAlignmentAnnotation() == null)
623 || (av.getAlignment().getAlignmentAnnotation().length < 1))
625 g.setColor(Color.white);
626 g.fillRect(0, 0, getSize().width, getSize().height);
627 g.setColor(Color.black);
628 if (av.validCharWidth)
630 g.drawString("Alignment has no annotations", 20, 15);
635 g.translate(0, -scrollOffset);
636 renderer.drawComponent(this, av, g, activeRow, startRes, endRes);
637 g.translate(0, +scrollOffset);
640 int scrollOffset = 0;
642 public void setScrollOffset(int value)
644 scrollOffset = value;
649 public FontMetrics getFontMetrics()
655 public Image getFadedImage()
661 public int getFadedImageWidth()