GPL license added
[jalview.git] / src / jalview / gui / 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.gui;\r
21 \r
22 import jalview.datamodel.*;\r
23 \r
24 import javax.swing.*;\r
25 import java.awt.*;\r
26 import java.awt.event.*;\r
27 import java.util.*;\r
28 import java.awt.image.*;\r
29 \r
30 public class AnnotationPanel extends JPanel implements MouseListener, MouseMotionListener, ActionListener, AdjustmentListener\r
31 {\r
32   AlignViewport av;\r
33   AlignmentPanel ap;\r
34   int activeRow =-1;\r
35 \r
36   ArrayList 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 \r
46   BufferedImage image;\r
47   Graphics2D gg;\r
48   FontMetrics fm;\r
49   int imgWidth=0;\r
50 \r
51   boolean fastPaint = false;\r
52 \r
53   public static int GRAPH_HEIGHT = 40;\r
54 \r
55 \r
56 \r
57   public AnnotationPanel(AlignmentPanel ap)\r
58   {\r
59     this.ap = ap;\r
60     av = ap.av;\r
61     this.setLayout(null);\r
62     addMouseListener(this);\r
63     addMouseMotionListener(this);\r
64     adjustPanelHeight();\r
65 \r
66 \r
67     ap.annotationScroller.getVerticalScrollBar().addAdjustmentListener( this );\r
68   }\r
69 \r
70   public void adjustmentValueChanged(AdjustmentEvent evt)\r
71   {\r
72     ap.alabels.setScrollOffset( -evt.getValue() );\r
73   }\r
74 \r
75   public void adjustPanelHeight()\r
76   {\r
77     // setHeight of panels\r
78     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
79     int height = 0;\r
80     if(aa!=null)\r
81     for (int i = 0; i < aa.length; i++)\r
82     {\r
83       if(!aa[i].visible)\r
84         continue;\r
85 \r
86       aa[i].height = 0;\r
87 \r
88       if(aa[i].hasText)\r
89         aa[i].height += av.charHeight;\r
90       if (aa[i].hasIcons)\r
91         aa[i].height += 16;\r
92 \r
93       if (aa[i].isGraph)\r
94         aa[i].height += GRAPH_HEIGHT;\r
95 \r
96       if(aa[i].height==0)\r
97         aa[i].height = 20;\r
98       height += aa[i].height;\r
99     }\r
100   else height=20;\r
101     this.setPreferredSize(new Dimension(1, height));\r
102 \r
103   }\r
104 \r
105   public void addEditableColumn(int i)\r
106   {\r
107     if(activeRow==-1)\r
108     {\r
109       AlignmentAnnotation [] aa = av.alignment.getAlignmentAnnotation();\r
110       for(int j=0; j<aa.length; j++)\r
111         if(aa[j].editable)\r
112         {\r
113           activeRow = j;\r
114           break;\r
115         }\r
116     }\r
117 \r
118     if(activeRes==null)\r
119     {\r
120       activeRes = new ArrayList();\r
121       activeRes.add(String.valueOf(i));\r
122       return;\r
123     }\r
124 \r
125     activeRes.add(String.valueOf(i));\r
126 \r
127   }\r
128 \r
129 \r
130   public void actionPerformed(ActionEvent evt)\r
131   {\r
132 \r
133     AlignmentAnnotation [] aa = av.alignment.getAlignmentAnnotation();\r
134     Annotation [] anot = aa[activeRow].annotations;\r
135 \r
136     if(evt.getActionCommand().equals(REMOVE))\r
137     {\r
138       for(int i=0; i<activeRes.size(); i++)\r
139       {\r
140         anot[Integer.parseInt(activeRes.get(i).toString())] = null;\r
141         anot[Integer.parseInt(activeRes.get(i).toString())] = null;\r
142       }\r
143     }\r
144     else if(evt.getActionCommand().equals(LABEL))\r
145     {\r
146       String label = JOptionPane.showInputDialog(this, "Enter Label ", "Enter label", JOptionPane.QUESTION_MESSAGE );\r
147       if(label==null)\r
148         label = "";\r
149 \r
150       if(label.length()>0 && !aa[activeRow].hasText)\r
151        aa[activeRow].hasText = true;\r
152 \r
153       for(int i=0; i<activeRes.size(); i++)\r
154       {\r
155         int index = Integer.parseInt(activeRes.get(i).toString());\r
156         if(anot[index]==null)\r
157           anot[index] = new Annotation(label, "", ' ',0);\r
158         anot[index].displayCharacter = label;\r
159       }\r
160     }\r
161     else if(evt.getActionCommand().equals(COLOUR))\r
162     {\r
163      Color col = JColorChooser.showDialog(this, "Choose foreground colour", Color.black);\r
164      for (int i = 0; i < activeRes.size(); i++)\r
165      {\r
166        int index = Integer.parseInt(activeRes.get(i).toString());\r
167        if (anot[index] == null)\r
168          anot[index] = new Annotation("", "", ' ', 0);\r
169        anot[index].colour = col;\r
170      }\r
171     }\r
172     else // HELIX OR SHEET\r
173     {\r
174     char type = 0;\r
175     String symbol = "\u03B1";\r
176     if(evt.getActionCommand().equals(HELIX))\r
177        type = 'H';\r
178     else if(evt.getActionCommand().equals(SHEET))\r
179     {\r
180       type = 'E';\r
181       symbol = "\u03B2";\r
182     }\r
183 \r
184     if(!aa[activeRow].hasIcons)\r
185       aa[activeRow].hasIcons = true;\r
186 \r
187 \r
188     String label = JOptionPane.showInputDialog("Enter a label for the structure?", symbol );\r
189     if(label==null)\r
190        label="";\r
191 \r
192     if(label.length()>0 && !aa[activeRow].hasText)\r
193        aa[activeRow].hasText = true;\r
194 \r
195     for(int i=0; i<activeRes.size(); i++)\r
196     {\r
197       int index = Integer.parseInt(activeRes.get(i).toString());\r
198       if (anot[index] == null)\r
199       {\r
200         anot[index] = new Annotation(label, "", type, 0);\r
201       }\r
202 \r
203         anot[ index ].secondaryStructure = type;\r
204         anot[ index ].displayCharacter = label;\r
205     }\r
206     }\r
207 \r
208     adjustPanelHeight();\r
209     activeRes = null;\r
210     repaint();\r
211     return;\r
212 \r
213 \r
214   }\r
215 \r
216   public void mousePressed(MouseEvent evt)\r
217   {\r
218     if (SwingUtilities.isRightMouseButton(evt))\r
219     {\r
220       if(activeRes==null)\r
221         return;\r
222 \r
223       JPopupMenu pop = new JPopupMenu("Structure type");\r
224       JMenuItem item = new JMenuItem(HELIX);\r
225       item.addActionListener(this);\r
226       pop.add(item);\r
227       item = new JMenuItem(SHEET);\r
228       item.addActionListener(this);\r
229       pop.add(item);\r
230       item = new JMenuItem(LABEL);\r
231       item.addActionListener(this);\r
232       pop.add(item);\r
233       item = new JMenuItem(COLOUR);\r
234       item.addActionListener(this);\r
235       pop.add(item);\r
236       item = new JMenuItem(REMOVE);\r
237       item.addActionListener(this);\r
238       pop.add(item);\r
239       pop.show(this, evt.getX(), evt.getY());\r
240       return;\r
241     }\r
242 \r
243 \r
244     AlignmentAnnotation [] aa = av.alignment.getAlignmentAnnotation();\r
245     if(aa==null)\r
246       return;\r
247 \r
248     int height = 0;\r
249     activeRow = -1;\r
250     for(int i=0; i<aa.length; i++)\r
251     {\r
252       height+= aa[i].height;\r
253 \r
254       if(evt.getY()<height)\r
255       {\r
256         if(!aa[i].editable)\r
257         {\r
258           activeRes = null;\r
259           continue;\r
260         }\r
261 \r
262         activeRow = i;\r
263         break;\r
264       }\r
265     }\r
266 \r
267 \r
268     int res = evt.getX() / av.getCharWidth() + av.getStartRes();\r
269 \r
270     if(evt.isControlDown() || evt.isAltDown())\r
271       addEditableColumn(res);\r
272 \r
273     else if(evt.isShiftDown())\r
274     {\r
275 \r
276       if(activeRes==null)\r
277         activeRes=new ArrayList();\r
278       else\r
279       {\r
280           int start =  Integer.parseInt( activeRes.get( activeRes.size()-1 ).toString() );\r
281           int end = res;\r
282           if(end<start)\r
283           {\r
284             int temp = end;\r
285             end = start;\r
286             start = temp;\r
287           }\r
288           for(int n=start; n<=end; n++)\r
289             addEditableColumn(n);\r
290 \r
291       }\r
292     }\r
293     else\r
294     {\r
295         activeRes = new ArrayList();\r
296         activeRes.add( String.valueOf(res) );\r
297     }\r
298 \r
299     repaint();\r
300 \r
301   }\r
302   public void mouseReleased(MouseEvent evt)\r
303   {  }\r
304   public void mouseEntered(MouseEvent evt)\r
305   {  }\r
306   public void mouseExited(MouseEvent evt)\r
307   {  }\r
308   public void mouseDragged(MouseEvent evt)\r
309   {  }\r
310   public void mouseMoved(MouseEvent evt)\r
311   {\r
312     ToolTipManager.sharedInstance().registerComponent(this);\r
313     AlignmentAnnotation [] aa = av.alignment.getAlignmentAnnotation();\r
314     if(aa==null)\r
315       return;\r
316 \r
317     int row = -1;\r
318     int height=0;\r
319     for(int i=0; i<aa.length; i++)\r
320     {\r
321 \r
322       if( aa[i].visible )\r
323         height += aa[i].height;\r
324 \r
325       if(evt.getY()<height)\r
326       {\r
327         row = i;\r
328         break;\r
329       }\r
330     }\r
331 \r
332     int res = evt.getX() / av.getCharWidth() + av.getStartRes();\r
333     if(row>-1 && res<aa[row].annotations.length && aa[row].annotations[res]!=null)\r
334     {\r
335       this.setToolTipText(aa[row].annotations[res].description);\r
336       StringBuffer text = new StringBuffer("Sequence position " + (res + 1) +\r
337                                            "  " +\r
338                                            aa[row].annotations[res].description);\r
339       ap.alignFrame.statusBar.setText(text.toString());\r
340     }\r
341 \r
342 \r
343   }\r
344   public void mouseClicked(MouseEvent evt) {}\r
345 \r
346 \r
347   public void paintComponent(Graphics g)\r
348   {\r
349     g.setColor(Color.white);\r
350     g.fillRect(0,0,getWidth(), getHeight());\r
351 \r
352     if(fastPaint)\r
353     {\r
354       g.drawImage(image, 0, 0, this);\r
355       fastPaint = false;\r
356       return;\r
357     }\r
358 \r
359     imgWidth = (av.endRes-av.startRes+1) *av.charWidth;\r
360 \r
361     image = new BufferedImage(imgWidth,\r
362                                 ap.annotationPanel.getHeight(),\r
363                                 BufferedImage.TYPE_INT_RGB);\r
364       gg = (Graphics2D) image.getGraphics();\r
365       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
366                           RenderingHints.VALUE_ANTIALIAS_ON);\r
367 \r
368       gg.setFont(av.getFont());\r
369       fm = gg.getFontMetrics();\r
370 \r
371     drawComponent( gg, av.startRes, av.endRes+1);\r
372     g.drawImage( image, 0, 0, this);\r
373 \r
374   }\r
375 \r
376   public void fastPaint(int horizontal)\r
377 {\r
378   if( horizontal == 0\r
379      || av.alignment.getAlignmentAnnotation()==null\r
380      || av.alignment.getAlignmentAnnotation().length<1\r
381     )\r
382   {\r
383     repaint();\r
384     return;\r
385   }\r
386 \r
387   gg.copyArea( 0,0, imgWidth, getHeight(), -horizontal*av.charWidth, 0 );\r
388   int sr=av.startRes, er=av.endRes+1, transX=0;\r
389 \r
390   if(horizontal>0) // scrollbar pulled right, image to the left\r
391   {\r
392     transX =  (er-sr-horizontal)*av.charWidth;\r
393     sr = er - horizontal ;\r
394   }\r
395   else if(horizontal<0)\r
396   {\r
397     er = sr-horizontal;\r
398   }\r
399 \r
400 \r
401   gg.translate(transX, 0);\r
402 \r
403   drawComponent(gg, sr, er);\r
404 \r
405   gg.translate( -transX, 0 );\r
406 \r
407   fastPaint = true;\r
408   repaint();\r
409 }\r
410 \r
411 \r
412   public void drawComponent(Graphics2D g, int startRes, int endRes)\r
413   {\r
414     g.setColor(Color.white);\r
415     g.fillRect(0,0,(endRes-startRes) *av.charWidth, getHeight());\r
416     if(av.alignment.getAlignmentAnnotation()==null || av.alignment.getAlignmentAnnotation().length<1)\r
417     {\r
418       g.setColor(Color.white);\r
419       g.fillRect(0,0,getWidth(), getHeight());\r
420       g.setColor(Color.black);\r
421       g.drawString("Alignment has no annotations",20,15);\r
422       return;\r
423     }\r
424 \r
425     AlignmentAnnotation [] aa = av.alignment.getAlignmentAnnotation();\r
426 \r
427     int j, x=0, y=0;\r
428     char [] lastSS = new char[aa.length];\r
429     int  [] lastSSX= new int[aa.length] ;\r
430     int iconOffset = av.charHeight/2;\r
431     boolean validRes = false;\r
432     //\u03B2 \u03B1\r
433 \r
434     for(int i=0; i<aa.length; i++)\r
435     {\r
436       AlignmentAnnotation row = aa[i];\r
437       if(!row.visible)\r
438         continue;\r
439 \r
440       if(row.isGraph)\r
441       {\r
442         // this is so that we draw the characters below the graph\r
443         y += row.height;\r
444         if(row.hasText)\r
445           y -= av.charHeight;\r
446       }\r
447       if(row.hasText)\r
448         iconOffset = av.charHeight/2;\r
449       else\r
450         iconOffset = 0;\r
451 \r
452       for(j=startRes; j<endRes; j++)\r
453       {\r
454         if(row.annotations.length<=j || row.annotations[j]==null)\r
455         validRes = false;\r
456        else\r
457          validRes = true;\r
458 \r
459        x = (j-startRes)*av.charWidth;\r
460 \r
461 \r
462        if(activeRow==i)\r
463        {\r
464 \r
465          g.setColor(Color.red);\r
466 \r
467          if(activeRes!=null)\r
468            for (int n = 0; n < activeRes.size(); n++)\r
469            {\r
470              int v = Integer.parseInt(activeRes.get(n).toString()) ;\r
471              if (v == j)\r
472                g.fillRect( (j-startRes) * av.charWidth, y, av.charWidth, row.height);\r
473            }\r
474        }\r
475 \r
476 \r
477 \r
478        if(validRes && row.annotations[j].displayCharacter.length()>0)\r
479        {\r
480          int charOffset = (av.charWidth - fm.charWidth(row.annotations[j].displayCharacter.charAt(0)))/2;\r
481          g.setColor( row.annotations[j].colour);\r
482           if(j==0)\r
483           {\r
484             if (row.annotations[0].secondaryStructure == 'H'\r
485                 || row.annotations[0].secondaryStructure == 'E')\r
486               g.drawString(row.annotations[j].displayCharacter, x,\r
487                            y + iconOffset + 2);\r
488           }\r
489          else if( (row.annotations[j].secondaryStructure=='H'\r
490                || row.annotations[j].secondaryStructure=='E') &&\r
491                (row.annotations[j-1]==null ||\r
492                 row.annotations[j].secondaryStructure!=row.annotations[j-1].secondaryStructure))\r
493 \r
494         g.drawString(row.annotations[j].displayCharacter, x, y + iconOffset + 2);\r
495 \r
496         if(!row.hasIcons)\r
497             g.drawString(row.annotations[j].displayCharacter, x + charOffset,\r
498                          y + iconOffset + 2);\r
499        }\r
500 \r
501        if(row.hasIcons)\r
502        if(!validRes || row.annotations[j].secondaryStructure!=lastSS[i])\r
503        {\r
504          switch (lastSS[i])\r
505          {\r
506            case 'H':\r
507              g.setColor(HELIX_COLOUR);\r
508              g.fillRoundRect(lastSSX[i], y+4 + iconOffset, x-lastSSX[i], 7, 8, 8);\r
509              break;\r
510            case 'E':\r
511              g.setColor(SHEET_COLOUR);\r
512              g.fillRect(lastSSX[i], y + 4 + iconOffset, x-lastSSX[i]-4, 7);\r
513              g.fillPolygon(new int[] {x - 4, x- 4, x }\r
514                            , new int[]{y+ iconOffset, y + 14+ iconOffset, y + 8+ iconOffset}, 3);\r
515              break;\r
516            case 'C':\r
517              break;\r
518            default :\r
519              g.setColor(Color.gray);\r
520              g.fillRect(lastSSX[i], y+6+ iconOffset, x-lastSSX[i], 2);\r
521              break;\r
522          }\r
523 \r
524          if(validRes)\r
525            lastSS[i] = row.annotations[j].secondaryStructure;\r
526           else\r
527             lastSS[i] = ' ';\r
528          lastSSX[i] = x;\r
529        }\r
530 \r
531        if (validRes && row.isGraph)\r
532        {\r
533          g.setColor(new Color(0,0,180));\r
534          int height = (int)((row.annotations[j].value / row.graphMax)*GRAPH_HEIGHT);\r
535 \r
536          if(row.windowLength>1)\r
537          {\r
538            int total =0;\r
539            for(int i2=j- (row.windowLength/2); i2<j+(row.windowLength/2); i2++)\r
540            {\r
541              if(i2<0 || i2>=av.alignment.getWidth())\r
542                continue;\r
543 \r
544              total += row.annotations[i2].value;\r
545            }\r
546 \r
547            total/=row.windowLength;\r
548            height = (int)( (total / row.graphMax) *GRAPH_HEIGHT);\r
549 \r
550          }\r
551          g.setColor(row.annotations[j].colour);\r
552          g.fillRect(x, y-height, av.charWidth, height );\r
553        }\r
554 \r
555 \r
556       }\r
557 \r
558       x+=av.charWidth;\r
559 \r
560       if(row.hasIcons)\r
561       switch (lastSS[i])\r
562       {\r
563         case 'H':\r
564           g.setColor(HELIX_COLOUR);\r
565           g.fillRoundRect(lastSSX[i], y+4+ iconOffset, x - lastSSX[i], 7, 8, 8);\r
566           break;\r
567         case 'E':\r
568           g.setColor(SHEET_COLOUR);\r
569           g.fillRect(lastSSX[i], y + 4+ iconOffset, x - lastSSX[i] - 4, 7);\r
570           g.fillPolygon(new int[]\r
571                         {x - 4, x - 4, x}\r
572                         , new int[]\r
573                         {y + iconOffset, y + 14+ iconOffset, y + 7+ iconOffset}\r
574                         , 3);\r
575           break;\r
576         case 'C':\r
577           break;\r
578         default:\r
579           g.setColor(Color.gray);\r
580           g.fillRect(lastSSX[i], y+6+ iconOffset, x-lastSSX[i], 2);\r
581           break;\r
582 \r
583       }\r
584 \r
585        if(row.isGraph && row.hasText)\r
586          y+=av.charHeight;\r
587        if(!row.isGraph)\r
588           y+=aa[i].height;\r
589     }\r
590   }\r
591 \r
592   // used by overview window\r
593   public void drawGraph(Graphics g, AlignmentAnnotation aa,int width, int y)\r
594   {\r
595     g.setColor(Color.white);\r
596     g.fillRect(0,0,width, y);\r
597     g.setColor(new Color(0,0,180));\r
598     int x = 0;\r
599     for(int j=0; j<aa.annotations.length; j++)\r
600     {\r
601       g.setColor(new Color(0, 0, 180));\r
602       int height = (int) ( (aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT );\r
603       g.fillRect(x, y - height, av.charWidth, height);\r
604       x+=av.charWidth;\r
605     }\r
606   }\r
607 }\r