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;
53 public static int GRAPH_HEIGHT = 40;
57 public AnnotationPanel(AlignmentPanel ap)
59 MAC = new jalview.util.Platform().isAMac();
66 addMouseMotionListener(this);
68 addMouseListener(this);
71 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
74 public AnnotationPanel(AlignViewport av)
79 public void adjustmentValueChanged(AdjustmentEvent evt)
81 ap.alabels.setScrollOffset( -evt.getValue());
87 * @param evt DOCUMENT ME!
89 public void actionPerformed(ActionEvent evt)
91 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
92 Annotation[] anot = aa[activeRow].annotations;
94 if (anot.length < av.getColumnSelection().getMax())
96 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
97 System.arraycopy(anot, 0, temp, 0, anot.length);
99 aa[activeRow].annotations = anot;
103 if (av.colSel != null && av.colSel.size() > 0
104 && anot[av.colSel.getMin()] != null)
105 label = anot[av.getColumnSelection().getMin()].displayCharacter;
108 if (evt.getActionCommand().equals(REMOVE))
110 for (int i = 0; i < av.getColumnSelection().size(); i++)
112 anot[av.getColumnSelection().columnAt(i)] = null;
115 else if (evt.getActionCommand().equals(LABEL))
117 label = enterLabel(label, "Enter Label");
124 if ( (label.length() > 0) && !aa[activeRow].hasText)
126 aa[activeRow].hasText = true;
129 for (int i = 0; i < av.getColumnSelection().size(); i++)
131 int index = av.getColumnSelection().columnAt(i);
133 if(!av.colSel.isVisible(index))
136 if (anot[index] == null)
138 anot[index] = new Annotation(label, "", ' ', 0);
141 anot[index].displayCharacter = label;
144 else if (evt.getActionCommand().equals(COLOUR))
146 UserDefinedColours udc = new UserDefinedColours(
148 Color.black, ap.alignFrame);
150 Color col = udc.getColor();
152 for (int i = 0; i < av.getColumnSelection().size(); i++)
154 int index = av.getColumnSelection().columnAt(i);
156 if(!av.colSel.isVisible(index))
159 if (anot[index] == null)
161 anot[index] = new Annotation("", "", ' ', 0);
164 anot[index].colour = col;
167 else // HELIX OR SHEET
170 String symbol = "\u03B1";
172 if (evt.getActionCommand().equals(HELIX))
176 else if (evt.getActionCommand().equals(SHEET))
182 if (!aa[activeRow].hasIcons)
184 aa[activeRow].hasIcons = true;
187 label = enterLabel(symbol, "Enter Label");
194 if ( (label.length() > 0) && !aa[activeRow].hasText)
196 aa[activeRow].hasText = true;
199 for (int i = 0; i < av.getColumnSelection().size(); i++)
201 int index = av.getColumnSelection().columnAt(i);
203 if(!av.colSel.isVisible(index))
206 if (anot[index] == null)
208 anot[index] = new Annotation(label, "", type, 0);
211 anot[index].secondaryStructure = type;
212 anot[index].displayCharacter = label;
222 String enterLabel(String text, String label)
224 EditNameDialog dialog = new EditNameDialog(text,null,label,null,
225 ap.alignFrame,"Enter Label", 400,200, true);
228 return dialog.getName();
233 public void mousePressed(MouseEvent evt)
235 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
245 for (int i = 0; i < aa.length; i++)
249 height += aa[i].height;
252 if (evt.getY() < height)
263 if ( (evt.getModifiers() & InputEvent.BUTTON3_MASK) ==
264 InputEvent.BUTTON3_MASK && activeRow != -1)
266 if (av.getColumnSelection() == null)
271 PopupMenu pop = new PopupMenu("Structure type");
272 MenuItem item = new MenuItem(HELIX);
273 item.addActionListener(this);
275 item = new MenuItem(SHEET);
276 item.addActionListener(this);
278 item = new MenuItem(LABEL);
279 item.addActionListener(this);
281 item = new MenuItem(COLOUR);
282 item.addActionListener(this);
284 item = new MenuItem(REMOVE);
285 item.addActionListener(this);
287 ap.alignFrame.add(pop);
288 pop.show(this, evt.getX(), evt.getY());
298 ap.scalePanel.mousePressed(evt);
301 public void mouseReleased(MouseEvent evt)
303 ap.scalePanel.mouseReleased(evt);
306 public void mouseClicked(MouseEvent evt)
309 public void mouseDragged(MouseEvent evt)
311 ap.scalePanel.mouseDragged(evt);
314 public void mouseMoved(MouseEvent evt)
316 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
324 for (int i = 0; i < aa.length; i++)
329 height += aa[i].height;
332 if (evt.getY() < height)
339 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
341 if (av.hasHiddenColumns)
343 res = av.getColumnSelection().adjustForHiddenColumns(res);
346 if (row > -1 && res < aa[row].annotations.length && aa[row].annotations[res] != null)
348 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
349 if (aa[row].annotations[res].description != null)
351 text.append(" " + aa[row].annotations[res].description);
353 ap.alignFrame.statusBar.setText(text.toString());
356 public void mouseEntered(MouseEvent evt)
358 ap.scalePanel.mouseEntered(evt);
360 public void mouseExited(MouseEvent evt)
362 ap.scalePanel.mouseExited(evt);
366 public int adjustPanelHeight()
368 // setHeight of panels
369 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
374 for (int i = 0; i < aa.length; i++)
385 aa[i].height += av.charHeight;
395 aa[i].height += aa[i].graphHeight;
398 if (aa[i].height == 0)
403 height += aa[i].height;
411 this.setSize(getSize().width, height);
419 public void addEditableColumn(int i)
423 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
429 for (int j = 0; j < aa.length; j++)
439 if (activeRes == null)
441 activeRes = new Vector();
442 activeRes.addElement(String.valueOf(i));
446 activeRes.addElement(String.valueOf(i));
450 public void update(Graphics g)
455 public void paint(Graphics g)
458 imgWidth = getSize().width;
459 //(av.endRes - av.startRes + 1) * av.charWidth;
461 if (image == null || imgWidth != image.getWidth(this))
463 image = createImage(imgWidth, ap.annotationPanel.getSize().height);
464 gg = image.getGraphics();
465 gg.setFont(av.getFont());
466 fm = gg.getFontMetrics();
472 g.drawImage(image, 0, 0, this);
477 gg.setColor(Color.white);
478 gg.fillRect(0, 0, getSize().width, getSize().height);
479 drawComponent(gg, av.startRes, av.endRes + 1);
481 g.drawImage(image, 0, 0, this);
484 public void fastPaint(int horizontal)
487 || av.alignment.getAlignmentAnnotation() == null
488 || av.alignment.getAlignmentAnnotation().length < 1
495 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal * av.charWidth, 0);
496 int sr = av.startRes, er = av.endRes + 1, transX = 0;
498 if (horizontal > 0) // scrollbar pulled right, image to the left
500 transX = (er - sr - horizontal) * av.charWidth;
501 sr = er - horizontal;
503 else if (horizontal < 0)
505 er = sr - horizontal;
508 gg.translate(transX, 0);
510 drawComponent(gg, sr, er);
512 gg.translate( -transX, 0);
521 * @param g DOCUMENT ME!
522 * @param startRes DOCUMENT ME!
523 * @param endRes DOCUMENT ME!
525 public void drawComponent(Graphics g, int startRes, int endRes)
527 g.setFont(av.getFont());
529 g.setColor(Color.white);
530 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
534 fm = g.getFontMetrics();
537 if ( (av.alignment.getAlignmentAnnotation() == null) ||
538 (av.alignment.getAlignmentAnnotation().length < 1))
540 g.setColor(Color.white);
541 g.fillRect(0, 0, getSize().width, getSize().height);
542 g.setColor(Color.black);
543 if (av.validCharWidth)
545 g.drawString("Alignment has no annotations", 20, 15);
551 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
558 int iconOffset = av.charHeight / 2;
559 boolean validRes = false;
561 boolean[] graphGroupDrawn = new boolean[aa.length];
564 for (int i = 0; i < aa.length; i++)
566 AlignmentAnnotation row = aa[i];
578 if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
583 // this is so that we draw the characters below the graph
594 iconOffset = av.charHeight / 2;
602 while (x < endRes - startRes)
604 if (av.hasHiddenColumns)
606 column = av.getColumnSelection().adjustForHiddenColumns(startRes + x);
607 if (column > row.annotations.length - 1)
614 column = startRes + x;
617 if ( (row.annotations.length <= column) ||
618 (row.annotations[column] == null))
629 g.setColor(Color.red);
631 if (av.getColumnSelection() != null)
633 for (int n = 0; n < av.getColumnSelection().size(); n++)
635 int v = av.getColumnSelection().columnAt(n);
639 g.fillRect(x * av.charWidth, y,
640 av.charWidth, av.charHeight);
648 if (av.validCharWidth && validRes &&
649 (row.annotations[column].displayCharacter!=null && row.annotations[column].displayCharacter.length() > 0))
651 int charOffset = (av.charWidth -
652 fm.charWidth(row.annotations[column].
653 displayCharacter.charAt(
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;
776 g.setColor(HELIX_COLOUR);
779 //Off by 1 offset when drawing rects and ovals
780 //to offscreen image on the MAC
781 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
782 (x * av.charWidth) - lastSSX, 7, 8, 8);
786 int sCol = (lastSSX / av.charWidth) + startRes;
788 int x2 = (x * av.charWidth);
791 row.annotations[sCol - 1] == null ||
792 row.annotations[sCol - 1].secondaryStructure != 'H')
794 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90, 180);
795 x1 += av.charWidth / 2;
798 if (row.annotations[column] == null ||
799 row.annotations[column].secondaryStructure != 'H')
801 g.fillArc( (x * av.charWidth) - av.charWidth,
802 y + 4 + iconOffset, av.charWidth, 8, 270,
804 x2 -= av.charWidth / 2;
807 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
812 g.setColor(SHEET_COLOUR);
814 if (row.annotations[endRes] == null
815 || row.annotations[endRes].secondaryStructure != 'E')
817 g.fillRect(lastSSX, y + 4 + iconOffset,
818 (x * av.charWidth) - lastSSX - 4, 7);
819 g.fillPolygon(new int[]
820 { (x * av.charWidth) - 4,
821 (x * av.charWidth) - 4,
825 y + iconOffset, y + 14 + iconOffset,
831 g.fillRect(lastSSX, y + 4 + iconOffset,
832 x * av.charWidth - lastSSX, 7);
837 g.setColor(Color.gray);
838 if (!av.wrapAlignment || endRes == av.endRes)
840 g.fillRect(lastSSX, y + 6 + iconOffset,
841 (x * av.charWidth) - lastSSX, 2);
850 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
852 if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
854 float groupmax = -999999, groupmin = 9999999;
855 for (int gg = 0; gg < aa.length; gg++)
857 if (aa[gg].graphGroup != row.graphGroup)
864 aa[gg].visible = false;
867 if (aa[gg].graphMax > groupmax)
869 groupmax = aa[gg].graphMax;
871 if (aa[gg].graphMin < groupmin)
873 groupmin = aa[gg].graphMin;
877 for (int gg = 0; gg < aa.length; gg++)
879 if (aa[gg].graphGroup == row.graphGroup)
881 drawLineGraph(g, aa[gg], startRes, endRes, y,
887 graphGroupDrawn[row.graphGroup] = true;
891 drawLineGraph(g, row, startRes, endRes,
892 y, row.graphMin, row.graphMax, row.graphHeight);
895 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
897 drawBarGraph(g, row, startRes, endRes,
898 row.graphMin, row.graphMax, y);
902 if (row.graph > 0 && row.hasText)
914 public void drawLineGraph(Graphics g, AlignmentAnnotation aa,
917 float min, float max,
920 if (sRes > aa.annotations.length)
927 //Adjustment for fastpaint to left
928 if (eRes < av.endRes)
933 eRes = Math.min(eRes, aa.annotations.length);
936 float range = max - min;
941 y2 = y - (int) ( (0 - min / range) * graphHeight);
944 g.setColor(Color.gray);
945 g.drawLine(x - av.charWidth, y2, (eRes - sRes) * av.charWidth, y2);
947 eRes = Math.min(eRes, aa.annotations.length);
950 int aaMax = aa.annotations.length - 1;
952 while (x < eRes - sRes)
955 if (av.hasHiddenColumns)
957 column = av.getColumnSelection().adjustForHiddenColumns(column);
965 if (aa.annotations[column] == null) // || coaa.annotations[column - 1] == null)
971 if (aa.annotations[column].colour == null)
972 g.setColor(Color.black);
974 g.setColor(aa.annotations[column].colour);
975 if (column==0 || aa.annotations[column-1]==null)
978 (int) ( ( (aa.annotations[column].value - min) / range) * graphHeight);
981 (int) ( ( (aa.annotations[column-1].value - min) / range) * graphHeight);
984 (int) ( ( (aa.annotations[column].value - min) / range) * graphHeight);
986 g.drawLine(x * av.charWidth - av.charWidth / 2, y1,
987 x * av.charWidth + av.charWidth / 2, y2);
991 if (aa.threshold != null)
993 g.setColor(aa.threshold.colour);
995 y2 = (int) (y - ( (aa.threshold.value - min) / range) * graphHeight);
996 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1000 public void drawBarGraph(Graphics g, AlignmentAnnotation aa,
1002 float min, float max,
1005 if (sRes > aa.annotations.length)
1010 eRes = Math.min(eRes, aa.annotations.length);
1012 int x = 0, y1 = y, y2 = y;
1014 float range = max - min;
1018 y2 = y - (int) ( (0 - min / (range)) * aa.graphHeight);
1021 g.setColor(Color.gray);
1023 g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
1026 int aaMax = aa.annotations.length - 1;
1028 while (x < eRes - sRes)
1031 if (av.hasHiddenColumns)
1033 column = av.getColumnSelection().adjustForHiddenColumns(column);
1041 if (aa.annotations[column] == null)
1047 if (aa.annotations[column].colour == null)
1048 g.setColor(Color.black);
1050 g.setColor(aa.annotations[column].colour);
1053 (int) ( ( (aa.annotations[column].value - min) / (range)) * aa.graphHeight);
1057 g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
1061 g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
1067 if (aa.threshold != null)
1069 g.setColor(aa.threshold.colour);
1070 y2 = (int) (y - ( (aa.threshold.value - min) / range) * aa.graphHeight);
1071 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1075 // used by overview window
1076 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y,
1079 eRes = Math.min(eRes, aa.annotations.length);
1080 g.setColor(Color.white);
1081 g.fillRect(0, 0, width, y);
1082 g.setColor(new Color(0, 0, 180));
1086 for (int j = sRes; j < eRes; j++)
1088 if (aa.annotations[j].colour == null)
1089 g.setColor(Color.black);
1091 g.setColor(aa.annotations[j].colour);
1093 height = (int) ( (aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
1098 g.fillRect(x, y - height, av.charWidth, height);