Brackets around orgin calculation
[jalview.git] / src / jalview / appletgui / AnnotationPanel.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 \r
27 import jalview.datamodel.*;\r
28 \r
29 public class AnnotationPanel\r
30     extends Panel implements AdjustmentListener\r
31 {\r
32   AlignViewport av;\r
33   AlignmentPanel ap;\r
34   int activeRow = -1;\r
35 \r
36   Vector activeRes;\r
37   static String HELIX = "Helix";\r
38   static String SHEET = "Sheet";\r
39   static String LABEL = "Label";\r
40   static String REMOVE = "Remove Annotation";\r
41   static String COLOUR = "Colour";\r
42   static Color HELIX_COLOUR = Color.red.darker();\r
43   static Color SHEET_COLOUR = Color.green.darker().darker();\r
44 \r
45   Image image;\r
46   Graphics gg;\r
47   FontMetrics fm;\r
48   int imgWidth = 0;\r
49 \r
50   boolean fastPaint = false;\r
51 \r
52   public static int GRAPH_HEIGHT = 40;\r
53 \r
54   boolean MAC = false;\r
55 \r
56   public AnnotationPanel(AlignmentPanel ap)\r
57   {\r
58     if (System.getProperty("os.name").startsWith("Mac"))\r
59       MAC = true;\r
60 \r
61     this.ap = ap;\r
62     av = ap.av;\r
63     setLayout(null);\r
64     adjustPanelHeight();\r
65 \r
66     addMouseMotionListener(new MouseMotionAdapter()\r
67     {\r
68       public void mouseMoved(MouseEvent evt)\r
69       {\r
70         doMouseMoved(evt);\r
71       }\r
72     });\r
73 \r
74     // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );\r
75   }\r
76 \r
77 \r
78   public AnnotationPanel(AlignViewport av)\r
79   {\r
80     this.av = av;\r
81   }\r
82 \r
83 \r
84   public void adjustmentValueChanged(AdjustmentEvent evt)\r
85   {\r
86     ap.alabels.setScrollOffset( -evt.getValue());\r
87   }\r
88 \r
89   public int adjustPanelHeight()\r
90   {\r
91     // setHeight of panels\r
92     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
93 \r
94 \r
95     int height = 0;\r
96     if (aa != null)\r
97     {\r
98       for (int i = 0; i < aa.length; i++)\r
99       {\r
100         if (!aa[i].visible)\r
101         {\r
102           continue;\r
103         }\r
104 \r
105         aa[i].height = 0;\r
106 \r
107         if (aa[i].hasText)\r
108         {\r
109           aa[i].height += av.charHeight;\r
110         }\r
111         if (aa[i].hasIcons)\r
112         {\r
113           aa[i].height += 16;\r
114         }\r
115 \r
116         if (aa[i].graph>0)\r
117         {\r
118           aa[i].height += GRAPH_HEIGHT;\r
119         }\r
120 \r
121         if (aa[i].height == 0)\r
122         {\r
123           aa[i].height = 20;\r
124         }\r
125         height += aa[i].height;\r
126       }\r
127     }\r
128     else\r
129     {\r
130       height = 20;\r
131     }\r
132 \r
133     this.setSize(getSize().width, height);\r
134 \r
135     repaint();\r
136 \r
137     return height;\r
138 \r
139   }\r
140 \r
141   public void addEditableColumn(int i)\r
142   {\r
143     if (activeRow == -1)\r
144     {\r
145       AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
146       if(aa ==null)\r
147         return;\r
148 \r
149       for (int j = 0; j < aa.length; j++)\r
150       {\r
151         if (aa[j].editable)\r
152         {\r
153           activeRow = j;\r
154           break;\r
155         }\r
156       }\r
157     }\r
158 \r
159     if (activeRes == null)\r
160     {\r
161       activeRes = new Vector();\r
162       activeRes.addElement(String.valueOf(i));\r
163       return;\r
164     }\r
165 \r
166     activeRes.addElement(String.valueOf(i));\r
167   }\r
168 \r
169   public void doMouseMoved(MouseEvent evt)\r
170   {\r
171     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
172     if (aa == null)\r
173     {\r
174       return;\r
175     }\r
176 \r
177     int row = -1;\r
178     int height = 0;\r
179     for (int i = 0; i < aa.length; i++)\r
180     {\r
181 \r
182       if (aa[i].visible)\r
183       {\r
184         height += aa[i].height;\r
185       }\r
186 \r
187       if (evt.getY() < height)\r
188       {\r
189         row = i;\r
190         break;\r
191       }\r
192     }\r
193 \r
194     int res = evt.getX() / av.getCharWidth() + av.getStartRes();\r
195     if (row > -1 && res < aa[row].annotations.length && aa[row].annotations[res] != null)\r
196     {\r
197       StringBuffer text = new StringBuffer("Sequence position " + (res + 1) +\r
198                                            "  " +\r
199                                            aa[row].annotations[res].description);\r
200       ap.alignFrame.statusBar.setText(text.toString());\r
201     }\r
202   }\r
203 \r
204   public void update(Graphics g)\r
205   {\r
206     paint(g);\r
207   }\r
208 \r
209   public void paint(Graphics g)\r
210   {\r
211 \r
212 \r
213     imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;\r
214 \r
215     if (image == null || imgWidth != image.getWidth(this))\r
216     {\r
217       image = createImage(imgWidth, ap.annotationPanel.getSize().height);\r
218       gg = image.getGraphics();\r
219       gg.setFont(av.getFont());\r
220       fm = gg.getFontMetrics();\r
221       fastPaint = false;\r
222     }\r
223 \r
224     if (fastPaint)\r
225     {\r
226       g.drawImage(image, 0, 0, this);\r
227       fastPaint = false;\r
228       return;\r
229     }\r
230 \r
231     drawComponent(gg, av.startRes, av.endRes + 1);\r
232     g.setColor(Color.white);\r
233     g.fillRect(0, 0, getSize().width, getSize().height);\r
234     g.drawImage(image, 0, 0, this);\r
235   }\r
236 \r
237   public void fastPaint(int horizontal)\r
238   {\r
239     if (horizontal == 0\r
240         || av.alignment.getAlignmentAnnotation() == null\r
241         || av.alignment.getAlignmentAnnotation().length < 1\r
242         )\r
243     {\r
244       repaint();\r
245       return;\r
246     }\r
247 \r
248     gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal * av.charWidth, 0);\r
249     int sr = av.startRes, er = av.endRes + 1, transX = 0;\r
250 \r
251     if (horizontal > 0) // scrollbar pulled right, image to the left\r
252     {\r
253       transX = (er - sr - horizontal) * av.charWidth;\r
254       sr = er - horizontal;\r
255     }\r
256     else if (horizontal < 0)\r
257     {\r
258       er = sr - horizontal;\r
259     }\r
260 \r
261     gg.translate(transX, 0);\r
262 \r
263     drawComponent(gg, sr, er);\r
264 \r
265     gg.translate( -transX, 0);\r
266 \r
267     fastPaint = true;\r
268     repaint();\r
269   }\r
270 \r
271   /**\r
272    * DOCUMENT ME!\r
273    *\r
274    * @param g DOCUMENT ME!\r
275    * @param startRes DOCUMENT ME!\r
276    * @param endRes DOCUMENT ME!\r
277    */\r
278   public void drawComponent(Graphics g, int startRes, int endRes)\r
279   {\r
280     g.setFont(av.getFont());\r
281 \r
282     if (fm == null)\r
283       fm = g.getFontMetrics();\r
284 \r
285 \r
286       g.setColor(Color.white);\r
287       g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);\r
288 \r
289       if ((av.alignment.getAlignmentAnnotation() == null) ||\r
290               (av.alignment.getAlignmentAnnotation().length < 1))\r
291       {\r
292           g.setColor(Color.white);\r
293           g.fillRect(0, 0, getSize().width, getSize().height);\r
294           g.setColor(Color.black);\r
295           if(av.validCharWidth)\r
296             g.drawString("Alignment has no annotations", 20, 15);\r
297 \r
298           return;\r
299       }\r
300 \r
301       AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
302 \r
303       int x = 0;\r
304       int y = 0;\r
305       char lastSS;\r
306       int lastSSX;\r
307       int iconOffset = av.charHeight / 2;\r
308       boolean validRes = false;\r
309 \r
310       boolean [] graphGroupDrawn = new boolean[aa.length];\r
311 \r
312 \r
313       //\u03B2 \u03B1\r
314       for (int i = 0; i < aa.length; i++)\r
315       {\r
316           AlignmentAnnotation row = aa[i];\r
317 \r
318           if (!row.visible)\r
319           {\r
320               continue;\r
321           }\r
322 \r
323           lastSS = ' ';\r
324           lastSSX = 0;\r
325 \r
326           if (row.graph>0)\r
327           {\r
328               if(row.graphGroup>-1 && graphGroupDrawn[ row.graphGroup ] )\r
329                 continue;\r
330 \r
331               // this is so that we draw the characters below the graph\r
332               y += row.height;\r
333 \r
334               if (row.hasText)\r
335               {\r
336                   y -= av.charHeight;\r
337               }\r
338           }\r
339 \r
340           if (row.hasText)\r
341           {\r
342               iconOffset = av.charHeight / 2;\r
343           }\r
344           else\r
345           {\r
346               iconOffset = 0;\r
347           }\r
348 \r
349           int column = startRes;\r
350           while(column < endRes)\r
351           {\r
352               if ((row.annotations.length <= column) ||\r
353                       (row.annotations[column] == null))\r
354               {\r
355                   validRes = false;\r
356               }\r
357               else\r
358               {\r
359                   validRes = true;\r
360               }\r
361 \r
362               x = (column - startRes) * av.charWidth;\r
363 \r
364 \r
365               if (av.validCharWidth && validRes &&\r
366                         (row.annotations[column].displayCharacter.length() > 0))\r
367               {\r
368                 int charOffset = (av.charWidth -\r
369                     fm.charWidth(row.annotations[column].displayCharacter.charAt(\r
370                             0))) / 2;\r
371                 g.setColor(row.annotations[column].colour);\r
372 \r
373                 if (column == 0 || row.graph>0)\r
374                 {\r
375                     g.drawString(row.annotations[column].displayCharacter, x+charOffset,\r
376                         y + iconOffset + 3);\r
377                 }\r
378                 else if (\r
379                     row.annotations[column - 1] == null\r
380                     ||(!row.annotations[column].displayCharacter.equals(\r
381                         row.annotations[column - 1].displayCharacter)\r
382                     ||\r
383               (row.annotations[column].displayCharacter.length() <2 &&\r
384                row.annotations[column].secondaryStructure==' ')))\r
385                 {\r
386                     g.drawString(row.annotations[column].displayCharacter, x+charOffset,\r
387                         y + iconOffset + 3);\r
388                     }\r
389               }\r
390 \r
391               if (row.hasIcons)\r
392               {\r
393                   if (!validRes ||\r
394                           (row.annotations[column].secondaryStructure != lastSS))\r
395                   {\r
396                       switch (lastSS)\r
397                       {\r
398                       case 'H':\r
399                         g.setColor(HELIX_COLOUR);\r
400                         if (MAC)\r
401                         {\r
402                           //Off by 1 offset when drawing rects and ovals\r
403                           //to offscreen image on the MAC\r
404                           g.fillRoundRect(lastSSX, y + 4 + iconOffset,\r
405                                           x - lastSSX, 7, 8, 8);\r
406                           break;\r
407                         }\r
408 \r
409                         int sCol = (lastSSX / av.charWidth) + startRes;\r
410                         int x1 = lastSSX;\r
411                         int x2 = x;\r
412 \r
413                        if(sCol==0 ||\r
414                           row.annotations[sCol-1]==null ||\r
415                           row.annotations[sCol-1].secondaryStructure!='H')\r
416                        {\r
417                          g.fillArc(lastSSX, y+4+iconOffset, av.charWidth, 8, 90,180) ;\r
418                          x1 += av.charWidth/2;\r
419                        }\r
420 \r
421                         if(row.annotations[column]==null ||\r
422                            row.annotations[column].secondaryStructure!='H')\r
423                         {\r
424                           g.fillArc(x-av.charWidth, y+4+iconOffset, av.charWidth, 8, 270,180);\r
425                           x2 -= av.charWidth/2;\r
426                         }\r
427 \r
428                         g.fillRect(x1, y+4+iconOffset, x2-x1, 8);\r
429                             break;\r
430 \r
431                       case 'E':\r
432                           g.setColor(SHEET_COLOUR);\r
433                           g.fillRect(lastSSX, y + 4 + iconOffset,\r
434                               x - lastSSX - 4, 7);\r
435                           g.fillPolygon(new int[] { x - 4, x - 4, x },\r
436                               new int[]\r
437                               {\r
438                                   y + iconOffset, y + 14 + iconOffset,\r
439                                   y + 8 + iconOffset\r
440                               }, 3);\r
441 \r
442                           break;\r
443 \r
444 \r
445                       default:\r
446                           g.setColor(Color.gray);\r
447                           g.fillRect(lastSSX, y + 6 + iconOffset,\r
448                               x - lastSSX, 2);\r
449 \r
450                           break;\r
451                       }\r
452 \r
453                       if (validRes)\r
454                       {\r
455                           lastSS = row.annotations[column].secondaryStructure;\r
456                       }\r
457                       else\r
458                       {\r
459                           lastSS = ' ';\r
460                       }\r
461 \r
462                       lastSSX = x;\r
463                   }\r
464               }\r
465 \r
466 \r
467           column++;\r
468           }\r
469 \r
470           x += av.charWidth;\r
471 \r
472           if (row.hasIcons)\r
473           {\r
474               switch (lastSS)\r
475               {\r
476               case 'H':\r
477                 g.setColor(HELIX_COLOUR);\r
478                 if (MAC)\r
479                 {\r
480                   //Off by 1 offset when drawing rects and ovals\r
481                   //to offscreen image on the MAC\r
482                   g.fillRoundRect(lastSSX, y + 4 + iconOffset,\r
483                                   x - lastSSX, 7, 8, 8);\r
484                   break;\r
485                 }\r
486 \r
487 \r
488                 int sCol = (lastSSX / av.charWidth) + startRes;\r
489                 int x1 = lastSSX;\r
490                 int x2 = x;\r
491 \r
492                if(sCol==0 ||\r
493                   row.annotations[sCol-1]==null ||\r
494                   row.annotations[sCol-1].secondaryStructure!='H')\r
495                {\r
496                  g.fillArc(lastSSX, y+4+iconOffset, av.charWidth, 8, 90,180) ;\r
497                  x1 += av.charWidth/2;\r
498                }\r
499 \r
500                 if(row.annotations[column]==null ||\r
501                    row.annotations[column].secondaryStructure!='H')\r
502                 {\r
503                   g.fillArc(x-av.charWidth, y+4+iconOffset, av.charWidth, 8, 270,180);\r
504                   x2 -= av.charWidth/2;\r
505                 }\r
506 \r
507                 g.fillRect(x1, y+4+iconOffset, x2-x1, 8);\r
508 \r
509                     break;\r
510 \r
511               case 'E':\r
512                   g.setColor(SHEET_COLOUR);\r
513 \r
514                   if (row.annotations.length > endRes\r
515                       && row.annotations[endRes].secondaryStructure != 'E')\r
516                   {\r
517                     g.fillRect(lastSSX, y + 4 + iconOffset,\r
518                                x - lastSSX - 4, 7);\r
519                     g.fillPolygon(new int[]\r
520                                   {x - 4, x - 4, x},\r
521                                   new int[]\r
522                                   {\r
523                                   y + iconOffset, y + 14 + iconOffset,\r
524                                   y + 7 + iconOffset\r
525                     }, 3);\r
526                   }\r
527                   else\r
528                     g.fillRect(lastSSX, y + 4 + iconOffset,\r
529                                x - lastSSX, 7);\r
530 \r
531                   break;\r
532 \r
533 \r
534               default:\r
535                   g.setColor(Color.gray);\r
536                   g.fillRect(lastSSX, y + 6 + iconOffset, x - lastSSX, 2);\r
537 \r
538                   break;\r
539               }\r
540           }\r
541 \r
542           if (row.graph>0)\r
543           {\r
544               if(row.graph == AlignmentAnnotation.LINE_GRAPH )\r
545               {\r
546                 if(row.graphGroup>-1 && !graphGroupDrawn[row.graphGroup])\r
547                  {\r
548                    float groupmax=-999999, groupmin=9999999;\r
549                    for(int gg=0; gg<aa.length; gg++)\r
550                    {\r
551                      if(aa[gg].graphGroup!=row.graphGroup)\r
552                        continue;\r
553 \r
554                      if(aa[gg]!=row)\r
555                        aa[gg].visible = false;\r
556 \r
557                      if(aa[gg].graphMax>groupmax)\r
558                        groupmax = aa[gg].graphMax;\r
559                      if(aa[gg].graphMin<groupmin)\r
560                        groupmin = aa[gg].graphMin;\r
561                    }\r
562 \r
563                    for (int gg = 0; gg < aa.length; gg++)\r
564                    {\r
565                      if (aa[gg].graphGroup == row.graphGroup)\r
566                      {\r
567                        drawLineGraph(g, aa[gg], startRes, endRes, y,\r
568                                      groupmin, groupmax,\r
569                                      row.graphHeight);\r
570                      }\r
571                    }\r
572 \r
573                    graphGroupDrawn[ row.graphGroup ] = true;\r
574                  }\r
575                  else\r
576                    drawLineGraph(g, row, startRes, endRes,\r
577                                  y, row.graphMin, row.graphMax, row.graphHeight  );\r
578               }\r
579               else if(row.graph == AlignmentAnnotation.BAR_GRAPH )\r
580                  drawBarGraph(g, row, startRes, endRes,\r
581                               row.graphMin, row.graphMax, y);\r
582           }\r
583 \r
584           if (row.graph>0 && row.hasText)\r
585           {\r
586               y += av.charHeight;\r
587           }\r
588 \r
589           if (row.graph==0)\r
590           {\r
591               y += aa[i].height;\r
592           }\r
593       }\r
594   }\r
595 \r
596   public void drawLineGraph(Graphics g, AlignmentAnnotation aa,\r
597                             int sRes, int eRes,\r
598                             int y,\r
599                             float min, float max,\r
600                             int graphHeight)\r
601   {\r
602     if(sRes>aa.annotations.length)\r
603       return;\r
604 \r
605 \r
606 \r
607     int x = 0;\r
608 \r
609     //Adjustment for fastpaint to left\r
610     if(eRes<av.endRes)\r
611       eRes++;\r
612 \r
613     eRes = Math.min(eRes, aa.annotations.length);\r
614 \r
615 \r
616     if(sRes==0)\r
617     {\r
618       sRes++;\r
619       x+=av.charWidth;\r
620     }\r
621 \r
622     int y1=y, y2=y;\r
623     float range = max - min;\r
624 \r
625     ////Draw origin\r
626     if(min<0)\r
627       y2 = y - (int)((0-min / range)*graphHeight);\r
628 \r
629     g.setColor(Color.gray);\r
630     g.drawLine(x-av.charWidth,y2,(eRes-sRes)*av.charWidth,y2);\r
631 \r
632 \r
633     for (int j = sRes; j < eRes; j++)\r
634     {\r
635       if(aa.annotations[j]==null || aa.annotations[j-1]==null)\r
636       {\r
637         x+=av.charWidth;\r
638         continue;\r
639       }\r
640         g.setColor(aa.annotations[j].colour);\r
641         y1 = y - (int) (((aa.annotations[j-1].value-min) / range) * graphHeight);\r
642         y2 = y - (int) (((aa.annotations[j].value-min) / range) * graphHeight);\r
643         g.drawLine(x-av.charWidth/2, y1, x+av.charWidth/2, y2);\r
644         x += av.charWidth;\r
645      }\r
646 \r
647 \r
648      if(aa.threshold!=null)\r
649      {\r
650          g.setColor(aa.threshold.colour);\r
651          y2 = (int)(y - ((aa.threshold.value-min) / range)*aa.graphHeight);\r
652          g.drawLine(0,y2,(eRes-sRes)*av.charWidth,y2);\r
653      }\r
654 \r
655   }\r
656 \r
657   public void drawBarGraph(Graphics g, AlignmentAnnotation aa,\r
658                            int sRes, int eRes,\r
659                            float min, float max,\r
660                            int y)\r
661   {\r
662     if(sRes>aa.annotations.length)\r
663       return;\r
664 \r
665 \r
666     eRes = Math.min(eRes, aa.annotations.length);\r
667 \r
668     int x=0, y1, y2;\r
669 \r
670     float range = max - min;\r
671 \r
672     y1 = y2 = y;\r
673 \r
674     if(min<0)\r
675       y2 = y -(int)( (0-min / (range))*aa.graphHeight);\r
676 \r
677     g.setColor(Color.gray);\r
678 \r
679     g.drawLine(x,y2,(eRes-sRes)*av.charWidth,y2);\r
680 \r
681     for (int j = sRes; j < eRes; j++)\r
682     {\r
683 \r
684       if (aa.annotations[j] == null)\r
685       {\r
686         x += av.charWidth;\r
687         continue;\r
688       }\r
689 \r
690         g.setColor(aa.annotations[j].colour);\r
691         y1 = y - (int) (((aa.annotations[j].value-min) / (range)) * aa.graphHeight);\r
692 \r
693         if(y1-y2>0)\r
694           g.fillRect(x, y2, av.charWidth, y1-y2 );\r
695         else\r
696           g.fillRect(x, y1, av.charWidth, y2-y1 );\r
697 \r
698         x += av.charWidth;\r
699     }\r
700 \r
701 \r
702     if(aa.threshold!=null)\r
703     {\r
704         g.setColor(aa.threshold.colour);\r
705         y2 = (int)(y - ((aa.threshold.value-min) / range)*aa.graphHeight);\r
706         g.drawLine(0,y2,(eRes-sRes)*av.charWidth,y2);\r
707     }\r
708 \r
709 \r
710   }\r
711 \r
712   // used by overview window\r
713   public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y, int sRes, int eRes)\r
714   {\r
715       g.setColor(Color.white);\r
716       g.fillRect(0, 0, width, y);\r
717       g.setColor(new Color(0, 0, 180));\r
718 \r
719       int x = 0, height;\r
720 \r
721       for (int j = sRes; j < eRes; j++)\r
722       {\r
723           g.setColor(aa.annotations[j].colour);\r
724 \r
725           height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);\r
726           if(height>y)\r
727               height = y;\r
728           g.fillRect(x, y - height, av.charWidth, height);\r
729           x += av.charWidth;\r
730       }\r
731     }\r
732 }\r