scroller is now a panel, removes border
[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         validRes = row.annotations[j]==null?false:true;\r
257 \r
258        x = (j-startRes)*av.charWidth;\r
259 \r
260 \r
261        if(activeRow==i)\r
262        {\r
263 \r
264          g.setColor(Color.red);\r
265 \r
266          if(activeRes!=null)\r
267            for (int n = 0; n < activeRes.size(); n++)\r
268            {\r
269              int v = Integer.parseInt(activeRes.elementAt(n).toString()) ;\r
270              if (v == j)\r
271                g.fillRect( (j-startRes) * av.charWidth, y, av.charWidth, row.height);\r
272            }\r
273        }\r
274 \r
275 \r
276 \r
277        if(validRes && row.annotations[j].displayCharacter.length()>0)\r
278        {\r
279          int charOffset = (av.charWidth - fm.charWidth(row.annotations[j].displayCharacter.charAt(0)))/2;\r
280          g.setColor( row.annotations[j].colour);\r
281           if(j==0)\r
282           {\r
283             if (row.annotations[0].secondaryStructure == 'H'\r
284                 || row.annotations[0].secondaryStructure == 'E')\r
285               g.drawString(row.annotations[j].displayCharacter, x,\r
286                            y + iconOffset + 2);\r
287           }\r
288          else if( (row.annotations[j].secondaryStructure=='H'\r
289                || row.annotations[j].secondaryStructure=='E') &&\r
290                (row.annotations[j-1]==null ||\r
291                 row.annotations[j].secondaryStructure!=row.annotations[j-1].secondaryStructure))\r
292 \r
293         g.drawString(row.annotations[j].displayCharacter, x, y + iconOffset + 2);\r
294 \r
295         if(!row.hasIcons)\r
296             g.drawString(row.annotations[j].displayCharacter, x + charOffset,\r
297                          y + iconOffset + 2);\r
298        }\r
299 \r
300        if(row.hasIcons)\r
301        if(!validRes || row.annotations[j].secondaryStructure!=lastSS[i])\r
302        {\r
303          switch (lastSS[i])\r
304          {\r
305            case 'H':\r
306              g.setColor(HELIX_COLOUR);\r
307              g.fillRoundRect(lastSSX[i], y+4 + iconOffset, x-lastSSX[i], 7, 8, 8);\r
308              break;\r
309            case 'E':\r
310              g.setColor(SHEET_COLOUR);\r
311              g.fillRect(lastSSX[i], y + 4 + iconOffset, x-lastSSX[i]-4, 7);\r
312              g.fillPolygon(new int[] {x - 4, x- 4, x }\r
313                            , new int[]{y+ iconOffset, y + 14+ iconOffset, y + 8+ iconOffset}, 3);\r
314              break;\r
315            case 'C':\r
316              break;\r
317            default :\r
318              g.setColor(Color.gray);\r
319              g.fillRect(lastSSX[i], y+6+ iconOffset, x-lastSSX[i], 2);\r
320              break;\r
321          }\r
322 \r
323          if(validRes)\r
324            lastSS[i] = row.annotations[j].secondaryStructure;\r
325           else\r
326             lastSS[i] = ' ';\r
327          lastSSX[i] = x;\r
328        }\r
329 \r
330        if (validRes && row.isGraph)\r
331        {\r
332          g.setColor(new Color(0,0,180));\r
333          int height = (int)((row.annotations[j].value / row.graphMax)*GRAPH_HEIGHT);\r
334 \r
335          if(row.windowLength>1)\r
336          {\r
337            int total =0;\r
338            for(int i2=j- (row.windowLength/2); i2<j+(row.windowLength/2); i2++)\r
339            {\r
340              if(i2<0 || i2>=av.alignment.getWidth())\r
341                continue;\r
342 \r
343              total += row.annotations[i2].value;\r
344            }\r
345 \r
346            total/=row.windowLength;\r
347            height = (int)( (total / row.graphMax) *GRAPH_HEIGHT);\r
348 \r
349          }\r
350          g.setColor(row.annotations[j].colour);\r
351          g.fillRect(x, y-height, av.charWidth, height );\r
352        }\r
353 \r
354 \r
355       }\r
356 \r
357       x+=av.charWidth;\r
358 \r
359       if(row.hasIcons)\r
360       switch (lastSS[i])\r
361       {\r
362         case 'H':\r
363           g.setColor(HELIX_COLOUR);\r
364           g.fillRoundRect(lastSSX[i], y+4+ iconOffset, x - lastSSX[i], 7, 8, 8);\r
365           break;\r
366         case 'E':\r
367           g.setColor(SHEET_COLOUR);\r
368           g.fillRect(lastSSX[i], y + 4+ iconOffset, x - lastSSX[i] - 4, 7);\r
369           g.fillPolygon(new int[]\r
370                         {x - 4, x - 4, x}\r
371                         , new int[]\r
372                         {y + iconOffset, y + 14+ iconOffset, y + 7+ iconOffset}\r
373                         , 3);\r
374           break;\r
375         case 'C':\r
376           break;\r
377         default:\r
378           g.setColor(Color.gray);\r
379           g.fillRect(lastSSX[i], y+6+ iconOffset, x-lastSSX[i], 2);\r
380           break;\r
381 \r
382       }\r
383 \r
384        if(row.isGraph && row.hasText)\r
385          y+=av.charHeight;\r
386        if(!row.isGraph)\r
387           y+=aa[i].height;\r
388     }\r
389   }\r
390 \r
391   // used by overview window\r
392   public void drawGraph(Graphics g, AlignmentAnnotation aa,int width, int y)\r
393   {\r
394     g.setColor(Color.white);\r
395     g.fillRect(0,0,width, y);\r
396     g.setColor(new Color(0,0,180));\r
397     int x = 0;\r
398     for(int j=0; j<aa.annotations.length; j++)\r
399     {\r
400       g.setColor(new Color(0, 0, 180));\r
401       int height = (int) ( (aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT );\r
402       g.fillRect(x, y - height, av.charWidth, height);\r
403       x+=av.charWidth;\r
404     }\r
405   }\r
406 }\r