2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 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
20 package jalview.appletgui;
\r
25 import java.awt.event.*;
\r
27 import jalview.datamodel.*;
\r
29 public class AnnotationPanel
\r
31 implements AdjustmentListener, ActionListener, MouseListener, MouseMotionListener
\r
38 static String HELIX = "Helix";
\r
39 static String SHEET = "Sheet";
\r
40 static String LABEL = "Label";
\r
41 static String REMOVE = "Remove Annotation";
\r
42 static String COLOUR = "Colour";
\r
43 static Color HELIX_COLOUR = Color.red.darker();
\r
44 static Color SHEET_COLOUR = Color.green.darker().darker();
\r
51 boolean fastPaint = false;
\r
53 public static int GRAPH_HEIGHT = 40;
\r
55 boolean MAC = false;
\r
57 public AnnotationPanel(AlignmentPanel ap)
\r
59 if (System.getProperty("os.name").startsWith("Mac"))
\r
67 adjustPanelHeight();
\r
69 addMouseMotionListener(this);
\r
71 addMouseListener(this);
\r
74 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
\r
77 public AnnotationPanel(AlignViewport av)
\r
82 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
84 ap.alabels.setScrollOffset( -evt.getValue());
\r
90 * @param evt DOCUMENT ME!
\r
92 public void actionPerformed(ActionEvent evt)
\r
94 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
95 Annotation[] anot = aa[activeRow].annotations;
\r
97 if (anot.length < av.getColumnSelection().getMax())
\r
99 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
\r
100 System.arraycopy(anot, 0, temp, 0, anot.length);
\r
102 aa[activeRow].annotations = anot;
\r
106 if (av.colSel != null && av.colSel.size() > 0
\r
107 && anot[av.colSel.getMin()] != null)
\r
108 label = anot[av.getColumnSelection().getMin()].displayCharacter;
\r
111 if (evt.getActionCommand().equals(REMOVE))
\r
113 for (int i = 0; i < av.getColumnSelection().size(); i++)
\r
115 anot[av.getColumnSelection().columnAt(i)] = null;
\r
118 else if (evt.getActionCommand().equals(LABEL))
\r
120 label = enterLabel(label, "Enter Label");
\r
127 if ( (label.length() > 0) && !aa[activeRow].hasText)
\r
129 aa[activeRow].hasText = true;
\r
132 for (int i = 0; i < av.getColumnSelection().size(); i++)
\r
134 int index = av.getColumnSelection().columnAt(i);
\r
136 if(!av.colSel.isVisible(index))
\r
139 if (anot[index] == null)
\r
141 anot[index] = new Annotation(label, "", ' ', 0);
\r
144 anot[index].displayCharacter = label;
\r
147 else if (evt.getActionCommand().equals(COLOUR))
\r
149 UserDefinedColours udc = new UserDefinedColours(
\r
151 Color.black, ap.alignFrame);
\r
153 Color col = udc.getColor();
\r
155 for (int i = 0; i < av.getColumnSelection().size(); i++)
\r
157 int index = av.getColumnSelection().columnAt(i);
\r
159 if(!av.colSel.isVisible(index))
\r
162 if (anot[index] == null)
\r
164 anot[index] = new Annotation("", "", ' ', 0);
\r
167 anot[index].colour = col;
\r
170 else // HELIX OR SHEET
\r
173 String symbol = "\u03B1";
\r
175 if (evt.getActionCommand().equals(HELIX))
\r
179 else if (evt.getActionCommand().equals(SHEET))
\r
185 if (!aa[activeRow].hasIcons)
\r
187 aa[activeRow].hasIcons = true;
\r
190 label = enterLabel(symbol, "Enter Label");
\r
197 if ( (label.length() > 0) && !aa[activeRow].hasText)
\r
199 aa[activeRow].hasText = true;
\r
202 for (int i = 0; i < av.getColumnSelection().size(); i++)
\r
204 int index = av.getColumnSelection().columnAt(i);
\r
206 if(!av.colSel.isVisible(index))
\r
209 if (anot[index] == null)
\r
211 anot[index] = new Annotation(label, "", type, 0);
\r
214 anot[index].secondaryStructure = type;
\r
215 anot[index].displayCharacter = label;
\r
219 adjustPanelHeight();
\r
225 String enterLabel(String text, String label)
\r
227 EditNameDialog dialog = new EditNameDialog(text,null,label,null,
\r
228 ap.alignFrame,"Enter Label", 400,200);
\r
231 return dialog.getName();
\r
236 public void mousePressed(MouseEvent evt)
\r
238 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
248 for (int i = 0; i < aa.length; i++)
\r
252 height += aa[i].height;
\r
255 if (evt.getY() < height)
\r
257 if (aa[i].editable)
\r
266 if ( (evt.getModifiers() & InputEvent.BUTTON3_MASK) ==
\r
267 InputEvent.BUTTON3_MASK && activeRow != -1)
\r
269 if (av.getColumnSelection() == null)
\r
274 PopupMenu pop = new PopupMenu("Structure type");
\r
275 MenuItem item = new MenuItem(HELIX);
\r
276 item.addActionListener(this);
\r
278 item = new MenuItem(SHEET);
\r
279 item.addActionListener(this);
\r
281 item = new MenuItem(LABEL);
\r
282 item.addActionListener(this);
\r
284 item = new MenuItem(COLOUR);
\r
285 item.addActionListener(this);
\r
287 item = new MenuItem(REMOVE);
\r
288 item.addActionListener(this);
\r
290 ap.alignFrame.add(pop);
\r
291 pop.show(this, evt.getX(), evt.getY());
\r
301 ap.scalePanel.mousePressed(evt);
\r
304 public void mouseReleased(MouseEvent evt)
\r
306 ap.scalePanel.mouseReleased(evt);
\r
309 public void mouseClicked(MouseEvent evt)
\r
312 public void mouseDragged(MouseEvent evt)
\r
314 ap.scalePanel.mouseDragged(evt);
\r
317 public void mouseMoved(MouseEvent evt)
\r
319 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
327 for (int i = 0; i < aa.length; i++)
\r
332 height += aa[i].height;
\r
335 if (evt.getY() < height)
\r
342 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
\r
344 if (av.hasHiddenColumns)
\r
346 res = av.getColumnSelection().adjustForHiddenColumns(res);
\r
349 if (row > -1 && res < aa[row].annotations.length && aa[row].annotations[res] != null)
\r
351 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
\r
352 if (aa[row].annotations[res].description != null)
\r
354 text.append(" " + aa[row].annotations[res].description);
\r
356 ap.alignFrame.statusBar.setText(text.toString());
\r
359 public void mouseEntered(MouseEvent evt)
\r
361 ap.scalePanel.mouseEntered(evt);
\r
363 public void mouseExited(MouseEvent evt)
\r
365 ap.scalePanel.mouseExited(evt);
\r
369 public int adjustPanelHeight()
\r
371 // setHeight of panels
\r
372 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
377 for (int i = 0; i < aa.length; i++)
\r
379 if (!aa[i].visible)
\r
388 aa[i].height += av.charHeight;
\r
390 if (aa[i].hasIcons)
\r
392 aa[i].height += 16;
\r
395 if (aa[i].graph > 0)
\r
397 aa[i].height += GRAPH_HEIGHT;
\r
400 if (aa[i].height == 0)
\r
404 height += aa[i].height;
\r
412 this.setSize(getSize().width, height);
\r
420 public void addEditableColumn(int i)
\r
422 if (activeRow == -1)
\r
424 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
430 for (int j = 0; j < aa.length; j++)
\r
432 if (aa[j].editable)
\r
440 if (activeRes == null)
\r
442 activeRes = new Vector();
\r
443 activeRes.addElement(String.valueOf(i));
\r
447 activeRes.addElement(String.valueOf(i));
\r
451 public void update(Graphics g)
\r
456 public void paint(Graphics g)
\r
459 imgWidth = getSize().width;
\r
460 //(av.endRes - av.startRes + 1) * av.charWidth;
\r
462 if (image == null || imgWidth != image.getWidth(this))
\r
464 image = createImage(imgWidth, ap.annotationPanel.getSize().height);
\r
465 gg = image.getGraphics();
\r
466 gg.setFont(av.getFont());
\r
467 fm = gg.getFontMetrics();
\r
473 g.drawImage(image, 0, 0, this);
\r
478 gg.setColor(Color.white);
\r
479 gg.fillRect(0, 0, getSize().width, getSize().height);
\r
480 drawComponent(gg, av.startRes, av.endRes + 1);
\r
482 g.drawImage(image, 0, 0, this);
\r
485 public void fastPaint(int horizontal)
\r
487 if (horizontal == 0
\r
488 || av.alignment.getAlignmentAnnotation() == null
\r
489 || av.alignment.getAlignmentAnnotation().length < 1
\r
496 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal * av.charWidth, 0);
\r
497 int sr = av.startRes, er = av.endRes + 1, transX = 0;
\r
499 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
501 transX = (er - sr - horizontal) * av.charWidth;
\r
502 sr = er - horizontal;
\r
504 else if (horizontal < 0)
\r
506 er = sr - horizontal;
\r
509 gg.translate(transX, 0);
\r
511 drawComponent(gg, sr, er);
\r
513 gg.translate( -transX, 0);
\r
522 * @param g DOCUMENT ME!
\r
523 * @param startRes DOCUMENT ME!
\r
524 * @param endRes DOCUMENT ME!
\r
526 public void drawComponent(Graphics g, int startRes, int endRes)
\r
528 g.setFont(av.getFont());
\r
530 g.setColor(Color.white);
\r
531 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
\r
535 fm = g.getFontMetrics();
\r
538 if ( (av.alignment.getAlignmentAnnotation() == null) ||
\r
539 (av.alignment.getAlignmentAnnotation().length < 1))
\r
541 g.setColor(Color.white);
\r
542 g.fillRect(0, 0, getSize().width, getSize().height);
\r
543 g.setColor(Color.black);
\r
544 if (av.validCharWidth)
\r
546 g.drawString("Alignment has no annotations", 20, 15);
\r
552 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
559 int iconOffset = av.charHeight / 2;
\r
560 boolean validRes = false;
\r
562 boolean[] graphGroupDrawn = new boolean[aa.length];
\r
565 for (int i = 0; i < aa.length; i++)
\r
567 AlignmentAnnotation row = aa[i];
\r
579 if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
\r
584 // this is so that we draw the characters below the graph
\r
589 y -= av.charHeight;
\r
595 iconOffset = av.charHeight / 2;
\r
603 while (x < endRes - startRes)
\r
605 if (av.hasHiddenColumns)
\r
607 column = av.getColumnSelection().adjustForHiddenColumns(startRes + x);
\r
608 if (column > row.annotations.length - 1)
\r
615 column = startRes + x;
\r
618 if ( (row.annotations.length <= column) ||
\r
619 (row.annotations[column] == null))
\r
628 if (activeRow == i)
\r
630 g.setColor(Color.red);
\r
632 if (av.getColumnSelection() != null)
\r
634 for (int n = 0; n < av.getColumnSelection().size(); n++)
\r
636 int v = av.getColumnSelection().columnAt(n);
\r
640 g.fillRect(x * av.charWidth, y,
\r
641 av.charWidth, av.charHeight);
\r
649 if (av.validCharWidth && validRes &&
\r
650 (row.annotations[column].displayCharacter.length() > 0))
\r
652 int charOffset = (av.charWidth -
\r
653 fm.charWidth(row.annotations[column].
\r
654 displayCharacter.charAt(
\r
657 if (row.annotations[column].colour == null)
\r
658 g.setColor(Color.black);
\r
660 g.setColor(row.annotations[column].colour);
\r
662 if (column == 0 || row.graph > 0)
\r
664 g.drawString(row.annotations[column].displayCharacter,
\r
665 (x * av.charWidth) + charOffset,
\r
666 y + iconOffset + 3);
\r
669 row.annotations[column - 1] == null
\r
670 || (!row.annotations[column].displayCharacter.equals(
\r
671 row.annotations[column - 1].displayCharacter)
\r
673 (row.annotations[column].displayCharacter.length() < 2 &&
\r
674 row.annotations[column].secondaryStructure == ' ')))
\r
676 g.drawString(row.annotations[column].displayCharacter,
\r
677 (x * av.charWidth) + charOffset,
\r
678 y + iconOffset + 3);
\r
685 (row.annotations[column].secondaryStructure != lastSS))
\r
690 g.setColor(HELIX_COLOUR);
\r
693 //Off by 1 offset when drawing rects and ovals
\r
694 //to offscreen image on the MAC
\r
695 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
\r
696 (x * av.charWidth) - lastSSX, 7, 8, 8);
\r
700 int sCol = (lastSSX / av.charWidth) + startRes;
\r
702 int x2 = (x * av.charWidth);
\r
705 row.annotations[sCol - 1] == null ||
\r
706 row.annotations[sCol - 1].secondaryStructure != 'H')
\r
708 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
\r
710 x1 += av.charWidth / 2;
\r
713 if (row.annotations[column] == null ||
\r
714 row.annotations[column].secondaryStructure != 'H')
\r
716 g.fillArc( (x * av.charWidth) - av.charWidth,
\r
717 y + 4 + iconOffset, av.charWidth, 8, 270, 180);
\r
718 x2 -= av.charWidth / 2;
\r
721 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
\r
725 g.setColor(SHEET_COLOUR);
\r
726 g.fillRect(lastSSX, y + 4 + iconOffset,
\r
727 (x * av.charWidth) - lastSSX - 4, 7);
\r
728 g.fillPolygon(new int[]
\r
729 { (x * av.charWidth) - 4,
\r
730 (x * av.charWidth) - 4,
\r
731 (x * av.charWidth)},
\r
734 y + iconOffset, y + 14 + iconOffset,
\r
741 g.setColor(Color.gray);
\r
742 g.fillRect(lastSSX, y + 6 + iconOffset,
\r
743 (x * av.charWidth) - lastSSX, 2);
\r
750 lastSS = row.annotations[column].secondaryStructure;
\r
757 lastSSX = (x * av.charWidth);
\r
765 if (column >= row.annotations.length)
\r
767 column = row.annotations.length - 1;
\r
777 g.setColor(HELIX_COLOUR);
\r
780 //Off by 1 offset when drawing rects and ovals
\r
781 //to offscreen image on the MAC
\r
782 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
\r
783 (x * av.charWidth) - lastSSX, 7, 8, 8);
\r
787 int sCol = (lastSSX / av.charWidth) + startRes;
\r
789 int x2 = (x * av.charWidth);
\r
792 row.annotations[sCol - 1] == null ||
\r
793 row.annotations[sCol - 1].secondaryStructure != 'H')
\r
795 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90, 180);
\r
796 x1 += av.charWidth / 2;
\r
799 if (row.annotations[column] == null ||
\r
800 row.annotations[column].secondaryStructure != 'H')
\r
802 g.fillArc( (x * av.charWidth) - av.charWidth,
\r
803 y + 4 + iconOffset, av.charWidth, 8, 270,
\r
805 x2 -= av.charWidth / 2;
\r
808 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
\r
813 g.setColor(SHEET_COLOUR);
\r
815 if (row.annotations[endRes] == null
\r
816 || row.annotations[endRes].secondaryStructure != 'E')
\r
818 g.fillRect(lastSSX, y + 4 + iconOffset,
\r
819 (x * av.charWidth) - lastSSX - 4, 7);
\r
820 g.fillPolygon(new int[]
\r
821 { (x * av.charWidth) - 4,
\r
822 (x * av.charWidth) - 4,
\r
823 (x * av.charWidth)},
\r
826 y + iconOffset, y + 14 + iconOffset,
\r
832 g.fillRect(lastSSX, y + 4 + iconOffset,
\r
833 x * av.charWidth - lastSSX, 7);
\r
838 g.setColor(Color.gray);
\r
839 if (!av.wrapAlignment || endRes == av.endRes)
\r
841 g.fillRect(lastSSX, y + 6 + iconOffset,
\r
842 (x * av.charWidth) - lastSSX, 2);
\r
851 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
853 if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
\r
855 float groupmax = -999999, groupmin = 9999999;
\r
856 for (int gg = 0; gg < aa.length; gg++)
\r
858 if (aa[gg].graphGroup != row.graphGroup)
\r
865 aa[gg].visible = false;
\r
868 if (aa[gg].graphMax > groupmax)
\r
870 groupmax = aa[gg].graphMax;
\r
872 if (aa[gg].graphMin < groupmin)
\r
874 groupmin = aa[gg].graphMin;
\r
878 for (int gg = 0; gg < aa.length; gg++)
\r
880 if (aa[gg].graphGroup == row.graphGroup)
\r
882 drawLineGraph(g, aa[gg], startRes, endRes, y,
\r
883 groupmin, groupmax,
\r
888 graphGroupDrawn[row.graphGroup] = true;
\r
892 drawLineGraph(g, row, startRes, endRes,
\r
893 y, row.graphMin, row.graphMax, row.graphHeight);
\r
896 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
898 drawBarGraph(g, row, startRes, endRes,
\r
899 row.graphMin, row.graphMax, y);
\r
903 if (row.graph > 0 && row.hasText)
\r
905 y += av.charHeight;
\r
908 if (row.graph == 0)
\r
915 public void drawLineGraph(Graphics g, AlignmentAnnotation aa,
\r
916 int sRes, int eRes,
\r
918 float min, float max,
\r
921 if (sRes > aa.annotations.length)
\r
928 //Adjustment for fastpaint to left
\r
929 if (eRes < av.endRes)
\r
934 eRes = Math.min(eRes, aa.annotations.length);
\r
942 int y1 = y, y2 = y;
\r
943 float range = max - min;
\r
948 y2 = y - (int) ( (0 - min / range) * graphHeight);
\r
951 g.setColor(Color.gray);
\r
952 g.drawLine(x - av.charWidth, y2, (eRes - sRes) * av.charWidth, y2);
\r
954 eRes = Math.min(eRes, aa.annotations.length);
\r
957 int aaMax = aa.annotations.length - 1;
\r
959 while (x < eRes - sRes)
\r
962 if (av.hasHiddenColumns)
\r
964 column = av.getColumnSelection().adjustForHiddenColumns(column);
\r
967 if (column > aaMax)
\r
972 if (aa.annotations[column] == null || aa.annotations[column - 1] == null)
\r
978 if (aa.annotations[column].colour == null)
\r
979 g.setColor(Color.black);
\r
981 g.setColor(aa.annotations[column].colour);
\r
984 (int) ( ( (aa.annotations[column - 1].value - min) / range) * graphHeight);
\r
986 (int) ( ( (aa.annotations[column].value - min) / range) * graphHeight);
\r
988 g.drawLine(x * av.charWidth - av.charWidth / 2, y1,
\r
989 x * av.charWidth + av.charWidth / 2, y2);
\r
993 if (aa.threshold != null)
\r
995 g.setColor(aa.threshold.colour);
\r
997 y2 = (int) (y - ( (aa.threshold.value - min) / range) * graphHeight);
\r
998 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
\r
1002 public void drawBarGraph(Graphics g, AlignmentAnnotation aa,
\r
1003 int sRes, int eRes,
\r
1004 float min, float max,
\r
1007 if (sRes > aa.annotations.length)
\r
1012 eRes = Math.min(eRes, aa.annotations.length);
\r
1014 int x = 0, y1 = y, y2 = y;
\r
1016 float range = max - min;
\r
1020 y2 = y - (int) ( (0 - min / (range)) * aa.graphHeight);
\r
1023 g.setColor(Color.gray);
\r
1025 g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
\r
1028 int aaMax = aa.annotations.length - 1;
\r
1030 while (x < eRes - sRes)
\r
1032 column = sRes + x;
\r
1033 if (av.hasHiddenColumns)
\r
1035 column = av.getColumnSelection().adjustForHiddenColumns(column);
\r
1038 if (column > aaMax)
\r
1043 if (aa.annotations[column] == null)
\r
1049 if (aa.annotations[column].colour == null)
\r
1050 g.setColor(Color.black);
\r
1052 g.setColor(aa.annotations[column].colour);
\r
1055 (int) ( ( (aa.annotations[column].value - min) / (range)) * aa.graphHeight);
\r
1059 g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
\r
1063 g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
\r
1069 if (aa.threshold != null)
\r
1071 g.setColor(aa.threshold.colour);
\r
1072 y2 = (int) (y - ( (aa.threshold.value - min) / range) * aa.graphHeight);
\r
1073 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
\r
1077 // used by overview window
\r
1078 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y,
\r
1079 int sRes, int eRes)
\r
1081 eRes = Math.min(eRes, aa.annotations.length);
\r
1082 g.setColor(Color.white);
\r
1083 g.fillRect(0, 0, width, y);
\r
1084 g.setColor(new Color(0, 0, 180));
\r
1086 int x = 0, height;
\r
1088 for (int j = sRes; j < eRes; j++)
\r
1090 if (aa.annotations[j].colour == null)
\r
1091 g.setColor(Color.black);
\r
1093 g.setColor(aa.annotations[j].colour);
\r
1095 height = (int) ( (aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
\r
1100 g.fillRect(x, y - height, av.charWidth, height);
\r
1101 x += av.charWidth;
\r