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 if (System.getProperty("os.name").startsWith("Mac"))
69 addMouseMotionListener(this);
71 addMouseListener(this);
74 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
77 public AnnotationPanel(AlignViewport av)
82 public void adjustmentValueChanged(AdjustmentEvent evt)
84 ap.alabels.setScrollOffset( -evt.getValue());
90 * @param evt DOCUMENT ME!
92 public void actionPerformed(ActionEvent evt)
94 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
95 Annotation[] anot = aa[activeRow].annotations;
97 if (anot.length < av.getColumnSelection().getMax())
99 Annotation[] temp = new Annotation[av.getColumnSelection().getMax() + 2];
100 System.arraycopy(anot, 0, temp, 0, anot.length);
102 aa[activeRow].annotations = anot;
106 if (av.colSel != null && av.colSel.size() > 0
107 && anot[av.colSel.getMin()] != null)
108 label = anot[av.getColumnSelection().getMin()].displayCharacter;
111 if (evt.getActionCommand().equals(REMOVE))
113 for (int i = 0; i < av.getColumnSelection().size(); i++)
115 anot[av.getColumnSelection().columnAt(i)] = null;
118 else if (evt.getActionCommand().equals(LABEL))
120 label = enterLabel(label, "Enter Label");
127 if ( (label.length() > 0) && !aa[activeRow].hasText)
129 aa[activeRow].hasText = true;
132 for (int i = 0; i < av.getColumnSelection().size(); i++)
134 int index = av.getColumnSelection().columnAt(i);
136 if(!av.colSel.isVisible(index))
139 if (anot[index] == null)
141 anot[index] = new Annotation(label, "", ' ', 0);
144 anot[index].displayCharacter = label;
147 else if (evt.getActionCommand().equals(COLOUR))
149 UserDefinedColours udc = new UserDefinedColours(
151 Color.black, ap.alignFrame);
153 Color col = udc.getColor();
155 for (int i = 0; i < av.getColumnSelection().size(); i++)
157 int index = av.getColumnSelection().columnAt(i);
159 if(!av.colSel.isVisible(index))
162 if (anot[index] == null)
164 anot[index] = new Annotation("", "", ' ', 0);
167 anot[index].colour = col;
170 else // HELIX OR SHEET
173 String symbol = "\u03B1";
175 if (evt.getActionCommand().equals(HELIX))
179 else if (evt.getActionCommand().equals(SHEET))
185 if (!aa[activeRow].hasIcons)
187 aa[activeRow].hasIcons = true;
190 label = enterLabel(symbol, "Enter Label");
197 if ( (label.length() > 0) && !aa[activeRow].hasText)
199 aa[activeRow].hasText = true;
202 for (int i = 0; i < av.getColumnSelection().size(); i++)
204 int index = av.getColumnSelection().columnAt(i);
206 if(!av.colSel.isVisible(index))
209 if (anot[index] == null)
211 anot[index] = new Annotation(label, "", type, 0);
214 anot[index].secondaryStructure = type;
215 anot[index].displayCharacter = label;
225 String enterLabel(String text, String label)
227 EditNameDialog dialog = new EditNameDialog(text,null,label,null,
228 ap.alignFrame,"Enter Label", 400,200, true);
231 return dialog.getName();
236 public void mousePressed(MouseEvent evt)
238 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
248 for (int i = 0; i < aa.length; i++)
252 height += aa[i].height;
255 if (evt.getY() < height)
266 if ( (evt.getModifiers() & InputEvent.BUTTON3_MASK) ==
267 InputEvent.BUTTON3_MASK && activeRow != -1)
269 if (av.getColumnSelection() == null)
274 PopupMenu pop = new PopupMenu("Structure type");
275 MenuItem item = new MenuItem(HELIX);
276 item.addActionListener(this);
278 item = new MenuItem(SHEET);
279 item.addActionListener(this);
281 item = new MenuItem(LABEL);
282 item.addActionListener(this);
284 item = new MenuItem(COLOUR);
285 item.addActionListener(this);
287 item = new MenuItem(REMOVE);
288 item.addActionListener(this);
290 ap.alignFrame.add(pop);
291 pop.show(this, evt.getX(), evt.getY());
301 ap.scalePanel.mousePressed(evt);
304 public void mouseReleased(MouseEvent evt)
306 ap.scalePanel.mouseReleased(evt);
309 public void mouseClicked(MouseEvent evt)
312 public void mouseDragged(MouseEvent evt)
314 ap.scalePanel.mouseDragged(evt);
317 public void mouseMoved(MouseEvent evt)
319 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
327 for (int i = 0; i < aa.length; i++)
332 height += aa[i].height;
335 if (evt.getY() < height)
342 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
344 if (av.hasHiddenColumns)
346 res = av.getColumnSelection().adjustForHiddenColumns(res);
349 if (row > -1 && res < aa[row].annotations.length && aa[row].annotations[res] != null)
351 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
352 if (aa[row].annotations[res].description != null)
354 text.append(" " + aa[row].annotations[res].description);
356 ap.alignFrame.statusBar.setText(text.toString());
359 public void mouseEntered(MouseEvent evt)
361 ap.scalePanel.mouseEntered(evt);
363 public void mouseExited(MouseEvent evt)
365 ap.scalePanel.mouseExited(evt);
369 public int adjustPanelHeight()
371 // setHeight of panels
372 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
377 for (int i = 0; i < aa.length; i++)
388 aa[i].height += av.charHeight;
398 aa[i].height += aa[i].graphHeight;
401 if (aa[i].height == 0)
406 height += aa[i].height;
414 this.setSize(getSize().width, height);
422 public void addEditableColumn(int i)
426 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
432 for (int j = 0; j < aa.length; j++)
442 if (activeRes == null)
444 activeRes = new Vector();
445 activeRes.addElement(String.valueOf(i));
449 activeRes.addElement(String.valueOf(i));
453 public void update(Graphics g)
458 public void paint(Graphics g)
461 imgWidth = getSize().width;
462 //(av.endRes - av.startRes + 1) * av.charWidth;
464 if (image == null || imgWidth != image.getWidth(this))
466 image = createImage(imgWidth, ap.annotationPanel.getSize().height);
467 gg = image.getGraphics();
468 gg.setFont(av.getFont());
469 fm = gg.getFontMetrics();
475 g.drawImage(image, 0, 0, this);
480 gg.setColor(Color.white);
481 gg.fillRect(0, 0, getSize().width, getSize().height);
482 drawComponent(gg, av.startRes, av.endRes + 1);
484 g.drawImage(image, 0, 0, this);
487 public void fastPaint(int horizontal)
490 || av.alignment.getAlignmentAnnotation() == null
491 || av.alignment.getAlignmentAnnotation().length < 1
498 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal * av.charWidth, 0);
499 int sr = av.startRes, er = av.endRes + 1, transX = 0;
501 if (horizontal > 0) // scrollbar pulled right, image to the left
503 transX = (er - sr - horizontal) * av.charWidth;
504 sr = er - horizontal;
506 else if (horizontal < 0)
508 er = sr - horizontal;
511 gg.translate(transX, 0);
513 drawComponent(gg, sr, er);
515 gg.translate( -transX, 0);
524 * @param g DOCUMENT ME!
525 * @param startRes DOCUMENT ME!
526 * @param endRes DOCUMENT ME!
528 public void drawComponent(Graphics g, int startRes, int endRes)
530 g.setFont(av.getFont());
532 g.setColor(Color.white);
533 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
537 fm = g.getFontMetrics();
540 if ( (av.alignment.getAlignmentAnnotation() == null) ||
541 (av.alignment.getAlignmentAnnotation().length < 1))
543 g.setColor(Color.white);
544 g.fillRect(0, 0, getSize().width, getSize().height);
545 g.setColor(Color.black);
546 if (av.validCharWidth)
548 g.drawString("Alignment has no annotations", 20, 15);
554 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
561 int iconOffset = av.charHeight / 2;
562 boolean validRes = false;
564 boolean[] graphGroupDrawn = new boolean[aa.length];
567 for (int i = 0; i < aa.length; i++)
569 AlignmentAnnotation row = aa[i];
581 if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
586 // this is so that we draw the characters below the graph
597 iconOffset = av.charHeight / 2;
605 while (x < endRes - startRes)
607 if (av.hasHiddenColumns)
609 column = av.getColumnSelection().adjustForHiddenColumns(startRes + x);
610 if (column > row.annotations.length - 1)
617 column = startRes + x;
620 if ( (row.annotations.length <= column) ||
621 (row.annotations[column] == null))
632 g.setColor(Color.red);
634 if (av.getColumnSelection() != null)
636 for (int n = 0; n < av.getColumnSelection().size(); n++)
638 int v = av.getColumnSelection().columnAt(n);
642 g.fillRect(x * av.charWidth, y,
643 av.charWidth, av.charHeight);
651 if (av.validCharWidth && validRes &&
652 (row.annotations[column].displayCharacter!=null && row.annotations[column].displayCharacter.length() > 0))
654 int charOffset = (av.charWidth -
655 fm.charWidth(row.annotations[column].
656 displayCharacter.charAt(
659 if (row.annotations[column].colour == null)
660 g.setColor(Color.black);
662 g.setColor(row.annotations[column].colour);
664 if (column == 0 || row.graph > 0)
666 g.drawString(row.annotations[column].displayCharacter,
667 (x * av.charWidth) + charOffset,
671 row.annotations[column - 1] == null
672 || (!row.annotations[column].displayCharacter.equals(
673 row.annotations[column - 1].displayCharacter)
675 (row.annotations[column].displayCharacter.length() < 2 &&
676 row.annotations[column].secondaryStructure == ' ')))
678 g.drawString(row.annotations[column].displayCharacter,
679 (x * av.charWidth) + charOffset,
687 (row.annotations[column].secondaryStructure != lastSS))
692 g.setColor(HELIX_COLOUR);
695 //Off by 1 offset when drawing rects and ovals
696 //to offscreen image on the MAC
697 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
698 (x * av.charWidth) - lastSSX, 7, 8, 8);
702 int sCol = (lastSSX / av.charWidth) + startRes;
704 int x2 = (x * av.charWidth);
707 row.annotations[sCol - 1] == null ||
708 row.annotations[sCol - 1].secondaryStructure != 'H')
710 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
712 x1 += av.charWidth / 2;
715 if (row.annotations[column] == null ||
716 row.annotations[column].secondaryStructure != 'H')
718 g.fillArc( (x * av.charWidth) - av.charWidth,
719 y + 4 + iconOffset, av.charWidth, 8, 270, 180);
720 x2 -= av.charWidth / 2;
723 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
727 g.setColor(SHEET_COLOUR);
728 g.fillRect(lastSSX, y + 4 + iconOffset,
729 (x * av.charWidth) - lastSSX - 4, 7);
730 g.fillPolygon(new int[]
731 { (x * av.charWidth) - 4,
732 (x * av.charWidth) - 4,
736 y + iconOffset, y + 14 + iconOffset,
743 g.setColor(Color.gray);
744 g.fillRect(lastSSX, y + 6 + iconOffset,
745 (x * av.charWidth) - lastSSX, 2);
752 lastSS = row.annotations[column].secondaryStructure;
759 lastSSX = (x * av.charWidth);
767 if (column >= row.annotations.length)
769 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 (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);