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
79 public AnnotationPanel(AlignViewport av)
\r
88 * @param evt DOCUMENT ME!
\r
90 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
92 ap.alabels.setScrollOffset(-evt.getValue());
\r
98 public int adjustPanelHeight()
\r
100 // setHeight of panels
\r
102 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
107 for (int i = 0; i < aa.length; i++)
\r
109 if (!aa[i].visible)
\r
118 aa[i].height += av.charHeight;
\r
121 if (aa[i].hasIcons)
\r
123 aa[i].height += 16;
\r
128 aa[i].height += GRAPH_HEIGHT;
\r
131 if (aa[i].height == 0)
\r
136 height += aa[i].height;
\r
144 this.setPreferredSize(new Dimension(1, height));
\r
152 * @param col DOCUMENT ME!
\r
154 public void removeEditableColumn(int col)
\r
156 if (activeRow == -1)
\r
158 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
162 for (int j = 0; j < aa.length; j++)
\r
164 if (aa[j].editable)
\r
173 if ((activeRes != null) && activeRes.contains(String.valueOf(col)))
\r
175 activeRes.removeElement(String.valueOf(col));
\r
184 * @param col DOCUMENT ME!
\r
186 public void addEditableColumn(int col)
\r
188 if (activeRow == -1)
\r
190 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
194 for (int j = 0; j < aa.length; j++)
\r
196 if (aa[j].editable)
\r
205 if (activeRes == null)
\r
207 activeRes = new Vector();
\r
210 if (!activeRes.contains(String.valueOf(col)))
\r
212 activeRes.addElement(String.valueOf(col));
\r
221 * @param evt DOCUMENT ME!
\r
223 public void actionPerformed(ActionEvent evt)
\r
225 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
226 Annotation[] anot = aa[activeRow].annotations;
\r
228 if (evt.getActionCommand().equals(REMOVE))
\r
230 for (int i = 0; i < activeRes.size(); i++)
\r
232 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
233 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
236 else if (evt.getActionCommand().equals(LABEL))
\r
238 String label = JOptionPane.showInputDialog(this, "Enter Label ",
\r
239 "Enter label", JOptionPane.QUESTION_MESSAGE);
\r
246 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
248 aa[activeRow].hasText = true;
\r
251 for (int i = 0; i < activeRes.size(); i++)
\r
253 int index = Integer.parseInt(activeRes.get(i).toString());
\r
255 if (anot[index] == null)
\r
257 anot[index] = new Annotation(label, "", ' ', 0);
\r
260 anot[index].displayCharacter = label;
\r
263 else if (evt.getActionCommand().equals(COLOUR))
\r
265 Color col = JColorChooser.showDialog(this,
\r
266 "Choose foreground colour", Color.black);
\r
268 for (int i = 0; i < activeRes.size(); i++)
\r
270 int index = Integer.parseInt(activeRes.get(i).toString());
\r
272 if (anot[index] == null)
\r
274 anot[index] = new Annotation("", "", ' ', 0);
\r
277 anot[index].colour = col;
\r
280 else // HELIX OR SHEET
\r
283 String symbol = "\u03B1";
\r
285 if (evt.getActionCommand().equals(HELIX))
\r
289 else if (evt.getActionCommand().equals(SHEET))
\r
295 if (!aa[activeRow].hasIcons)
\r
297 aa[activeRow].hasIcons = true;
\r
300 String label = JOptionPane.showInputDialog("Enter a label for the structure?",
\r
308 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
310 aa[activeRow].hasText = true;
\r
313 for (int i = 0; i < activeRes.size(); i++)
\r
315 int index = Integer.parseInt(activeRes.get(i).toString());
\r
317 if (anot[index] == null)
\r
319 anot[index] = new Annotation(label, "", type, 0);
\r
322 anot[index].secondaryStructure = type;
\r
323 anot[index].displayCharacter = label;
\r
327 adjustPanelHeight();
\r
337 * @param evt DOCUMENT ME!
\r
339 public void mousePressed(MouseEvent evt)
\r
344 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
346 for (int i = 0; i < aa.length; i++)
\r
350 height += aa[i].height;
\r
353 if (evt.getY() < height)
\r
355 if (aa[i].editable)
\r
368 if (SwingUtilities.isRightMouseButton(evt))
\r
370 if (activeRes == null)
\r
375 JPopupMenu pop = new JPopupMenu("Structure type");
\r
376 JMenuItem item = new JMenuItem(HELIX);
\r
377 item.addActionListener(this);
\r
379 item = new JMenuItem(SHEET);
\r
380 item.addActionListener(this);
\r
382 item = new JMenuItem(LABEL);
\r
383 item.addActionListener(this);
\r
385 item = new JMenuItem(COLOUR);
\r
386 item.addActionListener(this);
\r
388 item = new JMenuItem(REMOVE);
\r
389 item.addActionListener(this);
\r
391 pop.show(this, evt.getX(), evt.getY());
\r
401 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
403 if (evt.isControlDown() || evt.isAltDown())
\r
405 addEditableColumn(res);
\r
407 else if (evt.isShiftDown())
\r
409 if (activeRes == null)
\r
411 activeRes = new Vector();
\r
415 int start = Integer.parseInt(activeRes.get(activeRes.size() -
\r
426 for (int n = start; n <= end; n++)
\r
428 addEditableColumn(n);
\r
434 activeRes = new Vector();
\r
435 activeRes.addElement(String.valueOf(res));
\r
444 * @param evt DOCUMENT ME!
\r
446 public void mouseReleased(MouseEvent evt)
\r
453 * @param evt DOCUMENT ME!
\r
455 public void mouseEntered(MouseEvent evt)
\r
462 * @param evt DOCUMENT ME!
\r
464 public void mouseExited(MouseEvent evt)
\r
471 * @param evt DOCUMENT ME!
\r
473 public void mouseDragged(MouseEvent evt)
\r
480 * @param evt DOCUMENT ME!
\r
482 public void mouseMoved(MouseEvent evt)
\r
484 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
494 for (int i = 0; i < aa.length; i++)
\r
498 height += aa[i].height;
\r
501 if (evt.getY() < height)
\r
509 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
511 if ((row > -1) && (res < aa[row].annotations.length) &&
\r
512 (aa[row].annotations[res] != null))
\r
514 this.setToolTipText(aa[row].annotations[res].description);
\r
516 StringBuffer text = new StringBuffer("Sequence position " +
\r
517 (res + 1) + " " + aa[row].annotations[res].description);
\r
518 ap.alignFrame.statusBar.setText(text.toString());
\r
525 * @param evt DOCUMENT ME!
\r
527 public void mouseClicked(MouseEvent evt)
\r
534 * @param g DOCUMENT ME!
\r
536 public void paintComponent(Graphics g)
\r
538 g.setColor(Color.white);
\r
539 g.fillRect(0, 0, getWidth(), getHeight());
\r
541 if (fastPaint && image!=null)
\r
543 g.drawImage(image, 0, 0, this);
\r
548 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
\r
550 if (image == null || imgWidth != image.getWidth())
\r
552 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
\r
553 BufferedImage.TYPE_INT_RGB);
\r
554 gg = (Graphics2D) image.getGraphics();
\r
555 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
556 RenderingHints.VALUE_ANTIALIAS_ON);
\r
558 gg.setFont(av.getFont());
\r
559 fm = gg.getFontMetrics();
\r
562 drawComponent(gg, av.startRes, av.endRes + 1);
\r
563 g.drawImage(image, 0, 0, this);
\r
569 * @param horizontal DOCUMENT ME!
\r
571 public void fastPaint(int horizontal)
\r
573 if ((horizontal == 0) ||
\r
574 (av.alignment.getAlignmentAnnotation() == null) ||
\r
575 (av.alignment.getAlignmentAnnotation().length < 1))
\r
582 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
\r
584 int sr = av.startRes;
\r
585 int er = av.endRes + 1;
\r
588 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
590 transX = (er - sr - horizontal) * av.charWidth;
\r
591 sr = er - horizontal;
\r
593 else if (horizontal < 0)
\r
595 er = sr - horizontal;
\r
598 gg.translate(transX, 0);
\r
600 drawComponent(gg, sr, er);
\r
602 gg.translate(-transX, 0);
\r
613 * @param g DOCUMENT ME!
\r
614 * @param startRes DOCUMENT ME!
\r
615 * @param endRes DOCUMENT ME!
\r
617 public void drawComponent(Graphics g, int startRes, int endRes)
\r
619 g.setFont(av.getFont());
\r
622 fm = g.getFontMetrics();
\r
625 g.setColor(Color.white);
\r
626 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
\r
628 if ((av.alignment.getAlignmentAnnotation() == null) ||
\r
629 (av.alignment.getAlignmentAnnotation().length < 1))
\r
631 g.setColor(Color.white);
\r
632 g.fillRect(0, 0, getWidth(), getHeight());
\r
633 g.setColor(Color.black);
\r
634 g.drawString("Alignment has no annotations", 20, 15);
\r
639 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
644 char[] lastSS = new char[aa.length];
\r
645 int[] lastSSX = new int[aa.length];
\r
646 int iconOffset = av.charHeight / 2;
\r
647 boolean validRes = false;
\r
650 for (int i = 0; i < aa.length; i++)
\r
652 AlignmentAnnotation row = aa[i];
\r
662 // this is so that we draw the characters below the graph
\r
667 y -= av.charHeight;
\r
673 iconOffset = av.charHeight / 2;
\r
680 for (j = startRes; j < endRes; j++)
\r
682 if ((row.annotations.length <= j) ||
\r
683 (row.annotations[j] == null))
\r
692 x = (j - startRes) * av.charWidth;
\r
694 if (activeRow == i)
\r
696 g.setColor(Color.red);
\r
698 if (activeRes != null)
\r
700 for (int n = 0; n < activeRes.size(); n++)
\r
702 int v = Integer.parseInt(activeRes.get(n).toString());
\r
706 g.fillRect((j - startRes) * av.charWidth, y,
\r
707 av.charWidth, row.height);
\r
714 (row.annotations[j].displayCharacter.length() > 0))
\r
717 int charOffset = (av.charWidth -
\r
718 fm.charWidth(row.annotations[j].displayCharacter.charAt(
\r
720 g.setColor(row.annotations[j].colour);
\r
722 if (j == 0 || row.isGraph)
\r
724 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
725 y + iconOffset + 3);
\r
727 else if (((row.annotations[j - 1] == null) ||
\r
728 (row.annotations[j].displayCharacter != row.annotations[j -
\r
729 1].displayCharacter)))
\r
731 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
732 y + iconOffset + 3);
\r
739 (row.annotations[j].secondaryStructure != lastSS[i]))
\r
744 g.setColor(HELIX_COLOUR);
\r
745 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
746 x - lastSSX[i], 7, 8, 8);
\r
751 g.setColor(SHEET_COLOUR);
\r
752 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
753 x - lastSSX[i] - 4, 7);
\r
754 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
757 y + iconOffset, y + 14 + iconOffset,
\r
767 g.setColor(Color.gray);
\r
768 g.fillRect(lastSSX[i], y + 6 + iconOffset,
\r
769 x - lastSSX[i], 2);
\r
776 lastSS[i] = row.annotations[j].secondaryStructure;
\r
787 if (validRes && row.isGraph)
\r
789 g.setColor(new Color(0, 0, 180));
\r
791 int height = (int) ((row.annotations[j].value / row.graphMax) * GRAPH_HEIGHT);
\r
793 if (row.windowLength > 1)
\r
797 for (int i2 = j - (row.windowLength / 2);
\r
798 i2 < (j + (row.windowLength / 2)); i2++)
\r
800 if ((i2 < 0) || (i2 >= av.alignment.getWidth()))
\r
805 total += row.annotations[i2].value;
\r
808 total /= row.windowLength;
\r
809 height = (int) ((total / row.graphMax) * GRAPH_HEIGHT);
\r
812 g.setColor(row.annotations[j].colour);
\r
813 g.fillRect(x, y - height, av.charWidth, height);
\r
824 g.setColor(HELIX_COLOUR);
\r
825 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
826 x - lastSSX[i], 7, 8, 8);
\r
831 g.setColor(SHEET_COLOUR);
\r
832 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
833 x - lastSSX[i] - 4, 7);
\r
834 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
837 y + iconOffset, y + 14 + iconOffset,
\r
847 g.setColor(Color.gray);
\r
848 g.fillRect(lastSSX[i], y + 6 + iconOffset, x - lastSSX[i], 2);
\r
854 if (row.isGraph && row.hasText)
\r
856 y += av.charHeight;
\r
866 // used by overview window
\r
867 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y, int sRes, int eRes)
\r
869 g.setColor(Color.white);
\r
870 g.fillRect(0, 0, width, y);
\r
871 g.setColor(new Color(0, 0, 180));
\r
876 for (int j = sRes; j < eRes; j++)
\r
878 g.setColor(new Color(0, 0, 180));
\r
880 height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
\r
884 g.fillRect(x, y - height, av.charWidth, height);
\r