2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
21 import jalview.datamodel.*;
\r
24 import java.awt.event.*;
\r
25 import java.awt.image.*;
\r
29 import javax.swing.*;
\r
36 * @version $Revision$
\r
38 public class AnnotationPanel extends JPanel implements MouseListener,
\r
39 MouseMotionListener, ActionListener, AdjustmentListener
\r
41 static String HELIX = "Helix";
\r
42 static String SHEET = "Sheet";
\r
43 static String LABEL = "Label";
\r
44 static String REMOVE = "Remove Annotation";
\r
45 static String COLOUR = "Colour";
\r
46 static Color HELIX_COLOUR = Color.red.darker();
\r
47 static Color SHEET_COLOUR = Color.green.darker().darker();
\r
49 /** DOCUMENT ME!! */
\r
50 public static int GRAPH_HEIGHT = 40;
\r
55 BufferedImage image;
\r
59 boolean fastPaint = false;
\r
62 * Creates a new AnnotationPanel object.
\r
64 * @param ap DOCUMENT ME!
\r
66 public AnnotationPanel(AlignmentPanel ap)
\r
68 ToolTipManager.sharedInstance().registerComponent(this);
\r
69 ToolTipManager.sharedInstance().setInitialDelay(0);
\r
70 ToolTipManager.sharedInstance().setDismissDelay(10000);
\r
73 this.setLayout(null);
\r
74 addMouseListener(this);
\r
75 addMouseMotionListener(this);
\r
76 ap.annotationScroller.getVerticalScrollBar().addAdjustmentListener(this);
\r
82 * @param evt DOCUMENT ME!
\r
84 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
86 ap.alabels.setScrollOffset(-evt.getValue());
\r
92 public void adjustPanelHeight()
\r
94 // setHeight of panels
\r
96 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
101 for (int i = 0; i < aa.length; i++)
\r
103 if (!aa[i].visible)
\r
112 aa[i].height += av.charHeight;
\r
115 if (aa[i].hasIcons)
\r
117 aa[i].height += 16;
\r
122 aa[i].height += GRAPH_HEIGHT;
\r
125 if (aa[i].height == 0)
\r
130 height += aa[i].height;
\r
138 this.setPreferredSize(new Dimension(1, height));
\r
144 * @param col DOCUMENT ME!
\r
146 public void removeEditableColumn(int col)
\r
148 if (activeRow == -1)
\r
150 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
152 for (int j = 0; j < aa.length; j++)
\r
154 if (aa[j].editable)
\r
163 if ((activeRes != null) && activeRes.contains(String.valueOf(col)))
\r
165 activeRes.removeElement(String.valueOf(col));
\r
174 * @param col DOCUMENT ME!
\r
176 public void addEditableColumn(int col)
\r
178 if (activeRow == -1)
\r
180 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
182 for (int j = 0; j < aa.length; j++)
\r
184 if (aa[j].editable)
\r
193 if (activeRes == null)
\r
195 activeRes = new Vector();
\r
198 if (!activeRes.contains(String.valueOf(col)))
\r
200 activeRes.addElement(String.valueOf(col));
\r
209 * @param evt DOCUMENT ME!
\r
211 public void actionPerformed(ActionEvent evt)
\r
213 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
214 Annotation[] anot = aa[activeRow].annotations;
\r
216 if (evt.getActionCommand().equals(REMOVE))
\r
218 for (int i = 0; i < activeRes.size(); i++)
\r
220 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
221 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
224 else if (evt.getActionCommand().equals(LABEL))
\r
226 String label = JOptionPane.showInputDialog(this, "Enter Label ",
\r
227 "Enter label", JOptionPane.QUESTION_MESSAGE);
\r
234 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
236 aa[activeRow].hasText = true;
\r
239 for (int i = 0; i < activeRes.size(); i++)
\r
241 int index = Integer.parseInt(activeRes.get(i).toString());
\r
243 if (anot[index] == null)
\r
245 anot[index] = new Annotation(label, "", ' ', 0);
\r
248 anot[index].displayCharacter = label;
\r
251 else if (evt.getActionCommand().equals(COLOUR))
\r
253 Color col = JColorChooser.showDialog(this,
\r
254 "Choose foreground colour", Color.black);
\r
256 for (int i = 0; i < activeRes.size(); i++)
\r
258 int index = Integer.parseInt(activeRes.get(i).toString());
\r
260 if (anot[index] == null)
\r
262 anot[index] = new Annotation("", "", ' ', 0);
\r
265 anot[index].colour = col;
\r
268 else // HELIX OR SHEET
\r
271 String symbol = "\u03B1";
\r
273 if (evt.getActionCommand().equals(HELIX))
\r
277 else if (evt.getActionCommand().equals(SHEET))
\r
283 if (!aa[activeRow].hasIcons)
\r
285 aa[activeRow].hasIcons = true;
\r
288 String label = JOptionPane.showInputDialog("Enter a label for the structure?",
\r
296 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
298 aa[activeRow].hasText = true;
\r
301 for (int i = 0; i < activeRes.size(); i++)
\r
303 int index = Integer.parseInt(activeRes.get(i).toString());
\r
305 if (anot[index] == null)
\r
307 anot[index] = new Annotation(label, "", type, 0);
\r
310 anot[index].secondaryStructure = type;
\r
311 anot[index].displayCharacter = label;
\r
315 adjustPanelHeight();
\r
325 * @param evt DOCUMENT ME!
\r
327 public void mousePressed(MouseEvent evt)
\r
332 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
334 for (int i = 0; i < aa.length; i++)
\r
338 height += aa[i].height;
\r
341 if (evt.getY() < height)
\r
343 if (aa[i].editable)
\r
356 if (SwingUtilities.isRightMouseButton(evt))
\r
358 if (activeRes == null)
\r
363 JPopupMenu pop = new JPopupMenu("Structure type");
\r
364 JMenuItem item = new JMenuItem(HELIX);
\r
365 item.addActionListener(this);
\r
367 item = new JMenuItem(SHEET);
\r
368 item.addActionListener(this);
\r
370 item = new JMenuItem(LABEL);
\r
371 item.addActionListener(this);
\r
373 item = new JMenuItem(COLOUR);
\r
374 item.addActionListener(this);
\r
376 item = new JMenuItem(REMOVE);
\r
377 item.addActionListener(this);
\r
379 pop.show(this, evt.getX(), evt.getY());
\r
389 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
391 if (evt.isControlDown() || evt.isAltDown())
\r
393 addEditableColumn(res);
\r
395 else if (evt.isShiftDown())
\r
397 if (activeRes == null)
\r
399 activeRes = new Vector();
\r
403 int start = Integer.parseInt(activeRes.get(activeRes.size() -
\r
414 for (int n = start; n <= end; n++)
\r
416 addEditableColumn(n);
\r
422 activeRes = new Vector();
\r
423 activeRes.addElement(String.valueOf(res));
\r
432 * @param evt DOCUMENT ME!
\r
434 public void mouseReleased(MouseEvent evt)
\r
441 * @param evt DOCUMENT ME!
\r
443 public void mouseEntered(MouseEvent evt)
\r
450 * @param evt DOCUMENT ME!
\r
452 public void mouseExited(MouseEvent evt)
\r
459 * @param evt DOCUMENT ME!
\r
461 public void mouseDragged(MouseEvent evt)
\r
468 * @param evt DOCUMENT ME!
\r
470 public void mouseMoved(MouseEvent evt)
\r
472 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
482 for (int i = 0; i < aa.length; i++)
\r
486 height += aa[i].height;
\r
489 if (evt.getY() < height)
\r
497 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
499 if ((row > -1) && (res < aa[row].annotations.length) &&
\r
500 (aa[row].annotations[res] != null))
\r
502 this.setToolTipText(aa[row].annotations[res].description);
\r
504 StringBuffer text = new StringBuffer("Sequence position " +
\r
505 (res + 1) + " " + aa[row].annotations[res].description);
\r
506 ap.alignFrame.statusBar.setText(text.toString());
\r
513 * @param evt DOCUMENT ME!
\r
515 public void mouseClicked(MouseEvent evt)
\r
522 * @param g DOCUMENT ME!
\r
524 public void paintComponent(Graphics g)
\r
526 g.setColor(Color.white);
\r
527 g.fillRect(0, 0, getWidth(), getHeight());
\r
531 g.drawImage(image, 0, 0, this);
\r
536 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
\r
538 if (image == null || imgWidth != image.getWidth())
\r
540 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
\r
541 BufferedImage.TYPE_INT_RGB);
\r
542 gg = (Graphics2D) image.getGraphics();
\r
543 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
544 RenderingHints.VALUE_ANTIALIAS_ON);
\r
546 gg.setFont(av.getFont());
\r
547 fm = gg.getFontMetrics();
\r
550 drawComponent(gg, av.startRes, av.endRes + 1);
\r
551 g.drawImage(image, 0, 0, this);
\r
557 * @param horizontal DOCUMENT ME!
\r
559 public void fastPaint(int horizontal)
\r
561 if ((horizontal == 0) ||
\r
562 (av.alignment.getAlignmentAnnotation() == null) ||
\r
563 (av.alignment.getAlignmentAnnotation().length < 1))
\r
570 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
\r
572 int sr = av.startRes;
\r
573 int er = av.endRes + 1;
\r
576 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
578 transX = (er - sr - horizontal) * av.charWidth;
\r
579 sr = er - horizontal;
\r
581 else if (horizontal < 0)
\r
583 er = sr - horizontal;
\r
586 gg.translate(transX, 0);
\r
588 drawComponent(gg, sr, er);
\r
590 gg.translate(-transX, 0);
\r
601 * @param g DOCUMENT ME!
\r
602 * @param startRes DOCUMENT ME!
\r
603 * @param endRes DOCUMENT ME!
\r
605 public void drawComponent(Graphics2D g, int startRes, int endRes)
\r
607 g.setColor(Color.white);
\r
608 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
\r
610 if ((av.alignment.getAlignmentAnnotation() == null) ||
\r
611 (av.alignment.getAlignmentAnnotation().length < 1))
\r
613 g.setColor(Color.white);
\r
614 g.fillRect(0, 0, getWidth(), getHeight());
\r
615 g.setColor(Color.black);
\r
616 g.drawString("Alignment has no annotations", 20, 15);
\r
621 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
626 char[] lastSS = new char[aa.length];
\r
627 int[] lastSSX = new int[aa.length];
\r
628 int iconOffset = av.charHeight / 2;
\r
629 boolean validRes = false;
\r
632 for (int i = 0; i < aa.length; i++)
\r
634 AlignmentAnnotation row = aa[i];
\r
644 // this is so that we draw the characters below the graph
\r
649 y -= av.charHeight;
\r
655 iconOffset = av.charHeight / 2;
\r
662 for (j = startRes; j < endRes; j++)
\r
664 if ((row.annotations.length <= j) ||
\r
665 (row.annotations[j] == null))
\r
674 x = (j - startRes) * av.charWidth;
\r
676 if (activeRow == i)
\r
678 g.setColor(Color.red);
\r
680 if (activeRes != null)
\r
682 for (int n = 0; n < activeRes.size(); n++)
\r
684 int v = Integer.parseInt(activeRes.get(n).toString());
\r
688 g.fillRect((j - startRes) * av.charWidth, y,
\r
689 av.charWidth, row.height);
\r
696 (row.annotations[j].displayCharacter.length() > 0))
\r
699 int charOffset = (av.charWidth -
\r
700 fm.charWidth(row.annotations[j].displayCharacter.charAt(
\r
702 g.setColor(row.annotations[j].colour);
\r
704 if (j == 0 || row.isGraph)
\r
706 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
707 y + iconOffset + 3);
\r
709 else if (((row.annotations[j - 1] == null) ||
\r
710 (row.annotations[j].displayCharacter != row.annotations[j -
\r
711 1].displayCharacter)))
\r
713 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
714 y + iconOffset + 3);
\r
721 (row.annotations[j].secondaryStructure != lastSS[i]))
\r
726 g.setColor(HELIX_COLOUR);
\r
727 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
728 x - lastSSX[i], 7, 8, 8);
\r
733 g.setColor(SHEET_COLOUR);
\r
734 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
735 x - lastSSX[i] - 4, 7);
\r
736 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
739 y + iconOffset, y + 14 + iconOffset,
\r
749 g.setColor(Color.gray);
\r
750 g.fillRect(lastSSX[i], y + 6 + iconOffset,
\r
751 x - lastSSX[i], 2);
\r
758 lastSS[i] = row.annotations[j].secondaryStructure;
\r
769 if (validRes && row.isGraph)
\r
771 g.setColor(new Color(0, 0, 180));
\r
773 int height = (int) ((row.annotations[j].value / row.graphMax) * GRAPH_HEIGHT);
\r
775 if (row.windowLength > 1)
\r
779 for (int i2 = j - (row.windowLength / 2);
\r
780 i2 < (j + (row.windowLength / 2)); i2++)
\r
782 if ((i2 < 0) || (i2 >= av.alignment.getWidth()))
\r
787 total += row.annotations[i2].value;
\r
790 total /= row.windowLength;
\r
791 height = (int) ((total / row.graphMax) * GRAPH_HEIGHT);
\r
794 g.setColor(row.annotations[j].colour);
\r
795 g.fillRect(x, y - height, av.charWidth, height);
\r
806 g.setColor(HELIX_COLOUR);
\r
807 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
808 x - lastSSX[i], 7, 8, 8);
\r
813 g.setColor(SHEET_COLOUR);
\r
814 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
815 x - lastSSX[i] - 4, 7);
\r
816 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
819 y + iconOffset, y + 14 + iconOffset,
\r
829 g.setColor(Color.gray);
\r
830 g.fillRect(lastSSX[i], y + 6 + iconOffset, x - lastSSX[i], 2);
\r
836 if (row.isGraph && row.hasText)
\r
838 y += av.charHeight;
\r
848 // used by overview window
\r
849 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y, int sRes, int eRes)
\r
851 g.setColor(Color.white);
\r
852 g.fillRect(0, 0, width, y);
\r
853 g.setColor(new Color(0, 0, 180));
\r
857 for (int j = sRes; j < eRes; j++)
\r
859 g.setColor(new Color(0, 0, 180));
\r
861 int height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
\r
862 g.fillRect(x, y - height, av.charWidth, height);
\r