2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.datamodel.*;
28 public class AnnotationPanel
30 implements AdjustmentListener, ActionListener, MouseListener, MouseMotionListener
37 static String HELIX = "Helix";
38 static String SHEET = "Sheet";
39 static String LABEL = "Label";
40 static String REMOVE = "Remove Annotation";
41 static String COLOUR = "Colour";
42 static Color HELIX_COLOUR = Color.red.darker();
43 static Color SHEET_COLOUR = Color.green.darker().darker();
50 boolean fastPaint = false;
51 public static int GRAPH_HEIGHT = 40;
55 public AnnotationPanel(AlignmentPanel ap)
57 MAC = new jalview.util.Platform().isAMac();
63 addMouseMotionListener(this);
65 addMouseListener(this);
68 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
71 public AnnotationPanel(AlignViewport av)
76 public void adjustmentValueChanged(AdjustmentEvent evt)
78 ap.alabels.setScrollOffset( -evt.getValue());
84 * @param evt DOCUMENT ME!
86 public void actionPerformed(ActionEvent evt)
88 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
89 Annotation[] anot = aa[activeRow].annotations;
91 if (anot.length < av.getColumnSelection().getMax())
93 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
94 System.arraycopy(anot, 0, temp, 0, anot.length);
96 aa[activeRow].annotations = anot;
100 if (av.colSel != null && av.colSel.size() > 0
101 && anot[av.colSel.getMin()] != null)
102 label = anot[av.getColumnSelection().getMin()].displayCharacter;
105 if (evt.getActionCommand().equals(REMOVE))
107 for (int i = 0; i < av.getColumnSelection().size(); i++)
109 anot[av.getColumnSelection().columnAt(i)] = null;
112 else if (evt.getActionCommand().equals(LABEL))
114 label = enterLabel(label, "Enter Label");
121 if ( (label.length() > 0) && !aa[activeRow].hasText)
123 aa[activeRow].hasText = true;
126 for (int i = 0; i < av.getColumnSelection().size(); i++)
128 int index = av.getColumnSelection().columnAt(i);
130 if(!av.colSel.isVisible(index))
133 if (anot[index] == null)
135 anot[index] = new Annotation(label, "", ' ', 0);
138 anot[index].displayCharacter = label;
141 else if (evt.getActionCommand().equals(COLOUR))
143 UserDefinedColours udc = new UserDefinedColours(
145 Color.black, ap.alignFrame);
147 Color col = udc.getColor();
149 for (int i = 0; i < av.getColumnSelection().size(); i++)
151 int index = av.getColumnSelection().columnAt(i);
153 if(!av.colSel.isVisible(index))
156 if (anot[index] == null)
158 anot[index] = new Annotation("", "", ' ', 0);
161 anot[index].colour = col;
164 else // HELIX OR SHEET
167 String symbol = "\u03B1";
169 if (evt.getActionCommand().equals(HELIX))
173 else if (evt.getActionCommand().equals(SHEET))
179 if (!aa[activeRow].hasIcons)
181 aa[activeRow].hasIcons = true;
184 label = enterLabel(symbol, "Enter Label");
191 if ( (label.length() > 0) && !aa[activeRow].hasText)
193 aa[activeRow].hasText = true;
196 for (int i = 0; i < av.getColumnSelection().size(); i++)
198 int index = av.getColumnSelection().columnAt(i);
200 if(!av.colSel.isVisible(index))
203 if (anot[index] == null)
205 anot[index] = new Annotation(label, "", type, 0);
208 anot[index].secondaryStructure = type;
209 anot[index].displayCharacter = label;
219 String enterLabel(String text, String label)
221 EditNameDialog dialog = new EditNameDialog(text,null,label,null,
222 ap.alignFrame,"Enter Label", 400,200, true);
225 return dialog.getName();
230 public void mousePressed(MouseEvent evt)
232 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
242 for (int i = 0; i < aa.length; i++)
246 height += aa[i].height;
249 if (evt.getY() < height)
260 if ( (evt.getModifiers() & InputEvent.BUTTON3_MASK) ==
261 InputEvent.BUTTON3_MASK && activeRow != -1)
263 if (av.getColumnSelection() == null)
268 PopupMenu pop = new PopupMenu("Structure type");
269 MenuItem item = new MenuItem(HELIX);
270 item.addActionListener(this);
272 item = new MenuItem(SHEET);
273 item.addActionListener(this);
275 item = new MenuItem(LABEL);
276 item.addActionListener(this);
278 item = new MenuItem(COLOUR);
279 item.addActionListener(this);
281 item = new MenuItem(REMOVE);
282 item.addActionListener(this);
284 ap.alignFrame.add(pop);
285 pop.show(this, evt.getX(), evt.getY());
295 ap.scalePanel.mousePressed(evt);
298 public void mouseReleased(MouseEvent evt)
300 ap.scalePanel.mouseReleased(evt);
303 public void mouseClicked(MouseEvent evt)
306 public void mouseDragged(MouseEvent evt)
308 ap.scalePanel.mouseDragged(evt);
311 public void mouseMoved(MouseEvent evt)
313 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
321 for (int i = 0; i < aa.length; i++)
326 height += aa[i].height;
329 if (evt.getY() < height)
336 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
338 if (av.hasHiddenColumns)
340 res = av.getColumnSelection().adjustForHiddenColumns(res);
343 if (row > -1 && res < aa[row].annotations.length && aa[row].annotations[res] != null)
345 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
346 if (aa[row].annotations[res].description != null)
348 text.append(" " + aa[row].annotations[res].description);
350 ap.alignFrame.statusBar.setText(text.toString());
353 public void mouseEntered(MouseEvent evt)
355 ap.scalePanel.mouseEntered(evt);
357 public void mouseExited(MouseEvent evt)
359 ap.scalePanel.mouseExited(evt);
363 public int adjustPanelHeight()
365 // setHeight of panels
366 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
371 for (int i = 0; i < aa.length; i++)
382 aa[i].height += av.charHeight;
392 aa[i].height += aa[i].graphHeight;
395 if (aa[i].height == 0)
400 height += aa[i].height;
408 this.setSize(getSize().width, height);
416 public void addEditableColumn(int i)
420 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
426 for (int j = 0; j < aa.length; j++)
436 if (activeRes == null)
438 activeRes = new Vector();
439 activeRes.addElement(String.valueOf(i));
443 activeRes.addElement(String.valueOf(i));
447 public void update(Graphics g)
452 public void paint(Graphics g)
455 imgWidth = getSize().width;
456 //(av.endRes - av.startRes + 1) * av.charWidth;
458 if (image == null || imgWidth != image.getWidth(this))
460 image = createImage(imgWidth, ap.annotationPanel.getSize().height);
461 gg = image.getGraphics();
462 gg.setFont(av.getFont());
463 fm = gg.getFontMetrics();
469 g.drawImage(image, 0, 0, this);
474 gg.setColor(Color.white);
475 gg.fillRect(0, 0, getSize().width, getSize().height);
476 drawComponent(gg, av.startRes, av.endRes + 1);
478 g.drawImage(image, 0, 0, this);
481 public void fastPaint(int horizontal)
484 || av.alignment.getAlignmentAnnotation() == null
485 || av.alignment.getAlignmentAnnotation().length < 1
492 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal * av.charWidth, 0);
493 int sr = av.startRes, er = av.endRes + 1, transX = 0;
495 if (horizontal > 0) // scrollbar pulled right, image to the left
497 transX = (er - sr - horizontal) * av.charWidth;
498 sr = er - horizontal;
500 else if (horizontal < 0)
502 er = sr - horizontal;
505 gg.translate(transX, 0);
507 drawComponent(gg, sr, er);
509 gg.translate( -transX, 0);
518 * @param g DOCUMENT ME!
519 * @param startRes DOCUMENT ME!
520 * @param endRes DOCUMENT ME!
522 public void drawComponent(Graphics g, int startRes, int endRes)
524 g.setFont(av.getFont());
526 g.setColor(Color.white);
527 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
531 fm = g.getFontMetrics();
534 if ( (av.alignment.getAlignmentAnnotation() == null) ||
535 (av.alignment.getAlignmentAnnotation().length < 1))
537 g.setColor(Color.white);
538 g.fillRect(0, 0, getSize().width, getSize().height);
539 g.setColor(Color.black);
540 if (av.validCharWidth)
542 g.drawString("Alignment has no annotations", 20, 15);
548 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
555 int iconOffset = av.charHeight / 2;
556 boolean validRes = false;
557 boolean validEnd = false;
558 boolean[] graphGroupDrawn = new boolean[aa.length];
561 for (int i = 0; i < aa.length; i++)
563 AlignmentAnnotation row = aa[i];
575 if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
580 // this is so that we draw the characters below the graph
591 iconOffset = av.charHeight / 2;
599 while (x < endRes - startRes)
601 if (av.hasHiddenColumns)
603 column = av.getColumnSelection().adjustForHiddenColumns(startRes + x);
604 if (column > row.annotations.length - 1)
611 column = startRes + x;
614 if ( (row.annotations.length <= column) ||
615 (row.annotations[column] == null))
626 g.setColor(Color.red);
628 if (av.getColumnSelection() != null)
630 for (int n = 0; n < av.getColumnSelection().size(); n++)
632 int v = av.getColumnSelection().columnAt(n);
636 g.fillRect(x * av.charWidth, y,
637 av.charWidth, av.charHeight);
645 if (av.validCharWidth && validRes &&
646 (row.annotations[column].displayCharacter!=null && row.annotations[column].displayCharacter.length() > 0))
648 int charOffset = (av.getCentreColumnLabels()) ? ((av.charWidth -
649 fm.charsWidth(row.annotations[column].
650 displayCharacter.toCharArray(),0,
651 row.annotations[column].
652 displayCharacter.length())) / 2)
653 : (av.charWidth - fm.charWidth(row.annotations[column].
654 displayCharacter.charAt(0))) / 2;
656 if (row.annotations[column].colour == null)
657 g.setColor(Color.black);
659 g.setColor(row.annotations[column].colour);
661 if (column == 0 || row.graph > 0)
663 g.drawString(row.annotations[column].displayCharacter,
664 (x * av.charWidth) + charOffset,
668 row.annotations[column - 1] == null
669 || (!row.annotations[column].displayCharacter.equals(
670 row.annotations[column - 1].displayCharacter)
672 (row.annotations[column].displayCharacter.length() < 2 &&
673 row.annotations[column].secondaryStructure == ' ')))
675 g.drawString(row.annotations[column].displayCharacter,
676 (x * av.charWidth) + charOffset,
684 (row.annotations[column].secondaryStructure != lastSS))
689 g.setColor(HELIX_COLOUR);
692 //Off by 1 offset when drawing rects and ovals
693 //to offscreen image on the MAC
694 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
695 (x * av.charWidth) - lastSSX, 7, 8, 8);
699 int sCol = (lastSSX / av.charWidth) + startRes;
701 int x2 = (x * av.charWidth);
704 row.annotations[sCol - 1] == null ||
705 row.annotations[sCol - 1].secondaryStructure != 'H')
707 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
709 x1 += av.charWidth / 2;
712 if (row.annotations[column] == null ||
713 row.annotations[column].secondaryStructure != 'H')
715 g.fillArc( (x * av.charWidth) - av.charWidth,
716 y + 4 + iconOffset, av.charWidth, 8, 270, 180);
717 x2 -= av.charWidth / 2;
720 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
724 g.setColor(SHEET_COLOUR);
725 g.fillRect(lastSSX, y + 4 + iconOffset,
726 (x * av.charWidth) - lastSSX - 4, 7);
727 g.fillPolygon(new int[]
728 { (x * av.charWidth) - 4,
729 (x * av.charWidth) - 4,
733 y + iconOffset, y + 14 + iconOffset,
740 g.setColor(Color.gray);
741 g.fillRect(lastSSX, y + 6 + iconOffset,
742 (x * av.charWidth) - lastSSX, 2);
749 lastSS = row.annotations[column].secondaryStructure;
756 lastSSX = (x * av.charWidth);
764 if (column >= row.annotations.length)
766 column = row.annotations.length - 1;
779 g.setColor(HELIX_COLOUR);
782 //Off by 1 offset when drawing rects and ovals
783 //to offscreen image on the MAC
784 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
785 (x * av.charWidth) - lastSSX, 7, 8, 8);
789 int sCol = (lastSSX / av.charWidth) + startRes;
791 int x2 = (x * av.charWidth);
794 row.annotations[sCol - 1] == null ||
795 row.annotations[sCol - 1].secondaryStructure != 'H')
797 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90, 180);
798 x1 += av.charWidth / 2;
801 if (row.annotations[column] == null ||
802 row.annotations[column].secondaryStructure != 'H')
804 g.fillArc( (x * av.charWidth) - av.charWidth,
805 y + 4 + iconOffset, av.charWidth, 8, 270,
807 x2 -= av.charWidth / 2;
810 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
815 g.setColor(SHEET_COLOUR);
817 if (!validEnd || row.annotations[endRes] == null
818 || row.annotations[endRes].secondaryStructure != 'E')
820 g.fillRect(lastSSX, y + 4 + iconOffset,
821 (x * av.charWidth) - lastSSX - 4, 7);
822 g.fillPolygon(new int[]
823 { (x * av.charWidth) - 4,
824 (x * av.charWidth) - 4,
828 y + iconOffset, y + 14 + iconOffset,
834 g.fillRect(lastSSX, y + 4 + iconOffset,
835 x * av.charWidth - lastSSX, 7);
840 g.setColor(Color.gray);
841 if (!av.wrapAlignment || endRes == av.endRes)
843 g.fillRect(lastSSX, y + 6 + iconOffset,
844 (x * av.charWidth) - lastSSX, 2);
853 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
855 if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
857 float groupmax = -999999, groupmin = 9999999;
858 for (int gg = 0; gg < aa.length; gg++)
860 if (aa[gg].graphGroup != row.graphGroup)
867 aa[gg].visible = false;
870 if (aa[gg].graphMax > groupmax)
872 groupmax = aa[gg].graphMax;
874 if (aa[gg].graphMin < groupmin)
876 groupmin = aa[gg].graphMin;
880 for (int gg = 0; gg < aa.length; gg++)
882 if (aa[gg].graphGroup == row.graphGroup)
884 drawLineGraph(g, aa[gg], startRes, endRes, y,
890 graphGroupDrawn[row.graphGroup] = true;
894 drawLineGraph(g, row, startRes, endRes,
895 y, row.graphMin, row.graphMax, row.graphHeight);
898 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
900 drawBarGraph(g, row, startRes, endRes,
901 row.graphMin, row.graphMax, y);
905 if (row.graph > 0 && row.hasText)
917 public void drawLineGraph(Graphics g, AlignmentAnnotation aa,
920 float min, float max,
923 if (sRes > aa.annotations.length)
930 //Adjustment for fastpaint to left
931 if (eRes < av.endRes)
936 eRes = Math.min(eRes, aa.annotations.length);
939 float range = max - min;
944 y2 = y - (int) ( (0 - min / range) * graphHeight);
947 g.setColor(Color.gray);
948 g.drawLine(x - av.charWidth, y2, (eRes - sRes) * av.charWidth, y2);
950 eRes = Math.min(eRes, aa.annotations.length);
953 int aaMax = aa.annotations.length - 1;
955 while (x < eRes - sRes)
958 if (av.hasHiddenColumns)
960 column = av.getColumnSelection().adjustForHiddenColumns(column);
968 if (aa.annotations[column] == null) // || coaa.annotations[column - 1] == null)
974 if (aa.annotations[column].colour == null)
975 g.setColor(Color.black);
977 g.setColor(aa.annotations[column].colour);
978 if (column==0 || aa.annotations[column-1]==null)
981 (int) ( ( (aa.annotations[column].value - min) / range) * graphHeight);
984 (int) ( ( (aa.annotations[column-1].value - min) / range) * graphHeight);
987 (int) ( ( (aa.annotations[column].value - min) / range) * graphHeight);
989 g.drawLine(x * av.charWidth - av.charWidth / 2, y1,
990 x * av.charWidth + av.charWidth / 2, y2);
994 if (aa.threshold != null)
996 g.setColor(aa.threshold.colour);
998 y2 = (int) (y - ( (aa.threshold.value - min) / range) * graphHeight);
999 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1003 public void drawBarGraph(Graphics g, AlignmentAnnotation aa,
1005 float min, float max,
1008 if (sRes > aa.annotations.length)
1013 eRes = Math.min(eRes, aa.annotations.length);
1015 int x = 0, y1 = y, y2 = y;
1017 float range = max - min;
1021 y2 = y - (int) ( (0 - min / (range)) * aa.graphHeight);
1024 g.setColor(Color.gray);
1026 g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
1029 int aaMax = aa.annotations.length - 1;
1031 while (x < eRes - sRes)
1034 if (av.hasHiddenColumns)
1036 column = av.getColumnSelection().adjustForHiddenColumns(column);
1044 if (aa.annotations[column] == null)
1050 if (aa.annotations[column].colour == null)
1051 g.setColor(Color.black);
1053 g.setColor(aa.annotations[column].colour);
1056 (int) ( ( (aa.annotations[column].value - min) / (range)) * aa.graphHeight);
1060 g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
1064 g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
1070 if (aa.threshold != null)
1072 g.setColor(aa.threshold.colour);
1073 y2 = (int) (y - ( (aa.threshold.value - min) / range) * aa.graphHeight);
1074 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1078 // used by overview window
1079 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y,
1082 eRes = Math.min(eRes, aa.annotations.length);
1083 g.setColor(Color.white);
1084 g.fillRect(0, 0, width, y);
1085 g.setColor(new Color(0, 0, 180));
1089 for (int j = sRes; j < eRes; j++)
1091 if (aa.annotations[j].colour == null)
1092 g.setColor(Color.black);
1094 g.setColor(aa.annotations[j].colour);
1096 height = (int) ( (aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
1101 g.fillRect(x, y - height, av.charWidth, height);