2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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;
62 //Used For mouse Dragging and resizing graphs
63 int graphStretch = -1;
65 int graphStretchY = -1;
67 boolean mouseDragging=false;
69 public static int GRAPH_HEIGHT = 40;
73 public AnnotationPanel(AlignmentPanel ap)
75 MAC = new jalview.util.Platform().isAMac();
81 addMouseMotionListener(this);
83 addMouseListener(this);
85 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
88 public AnnotationPanel(AlignViewport av)
93 public void adjustmentValueChanged(AdjustmentEvent evt)
95 ap.alabels.setScrollOffset(-evt.getValue());
104 public void actionPerformed(ActionEvent evt)
106 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
107 Annotation[] anot = aa[activeRow].annotations;
109 if (anot.length < av.getColumnSelection().getMax())
111 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
112 System.arraycopy(anot, 0, temp, 0, anot.length);
114 aa[activeRow].annotations = anot;
118 if (av.colSel != null && av.colSel.size() > 0
119 && anot[av.colSel.getMin()] != null)
120 label = anot[av.getColumnSelection().getMin()].displayCharacter;
122 if (evt.getActionCommand().equals(REMOVE))
124 for (int i = 0; i < av.getColumnSelection().size(); i++)
126 anot[av.getColumnSelection().columnAt(i)] = null;
129 else if (evt.getActionCommand().equals(LABEL))
131 label = enterLabel(label, "Enter Label");
138 if ((label.length() > 0) && !aa[activeRow].hasText)
140 aa[activeRow].hasText = true;
143 for (int i = 0; i < av.getColumnSelection().size(); i++)
145 int index = av.getColumnSelection().columnAt(i);
147 if (!av.colSel.isVisible(index))
150 if (anot[index] == null)
152 anot[index] = new Annotation(label, "", ' ', 0);
155 anot[index].displayCharacter = label;
158 else if (evt.getActionCommand().equals(COLOUR))
160 UserDefinedColours udc = new UserDefinedColours(this, Color.black,
163 Color col = udc.getColor();
165 for (int i = 0; i < av.getColumnSelection().size(); i++)
167 int index = av.getColumnSelection().columnAt(i);
169 if (!av.colSel.isVisible(index))
172 if (anot[index] == null)
174 anot[index] = new Annotation("", "", ' ', 0);
177 anot[index].colour = col;
184 String symbol = "\u03B1";
186 if (evt.getActionCommand().equals(HELIX))
190 else if (evt.getActionCommand().equals(SHEET))
196 if (!aa[activeRow].hasIcons)
198 aa[activeRow].hasIcons = true;
201 label = enterLabel(symbol, "Enter Label");
208 if ((label.length() > 0) && !aa[activeRow].hasText)
210 aa[activeRow].hasText = true;
213 for (int i = 0; i < av.getColumnSelection().size(); i++)
215 int index = av.getColumnSelection().columnAt(i);
217 if (!av.colSel.isVisible(index))
220 if (anot[index] == null)
222 anot[index] = new Annotation(label, "", type, 0);
225 anot[index].secondaryStructure = type;
226 anot[index].displayCharacter = label;
236 String enterLabel(String text, String label)
238 EditNameDialog dialog = new EditNameDialog(text, null, label, null,
239 ap.alignFrame, "Enter Label", 400, 200, true);
242 return dialog.getName();
247 public void mousePressed(MouseEvent evt)
249 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
258 for (int i = 0; i < aa.length; i++)
262 height += aa[i].height;
265 if (evt.getY() < height)
271 else if (aa[i].graph > 0)
275 graphStretchY = evt.getY();
283 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK
286 if (av.getColumnSelection() == null)
291 PopupMenu pop = new PopupMenu("Structure type");
292 MenuItem item = new MenuItem(HELIX);
293 item.addActionListener(this);
295 item = new MenuItem(SHEET);
296 item.addActionListener(this);
298 item = new MenuItem(LABEL);
299 item.addActionListener(this);
301 item = new MenuItem(COLOUR);
302 item.addActionListener(this);
304 item = new MenuItem(REMOVE);
305 item.addActionListener(this);
307 ap.alignFrame.add(pop);
308 pop.show(this, evt.getX(), evt.getY());
318 ap.scalePanel.mousePressed(evt);
321 public void mouseReleased(MouseEvent evt)
325 mouseDragging = false;
326 ap.scalePanel.mouseReleased(evt);
329 public void mouseClicked(MouseEvent evt)
333 public void mouseDragged(MouseEvent evt)
335 if (graphStretch > -1)
337 av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY
339 if (av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight < 0)
341 av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight = 0;
343 graphStretchY = evt.getY();
345 ap.paintAlignment(true);
349 ap.scalePanel.mouseDragged(evt);
353 public void mouseMoved(MouseEvent evt)
355 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
363 for (int i = 0; i < aa.length; i++)
368 height += aa[i].height;
371 if (evt.getY() < height)
378 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
380 if (av.hasHiddenColumns)
382 res = av.getColumnSelection().adjustForHiddenColumns(res);
385 if (row > -1 && res < aa[row].annotations.length
386 && aa[row].annotations[res] != null)
388 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
389 if (aa[row].annotations[res].description != null)
391 text.append(" " + aa[row].annotations[res].description);
393 ap.alignFrame.statusBar.setText(text.toString());
397 public void mouseEntered(MouseEvent evt)
399 ap.scalePanel.mouseEntered(evt);
402 public void mouseExited(MouseEvent evt)
404 ap.scalePanel.mouseExited(evt);
407 public int adjustPanelHeight()
409 // setHeight of panels
410 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
415 for (int i = 0; i < aa.length; i++)
426 aa[i].height += av.charHeight;
436 aa[i].height += aa[i].graphHeight;
439 if (aa[i].height == 0)
444 height += aa[i].height;
452 this.setSize(getSize().width, height);
460 public void addEditableColumn(int i)
464 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
470 for (int j = 0; j < aa.length; j++)
480 if (activeRes == null)
482 activeRes = new Vector();
483 activeRes.addElement(String.valueOf(i));
487 activeRes.addElement(String.valueOf(i));
490 public void update(Graphics g)
495 public void paint(Graphics g)
498 imgWidth = getSize().width;
499 // (av.endRes - av.startRes + 1) * av.charWidth;
501 if (image == null || imgWidth != image.getWidth(this))
503 image = createImage(imgWidth, ap.annotationPanel.getSize().height);
504 gg = image.getGraphics();
505 gg.setFont(av.getFont());
506 fm = gg.getFontMetrics();
512 g.drawImage(image, 0, 0, this);
517 gg.setColor(Color.white);
518 gg.fillRect(0, 0, getSize().width, getSize().height);
519 drawComponent(gg, av.startRes, av.endRes + 1);
521 g.drawImage(image, 0, 0, this);
524 public void fastPaint(int horizontal)
526 if (horizontal == 0 || av.alignment.getAlignmentAnnotation() == null
527 || av.alignment.getAlignmentAnnotation().length < 1)
533 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal
535 int sr = av.startRes, er = av.endRes + 1, transX = 0;
537 if (horizontal > 0) // scrollbar pulled right, image to the left
539 transX = (er - sr - horizontal) * av.charWidth;
540 sr = er - horizontal;
542 else if (horizontal < 0)
544 er = sr - horizontal;
547 gg.translate(transX, 0);
549 drawComponent(gg, sr, er);
551 gg.translate(-transX, 0);
567 public void drawComponent(Graphics g, int startRes, int endRes)
569 g.setFont(av.getFont());
571 g.setColor(Color.white);
572 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
576 fm = g.getFontMetrics();
579 if ((av.alignment.getAlignmentAnnotation() == null)
580 || (av.alignment.getAlignmentAnnotation().length < 1))
582 g.setColor(Color.white);
583 g.fillRect(0, 0, getSize().width, getSize().height);
584 g.setColor(Color.black);
585 if (av.validCharWidth)
587 g.drawString("Alignment has no annotations", 20, 15);
593 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
600 int iconOffset = av.charHeight / 2;
601 boolean validRes = false;
602 boolean validEnd = false;
603 boolean[] graphGroupDrawn = new boolean[aa.length];
606 for (int i = 0; i < aa.length; i++)
608 AlignmentAnnotation row = aa[i];
620 if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
625 // this is so that we draw the characters below the graph
636 iconOffset = av.charHeight / 2;
644 while (x < endRes - startRes)
646 if (av.hasHiddenColumns)
648 column = av.getColumnSelection().adjustForHiddenColumns(
650 if (column > row.annotations.length - 1)
657 column = startRes + x;
660 if ((row.annotations.length <= column)
661 || (row.annotations[column] == null))
672 g.setColor(Color.red);
674 if (av.getColumnSelection() != null)
676 for (int n = 0; n < av.getColumnSelection().size(); n++)
678 int v = av.getColumnSelection().columnAt(n);
683 .fillRect(x * av.charWidth, y, av.charWidth,
690 if (av.validCharWidth
692 && (row.annotations[column].displayCharacter != null && row.annotations[column].displayCharacter
695 int charOffset = (av.getCentreColumnLabels()) ? ((av.charWidth - fm
696 .charsWidth(row.annotations[column].displayCharacter
698 row.annotations[column].displayCharacter.length())) / 2)
700 .charWidth(row.annotations[column].displayCharacter
703 if (row.annotations[column].colour == null)
704 g.setColor(Color.black);
706 g.setColor(row.annotations[column].colour);
708 if (column == 0 || row.graph > 0)
710 g.drawString(row.annotations[column].displayCharacter,
711 (x * av.charWidth) + charOffset, y + iconOffset + 3);
713 else if (row.annotations[column - 1] == null
714 || (!row.annotations[column].displayCharacter
715 .equals(row.annotations[column - 1].displayCharacter) || (row.annotations[column].displayCharacter
716 .length() < 2 && row.annotations[column].secondaryStructure == ' ')))
718 g.drawString(row.annotations[column].displayCharacter,
719 (x * av.charWidth) + charOffset, y + iconOffset + 3);
726 || (row.annotations[column].secondaryStructure != lastSS))
731 g.setColor(HELIX_COLOUR);
734 // Off by 1 offset when drawing rects and ovals
735 // to offscreen image on the MAC
736 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
737 (x * av.charWidth) - lastSSX, 7, 8, 8);
741 int sCol = (lastSSX / av.charWidth) + startRes;
743 int x2 = (x * av.charWidth);
746 || row.annotations[sCol - 1] == null
747 || row.annotations[sCol - 1].secondaryStructure != 'H')
749 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
751 x1 += av.charWidth / 2;
754 if (!validRes || row.annotations[column] == null
755 || row.annotations[column].secondaryStructure != 'H')
757 g.fillArc((x * av.charWidth) - av.charWidth, y + 4
758 + iconOffset, av.charWidth, 8, 270, 180);
759 x2 -= av.charWidth / 2;
762 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
766 g.setColor(SHEET_COLOUR);
767 g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
769 g.fillPolygon(new int[]
770 { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
771 (x * av.charWidth) }, new int[]
772 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
778 g.setColor(Color.gray);
779 g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
787 lastSS = row.annotations[column].secondaryStructure;
794 lastSSX = (x * av.charWidth);
802 if (column >= row.annotations.length)
804 column = row.annotations.length - 1;
819 g.setColor(HELIX_COLOUR);
822 // Off by 1 offset when drawing rects and ovals
823 // to offscreen image on the MAC
824 g.fillRoundRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
829 int sCol = (lastSSX / av.charWidth) + startRes;
831 int x2 = (x * av.charWidth);
833 if (sCol == 0 || row.annotations[sCol - 1] == null
834 || row.annotations[sCol - 1].secondaryStructure != 'H')
837 .fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8,
839 x1 += av.charWidth / 2;
842 if (row.annotations[column] == null
843 || row.annotations[column].secondaryStructure != 'H')
845 g.fillArc((x * av.charWidth) - av.charWidth,
846 y + 4 + iconOffset, av.charWidth, 8, 270, 180);
847 x2 -= av.charWidth / 2;
850 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
855 g.setColor(SHEET_COLOUR);
857 if (!validEnd || row.annotations[endRes] == null
858 || row.annotations[endRes].secondaryStructure != 'E')
860 g.fillRect(lastSSX, y + 4 + iconOffset, (x * av.charWidth)
862 g.fillPolygon(new int[]
863 { (x * av.charWidth) - 4, (x * av.charWidth) - 4,
864 (x * av.charWidth) }, new int[]
865 { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset }, 3);
869 g.fillRect(lastSSX, y + 4 + iconOffset, x * av.charWidth
875 g.setColor(Color.gray);
876 if (!av.wrapAlignment || endRes == av.endRes)
878 g.fillRect(lastSSX, y + 6 + iconOffset, (x * av.charWidth)
886 if (row.graph > 0 && row.graphHeight> 0)
888 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
890 if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
892 float groupmax = -999999, groupmin = 9999999;
893 for (int gg = 0; gg < aa.length; gg++)
895 if (aa[gg].graphGroup != row.graphGroup)
902 aa[gg].visible = false;
905 if (aa[gg].graphMax > groupmax)
907 groupmax = aa[gg].graphMax;
909 if (aa[gg].graphMin < groupmin)
911 groupmin = aa[gg].graphMin;
915 for (int gg = 0; gg < aa.length; gg++)
917 if (aa[gg].graphGroup == row.graphGroup)
919 drawLineGraph(g, aa[gg], startRes, endRes, y, groupmin,
920 groupmax, row.graphHeight);
924 graphGroupDrawn[row.graphGroup] = true;
928 drawLineGraph(g, row, startRes, endRes, y, row.graphMin,
929 row.graphMax, row.graphHeight);
932 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
934 drawBarGraph(g, row, startRes, endRes, row.graphMin,
939 if (row.graph > 0 && row.hasText)
951 public void drawLineGraph(Graphics g, AlignmentAnnotation aa, int sRes,
952 int eRes, int y, float min, float max, int graphHeight)
954 if (sRes > aa.annotations.length)
961 // Adjustment for fastpaint to left
962 if (eRes < av.endRes)
967 eRes = Math.min(eRes, aa.annotations.length);
970 float range = max - min;
975 y2 = y - (int) ((0 - min / range) * graphHeight);
978 g.setColor(Color.gray);
979 g.drawLine(x - av.charWidth, y2, (eRes - sRes) * av.charWidth, y2);
981 eRes = Math.min(eRes, aa.annotations.length);
984 int aaMax = aa.annotations.length - 1;
986 while (x < eRes - sRes)
989 if (av.hasHiddenColumns)
991 column = av.getColumnSelection().adjustForHiddenColumns(column);
999 if (aa.annotations[column] == null) // || coaa.annotations[column - 1] ==
1006 if (aa.annotations[column].colour == null)
1007 g.setColor(Color.black);
1009 g.setColor(aa.annotations[column].colour);
1010 if (column == 0 || aa.annotations[column - 1] == null)
1013 - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
1018 - (int) (((aa.annotations[column - 1].value - min) / range) * graphHeight);
1021 - (int) (((aa.annotations[column].value - min) / range) * graphHeight);
1023 g.drawLine(x * av.charWidth - av.charWidth / 2, y1, x * av.charWidth
1024 + av.charWidth / 2, y2);
1028 if (aa.threshold != null)
1030 g.setColor(aa.threshold.colour);
1032 y2 = (int) (y - ((aa.threshold.value - min) / range) * graphHeight);
1033 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1037 public void drawBarGraph(Graphics g, AlignmentAnnotation aa, int sRes,
1038 int eRes, float min, float max, int y)
1040 if (sRes > aa.annotations.length)
1045 eRes = Math.min(eRes, aa.annotations.length);
1047 int x = 0, y1 = y, y2 = y;
1049 float range = max - min;
1053 y2 = y - (int) ((0 - min / (range)) * aa.graphHeight);
1056 g.setColor(Color.gray);
1058 g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
1061 int aaMax = aa.annotations.length - 1;
1063 while (x < eRes - sRes)
1066 if (av.hasHiddenColumns)
1068 column = av.getColumnSelection().adjustForHiddenColumns(column);
1076 if (aa.annotations[column] == null)
1082 if (aa.annotations[column].colour == null)
1083 g.setColor(Color.black);
1085 g.setColor(aa.annotations[column].colour);
1088 - (int) (((aa.annotations[column].value - min) / (range)) * aa.graphHeight);
1092 g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
1096 g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
1102 if (aa.threshold != null)
1104 g.setColor(aa.threshold.colour);
1105 y2 = (int) (y - ((aa.threshold.value - min) / range) * aa.graphHeight);
1106 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
1110 // used by overview window
1111 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width,
1112 int y, int sRes, int eRes)
1114 eRes = Math.min(eRes, aa.annotations.length);
1115 g.setColor(Color.white);
1116 g.fillRect(0, 0, width, y);
1117 g.setColor(new Color(0, 0, 180));
1121 for (int j = sRes; j < eRes; j++)
1123 if (aa.annotations[j].colour == null)
1124 g.setColor(Color.black);
1126 g.setColor(aa.annotations[j].colour);
1128 height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
1133 g.fillRect(x, y - height, av.charWidth, height);