2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 package jalview.appletgui;
25 import java.awt.event.*;
27 import jalview.datamodel.*;
29 public class AnnotationPanel
31 implements AdjustmentListener, ActionListener, MouseListener, MouseMotionListener
38 static String HELIX = "Helix";
39 static String SHEET = "Sheet";
40 static String LABEL = "Label";
41 static String REMOVE = "Remove Annotation";
42 static String COLOUR = "Colour";
43 static Color HELIX_COLOUR = Color.red.darker();
44 static Color SHEET_COLOUR = Color.green.darker().darker();
51 boolean fastPaint = false;
52 public static int GRAPH_HEIGHT = 40;
56 public AnnotationPanel(AlignmentPanel ap)
58 MAC = new jalview.util.Platform().isAMac();
64 addMouseMotionListener(this);
66 addMouseListener(this);
69 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
72 public AnnotationPanel(AlignViewport av)
77 public void adjustmentValueChanged(AdjustmentEvent evt)
79 ap.alabels.setScrollOffset( -evt.getValue());
85 * @param evt DOCUMENT ME!
87 public void actionPerformed(ActionEvent evt)
89 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
90 Annotation[] anot = aa[activeRow].annotations;
92 if (anot.length < av.getColumnSelection().getMax())
94 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
95 System.arraycopy(anot, 0, temp, 0, anot.length);
97 aa[activeRow].annotations = anot;
101 if (av.colSel != null && av.colSel.size() > 0
102 && anot[av.colSel.getMin()] != null)
103 label = anot[av.getColumnSelection().getMin()].displayCharacter;
106 if (evt.getActionCommand().equals(REMOVE))
108 for (int i = 0; i < av.getColumnSelection().size(); i++)
110 anot[av.getColumnSelection().columnAt(i)] = null;
113 else if (evt.getActionCommand().equals(LABEL))
115 label = enterLabel(label, "Enter Label");
122 if ( (label.length() > 0) && !aa[activeRow].hasText)
124 aa[activeRow].hasText = true;
127 for (int i = 0; i < av.getColumnSelection().size(); i++)
129 int index = av.getColumnSelection().columnAt(i);
131 if(!av.colSel.isVisible(index))
134 if (anot[index] == null)
136 anot[index] = new Annotation(label, "", ' ', 0);
139 anot[index].displayCharacter = label;
142 else if (evt.getActionCommand().equals(COLOUR))
144 UserDefinedColours udc = new UserDefinedColours(
146 Color.black, ap.alignFrame);
148 Color col = udc.getColor();
150 for (int i = 0; i < av.getColumnSelection().size(); i++)
152 int index = av.getColumnSelection().columnAt(i);
154 if(!av.colSel.isVisible(index))
157 if (anot[index] == null)
159 anot[index] = new Annotation("", "", ' ', 0);
162 anot[index].colour = col;
165 else // HELIX OR SHEET
168 String symbol = "\u03B1";
170 if (evt.getActionCommand().equals(HELIX))
174 else if (evt.getActionCommand().equals(SHEET))
180 if (!aa[activeRow].hasIcons)
182 aa[activeRow].hasIcons = true;
185 label = enterLabel(symbol, "Enter Label");
192 if ( (label.length() > 0) && !aa[activeRow].hasText)
194 aa[activeRow].hasText = true;
197 for (int i = 0; i < av.getColumnSelection().size(); i++)
199 int index = av.getColumnSelection().columnAt(i);
201 if(!av.colSel.isVisible(index))
204 if (anot[index] == null)
206 anot[index] = new Annotation(label, "", type, 0);
209 anot[index].secondaryStructure = type;
210 anot[index].displayCharacter = label;
220 String enterLabel(String text, String label)
222 EditNameDialog dialog = new EditNameDialog(text,null,label,null,
223 ap.alignFrame,"Enter Label", 400,200, true);
226 return dialog.getName();
231 public void mousePressed(MouseEvent evt)
233 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
243 for (int i = 0; i < aa.length; i++)
247 height += aa[i].height;
250 if (evt.getY() < height)
261 if ( (evt.getModifiers() & InputEvent.BUTTON3_MASK) ==
262 InputEvent.BUTTON3_MASK && activeRow != -1)
264 if (av.getColumnSelection() == null)
269 PopupMenu pop = new PopupMenu("Structure type");
270 MenuItem item = new MenuItem(HELIX);
271 item.addActionListener(this);
273 item = new MenuItem(SHEET);
274 item.addActionListener(this);
276 item = new MenuItem(LABEL);
277 item.addActionListener(this);
279 item = new MenuItem(COLOUR);
280 item.addActionListener(this);
282 item = new MenuItem(REMOVE);
283 item.addActionListener(this);
285 ap.alignFrame.add(pop);
286 pop.show(this, evt.getX(), evt.getY());
296 ap.scalePanel.mousePressed(evt);
299 public void mouseReleased(MouseEvent evt)
301 ap.scalePanel.mouseReleased(evt);
304 public void mouseClicked(MouseEvent evt)
307 public void mouseDragged(MouseEvent evt)
309 ap.scalePanel.mouseDragged(evt);
312 public void mouseMoved(MouseEvent evt)
314 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
322 for (int i = 0; i < aa.length; i++)
327 height += aa[i].height;
330 if (evt.getY() < height)
337 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
339 if (av.hasHiddenColumns)
341 res = av.getColumnSelection().adjustForHiddenColumns(res);
344 if (row > -1 && res < aa[row].annotations.length && aa[row].annotations[res] != null)
346 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
347 if (aa[row].annotations[res].description != null)
349 text.append(" " + aa[row].annotations[res].description);
351 ap.alignFrame.statusBar.setText(text.toString());
354 public void mouseEntered(MouseEvent evt)
356 ap.scalePanel.mouseEntered(evt);
358 public void mouseExited(MouseEvent evt)
360 ap.scalePanel.mouseExited(evt);
364 public int adjustPanelHeight()
366 // setHeight of panels
367 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
372 for (int i = 0; i < aa.length; i++)
383 aa[i].height += av.charHeight;
393 aa[i].height += aa[i].graphHeight;
396 if (aa[i].height == 0)
401 height += aa[i].height;
409 this.setSize(getSize().width, height);
417 public void addEditableColumn(int i)
421 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
427 for (int j = 0; j < aa.length; j++)
437 if (activeRes == null)
439 activeRes = new Vector();
440 activeRes.addElement(String.valueOf(i));
444 activeRes.addElement(String.valueOf(i));
448 public void update(Graphics g)
453 public void paint(Graphics g)
456 imgWidth = getSize().width;
457 //(av.endRes - av.startRes + 1) * av.charWidth;
459 if (image == null || imgWidth != image.getWidth(this))
461 image = createImage(imgWidth, ap.annotationPanel.getSize().height);
462 gg = image.getGraphics();
463 gg.setFont(av.getFont());
464 fm = gg.getFontMetrics();
470 g.drawImage(image, 0, 0, this);
475 gg.setColor(Color.white);
476 gg.fillRect(0, 0, getSize().width, getSize().height);
477 drawComponent(gg, av.startRes, av.endRes + 1);
479 g.drawImage(image, 0, 0, this);
482 public void fastPaint(int horizontal)
485 || av.alignment.getAlignmentAnnotation() == null
486 || av.alignment.getAlignmentAnnotation().length < 1
493 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal * av.charWidth, 0);
494 int sr = av.startRes, er = av.endRes + 1, transX = 0;
496 if (horizontal > 0) // scrollbar pulled right, image to the left
498 transX = (er - sr - horizontal) * av.charWidth;
499 sr = er - horizontal;
501 else if (horizontal < 0)
503 er = sr - horizontal;
506 gg.translate(transX, 0);
508 drawComponent(gg, sr, er);
510 gg.translate( -transX, 0);
519 * @param g DOCUMENT ME!
520 * @param startRes DOCUMENT ME!
521 * @param endRes DOCUMENT ME!
523 public void drawComponent(Graphics g, int startRes, int endRes)
525 g.setFont(av.getFont());
527 g.setColor(Color.white);
528 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
532 fm = g.getFontMetrics();
535 if ( (av.alignment.getAlignmentAnnotation() == null) ||
536 (av.alignment.getAlignmentAnnotation().length < 1))
538 g.setColor(Color.white);
539 g.fillRect(0, 0, getSize().width, getSize().height);
540 g.setColor(Color.black);
541 if (av.validCharWidth)
543 g.drawString("Alignment has no annotations", 20, 15);
549 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
556 int iconOffset = av.charHeight / 2;
557 boolean validRes = false;
559 boolean[] graphGroupDrawn = new boolean[aa.length];
562 for (int i = 0; i < aa.length; i++)
564 AlignmentAnnotation row = aa[i];
576 if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
581 // this is so that we draw the characters below the graph
592 iconOffset = av.charHeight / 2;
600 while (x < endRes - startRes)
602 if (av.hasHiddenColumns)
604 column = av.getColumnSelection().adjustForHiddenColumns(startRes + x);
605 if (column > row.annotations.length - 1)
612 column = startRes + x;
615 if ( (row.annotations.length <= column) ||
616 (row.annotations[column] == null))
627 g.setColor(Color.red);
629 if (av.getColumnSelection() != null)
631 for (int n = 0; n < av.getColumnSelection().size(); n++)
633 int v = av.getColumnSelection().columnAt(n);
637 g.fillRect(x * av.charWidth, y,
638 av.charWidth, av.charHeight);
646 if (av.validCharWidth && validRes &&
647 (row.annotations[column].displayCharacter!=null && row.annotations[column].displayCharacter.length() > 0))
649 int charOffset = (av.getCentreColumnLabels()) ? ((av.charWidth -
650 fm.charsWidth(row.annotations[column].
651 displayCharacter.toCharArray(),0,
652 row.annotations[column].
653 displayCharacter.length())) / 2)
654 : (av.charWidth - fm.charWidth(row.annotations[column].
655 displayCharacter.charAt(0))) / 2;
657 if (row.annotations[column].colour == null)
658 g.setColor(Color.black);
660 g.setColor(row.annotations[column].colour);
662 if (column == 0 || row.graph > 0)
664 g.drawString(row.annotations[column].displayCharacter,
665 (x * av.charWidth) + charOffset,
669 row.annotations[column - 1] == null
670 || (!row.annotations[column].displayCharacter.equals(
671 row.annotations[column - 1].displayCharacter)
673 (row.annotations[column].displayCharacter.length() < 2 &&
674 row.annotations[column].secondaryStructure == ' ')))
676 g.drawString(row.annotations[column].displayCharacter,
677 (x * av.charWidth) + charOffset,
685 (row.annotations[column].secondaryStructure != lastSS))
690 g.setColor(HELIX_COLOUR);
693 //Off by 1 offset when drawing rects and ovals
694 //to offscreen image on the MAC
695 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
696 (x * av.charWidth) - lastSSX, 7, 8, 8);
700 int sCol = (lastSSX / av.charWidth) + startRes;
702 int x2 = (x * av.charWidth);
705 row.annotations[sCol - 1] == null ||
706 row.annotations[sCol - 1].secondaryStructure != 'H')
708 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
710 x1 += av.charWidth / 2;
713 if (row.annotations[column] == null ||
714 row.annotations[column].secondaryStructure != 'H')
716 g.fillArc( (x * av.charWidth) - av.charWidth,
717 y + 4 + iconOffset, av.charWidth, 8, 270, 180);
718 x2 -= av.charWidth / 2;
721 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
725 g.setColor(SHEET_COLOUR);
726 g.fillRect(lastSSX, y + 4 + iconOffset,
727 (x * av.charWidth) - lastSSX - 4, 7);
728 g.fillPolygon(new int[]
729 { (x * av.charWidth) - 4,
730 (x * av.charWidth) - 4,
734 y + iconOffset, y + 14 + iconOffset,
741 g.setColor(Color.gray);
742 g.fillRect(lastSSX, y + 6 + iconOffset,
743 (x * av.charWidth) - lastSSX, 2);
750 lastSS = row.annotations[column].secondaryStructure;
757 lastSSX = (x * av.charWidth);
765 if (column >= row.annotations.length)
767 column = row.annotations.length - 1;
777 g.setColor(HELIX_COLOUR);
780 //Off by 1 offset when drawing rects and ovals
781 //to offscreen image on the MAC
782 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
783 (x * av.charWidth) - lastSSX, 7, 8, 8);
787 int sCol = (lastSSX / av.charWidth) + startRes;
789 int x2 = (x * av.charWidth);
792 row.annotations[sCol - 1] == null ||
793 row.annotations[sCol - 1].secondaryStructure != 'H')
795 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90, 180);
796 x1 += av.charWidth / 2;
799 if (row.annotations[column] == null ||
800 row.annotations[column].secondaryStructure != 'H')
802 g.fillArc( (x * av.charWidth) - av.charWidth,
803 y + 4 + iconOffset, av.charWidth, 8, 270,
805 x2 -= av.charWidth / 2;
808 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
813 g.setColor(SHEET_COLOUR);
815 if (row.annotations[endRes] == null
816 || row.annotations[endRes].secondaryStructure != 'E')
818 g.fillRect(lastSSX, y + 4 + iconOffset,
819 (x * av.charWidth) - lastSSX - 4, 7);
820 g.fillPolygon(new int[]
821 { (x * av.charWidth) - 4,
822 (x * av.charWidth) - 4,
826 y + iconOffset, y + 14 + iconOffset,
832 g.fillRect(lastSSX, y + 4 + iconOffset,
833 x * av.charWidth - lastSSX, 7);
838 g.setColor(Color.gray);
839 if (!av.wrapAlignment || endRes == av.endRes)
841 g.fillRect(lastSSX, y + 6 + iconOffset,
842 (x * av.charWidth) - lastSSX, 2);
851 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
853 if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
855 float groupmax = -999999, groupmin = 9999999;
856 for (int gg = 0; gg < aa.length; gg++)
858 if (aa[gg].graphGroup != row.graphGroup)
865 aa[gg].visible = false;
868 if (aa[gg].graphMax > groupmax)
870 groupmax = aa[gg].graphMax;
872 if (aa[gg].graphMin < groupmin)
874 groupmin = aa[gg].graphMin;
878 for (int gg = 0; gg < aa.length; gg++)
880 if (aa[gg].graphGroup == row.graphGroup)
882 drawLineGraph(g, aa[gg], startRes, endRes, y,
888 graphGroupDrawn[row.graphGroup] = true;
892 drawLineGraph(g, row, startRes, endRes,
893 y, row.graphMin, row.graphMax, row.graphHeight);
896 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
898 drawBarGraph(g, row, startRes, endRes,
899 row.graphMin, row.graphMax, y);
903 if (row.graph > 0 && row.hasText)
915 public void drawLineGraph(Graphics g, AlignmentAnnotation aa,
918 float min, float max,
921 if (sRes > aa.annotations.length)
928 //Adjustment for fastpaint to left
929 if (eRes < av.endRes)
934 eRes = Math.min(eRes, aa.annotations.length);
937 float range = max - min;
942 y2 = y - (int) ( (0 - min / range) * graphHeight);
945 g.setColor(Color.gray);
946 g.drawLine(x - av.charWidth, y2, (eRes - sRes) * av.charWidth, y2);
948 eRes = Math.min(eRes, aa.annotations.length);
951 int aaMax = aa.annotations.length - 1;
953 while (x < eRes - sRes)
956 if (av.hasHiddenColumns)
958 column = av.getColumnSelection().adjustForHiddenColumns(column);
966 if (aa.annotations[column] == null) // || coaa.annotations[column - 1] == null)
972 if (aa.annotations[column].colour == null)
973 g.setColor(Color.black);
975 g.setColor(aa.annotations[column].colour);
976 if (column==0 || aa.annotations[column-1]==null)
979 (int) ( ( (aa.annotations[column].value - min) / range) * graphHeight);
982 (int) ( ( (aa.annotations[column-1].value - min) / range) * graphHeight);
985 (int) ( ( (aa.annotations[column].value - min) / range) * graphHeight);
987 g.drawLine(x * av.charWidth - av.charWidth / 2, y1,
988 x * av.charWidth + av.charWidth / 2, y2);
992 if (aa.threshold != null)
994 g.setColor(aa.threshold.colour);
996 y2 = (int) (y - ( (aa.threshold.value - min) / range) * graphHeight);
997 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1001 public void drawBarGraph(Graphics g, AlignmentAnnotation aa,
1003 float min, float max,
1006 if (sRes > aa.annotations.length)
1011 eRes = Math.min(eRes, aa.annotations.length);
1013 int x = 0, y1 = y, y2 = y;
1015 float range = max - min;
1019 y2 = y - (int) ( (0 - min / (range)) * aa.graphHeight);
1022 g.setColor(Color.gray);
1024 g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
1027 int aaMax = aa.annotations.length - 1;
1029 while (x < eRes - sRes)
1032 if (av.hasHiddenColumns)
1034 column = av.getColumnSelection().adjustForHiddenColumns(column);
1042 if (aa.annotations[column] == null)
1048 if (aa.annotations[column].colour == null)
1049 g.setColor(Color.black);
1051 g.setColor(aa.annotations[column].colour);
1054 (int) ( ( (aa.annotations[column].value - min) / (range)) * aa.graphHeight);
1058 g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
1062 g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
1068 if (aa.threshold != null)
1070 g.setColor(aa.threshold.colour);
1071 y2 = (int) (y - ( (aa.threshold.value - min) / range) * aa.graphHeight);
1072 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1076 // used by overview window
1077 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y,
1080 eRes = Math.min(eRes, aa.annotations.length);
1081 g.setColor(Color.white);
1082 g.fillRect(0, 0, width, y);
1083 g.setColor(new Color(0, 0, 180));
1087 for (int j = sRes; j < eRes; j++)
1089 if (aa.annotations[j].colour == null)
1090 g.setColor(Color.black);
1092 g.setColor(aa.annotations[j].colour);
1094 height = (int) ( (aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
1099 g.fillRect(x, y - height, av.charWidth, height);