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