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 final String HELIX = "Helix";
\r
42 final String SHEET = "Sheet";
\r
43 final String LABEL = "Label";
\r
44 final String REMOVE = "Remove Annotation";
\r
45 final String COLOUR = "Colour";
\r
46 final Color HELIX_COLOUR = Color.red.darker();
\r
47 final Color SHEET_COLOUR = Color.green.darker().darker();
\r
49 /** DOCUMENT ME!! */
\r
54 BufferedImage image;
\r
58 boolean fastPaint = false;
\r
60 //Used For mouse Dragging and resizing graphs
\r
61 int graphStretch = -1;
\r
62 int graphStretchY = -1;
\r
65 * Creates a new AnnotationPanel object.
\r
67 * @param ap DOCUMENT ME!
\r
69 public AnnotationPanel(AlignmentPanel ap)
\r
71 ToolTipManager.sharedInstance().registerComponent(this);
\r
72 ToolTipManager.sharedInstance().setInitialDelay(0);
\r
73 ToolTipManager.sharedInstance().setDismissDelay(10000);
\r
76 this.setLayout(null);
\r
77 addMouseListener(this);
\r
78 addMouseMotionListener(this);
\r
79 ap.annotationScroller.getVerticalScrollBar().addAdjustmentListener(this);
\r
82 public AnnotationPanel(AlignViewport av)
\r
91 * @param evt DOCUMENT ME!
\r
93 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
95 ap.alabels.setScrollOffset(-evt.getValue());
\r
101 public int adjustPanelHeight()
\r
103 // setHeight of panels
\r
105 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
110 for (int i = 0; i < aa.length; i++)
\r
112 if (!aa[i].visible)
\r
121 aa[i].height += av.charHeight;
\r
124 if (aa[i].hasIcons)
\r
126 aa[i].height += 16;
\r
131 aa[i].height += aa[i].graphHeight;
\r
134 if (aa[i].height == 0)
\r
139 height += aa[i].height;
\r
147 this.setPreferredSize(new Dimension(1, height));
\r
155 * @param col DOCUMENT ME!
\r
157 public void removeEditableColumn(int col)
\r
159 if (activeRow == -1)
\r
161 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
165 for (int j = 0; j < aa.length; j++)
\r
167 if (aa[j].editable)
\r
176 if ((activeRes != null) && activeRes.contains(String.valueOf(col)))
\r
178 activeRes.removeElement(String.valueOf(col));
\r
187 * @param col DOCUMENT ME!
\r
189 public void addEditableColumn(int col)
\r
191 if (activeRow == -1)
\r
193 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
197 for (int j = 0; j < aa.length; j++)
\r
199 if (aa[j].editable)
\r
208 if (activeRes == null)
\r
210 activeRes = new Vector();
\r
213 if (!activeRes.contains(String.valueOf(col)))
\r
215 activeRes.addElement(String.valueOf(col));
\r
224 * @param evt DOCUMENT ME!
\r
226 public void actionPerformed(ActionEvent evt)
\r
228 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
229 Annotation[] anot = aa[activeRow].annotations;
\r
231 if (evt.getActionCommand().equals(REMOVE))
\r
233 for (int i = 0; i < activeRes.size(); i++)
\r
235 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
236 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
239 else if (evt.getActionCommand().equals(LABEL))
\r
241 String label = JOptionPane.showInputDialog(this, "Enter Label ",
\r
242 "Enter label", JOptionPane.QUESTION_MESSAGE);
\r
249 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
251 aa[activeRow].hasText = true;
\r
254 for (int i = 0; i < activeRes.size(); i++)
\r
256 int index = Integer.parseInt(activeRes.get(i).toString());
\r
258 if (anot[index] == null)
\r
260 anot[index] = new Annotation(label, "", ' ', 0);
\r
263 anot[index].displayCharacter = label;
\r
266 else if (evt.getActionCommand().equals(COLOUR))
\r
268 Color col = JColorChooser.showDialog(this,
\r
269 "Choose foreground colour", Color.black);
\r
271 for (int i = 0; i < activeRes.size(); i++)
\r
273 int index = Integer.parseInt(activeRes.get(i).toString());
\r
275 if (anot[index] == null)
\r
277 anot[index] = new Annotation("", "", ' ', 0);
\r
280 anot[index].colour = col;
\r
283 else // HELIX OR SHEET
\r
286 String symbol = "\u03B1";
\r
288 if (evt.getActionCommand().equals(HELIX))
\r
292 else if (evt.getActionCommand().equals(SHEET))
\r
298 if (!aa[activeRow].hasIcons)
\r
300 aa[activeRow].hasIcons = true;
\r
303 String label = JOptionPane.showInputDialog("Enter a label for the structure?",
\r
311 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
313 aa[activeRow].hasText = true;
\r
316 for (int i = 0; i < activeRes.size(); i++)
\r
318 int index = Integer.parseInt(activeRes.get(i).toString());
\r
320 if (anot[index] == null)
\r
322 anot[index] = new Annotation(label, "", type, 0);
\r
325 anot[index].secondaryStructure = type;
\r
326 anot[index].displayCharacter = label;
\r
330 adjustPanelHeight();
\r
340 * @param evt DOCUMENT ME!
\r
342 public void mousePressed(MouseEvent evt)
\r
347 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
351 for (int i = 0; i < aa.length; i++)
\r
355 height += aa[i].height;
\r
358 if (evt.getY() < height)
\r
360 if (aa[i].editable)
\r
364 else if(aa[i].graph>0)
\r
368 graphStretchY = evt.getY();
\r
380 if (SwingUtilities.isRightMouseButton(evt))
\r
382 if (activeRes == null)
\r
387 JPopupMenu pop = new JPopupMenu("Structure type");
\r
388 JMenuItem item = new JMenuItem(HELIX);
\r
389 item.addActionListener(this);
\r
391 item = new JMenuItem(SHEET);
\r
392 item.addActionListener(this);
\r
394 item = new JMenuItem(LABEL);
\r
395 item.addActionListener(this);
\r
397 item = new JMenuItem(COLOUR);
\r
398 item.addActionListener(this);
\r
400 item = new JMenuItem(REMOVE);
\r
401 item.addActionListener(this);
\r
403 pop.show(this, evt.getX(), evt.getY());
\r
413 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
415 if (evt.isControlDown() || evt.isAltDown())
\r
417 addEditableColumn(res);
\r
419 else if (evt.isShiftDown())
\r
421 if (activeRes == null)
\r
423 activeRes = new Vector();
\r
427 int start = Integer.parseInt(activeRes.get(activeRes.size() -
\r
438 for (int n = start; n <= end; n++)
\r
440 addEditableColumn(n);
\r
446 activeRes = new Vector();
\r
447 activeRes.addElement(String.valueOf(res));
\r
454 * @param evt DOCUMENT ME!
\r
456 public void mouseReleased(MouseEvent evt)
\r
459 graphStretchY = -1;
\r
465 * @param evt DOCUMENT ME!
\r
467 public void mouseEntered(MouseEvent evt)
\r
474 * @param evt DOCUMENT ME!
\r
476 public void mouseExited(MouseEvent evt)
\r
483 * @param evt DOCUMENT ME!
\r
485 public void mouseDragged(MouseEvent evt)
\r
487 if(graphStretch>-1)
\r
489 av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY - evt.getY();
\r
490 if(av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight <10)
\r
491 av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight = 10;
\r
492 graphStretchY = evt.getY();
\r
493 adjustPanelHeight();
\r
501 * @param evt DOCUMENT ME!
\r
503 public void mouseMoved(MouseEvent evt)
\r
505 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
515 for (int i = 0; i < aa.length; i++)
\r
519 height += aa[i].height;
\r
522 if (evt.getY() < height)
\r
530 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
532 if (row > -1 && res<aa[row].annotations.length)
\r
534 if(aa[row].graphGroup>-1)
\r
536 StringBuffer tip = new StringBuffer("<html>");
\r
537 for (int gg = 0; gg < aa.length; gg++)
\r
539 if (aa[gg].graphGroup == aa[row].graphGroup && aa[gg].annotations[res]!=null)
\r
540 tip.append(aa[gg].label+" "+aa[gg].annotations[res].description+"<br>" );
\r
542 if(tip.length()!=6)
\r
544 tip.setLength(tip.length() - 4);
\r
545 this.setToolTipText(tip.toString() + "</html>");
\r
548 else if(aa[row].annotations[res] != null)
\r
549 this.setToolTipText(aa[row].annotations[res].description);
\r
551 if(aa[row].annotations[res]!=null)
\r
553 StringBuffer text = new StringBuffer("Sequence position " +
\r
555 aa[row].annotations[res].description);
\r
557 ap.alignFrame.statusBar.setText(text.toString());
\r
565 * @param evt DOCUMENT ME!
\r
567 public void mouseClicked(MouseEvent evt)
\r
574 * @param g DOCUMENT ME!
\r
576 public void paintComponent(Graphics g)
\r
578 g.setColor(Color.white);
\r
579 g.fillRect(0, 0, getWidth(), getHeight());
\r
583 || (getVisibleRect().width != g.getClipBounds().width)
\r
584 || (getVisibleRect().height != g.getClipBounds().height))
\r
586 g.drawImage(image, 0, 0, this);
\r
591 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
\r
593 if (image == null || imgWidth != image.getWidth()
\r
594 || image.getHeight(this) != getHeight())
\r
596 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
\r
597 BufferedImage.TYPE_INT_RGB);
\r
598 gg = (Graphics2D) image.getGraphics();
\r
601 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
602 RenderingHints.VALUE_ANTIALIAS_ON);
\r
604 gg.setFont(av.getFont());
\r
605 fm = gg.getFontMetrics();
\r
609 drawComponent(gg, av.startRes, av.endRes + 1);
\r
610 g.drawImage(image, 0, 0, this);
\r
616 * @param horizontal DOCUMENT ME!
\r
618 public void fastPaint(int horizontal)
\r
620 if ((horizontal == 0) || gg==null ||
\r
621 (av.alignment.getAlignmentAnnotation() == null) ||
\r
622 (av.alignment.getAlignmentAnnotation().length < 1))
\r
628 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
\r
630 int sr = av.startRes;
\r
631 int er = av.endRes + 1;
\r
634 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
636 transX = (er - sr - horizontal) * av.charWidth;
\r
637 sr = er - horizontal;
\r
639 else if (horizontal < 0)
\r
641 er = sr - horizontal;
\r
644 gg.translate(transX, 0);
\r
646 drawComponent(gg, sr, er);
\r
648 gg.translate(-transX, 0);
\r
659 * @param g DOCUMENT ME!
\r
660 * @param startRes DOCUMENT ME!
\r
661 * @param endRes DOCUMENT ME!
\r
663 public void drawComponent(Graphics g, int startRes, int endRes)
\r
665 g.setFont(av.getFont());
\r
668 fm = g.getFontMetrics();
\r
671 g.setColor(Color.white);
\r
672 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
\r
674 if ((av.alignment.getAlignmentAnnotation() == null) ||
\r
675 (av.alignment.getAlignmentAnnotation().length < 1))
\r
677 g.setColor(Color.white);
\r
678 g.fillRect(0, 0, getWidth(), getHeight());
\r
679 g.setColor(Color.black);
\r
680 g.drawString("Alignment has no annotations", 20, 15);
\r
685 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
690 char[] lastSS = new char[aa.length];
\r
691 int[] lastSSX = new int[aa.length];
\r
692 int iconOffset = av.charHeight / 2;
\r
693 boolean validRes = false;
\r
695 boolean [] graphGroupDrawn = new boolean[aa.length];
\r
699 for (int i = 0; i < aa.length; i++)
\r
701 AlignmentAnnotation row = aa[i];
\r
712 if(row.graphGroup>-1 && graphGroupDrawn[ row.graphGroup ] )
\r
715 // this is so that we draw the characters below the graph
\r
720 y -= av.charHeight;
\r
726 iconOffset = av.charHeight / 2;
\r
733 for (j = startRes; j < endRes; j++)
\r
735 if ((row.annotations.length <= j) ||
\r
736 (row.annotations[j] == null))
\r
745 x = (j - startRes) * av.charWidth;
\r
747 if (activeRow == i)
\r
749 g.setColor(Color.red);
\r
751 if (activeRes != null)
\r
753 for (int n = 0; n < activeRes.size(); n++)
\r
755 int v = Integer.parseInt(activeRes.get(n).toString());
\r
759 g.fillRect((j - startRes) * av.charWidth, y,
\r
760 av.charWidth, row.height);
\r
767 (row.annotations[j].displayCharacter.length() > 0))
\r
770 int charOffset = (av.charWidth -
\r
771 fm.charWidth(row.annotations[j].displayCharacter.charAt(
\r
773 g.setColor(row.annotations[j].colour);
\r
775 if (j == 0 || row.graph>0)
\r
777 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
778 y + iconOffset + 3);
\r
780 else if (((row.annotations[j - 1] == null) ||
\r
781 (row.annotations[j].displayCharacter != row.annotations[j -
\r
782 1].displayCharacter)))
\r
784 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
785 y + iconOffset + 3);
\r
792 (row.annotations[j].secondaryStructure != lastSS[i]))
\r
797 g.setColor(HELIX_COLOUR);
\r
798 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
799 x - lastSSX[i], 7, 8, 8);
\r
804 g.setColor(SHEET_COLOUR);
\r
805 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
806 x - lastSSX[i] - 4, 7);
\r
807 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
810 y + iconOffset, y + 14 + iconOffset,
\r
818 g.setColor(Color.gray);
\r
819 g.fillRect(lastSSX[i], y + 6 + iconOffset,
\r
820 x - lastSSX[i], 2);
\r
827 lastSS[i] = row.annotations[j].secondaryStructure;
\r
846 g.setColor(HELIX_COLOUR);
\r
847 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
848 x - lastSSX[i], 7, 8, 8);
\r
853 g.setColor(SHEET_COLOUR);
\r
855 if (row.annotations[endRes] !=null
\r
856 && row.annotations[endRes].secondaryStructure != 'E')
\r
858 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
859 x - lastSSX[i] - 4, 7);
\r
860 g.fillPolygon(new int[]
\r
864 y + iconOffset, y + 14 + iconOffset,
\r
869 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
870 x - lastSSX[i], 7);
\r
878 g.setColor(Color.gray);
\r
879 g.fillRect(lastSSX[i], y + 6 + iconOffset, x - lastSSX[i], 2);
\r
887 if(row.graph == AlignmentAnnotation.LINE_GRAPH )
\r
889 if(row.graphGroup>-1 && !graphGroupDrawn[row.graphGroup])
\r
891 float groupmax=-999999, groupmin=9999999;
\r
892 for(int gg=0; gg<aa.length; gg++)
\r
894 if(aa[gg].graphGroup!=row.graphGroup)
\r
898 aa[gg].visible = false;
\r
900 if(aa[gg].graphMax>groupmax)
\r
901 groupmax = aa[gg].graphMax;
\r
902 if(aa[gg].graphMin<groupmin)
\r
903 groupmin = aa[gg].graphMin;
\r
906 for (int gg = 0; gg < aa.length; gg++)
\r
908 if (aa[gg].graphGroup == row.graphGroup)
\r
910 drawLineGraph(g, aa[gg], startRes, endRes, y,
\r
911 groupmin, groupmax,
\r
916 graphGroupDrawn[ row.graphGroup ] = true;
\r
919 drawLineGraph(g, row, startRes, endRes,
\r
920 y, row.graphMin, row.graphMax, row.graphHeight );
\r
922 else if(row.graph == AlignmentAnnotation.BAR_GRAPH )
\r
923 drawBarGraph(g, row, startRes, endRes,
\r
924 row.graphMin, row.graphMax, y);
\r
927 if (row.graph>0 && row.hasText)
\r
929 y += av.charHeight;
\r
939 public void drawLineGraph(Graphics g, AlignmentAnnotation aa,
\r
940 int sRes, int eRes,
\r
942 float min, float max,
\r
945 if(sRes>aa.annotations.length)
\r
950 //Adjustment for fastpaint to left
\r
961 float range = max - min;
\r
965 y2 = (int)(y - (0-min / range)*graphHeight);
\r
967 g.setColor(Color.gray);
\r
968 g.drawLine(x-av.charWidth,y2,(eRes-sRes+1)*av.charWidth,y2);
\r
971 if(aa.threshold!=null)
\r
973 g.setColor(aa.threshold.colour);
\r
974 Graphics2D g2 = (Graphics2D)g;
\r
975 g2.setStroke(new BasicStroke(1,
\r
976 BasicStroke.CAP_SQUARE,
\r
977 BasicStroke.JOIN_ROUND, 3f,
\r
978 new float[] { 5f, 3f }, 0f));
\r
980 y2 = (int)(y - ((aa.threshold.value-min) / range)*graphHeight);
\r
981 g.drawLine(x-av.charWidth,y2,(eRes-sRes+1)*av.charWidth,y2);
\r
982 g2.setStroke(new BasicStroke());
\r
985 eRes = Math.min(eRes, aa.annotations.length);
\r
986 for (int j = sRes; j < eRes; j++)
\r
988 if(aa.annotations[j]==null || aa.annotations[j-1]==null)
\r
993 g.setColor(aa.annotations[j].colour);
\r
994 y1 = y - (int) (((aa.annotations[j-1].value-min) / range) * graphHeight);
\r
995 y2 = y - (int) (((aa.annotations[j].value-min) / range) * graphHeight);
\r
996 g.drawLine(x-av.charWidth/2, y1, x+av.charWidth/2, y2);
\r
1001 public void drawBarGraph(Graphics g, AlignmentAnnotation aa,
\r
1002 int sRes, int eRes,
\r
1003 float min, float max,
\r
1006 if(sRes>aa.annotations.length)
\r
1011 float range = max - min;
\r
1013 if(aa.threshold!=null)
\r
1015 g.setColor(aa.threshold.colour);
\r
1016 Graphics2D g2 = (Graphics2D)g;
\r
1017 g2.setStroke(new BasicStroke(1,
\r
1018 BasicStroke.CAP_SQUARE,
\r
1019 BasicStroke.JOIN_ROUND, 3f,
\r
1020 new float[] { 5f, 3f }, 0f));
\r
1022 y2 = (int)(y - ((aa.threshold.value-min) / range)*aa.graphHeight);
\r
1023 g.drawLine(x-av.charWidth,y2,(eRes-sRes+1)*av.charWidth,y2);
\r
1024 g2.setStroke(new BasicStroke());
\r
1030 y2 = (int)(y - (0-min / (range))*aa.graphHeight);
\r
1032 g.setColor(Color.gray);
\r
1034 g.drawLine(x,y2,(eRes-sRes+1)*av.charWidth,y2);
\r
1036 eRes = Math.min(eRes, aa.annotations.length-1);
\r
1039 for (int j = sRes; j < eRes; j++)
\r
1042 if (aa.annotations[j] == null)
\r
1044 x += av.charWidth;
\r
1048 g.setColor(aa.annotations[j].colour);
\r
1049 y1 = y - (int) (((aa.annotations[j].value-min) / (range)) * aa.graphHeight);
\r
1052 g.fillRect(x, y2, av.charWidth, y1-y2 );
\r
1054 g.fillRect(x, y1, av.charWidth, y2-y1 );
\r
1056 x += av.charWidth;
\r
1061 // used by overview window
\r
1062 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y, int sRes, int eRes)
\r
1064 g.setColor(Color.white);
\r
1065 g.fillRect(0, 0, width, y);
\r
1066 g.setColor(new Color(0, 0, 180));
\r
1068 int x = 0, height;
\r
1070 for (int j = sRes; j < eRes; j++)
\r
1072 g.setColor(aa.annotations[j].colour);
\r
1074 height = (int) ((aa.annotations[j].value / aa.graphMax) * y);
\r
1078 g.fillRect(x, y - height, av.charWidth, height);
\r
1079 x += av.charWidth;
\r