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 extends Panel implements AdjustmentListener,
29 ActionListener, MouseListener, MouseMotionListener
39 static String HELIX = "Helix";
41 static String SHEET = "Sheet";
43 static String LABEL = "Label";
45 static String REMOVE = "Remove Annotation";
47 static String COLOUR = "Colour";
49 static Color HELIX_COLOUR = Color.red.darker();
51 static Color SHEET_COLOUR = Color.green.darker().darker();
61 boolean fastPaint = false;
63 public static int GRAPH_HEIGHT = 40;
67 public AnnotationPanel(AlignmentPanel ap)
69 MAC = new jalview.util.Platform().isAMac();
75 addMouseMotionListener(this);
77 addMouseListener(this);
79 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
82 public AnnotationPanel(AlignViewport av)
87 public void adjustmentValueChanged(AdjustmentEvent evt)
89 ap.alabels.setScrollOffset(-evt.getValue());
98 public void actionPerformed(ActionEvent evt)
100 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
101 Annotation[] anot = aa[activeRow].annotations;
103 if (anot.length < av.getColumnSelection().getMax())
105 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
106 System.arraycopy(anot, 0, temp, 0, anot.length);
108 aa[activeRow].annotations = anot;
112 if (av.colSel != null && av.colSel.size() > 0
113 && anot[av.colSel.getMin()] != null)
114 label = anot[av.getColumnSelection().getMin()].displayCharacter;
116 if (evt.getActionCommand().equals(REMOVE))
118 for (int i = 0; i < av.getColumnSelection().size(); i++)
120 anot[av.getColumnSelection().columnAt(i)] = null;
123 else if (evt.getActionCommand().equals(LABEL))
125 label = enterLabel(label, "Enter Label");
132 if ((label.length() > 0) && !aa[activeRow].hasText)
134 aa[activeRow].hasText = true;
137 for (int i = 0; i < av.getColumnSelection().size(); i++)
139 int index = av.getColumnSelection().columnAt(i);
141 if (!av.colSel.isVisible(index))
144 if (anot[index] == null)
146 anot[index] = new Annotation(label, "", ' ', 0);
149 anot[index].displayCharacter = label;
152 else if (evt.getActionCommand().equals(COLOUR))
154 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
157 Color col = udc.getColor();
159 for (int i = 0; i < av.getColumnSelection().size(); i++)
161 int index = av.getColumnSelection().columnAt(i);
163 if (!av.colSel.isVisible(index))
166 if (anot[index] == null)
168 anot[index] = new Annotation("", "", ' ', 0);
171 anot[index].colour = col;
178 String symbol = "\u03B1";
180 if (evt.getActionCommand().equals(HELIX))
184 else if (evt.getActionCommand().equals(SHEET))
190 if (!aa[activeRow].hasIcons)
192 aa[activeRow].hasIcons = true;
195 label = enterLabel(symbol, "Enter Label");
202 if ((label.length() > 0) && !aa[activeRow].hasText)
204 aa[activeRow].hasText = true;
207 for (int i = 0; i < av.getColumnSelection().size(); i++)
209 int index = av.getColumnSelection().columnAt(i);
211 if (!av.colSel.isVisible(index))
214 if (anot[index] == null)
216 anot[index] = new Annotation(label, "", type, 0);
219 anot[index].secondaryStructure = type;
220 anot[index].displayCharacter = label;
230 String enterLabel(String text, String label)
232 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
233 ap.alignFrame, "Enter Label", 400, 200, true);
236 return dialog.getName();
241 public void mousePressed(MouseEvent evt)
243 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
252 for (int i = 0; i < aa.length; i++)
256 height += aa[i].height;
259 if (evt.getY() < height)
270 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
273 if (av.getColumnSelection() == null)
278 PopupMenu pop = new PopupMenu("Structure type");
279 MenuItem item = new MenuItem(HELIX);
280 item.addActionListener(this);
282 item = new MenuItem(SHEET);
283 item.addActionListener(this);
285 item = new MenuItem(LABEL);
286 item.addActionListener(this);
288 item = new MenuItem(COLOUR);
289 item.addActionListener(this);
291 item = new MenuItem(REMOVE);
292 item.addActionListener(this);
294 ap.alignFrame.add(pop);
295 pop.show(this, evt.getX(), evt.getY());
305 ap.scalePanel.mousePressed(evt);
308 public void mouseReleased(MouseEvent evt)
310 ap.scalePanel.mouseReleased(evt);
313 public void mouseClicked(MouseEvent evt)
317 public void mouseDragged(MouseEvent evt)
319 ap.scalePanel.mouseDragged(evt);
322 public void mouseMoved(MouseEvent evt)
324 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
332 for (int i = 0; i < aa.length; i++)
337 height += aa[i].height;
340 if (evt.getY() < height)
347 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
349 if (av.hasHiddenColumns)
351 res = av.getColumnSelection().adjustForHiddenColumns(res);
354 if (row > -1 && res < aa[row].annotations.length
355 && aa[row].annotations[res] != null)
357 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
358 if (aa[row].annotations[res].description != null)
360 text.append(" " + aa[row].annotations[res].description);
362 ap.alignFrame.statusBar.setText(text.toString());
366 public void mouseEntered(MouseEvent evt)
368 ap.scalePanel.mouseEntered(evt);
371 public void mouseExited(MouseEvent evt)
373 ap.scalePanel.mouseExited(evt);
376 public int adjustPanelHeight()
378 // setHeight of panels
379 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
384 for (int i = 0; i < aa.length; i++)
395 aa[i].height += av.charHeight;
405 aa[i].height += aa[i].graphHeight;
408 if (aa[i].height == 0)
413 height += aa[i].height;
421 this.setSize(getSize().width, height);
429 public void addEditableColumn(int i)
433 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
439 for (int j = 0; j < aa.length; j++)
449 if (activeRes == null)
451 activeRes = new Vector();
452 activeRes.addElement(String.valueOf(i));
456 activeRes.addElement(String.valueOf(i));
459 public void update(Graphics g)
464 public void paint(Graphics g)
467 imgWidth = getSize().width;
468 // (av.endRes - av.startRes + 1) * av.charWidth;
470 if (image == null || imgWidth != image.getWidth(this))
472 image = createImage(imgWidth, ap.annotationPanel.getSize().height);
473 gg = image.getGraphics();
474 gg.setFont(av.getFont());
475 fm = gg.getFontMetrics();
481 g.drawImage(image, 0, 0, this);
486 gg.setColor(Color.white);
487 gg.fillRect(0, 0, getSize().width, getSize().height);
488 drawComponent(gg, av.startRes, av.endRes + 1);
490 g.drawImage(image, 0, 0, this);
493 public void fastPaint(int horizontal)
495 if (horizontal == 0 || av.alignment.getAlignmentAnnotation() == null
496 || av.alignment.getAlignmentAnnotation().length < 1)
502 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
504 int sr = av.startRes, er = av.endRes + 1, transX = 0;
506 if (horizontal > 0) // scrollbar pulled right, image to the left
508 transX = (er - sr - horizontal) * av.charWidth;
509 sr = er - horizontal;
511 else if (horizontal < 0)
513 er = sr - horizontal;
516 gg.translate(transX, 0);
518 drawComponent(gg, sr, er);
520 gg.translate(-transX, 0);
536 public void drawComponent(Graphics g, int startRes, int endRes)
538 g.setFont(av.getFont());
540 g.setColor(Color.white);
541 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
545 fm = g.getFontMetrics();
548 if ((av.alignment.getAlignmentAnnotation() == null)
549 || (av.alignment.getAlignmentAnnotation().length < 1))
551 g.setColor(Color.white);
552 g.fillRect(0, 0, getSize().width, getSize().height);
553 g.setColor(Color.black);
554 if (av.validCharWidth)
556 g.drawString("Alignment has no annotations", 20, 15);
562 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
569 int iconOffset = av.charHeight / 2;
570 boolean validRes = false;
571 boolean validEnd = false;
572 boolean[] graphGroupDrawn = new boolean[aa.length];
575 for (int i = 0; i < aa.length; i++)
577 AlignmentAnnotation row = aa[i];
589 if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
594 // this is so that we draw the characters below the graph
605 iconOffset = av.charHeight / 2;
613 while (x < endRes - startRes)
615 if (av.hasHiddenColumns)
617 column = av.getColumnSelection().adjustForHiddenColumns(
619 if (column > row.annotations.length - 1)
626 column = startRes + x;
629 if ((row.annotations.length <= column)
630 || (row.annotations[column] == null))
641 g.setColor(Color.red);
643 if (av.getColumnSelection() != null)
645 for (int n = 0; n < av.getColumnSelection().size(); n++)
647 int v = av.getColumnSelection().columnAt(n);
652 .fillRect(x * av.charWidth, y, av.charWidth,
659 if (av.validCharWidth
661 && (row.annotations[column].displayCharacter != null && row.annotations[column].displayCharacter
664 int charOffset = (av.getCentreColumnLabels()) ? ((av.charWidth - fm
665 .charsWidth(row.annotations[column].displayCharacter
667 row.annotations[column].displayCharacter.length())) / 2)
669 .charWidth(row.annotations[column].displayCharacter
672 if (row.annotations[column].colour == null)
673 g.setColor(Color.black);
675 g.setColor(row.annotations[column].colour);
677 if (column == 0 || row.graph > 0)
679 g.drawString(row.annotations[column].displayCharacter,
680 (x * av.charWidth) + charOffset, y + iconOffset + 3);
682 else if (row.annotations[column - 1] == null
683 || (!row.annotations[column].displayCharacter
684 .equals(row.annotations[column - 1].displayCharacter) || (row.annotations[column].displayCharacter
685 .length() < 2 && row.annotations[column].secondaryStructure == ' ')))
687 g.drawString(row.annotations[column].displayCharacter,
688 (x * av.charWidth) + charOffset, y + iconOffset + 3);
695 || (row.annotations[column].secondaryStructure != lastSS))
700 g.setColor(HELIX_COLOUR);
703 // Off by 1 offset when drawing rects and ovals
704 // to offscreen image on the MAC
705 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
706 (x * av.charWidth) - lastSSX, 7, 8, 8);
710 int sCol = (lastSSX / av.charWidth) + startRes;
712 int x2 = (x * av.charWidth);
715 || row.annotations[sCol - 1] == null
716 || row.annotations[sCol - 1].secondaryStructure != 'H')
718 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
720 x1 += av.charWidth / 2;
723 if (row.annotations[column] == null
724 || row.annotations[column].secondaryStructure != 'H')
726 g.fillArc((x * av.charWidth) - av.charWidth, y + 4
727 + iconOffset, av.charWidth, 8, 270, 180);
728 x2 -= av.charWidth / 2;
731 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
735 g.setColor(SHEET_COLOUR);
736 g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
738 g.fillPolygon(new int[]
739 { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
740 (x * av.charWidth) }, new int[]
741 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
747 g.setColor(Color.gray);
748 g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
756 lastSS = row.annotations[column].secondaryStructure;
763 lastSSX = (x * av.charWidth);
771 if (column >= row.annotations.length)
773 column = row.annotations.length - 1;
788 g.setColor(HELIX_COLOUR);
791 // Off by 1 offset when drawing rects and ovals
792 // to offscreen image on the MAC
793 g.fillRoundRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
798 int sCol = (lastSSX / av.charWidth) + startRes;
800 int x2 = (x * av.charWidth);
802 if (sCol == 0 || row.annotations[sCol - 1] == null
803 || row.annotations[sCol - 1].secondaryStructure != 'H')
806 .fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8,
808 x1 += av.charWidth / 2;
811 if (row.annotations[column] == null
812 || row.annotations[column].secondaryStructure != 'H')
814 g.fillArc((x * av.charWidth) - av.charWidth,
815 y + 4 + iconOffset, av.charWidth, 8, 270, 180);
816 x2 -= av.charWidth / 2;
819 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
824 g.setColor(SHEET_COLOUR);
826 if (!validEnd || row.annotations[endRes] == null
827 || row.annotations[endRes].secondaryStructure != 'E')
829 g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
831 g.fillPolygon(new int[]
832 { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
833 (x * av.charWidth) }, new int[]
834 { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset }, 3);
838 g.fillRect(lastSSX, y + 4 + iconOffset, x * av.charWidth
844 g.setColor(Color.gray);
845 if (!av.wrapAlignment || endRes == av.endRes)
847 g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
857 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
859 if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
861 float groupmax = -999999, groupmin = 9999999;
862 for (int gg = 0; gg < aa.length; gg++)
864 if (aa[gg].graphGroup != row.graphGroup)
871 aa[gg].visible = false;
874 if (aa[gg].graphMax > groupmax)
876 groupmax = aa[gg].graphMax;
878 if (aa[gg].graphMin < groupmin)
880 groupmin = aa[gg].graphMin;
884 for (int gg = 0; gg < aa.length; gg++)
886 if (aa[gg].graphGroup == row.graphGroup)
888 drawLineGraph(g, aa[gg], startRes, endRes, y, groupmin,
889 groupmax, row.graphHeight);
893 graphGroupDrawn[row.graphGroup] = true;
897 drawLineGraph(g, row, startRes, endRes, y, row.graphMin,
898 row.graphMax, row.graphHeight);
901 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
903 drawBarGraph(g, row, startRes, endRes, row.graphMin,
908 if (row.graph > 0 && row.hasText)
920 public void drawLineGraph(Graphics g, AlignmentAnnotation aa, int sRes,
921 int eRes, int y, float min, float max, int graphHeight)
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] ==
975 if (aa.annotations[column].colour == null)
976 g.setColor(Color.black);
978 g.setColor(aa.annotations[column].colour);
979 if (column == 0 || aa.annotations[column - 1] == null)
982 - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
987 - (int) (((aa.annotations[column - 1].value - min) / range) * graphHeight);
990 - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
992 g.drawLine(x * av.charWidth - av.charWidth / 2, y1, x * av.charWidth
993 + av.charWidth / 2, y2);
997 if (aa.threshold != null)
999 g.setColor(aa.threshold.colour);
1001 y2 = (int) (y - ((aa.threshold.value - min) / range) * graphHeight);
1002 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1006 public void drawBarGraph(Graphics g, AlignmentAnnotation aa, int sRes,
1007 int eRes, float min, float max, int y)
1009 if (sRes > aa.annotations.length)
1014 eRes = Math.min(eRes, aa.annotations.length);
1016 int x = 0, y1 = y, y2 = y;
1018 float range = max - min;
1022 y2 = y - (int) ((0 - min / (range)) * aa.graphHeight);
1025 g.setColor(Color.gray);
1027 g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
1030 int aaMax = aa.annotations.length - 1;
1032 while (x < eRes - sRes)
1035 if (av.hasHiddenColumns)
1037 column = av.getColumnSelection().adjustForHiddenColumns(column);
1045 if (aa.annotations[column] == null)
1051 if (aa.annotations[column].colour == null)
1052 g.setColor(Color.black);
1054 g.setColor(aa.annotations[column].colour);
1057 - (int) (((aa.annotations[column].value - min) / (range)) * aa.graphHeight);
1061 g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
1065 g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
1071 if (aa.threshold != null)
1073 g.setColor(aa.threshold.colour);
1074 y2 = (int) (y - ((aa.threshold.value - min) / range) * aa.graphHeight);
1075 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1079 // used by overview window
1080 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width,
1081 int y, int sRes, int eRes)
1083 eRes = Math.min(eRes, aa.annotations.length);
1084 g.setColor(Color.white);
1085 g.fillRect(0, 0, width, y);
1086 g.setColor(new Color(0, 0, 180));
1090 for (int j = sRes; j < eRes; j++)
1092 if (aa.annotations[j].colour == null)
1093 g.setColor(Color.black);
1095 g.setColor(aa.annotations[j].colour);
1097 height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
1102 g.fillRect(x, y - height, av.charWidth, height);