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
184 for (int j = 0; j < aa.length; j++)
\r
186 if (aa[j].editable)
\r
195 if (activeRes == null)
\r
197 activeRes = new Vector();
\r
200 if (!activeRes.contains(String.valueOf(col)))
\r
202 activeRes.addElement(String.valueOf(col));
\r
211 * @param evt DOCUMENT ME!
\r
213 public void actionPerformed(ActionEvent evt)
\r
215 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
216 Annotation[] anot = aa[activeRow].annotations;
\r
218 if (evt.getActionCommand().equals(REMOVE))
\r
220 for (int i = 0; i < activeRes.size(); i++)
\r
222 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
223 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
226 else if (evt.getActionCommand().equals(LABEL))
\r
228 String label = JOptionPane.showInputDialog(this, "Enter Label ",
\r
229 "Enter label", JOptionPane.QUESTION_MESSAGE);
\r
236 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
238 aa[activeRow].hasText = true;
\r
241 for (int i = 0; i < activeRes.size(); i++)
\r
243 int index = Integer.parseInt(activeRes.get(i).toString());
\r
245 if (anot[index] == null)
\r
247 anot[index] = new Annotation(label, "", ' ', 0);
\r
250 anot[index].displayCharacter = label;
\r
253 else if (evt.getActionCommand().equals(COLOUR))
\r
255 Color col = JColorChooser.showDialog(this,
\r
256 "Choose foreground colour", Color.black);
\r
258 for (int i = 0; i < activeRes.size(); i++)
\r
260 int index = Integer.parseInt(activeRes.get(i).toString());
\r
262 if (anot[index] == null)
\r
264 anot[index] = new Annotation("", "", ' ', 0);
\r
267 anot[index].colour = col;
\r
270 else // HELIX OR SHEET
\r
273 String symbol = "\u03B1";
\r
275 if (evt.getActionCommand().equals(HELIX))
\r
279 else if (evt.getActionCommand().equals(SHEET))
\r
285 if (!aa[activeRow].hasIcons)
\r
287 aa[activeRow].hasIcons = true;
\r
290 String label = JOptionPane.showInputDialog("Enter a label for the structure?",
\r
298 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
300 aa[activeRow].hasText = true;
\r
303 for (int i = 0; i < activeRes.size(); i++)
\r
305 int index = Integer.parseInt(activeRes.get(i).toString());
\r
307 if (anot[index] == null)
\r
309 anot[index] = new Annotation(label, "", type, 0);
\r
312 anot[index].secondaryStructure = type;
\r
313 anot[index].displayCharacter = label;
\r
317 adjustPanelHeight();
\r
327 * @param evt DOCUMENT ME!
\r
329 public void mousePressed(MouseEvent evt)
\r
334 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
336 for (int i = 0; i < aa.length; i++)
\r
340 height += aa[i].height;
\r
343 if (evt.getY() < height)
\r
345 if (aa[i].editable)
\r
358 if (SwingUtilities.isRightMouseButton(evt))
\r
360 if (activeRes == null)
\r
365 JPopupMenu pop = new JPopupMenu("Structure type");
\r
366 JMenuItem item = new JMenuItem(HELIX);
\r
367 item.addActionListener(this);
\r
369 item = new JMenuItem(SHEET);
\r
370 item.addActionListener(this);
\r
372 item = new JMenuItem(LABEL);
\r
373 item.addActionListener(this);
\r
375 item = new JMenuItem(COLOUR);
\r
376 item.addActionListener(this);
\r
378 item = new JMenuItem(REMOVE);
\r
379 item.addActionListener(this);
\r
381 pop.show(this, evt.getX(), evt.getY());
\r
391 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
393 if (evt.isControlDown() || evt.isAltDown())
\r
395 addEditableColumn(res);
\r
397 else if (evt.isShiftDown())
\r
399 if (activeRes == null)
\r
401 activeRes = new Vector();
\r
405 int start = Integer.parseInt(activeRes.get(activeRes.size() -
\r
416 for (int n = start; n <= end; n++)
\r
418 addEditableColumn(n);
\r
424 activeRes = new Vector();
\r
425 activeRes.addElement(String.valueOf(res));
\r
434 * @param evt DOCUMENT ME!
\r
436 public void mouseReleased(MouseEvent evt)
\r
443 * @param evt DOCUMENT ME!
\r
445 public void mouseEntered(MouseEvent evt)
\r
452 * @param evt DOCUMENT ME!
\r
454 public void mouseExited(MouseEvent evt)
\r
461 * @param evt DOCUMENT ME!
\r
463 public void mouseDragged(MouseEvent evt)
\r
470 * @param evt DOCUMENT ME!
\r
472 public void mouseMoved(MouseEvent evt)
\r
474 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
484 for (int i = 0; i < aa.length; i++)
\r
488 height += aa[i].height;
\r
491 if (evt.getY() < height)
\r
499 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
501 if ((row > -1) && (res < aa[row].annotations.length) &&
\r
502 (aa[row].annotations[res] != null))
\r
504 this.setToolTipText(aa[row].annotations[res].description);
\r
506 StringBuffer text = new StringBuffer("Sequence position " +
\r
507 (res + 1) + " " + aa[row].annotations[res].description);
\r
508 ap.alignFrame.statusBar.setText(text.toString());
\r
515 * @param evt DOCUMENT ME!
\r
517 public void mouseClicked(MouseEvent evt)
\r
524 * @param g DOCUMENT ME!
\r
526 public void paintComponent(Graphics g)
\r
528 g.setColor(Color.white);
\r
529 g.fillRect(0, 0, getWidth(), getHeight());
\r
533 g.drawImage(image, 0, 0, this);
\r
538 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
\r
540 if (image == null || imgWidth != image.getWidth())
\r
542 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
\r
543 BufferedImage.TYPE_INT_RGB);
\r
544 gg = (Graphics2D) image.getGraphics();
\r
545 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
546 RenderingHints.VALUE_ANTIALIAS_ON);
\r
548 gg.setFont(av.getFont());
\r
549 fm = gg.getFontMetrics();
\r
552 drawComponent(gg, av.startRes, av.endRes + 1);
\r
553 g.drawImage(image, 0, 0, this);
\r
559 * @param horizontal DOCUMENT ME!
\r
561 public void fastPaint(int horizontal)
\r
563 if ((horizontal == 0) ||
\r
564 (av.alignment.getAlignmentAnnotation() == null) ||
\r
565 (av.alignment.getAlignmentAnnotation().length < 1))
\r
572 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
\r
574 int sr = av.startRes;
\r
575 int er = av.endRes + 1;
\r
578 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
580 transX = (er - sr - horizontal) * av.charWidth;
\r
581 sr = er - horizontal;
\r
583 else if (horizontal < 0)
\r
585 er = sr - horizontal;
\r
588 gg.translate(transX, 0);
\r
590 drawComponent(gg, sr, er);
\r
592 gg.translate(-transX, 0);
\r
603 * @param g DOCUMENT ME!
\r
604 * @param startRes DOCUMENT ME!
\r
605 * @param endRes DOCUMENT ME!
\r
607 public void drawComponent(Graphics2D g, int startRes, int endRes)
\r
609 g.setColor(Color.white);
\r
610 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
\r
612 if ((av.alignment.getAlignmentAnnotation() == null) ||
\r
613 (av.alignment.getAlignmentAnnotation().length < 1))
\r
615 g.setColor(Color.white);
\r
616 g.fillRect(0, 0, getWidth(), getHeight());
\r
617 g.setColor(Color.black);
\r
618 g.drawString("Alignment has no annotations", 20, 15);
\r
623 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
628 char[] lastSS = new char[aa.length];
\r
629 int[] lastSSX = new int[aa.length];
\r
630 int iconOffset = av.charHeight / 2;
\r
631 boolean validRes = false;
\r
634 for (int i = 0; i < aa.length; i++)
\r
636 AlignmentAnnotation row = aa[i];
\r
646 // this is so that we draw the characters below the graph
\r
651 y -= av.charHeight;
\r
657 iconOffset = av.charHeight / 2;
\r
664 for (j = startRes; j < endRes; j++)
\r
666 if ((row.annotations.length <= j) ||
\r
667 (row.annotations[j] == null))
\r
676 x = (j - startRes) * av.charWidth;
\r
678 if (activeRow == i)
\r
680 g.setColor(Color.red);
\r
682 if (activeRes != null)
\r
684 for (int n = 0; n < activeRes.size(); n++)
\r
686 int v = Integer.parseInt(activeRes.get(n).toString());
\r
690 g.fillRect((j - startRes) * av.charWidth, y,
\r
691 av.charWidth, row.height);
\r
698 (row.annotations[j].displayCharacter.length() > 0))
\r
701 int charOffset = (av.charWidth -
\r
702 fm.charWidth(row.annotations[j].displayCharacter.charAt(
\r
704 g.setColor(row.annotations[j].colour);
\r
706 if (j == 0 || row.isGraph)
\r
708 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
709 y + iconOffset + 3);
\r
711 else if (((row.annotations[j - 1] == null) ||
\r
712 (row.annotations[j].displayCharacter != row.annotations[j -
\r
713 1].displayCharacter)))
\r
715 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
716 y + iconOffset + 3);
\r
723 (row.annotations[j].secondaryStructure != lastSS[i]))
\r
728 g.setColor(HELIX_COLOUR);
\r
729 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
730 x - lastSSX[i], 7, 8, 8);
\r
735 g.setColor(SHEET_COLOUR);
\r
736 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
737 x - lastSSX[i] - 4, 7);
\r
738 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
741 y + iconOffset, y + 14 + iconOffset,
\r
751 g.setColor(Color.gray);
\r
752 g.fillRect(lastSSX[i], y + 6 + iconOffset,
\r
753 x - lastSSX[i], 2);
\r
760 lastSS[i] = row.annotations[j].secondaryStructure;
\r
771 if (validRes && row.isGraph)
\r
773 g.setColor(new Color(0, 0, 180));
\r
775 int height = (int) ((row.annotations[j].value / row.graphMax) * GRAPH_HEIGHT);
\r
777 if (row.windowLength > 1)
\r
781 for (int i2 = j - (row.windowLength / 2);
\r
782 i2 < (j + (row.windowLength / 2)); i2++)
\r
784 if ((i2 < 0) || (i2 >= av.alignment.getWidth()))
\r
789 total += row.annotations[i2].value;
\r
792 total /= row.windowLength;
\r
793 height = (int) ((total / row.graphMax) * GRAPH_HEIGHT);
\r
796 g.setColor(row.annotations[j].colour);
\r
797 g.fillRect(x, y - height, av.charWidth, height);
\r
808 g.setColor(HELIX_COLOUR);
\r
809 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
810 x - lastSSX[i], 7, 8, 8);
\r
815 g.setColor(SHEET_COLOUR);
\r
816 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
817 x - lastSSX[i] - 4, 7);
\r
818 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
821 y + iconOffset, y + 14 + iconOffset,
\r
831 g.setColor(Color.gray);
\r
832 g.fillRect(lastSSX[i], y + 6 + iconOffset, x - lastSSX[i], 2);
\r
838 if (row.isGraph && row.hasText)
\r
840 y += av.charHeight;
\r
850 // used by overview window
\r
851 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y, int sRes, int eRes)
\r
853 g.setColor(Color.white);
\r
854 g.fillRect(0, 0, width, y);
\r
855 g.setColor(new Color(0, 0, 180));
\r
860 for (int j = sRes; j < eRes; j++)
\r
862 g.setColor(new Color(0, 0, 180));
\r
864 height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
\r
868 g.fillRect(x, y - height, av.charWidth, height);
\r