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;
558 boolean validEnd = 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;
780 g.setColor(HELIX_COLOUR);
783 //Off by 1 offset when drawing rects and ovals
784 //to offscreen image on the MAC
785 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
786 (x * av.charWidth) - lastSSX, 7, 8, 8);
790 int sCol = (lastSSX / av.charWidth) + startRes;
792 int x2 = (x * av.charWidth);
795 row.annotations[sCol - 1] == null ||
796 row.annotations[sCol - 1].secondaryStructure != 'H')
798 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90, 180);
799 x1 += av.charWidth / 2;
802 if (row.annotations[column] == null ||
803 row.annotations[column].secondaryStructure != 'H')
805 g.fillArc( (x * av.charWidth) - av.charWidth,
806 y + 4 + iconOffset, av.charWidth, 8, 270,
808 x2 -= av.charWidth / 2;
811 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
816 g.setColor(SHEET_COLOUR);
818 if (!validEnd || row.annotations[endRes] == null
819 || row.annotations[endRes].secondaryStructure != 'E')
821 g.fillRect(lastSSX, y + 4 + iconOffset,
822 (x * av.charWidth) - lastSSX - 4, 7);
823 g.fillPolygon(new int[]
824 { (x * av.charWidth) - 4,
825 (x * av.charWidth) - 4,
829 y + iconOffset, y + 14 + iconOffset,
835 g.fillRect(lastSSX, y + 4 + iconOffset,
836 x * av.charWidth - lastSSX, 7);
841 g.setColor(Color.gray);
842 if (!av.wrapAlignment || endRes == av.endRes)
844 g.fillRect(lastSSX, y + 6 + iconOffset,
845 (x * av.charWidth) - lastSSX, 2);
854 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
856 if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
858 float groupmax = -999999, groupmin = 9999999;
859 for (int gg = 0; gg < aa.length; gg++)
861 if (aa[gg].graphGroup != row.graphGroup)
868 aa[gg].visible = false;
871 if (aa[gg].graphMax > groupmax)
873 groupmax = aa[gg].graphMax;
875 if (aa[gg].graphMin < groupmin)
877 groupmin = aa[gg].graphMin;
881 for (int gg = 0; gg < aa.length; gg++)
883 if (aa[gg].graphGroup == row.graphGroup)
885 drawLineGraph(g, aa[gg], startRes, endRes, y,
891 graphGroupDrawn[row.graphGroup] = true;
895 drawLineGraph(g, row, startRes, endRes,
896 y, row.graphMin, row.graphMax, row.graphHeight);
899 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
901 drawBarGraph(g, row, startRes, endRes,
902 row.graphMin, row.graphMax, y);
906 if (row.graph > 0 && row.hasText)
918 public void drawLineGraph(Graphics g, AlignmentAnnotation aa,
921 float min, float max,
924 if (sRes > aa.annotations.length)
931 //Adjustment for fastpaint to left
932 if (eRes < av.endRes)
937 eRes = Math.min(eRes, aa.annotations.length);
940 float range = max - min;
945 y2 = y - (int) ( (0 - min / range) * graphHeight);
948 g.setColor(Color.gray);
949 g.drawLine(x - av.charWidth, y2, (eRes - sRes) * av.charWidth, y2);
951 eRes = Math.min(eRes, aa.annotations.length);
954 int aaMax = aa.annotations.length - 1;
956 while (x < eRes - sRes)
959 if (av.hasHiddenColumns)
961 column = av.getColumnSelection().adjustForHiddenColumns(column);
969 if (aa.annotations[column] == null) // || coaa.annotations[column - 1] == null)
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);
985 (int) ( ( (aa.annotations[column-1].value - min) / range) * graphHeight);
988 (int) ( ( (aa.annotations[column].value - min) / range) * graphHeight);
990 g.drawLine(x * av.charWidth - av.charWidth / 2, y1,
991 x * av.charWidth + av.charWidth / 2, y2);
995 if (aa.threshold != null)
997 g.setColor(aa.threshold.colour);
999 y2 = (int) (y - ( (aa.threshold.value - min) / range) * graphHeight);
1000 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1004 public void drawBarGraph(Graphics g, AlignmentAnnotation aa,
1006 float min, float max,
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, int y,
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);