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
154 for (int j = 0; j < aa.length; j++)
\r
156 if (aa[j].editable)
\r
165 if ((activeRes != null) && activeRes.contains(String.valueOf(col)))
\r
167 activeRes.removeElement(String.valueOf(col));
\r
176 * @param col DOCUMENT ME!
\r
178 public void addEditableColumn(int col)
\r
180 if (activeRow == -1)
\r
182 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
186 for (int j = 0; j < aa.length; j++)
\r
188 if (aa[j].editable)
\r
197 if (activeRes == null)
\r
199 activeRes = new Vector();
\r
202 if (!activeRes.contains(String.valueOf(col)))
\r
204 activeRes.addElement(String.valueOf(col));
\r
213 * @param evt DOCUMENT ME!
\r
215 public void actionPerformed(ActionEvent evt)
\r
217 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
218 Annotation[] anot = aa[activeRow].annotations;
\r
220 if (evt.getActionCommand().equals(REMOVE))
\r
222 for (int i = 0; i < activeRes.size(); i++)
\r
224 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
225 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
228 else if (evt.getActionCommand().equals(LABEL))
\r
230 String label = JOptionPane.showInputDialog(this, "Enter Label ",
\r
231 "Enter label", JOptionPane.QUESTION_MESSAGE);
\r
238 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
240 aa[activeRow].hasText = true;
\r
243 for (int i = 0; i < activeRes.size(); i++)
\r
245 int index = Integer.parseInt(activeRes.get(i).toString());
\r
247 if (anot[index] == null)
\r
249 anot[index] = new Annotation(label, "", ' ', 0);
\r
252 anot[index].displayCharacter = label;
\r
255 else if (evt.getActionCommand().equals(COLOUR))
\r
257 Color col = JColorChooser.showDialog(this,
\r
258 "Choose foreground colour", Color.black);
\r
260 for (int i = 0; i < activeRes.size(); i++)
\r
262 int index = Integer.parseInt(activeRes.get(i).toString());
\r
264 if (anot[index] == null)
\r
266 anot[index] = new Annotation("", "", ' ', 0);
\r
269 anot[index].colour = col;
\r
272 else // HELIX OR SHEET
\r
275 String symbol = "\u03B1";
\r
277 if (evt.getActionCommand().equals(HELIX))
\r
281 else if (evt.getActionCommand().equals(SHEET))
\r
287 if (!aa[activeRow].hasIcons)
\r
289 aa[activeRow].hasIcons = true;
\r
292 String label = JOptionPane.showInputDialog("Enter a label for the structure?",
\r
300 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
302 aa[activeRow].hasText = true;
\r
305 for (int i = 0; i < activeRes.size(); i++)
\r
307 int index = Integer.parseInt(activeRes.get(i).toString());
\r
309 if (anot[index] == null)
\r
311 anot[index] = new Annotation(label, "", type, 0);
\r
314 anot[index].secondaryStructure = type;
\r
315 anot[index].displayCharacter = label;
\r
319 adjustPanelHeight();
\r
329 * @param evt DOCUMENT ME!
\r
331 public void mousePressed(MouseEvent evt)
\r
336 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
338 for (int i = 0; i < aa.length; i++)
\r
342 height += aa[i].height;
\r
345 if (evt.getY() < height)
\r
347 if (aa[i].editable)
\r
360 if (SwingUtilities.isRightMouseButton(evt))
\r
362 if (activeRes == null)
\r
367 JPopupMenu pop = new JPopupMenu("Structure type");
\r
368 JMenuItem item = new JMenuItem(HELIX);
\r
369 item.addActionListener(this);
\r
371 item = new JMenuItem(SHEET);
\r
372 item.addActionListener(this);
\r
374 item = new JMenuItem(LABEL);
\r
375 item.addActionListener(this);
\r
377 item = new JMenuItem(COLOUR);
\r
378 item.addActionListener(this);
\r
380 item = new JMenuItem(REMOVE);
\r
381 item.addActionListener(this);
\r
383 pop.show(this, evt.getX(), evt.getY());
\r
393 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
395 if (evt.isControlDown() || evt.isAltDown())
\r
397 addEditableColumn(res);
\r
399 else if (evt.isShiftDown())
\r
401 if (activeRes == null)
\r
403 activeRes = new Vector();
\r
407 int start = Integer.parseInt(activeRes.get(activeRes.size() -
\r
418 for (int n = start; n <= end; n++)
\r
420 addEditableColumn(n);
\r
426 activeRes = new Vector();
\r
427 activeRes.addElement(String.valueOf(res));
\r
436 * @param evt DOCUMENT ME!
\r
438 public void mouseReleased(MouseEvent evt)
\r
445 * @param evt DOCUMENT ME!
\r
447 public void mouseEntered(MouseEvent evt)
\r
454 * @param evt DOCUMENT ME!
\r
456 public void mouseExited(MouseEvent evt)
\r
463 * @param evt DOCUMENT ME!
\r
465 public void mouseDragged(MouseEvent evt)
\r
472 * @param evt DOCUMENT ME!
\r
474 public void mouseMoved(MouseEvent evt)
\r
476 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
486 for (int i = 0; i < aa.length; i++)
\r
490 height += aa[i].height;
\r
493 if (evt.getY() < height)
\r
501 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
503 if ((row > -1) && (res < aa[row].annotations.length) &&
\r
504 (aa[row].annotations[res] != null))
\r
506 this.setToolTipText(aa[row].annotations[res].description);
\r
508 StringBuffer text = new StringBuffer("Sequence position " +
\r
509 (res + 1) + " " + aa[row].annotations[res].description);
\r
510 ap.alignFrame.statusBar.setText(text.toString());
\r
517 * @param evt DOCUMENT ME!
\r
519 public void mouseClicked(MouseEvent evt)
\r
526 * @param g DOCUMENT ME!
\r
528 public void paintComponent(Graphics g)
\r
530 g.setColor(Color.white);
\r
531 g.fillRect(0, 0, getWidth(), getHeight());
\r
535 g.drawImage(image, 0, 0, this);
\r
540 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
\r
542 if (image == null || imgWidth != image.getWidth())
\r
544 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
\r
545 BufferedImage.TYPE_INT_RGB);
\r
546 gg = (Graphics2D) image.getGraphics();
\r
547 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
548 RenderingHints.VALUE_ANTIALIAS_ON);
\r
550 gg.setFont(av.getFont());
\r
551 fm = gg.getFontMetrics();
\r
554 drawComponent(gg, av.startRes, av.endRes + 1);
\r
555 g.drawImage(image, 0, 0, this);
\r
561 * @param horizontal DOCUMENT ME!
\r
563 public void fastPaint(int horizontal)
\r
565 if ((horizontal == 0) ||
\r
566 (av.alignment.getAlignmentAnnotation() == null) ||
\r
567 (av.alignment.getAlignmentAnnotation().length < 1))
\r
574 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
\r
576 int sr = av.startRes;
\r
577 int er = av.endRes + 1;
\r
580 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
582 transX = (er - sr - horizontal) * av.charWidth;
\r
583 sr = er - horizontal;
\r
585 else if (horizontal < 0)
\r
587 er = sr - horizontal;
\r
590 gg.translate(transX, 0);
\r
592 drawComponent(gg, sr, er);
\r
594 gg.translate(-transX, 0);
\r
605 * @param g DOCUMENT ME!
\r
606 * @param startRes DOCUMENT ME!
\r
607 * @param endRes DOCUMENT ME!
\r
609 public void drawComponent(Graphics2D g, int startRes, int endRes)
\r
611 g.setColor(Color.white);
\r
612 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
\r
614 if ((av.alignment.getAlignmentAnnotation() == null) ||
\r
615 (av.alignment.getAlignmentAnnotation().length < 1))
\r
617 g.setColor(Color.white);
\r
618 g.fillRect(0, 0, getWidth(), getHeight());
\r
619 g.setColor(Color.black);
\r
620 g.drawString("Alignment has no annotations", 20, 15);
\r
625 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
630 char[] lastSS = new char[aa.length];
\r
631 int[] lastSSX = new int[aa.length];
\r
632 int iconOffset = av.charHeight / 2;
\r
633 boolean validRes = false;
\r
636 for (int i = 0; i < aa.length; i++)
\r
638 AlignmentAnnotation row = aa[i];
\r
648 // this is so that we draw the characters below the graph
\r
653 y -= av.charHeight;
\r
659 iconOffset = av.charHeight / 2;
\r
666 for (j = startRes; j < endRes; j++)
\r
668 if ((row.annotations.length <= j) ||
\r
669 (row.annotations[j] == null))
\r
678 x = (j - startRes) * av.charWidth;
\r
680 if (activeRow == i)
\r
682 g.setColor(Color.red);
\r
684 if (activeRes != null)
\r
686 for (int n = 0; n < activeRes.size(); n++)
\r
688 int v = Integer.parseInt(activeRes.get(n).toString());
\r
692 g.fillRect((j - startRes) * av.charWidth, y,
\r
693 av.charWidth, row.height);
\r
700 (row.annotations[j].displayCharacter.length() > 0))
\r
703 int charOffset = (av.charWidth -
\r
704 fm.charWidth(row.annotations[j].displayCharacter.charAt(
\r
706 g.setColor(row.annotations[j].colour);
\r
708 if (j == 0 || row.isGraph)
\r
710 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
711 y + iconOffset + 3);
\r
713 else if (((row.annotations[j - 1] == null) ||
\r
714 (row.annotations[j].displayCharacter != row.annotations[j -
\r
715 1].displayCharacter)))
\r
717 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
718 y + iconOffset + 3);
\r
725 (row.annotations[j].secondaryStructure != lastSS[i]))
\r
730 g.setColor(HELIX_COLOUR);
\r
731 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
732 x - lastSSX[i], 7, 8, 8);
\r
737 g.setColor(SHEET_COLOUR);
\r
738 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
739 x - lastSSX[i] - 4, 7);
\r
740 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
743 y + iconOffset, y + 14 + iconOffset,
\r
753 g.setColor(Color.gray);
\r
754 g.fillRect(lastSSX[i], y + 6 + iconOffset,
\r
755 x - lastSSX[i], 2);
\r
762 lastSS[i] = row.annotations[j].secondaryStructure;
\r
773 if (validRes && row.isGraph)
\r
775 g.setColor(new Color(0, 0, 180));
\r
777 int height = (int) ((row.annotations[j].value / row.graphMax) * GRAPH_HEIGHT);
\r
779 if (row.windowLength > 1)
\r
783 for (int i2 = j - (row.windowLength / 2);
\r
784 i2 < (j + (row.windowLength / 2)); i2++)
\r
786 if ((i2 < 0) || (i2 >= av.alignment.getWidth()))
\r
791 total += row.annotations[i2].value;
\r
794 total /= row.windowLength;
\r
795 height = (int) ((total / row.graphMax) * GRAPH_HEIGHT);
\r
798 g.setColor(row.annotations[j].colour);
\r
799 g.fillRect(x, y - height, av.charWidth, height);
\r
810 g.setColor(HELIX_COLOUR);
\r
811 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
812 x - lastSSX[i], 7, 8, 8);
\r
817 g.setColor(SHEET_COLOUR);
\r
818 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
819 x - lastSSX[i] - 4, 7);
\r
820 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
823 y + iconOffset, y + 14 + iconOffset,
\r
833 g.setColor(Color.gray);
\r
834 g.fillRect(lastSSX[i], y + 6 + iconOffset, x - lastSSX[i], 2);
\r
840 if (row.isGraph && row.hasText)
\r
842 y += av.charHeight;
\r
852 // used by overview window
\r
853 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y, int sRes, int eRes)
\r
855 g.setColor(Color.white);
\r
856 g.fillRect(0, 0, width, y);
\r
857 g.setColor(new Color(0, 0, 180));
\r
862 for (int j = sRes; j < eRes; j++)
\r
864 g.setColor(new Color(0, 0, 180));
\r
866 height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
\r
870 g.fillRect(x, y - height, av.charWidth, height);
\r