check for null residues
[jalview.git] / src / jalview / appletgui / AnnotationPanel.java
1 package jalview.appletgui;\r
2 \r
3 import jalview.datamodel.*;\r
4 \r
5 import java.awt.*;\r
6 import java.awt.event.*;\r
7 import java.util.*;\r
8 \r
9 public class AnnotationPanel extends Panel implements AdjustmentListener\r
10 {\r
11   AlignViewport av;\r
12   AlignmentPanel ap;\r
13   int activeRow =-1;\r
14 \r
15   Vector activeRes;\r
16   static String HELIX ="Helix";\r
17   static String SHEET ="Sheet";\r
18   static String LABEL ="Label";\r
19   static String REMOVE="Remove Annotation";\r
20   static String COLOUR="Colour";\r
21   static Color HELIX_COLOUR = Color.red.darker();\r
22   static Color SHEET_COLOUR = Color.green.darker().darker();\r
23 \r
24 \r
25   Image image;\r
26   Graphics gg;\r
27   FontMetrics fm;\r
28   int imgWidth=0;\r
29 \r
30   boolean fastPaint = false;\r
31 \r
32   public static int GRAPH_HEIGHT = 40;\r
33 \r
34 \r
35 \r
36   public AnnotationPanel(AlignmentPanel ap)\r
37   {\r
38     this.ap = ap;\r
39     av = ap.av;\r
40     this.setLayout(null);\r
41     adjustPanelHeight();\r
42 \r
43     addMouseMotionListener(new MouseMotionAdapter()\r
44     {public void mouseMoved(MouseEvent evt)\r
45           { doMouseMoved(evt); }\r
46     });\r
47 \r
48    // ap.annotationScroller.getVAdjustable().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 \r
83    this.setSize(getSize().width, height);\r
84    ap.annotationScroller.setSize(getSize().width, height);\r
85 \r
86 //  ap.annotationSpaceFillerHolder.setSize(d.width,annotationPanel.getSize().height);\r
87 \r
88    repaint();\r
89 \r
90   }\r
91 \r
92   public void addEditableColumn(int i)\r
93   {\r
94     if(activeRow==-1)\r
95     {\r
96       AlignmentAnnotation [] aa = av.alignment.getAlignmentAnnotation();\r
97       for(int j=0; j<aa.length; j++)\r
98         if(aa[j].editable)\r
99         {\r
100           activeRow = j;\r
101           break;\r
102         }\r
103     }\r
104 \r
105     if(activeRes==null)\r
106     {\r
107       activeRes = new Vector();\r
108       activeRes.addElement(String.valueOf(i));\r
109       return;\r
110     }\r
111 \r
112     activeRes.addElement(String.valueOf(i));\r
113   }\r
114 \r
115  public void doMouseMoved(MouseEvent evt)\r
116  {\r
117    AlignmentAnnotation [] aa = av.alignment.getAlignmentAnnotation();\r
118    if(aa==null)\r
119      return;\r
120 \r
121    int row = -1;\r
122    int height=0;\r
123    for(int i=0; i<aa.length; i++)\r
124    {\r
125 \r
126      if( aa[i].visible )\r
127        height += aa[i].height;\r
128 \r
129      if(evt.getY()<height)\r
130      {\r
131        row = i;\r
132        break;\r
133      }\r
134    }\r
135 \r
136    int res = evt.getX() / av.getCharWidth() + av.getStartRes();\r
137    if(row>-1 && res<aa[row].annotations.length && aa[row].annotations[res]!=null)\r
138    {\r
139      StringBuffer text = new StringBuffer("Sequence position " + (res + 1) +\r
140                                           "  " +\r
141                                           aa[row].annotations[res].description);\r
142      ap.alignFrame.statusBar.setText(text.toString());\r
143    }\r
144  }\r
145 \r
146 \r
147 \r
148   public void update(Graphics g)\r
149   {\r
150     paint(g);\r
151   }\r
152 \r
153   public void paint(Graphics g)\r
154   {\r
155     imgWidth = (av.endRes-av.startRes+1) *av.charWidth;\r
156 \r
157     if(image==null || imgWidth != image.getWidth(this))\r
158       {\r
159         image = createImage(imgWidth, ap.annotationPanel.getSize().height);\r
160         gg = image.getGraphics();\r
161         gg.setFont(av.getFont());\r
162         fm = gg.getFontMetrics();\r
163         fastPaint = false;\r
164     }\r
165 \r
166     if(fastPaint)\r
167     {\r
168       g.drawImage(image, 0, 0, this);\r
169       fastPaint = false;\r
170       return;\r
171     }\r
172 \r
173     drawComponent( gg, av.startRes, av.endRes+1);\r
174     g.drawImage( image, 0, 0, this);\r
175 \r
176   }\r
177 \r
178   public void fastPaint(int horizontal)\r
179 {\r
180   if( horizontal == 0\r
181      || av.alignment.getAlignmentAnnotation()==null\r
182      || av.alignment.getAlignmentAnnotation().length<1\r
183     )\r
184   {\r
185     repaint();\r
186     return;\r
187   }\r
188 \r
189   gg.copyArea( 0,0, imgWidth, getSize().height, -horizontal*av.charWidth, 0 );\r
190   int sr=av.startRes, er=av.endRes+1, transX=0;\r
191 \r
192   if(horizontal>0) // scrollbar pulled right, image to the left\r
193   {\r
194     transX =  (er-sr-horizontal)*av.charWidth;\r
195     sr = er - horizontal ;\r
196   }\r
197   else if(horizontal<0)\r
198   {\r
199     er = sr-horizontal;\r
200   }\r
201 \r
202 \r
203   gg.translate(transX, 0);\r
204 \r
205   drawComponent(gg, sr, er);\r
206 \r
207   gg.translate( -transX, 0 );\r
208 \r
209   fastPaint = true;\r
210   repaint();\r
211 }\r
212 \r
213 \r
214   public void drawComponent(Graphics g, int startRes, int endRes)\r
215   {\r
216     g.setColor(Color.white);\r
217     g.fillRect(0,0,(endRes-startRes) *av.charWidth, getSize().height);\r
218     if(av.alignment.getAlignmentAnnotation()==null || av.alignment.getAlignmentAnnotation().length<1)\r
219     {\r
220       g.setColor(Color.white);\r
221       g.fillRect(0,0,getSize().width, getSize().height);\r
222       g.setColor(Color.black);\r
223       g.drawString("Alignment has no annotations",20,15);\r
224       return;\r
225     }\r
226 \r
227     AlignmentAnnotation [] aa = av.alignment.getAlignmentAnnotation();\r
228 \r
229     int j, x=0, y=0;\r
230     char [] lastSS = new char[aa.length];\r
231     int  [] lastSSX= new int[aa.length] ;\r
232     int iconOffset = av.charHeight/2;\r
233     boolean validRes = false;\r
234     //\u03B2 \u03B1\r
235 \r
236     for(int i=0; i<aa.length; i++)\r
237     {\r
238       AlignmentAnnotation row = aa[i];\r
239       if(!row.visible)\r
240         continue;\r
241 \r
242       if(row.isGraph)\r
243       {\r
244         // this is so that we draw the characters below the graph\r
245         y += row.height;\r
246         if(row.hasText)\r
247           y -= av.charHeight;\r
248       }\r
249       if(row.hasText)\r
250         iconOffset = av.charHeight/2;\r
251       else\r
252         iconOffset = 0;\r
253 \r
254       for(j=startRes; j<endRes; j++)\r
255       {\r
256         if(row.annotations.length<=j || row.annotations[j]==null)\r
257         validRes = false;\r
258        else\r
259          validRes = true;\r
260 \r
261        x = (j-startRes)*av.charWidth;\r
262 \r
263 \r
264        if(activeRow==i)\r
265        {\r
266 \r
267          g.setColor(Color.red);\r
268 \r
269          if(activeRes!=null)\r
270            for (int n = 0; n < activeRes.size(); n++)\r
271            {\r
272              int v = Integer.parseInt(activeRes.elementAt(n).toString()) ;\r
273              if (v == j)\r
274                g.fillRect( (j-startRes) * av.charWidth, y, av.charWidth, row.height);\r
275            }\r
276        }\r
277 \r
278 \r
279 \r
280        if(validRes && row.annotations[j].displayCharacter.length()>0)\r
281        {\r
282          int charOffset = (av.charWidth - fm.charWidth(row.annotations[j].displayCharacter.charAt(0)))/2;\r
283          g.setColor( row.annotations[j].colour);\r
284           if(j==0)\r
285           {\r
286             if (row.annotations[0].secondaryStructure == 'H'\r
287                 || row.annotations[0].secondaryStructure == 'E')\r
288               g.drawString(row.annotations[j].displayCharacter, x,\r
289                            y + iconOffset + 2);\r
290           }\r
291          else if( (row.annotations[j].secondaryStructure=='H'\r
292                || row.annotations[j].secondaryStructure=='E') &&\r
293                (row.annotations[j-1]==null ||\r
294                 row.annotations[j].secondaryStructure!=row.annotations[j-1].secondaryStructure))\r
295 \r
296         g.drawString(row.annotations[j].displayCharacter, x, y + iconOffset + 2);\r
297 \r
298         if(!row.hasIcons)\r
299             g.drawString(row.annotations[j].displayCharacter, x + charOffset,\r
300                          y + iconOffset + 2);\r
301        }\r
302 \r
303        if(row.hasIcons)\r
304        if(!validRes || row.annotations[j].secondaryStructure!=lastSS[i])\r
305        {\r
306          switch (lastSS[i])\r
307          {\r
308            case 'H':\r
309              g.setColor(HELIX_COLOUR);\r
310              g.fillRoundRect(lastSSX[i], y+4 + iconOffset, x-lastSSX[i], 7, 8, 8);\r
311              break;\r
312            case 'E':\r
313              g.setColor(SHEET_COLOUR);\r
314              g.fillRect(lastSSX[i], y + 4 + iconOffset, x-lastSSX[i]-4, 7);\r
315              g.fillPolygon(new int[] {x - 4, x- 4, x }\r
316                            , new int[]{y+ iconOffset, y + 14+ iconOffset, y + 8+ iconOffset}, 3);\r
317              break;\r
318            case 'C':\r
319              break;\r
320            default :\r
321              g.setColor(Color.gray);\r
322              g.fillRect(lastSSX[i], y+6+ iconOffset, x-lastSSX[i], 2);\r
323              break;\r
324          }\r
325 \r
326          if(validRes)\r
327            lastSS[i] = row.annotations[j].secondaryStructure;\r
328           else\r
329             lastSS[i] = ' ';\r
330          lastSSX[i] = x;\r
331        }\r
332 \r
333        if (validRes && row.isGraph)\r
334        {\r
335          g.setColor(new Color(0,0,180));\r
336          int height = (int)((row.annotations[j].value / row.graphMax)*GRAPH_HEIGHT);\r
337 \r
338          if(row.windowLength>1)\r
339          {\r
340            int total =0;\r
341            for(int i2=j- (row.windowLength/2); i2<j+(row.windowLength/2); i2++)\r
342            {\r
343              if(i2<0 || i2>=av.alignment.getWidth())\r
344                continue;\r
345 \r
346              total += row.annotations[i2].value;\r
347            }\r
348 \r
349            total/=row.windowLength;\r
350            height = (int)( (total / row.graphMax) *GRAPH_HEIGHT);\r
351 \r
352          }\r
353          g.setColor(row.annotations[j].colour);\r
354          g.fillRect(x, y-height, av.charWidth, height );\r
355        }\r
356 \r
357 \r
358       }\r
359 \r
360       x+=av.charWidth;\r
361 \r
362       if(row.hasIcons)\r
363       switch (lastSS[i])\r
364       {\r
365         case 'H':\r
366           g.setColor(HELIX_COLOUR);\r
367           g.fillRoundRect(lastSSX[i], y+4+ iconOffset, x - lastSSX[i], 7, 8, 8);\r
368           break;\r
369         case 'E':\r
370           g.setColor(SHEET_COLOUR);\r
371           g.fillRect(lastSSX[i], y + 4+ iconOffset, x - lastSSX[i] - 4, 7);\r
372           g.fillPolygon(new int[]\r
373                         {x - 4, x - 4, x}\r
374                         , new int[]\r
375                         {y + iconOffset, y + 14+ iconOffset, y + 7+ iconOffset}\r
376                         , 3);\r
377           break;\r
378         case 'C':\r
379           break;\r
380         default:\r
381           g.setColor(Color.gray);\r
382           g.fillRect(lastSSX[i], y+6+ iconOffset, x-lastSSX[i], 2);\r
383           break;\r
384 \r
385       }\r
386 \r
387        if(row.isGraph && row.hasText)\r
388          y+=av.charHeight;\r
389        if(!row.isGraph)\r
390           y+=aa[i].height;\r
391     }\r
392   }\r
393 \r
394   // used by overview window\r
395   public void drawGraph(Graphics g, AlignmentAnnotation aa,int width, int y)\r
396   {\r
397     g.setColor(Color.white);\r
398     g.fillRect(0,0,width, y);\r
399     g.setColor(new Color(0,0,180));\r
400     int x = 0;\r
401     for(int j=0; j<aa.annotations.length; j++)\r
402     {\r
403       g.setColor(new Color(0, 0, 180));\r
404       int height = (int) ( (aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT );\r
405       g.fillRect(x, y - height, av.charWidth, height);\r
406       x+=av.charWidth;\r
407     }\r
408   }\r
409 }\r