if image is null, repaint
[jalview.git] / src / jalview / gui / SeqCanvas.java
1 package jalview.gui;\r
2 \r
3 import java.awt.*;\r
4 import javax.swing.*;\r
5 import jalview.datamodel.*;\r
6 import jalview.analysis.*;\r
7 \r
8 \r
9 public class SeqCanvas extends JPanel\r
10 {\r
11      FeatureRenderer fr;\r
12     Image             img;\r
13     Graphics2D          gg;\r
14     int               imgWidth;\r
15     int               imgHeight;\r
16 \r
17     AlignViewport     av;\r
18 \r
19     boolean showScores = false;\r
20     boolean displaySearch = false;\r
21     int [] searchResults = null;\r
22 \r
23     int chunkHeight;\r
24     int chunkWidth;\r
25 \r
26     boolean fastPaint = false;\r
27 \r
28 \r
29     public SeqCanvas(AlignViewport av)\r
30     {\r
31         this.av         = av;\r
32        fr = new FeatureRenderer(av);\r
33        setLayout(new BorderLayout());\r
34        PaintRefresher.Register(this);\r
35 \r
36     }\r
37 \r
38   void drawNorthScale(Graphics g, int startx, int endx,int ypos) {\r
39     int scalestartx = startx - startx % 10 + 10;\r
40 \r
41     g.setColor(Color.black);\r
42 \r
43     // NORTH SCALE\r
44     for (int i = scalestartx; i < endx; i += 10)\r
45     {\r
46       String string = String.valueOf(i);\r
47       g.drawString(string, (i - startx - 1) * av.charWidth,\r
48                    ypos - av.charHeight / 2);\r
49 \r
50       g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,\r
51                  ypos + 2 - av.charHeight / 2,\r
52                  (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);\r
53 \r
54     }\r
55   }\r
56 \r
57   void drawWestScale(Graphics g, int startx, int endx, int ypos)\r
58   {\r
59     FontMetrics fm = getFontMetrics(av.getFont());\r
60     ypos+= av.charHeight;\r
61       // EAST SCALE\r
62     for (int i = 0; i < av.alignment.getHeight(); i++)\r
63     {\r
64       SequenceI seq = av.alignment.getSequenceAt(i);\r
65       int index = startx;\r
66       int value = -1;\r
67       while (index < endx)\r
68       {\r
69         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))\r
70         {\r
71           index++;\r
72           continue;\r
73         }\r
74 \r
75         value = av.alignment.getSequenceAt(i).findPosition(index);\r
76         break;\r
77       }\r
78       if(value!=-1)\r
79       {\r
80         int x = fm.stringWidth("000") - fm.stringWidth(value+"");\r
81         g.drawString(value + "", x,  ypos +  i*av.charHeight - av.charHeight/5);\r
82       }\r
83     }\r
84   }\r
85 \r
86   void drawEastScale(Graphics g, int startx, int endx, int ypos)\r
87 {\r
88     ypos+= av.charHeight;\r
89     // EAST SCALE\r
90   for (int i = 0; i < av.alignment.getHeight(); i++)\r
91   {\r
92     SequenceI seq = av.alignment.getSequenceAt(i);\r
93     int index = endx;\r
94     int value = -1;\r
95     while (index > startx)\r
96     {\r
97       if (jalview.util.Comparison.isGap(seq.getCharAt(index)))\r
98       {\r
99         index--;\r
100         continue;\r
101       }\r
102 \r
103       value = av.alignment.getSequenceAt(i).findPosition(index);\r
104       break;\r
105     }\r
106     if(value!=-1)\r
107        g.drawString(value + "", 0,  ypos +  i*av.charHeight - av.charHeight/5);\r
108   }\r
109 \r
110 }\r
111 \r
112 \r
113 \r
114 \r
115 \r
116 public void fastPaint(int horizontal, int vertical)\r
117 {\r
118     if (horizontal == 0 && vertical == 0 || gg==null)\r
119       return;\r
120 \r
121     gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,\r
122                 -vertical * av.charHeight);\r
123 \r
124     int sr = av.startRes, er = av.endRes + 1, ss = av.startSeq, es = av.endSeq,\r
125         transX = 0, transY = 0;\r
126     if (horizontal > 0) // scrollbar pulled right, image to the left\r
127     {\r
128       transX = (er - sr - horizontal) * av.charWidth;\r
129       sr = er - horizontal;\r
130     }\r
131     else if (horizontal < 0)\r
132       er = sr - horizontal;\r
133 \r
134     else if (vertical > 0) // scroll down\r
135     {\r
136       transY = imgHeight - vertical * av.charHeight;\r
137       ss = es - vertical;\r
138     }\r
139     else if (vertical < 0)\r
140       es = ss - vertical;\r
141 \r
142     gg.translate(transX, transY);\r
143 \r
144     gg.setColor(Color.white);\r
145     gg.fillRect(0,0, (er-sr)*av.charWidth, (es-ss)*av.charHeight);\r
146     drawPanel(gg, sr, er, ss, es, sr, ss, 0);\r
147     gg.translate( -transX, -transY);\r
148 \r
149     fastPaint = true;\r
150     repaint();\r
151 \r
152 }\r
153 \r
154 /**\r
155  * Definitions of startx and endx (hopefully):\r
156  * SMJS This is what I'm working towards!\r
157  *   startx is the first residue (starting at 0) to display.\r
158  *   endx   is the last residue to display (starting at 0).\r
159  *   starty is the first sequence to display (starting at 0).\r
160  *   endy   is the last sequence to display (starting at 0).\r
161  * NOTE 1: The av limits are set in setFont in this class and\r
162  * in the adjustment listener in SeqPanel when the scrollbars move.\r
163  */\r
164 \r
165   public void paintComponent(Graphics g)\r
166   {\r
167     g.setColor(Color.white);\r
168     g.fillRect(0, 0, getWidth(), getHeight());\r
169 \r
170     if (fastPaint)\r
171     {\r
172       g.drawImage(img, 0, 0, this);\r
173       fastPaint = false;\r
174       return;\r
175     }\r
176 \r
177     // this draws the whole of the alignment\r
178       imgWidth  = getWidth();\r
179       imgHeight = getHeight();\r
180       if(imgWidth==0 || imgHeight==0)\r
181         return;\r
182 \r
183       imgWidth -= imgWidth%av.charWidth;\r
184       imgHeight-= imgHeight%av.charHeight;\r
185 \r
186       img = createImage(imgWidth,imgHeight);\r
187       gg  = (Graphics2D)img.getGraphics();\r
188       gg.setFont(av.getFont());\r
189       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);\r
190 \r
191       gg.setColor(Color.white);\r
192       gg.fillRect(0,0,imgWidth,imgHeight);\r
193 \r
194     chunkWidth  =   getWrappedCanvasWidth( getWidth() );\r
195     chunkHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
196 \r
197     av.setChunkHeight(chunkHeight);\r
198     av.setChunkWidth(chunkWidth);\r
199 \r
200 \r
201     if (av.getWrapAlignment())\r
202       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);\r
203     else\r
204       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);\r
205 \r
206     g.drawImage(img, 0, 0, this);\r
207 \r
208   }\r
209 \r
210   public int getWrappedCanvasWidth(int cwidth)\r
211   {\r
212     FontMetrics fm = getFontMetrics(av.getFont());\r
213 \r
214     int LABEL_EAST = 0;\r
215     if(av.scaleRightWrapped)\r
216       LABEL_EAST = fm.stringWidth( av.alignment.getWidth()+"000" );\r
217     int LABEL_WEST = 0;\r
218     if(av.scaleLeftWrapped)\r
219       LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"0" );\r
220 \r
221     return  (cwidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
222   }\r
223 \r
224   public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)\r
225   {\r
226       AlignmentI al = av.getAlignment();\r
227 \r
228       FontMetrics fm = getFontMetrics(av.getFont());\r
229 \r
230       int LABEL_EAST = 0;\r
231       if(av.scaleRightWrapped)\r
232         LABEL_EAST = fm.stringWidth( al.getWidth()+"000" );\r
233       int LABEL_WEST = 0;\r
234       if(av.scaleLeftWrapped)\r
235         LABEL_WEST = fm.stringWidth( al.getWidth()+"0" );\r
236 \r
237       int cWidth  =   (canvasWidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
238       int cHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
239 \r
240       int  endx   = startRes+cWidth-1;\r
241       int  ypos  = 2*av.charHeight;\r
242 \r
243 \r
244       while (ypos <= canvasHeight && endx <al.getWidth())\r
245       {\r
246         g.setColor(Color.black);\r
247 \r
248         if(av.scaleLeftWrapped)\r
249           drawWestScale(g, startRes, endx, ypos);\r
250 \r
251         if(av.scaleRightWrapped)\r
252         {\r
253           g.translate(canvasWidth - LABEL_EAST +av.charWidth, 0);\r
254           drawEastScale(g, startRes, endx, ypos);\r
255           g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);\r
256         }\r
257 \r
258 \r
259         g.translate(LABEL_WEST,0);\r
260         if(av.scaleAboveWrapped)\r
261           drawNorthScale(g, startRes, endx, ypos);\r
262 \r
263         drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);\r
264         g.translate(-LABEL_WEST,0);\r
265 \r
266         ypos += cHeight;\r
267         startRes += cWidth;\r
268         endx = startRes + cWidth - 1;\r
269 \r
270         if (endx > al.getWidth())\r
271           endx = al.getWidth();\r
272       }\r
273 \r
274   }\r
275 \r
276 \r
277   public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
278 \r
279     Graphics2D g = (Graphics2D)g1;\r
280     g.setFont(av.getFont());\r
281     RendererI sr = av.getRenderer();\r
282 \r
283     /*Vector    pid    = av.getConsensus(false);\r
284     Vector tmpseq = new Vector();\r
285     for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
286         if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))\r
287             tmpseq.addElement(av.getAlignment().getSequenceAt(i));\r
288 \r
289     if (sr instanceof SequenceRenderer)\r
290         pid    = AAFrequency.calculate(tmpseq,x1,x2);\r
291 \r
292     else if (sr instanceof GraphRenderer)\r
293         pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),\r
294                                        av.getAlignment().getSequences(),\r
295                                        av.getPIDWindow(),x1,x2);\r
296 \r
297 */\r
298 \r
299    /* if (y2 > starty && y1 < av.getEndSeq())\r
300     {\r
301        fillBackground(g,\r
302                    Color.red,\r
303                    (x1-startx)*charWidth,\r
304                    offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),\r
305                    (x2-x1+1)*charWidth,\r
306                    offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));\r
307     }*/\r
308 \r
309     SequenceI nextSeq;\r
310 \r
311     /// First draw the sequences\r
312     /////////////////////////////\r
313     for (int i = y1 ; i < y2 ;i++)\r
314     {\r
315      nextSeq = av.alignment.getSequenceAt(i);\r
316 \r
317      sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,\r
318                  (x1 - startx) * av.charWidth,\r
319                  offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
320                  av.charWidth,av.charHeight,null, i);\r
321 \r
322      if(av.showSequenceFeatures)\r
323      {\r
324        fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,\r
325                        (x1 - startx) * av.charWidth,\r
326                        offset +\r
327                        AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
328                        av.charWidth, av.charHeight, null, i);\r
329      }\r
330     }\r
331     //\r
332     /////////////////////////////////////\r
333 \r
334     // Now outline any areas if necessary\r
335     /////////////////////////////////////\r
336     SequenceGroup group = av.getSelectionGroup();\r
337     java.util.Vector groups = av.alignment.getGroups();\r
338 \r
339     int sx = -1, sy = -1, ex = -1;\r
340     int groupIndex = -1;\r
341     if (group == null && groups.size() > 0)\r
342     {\r
343       group = (SequenceGroup) groups.elementAt(0);\r
344       groupIndex = 0;\r
345     }\r
346 \r
347     if (group != null)\r
348       do\r
349       {\r
350         int oldY = -1;\r
351         int i = 0;\r
352         boolean inGroup = false;\r
353         int top=-1, bottom =-1;\r
354         for (i = y1; i < y2; i++)\r
355         {\r
356           sx = (group.getStartRes() - startx) * av.charWidth;\r
357           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
358           ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth;\r
359 \r
360           if (av.getWrapAlignment())\r
361           {\r
362             if (sx < 0)\r
363               sx = 0;\r
364 \r
365             if (ex > getWidth())\r
366             {\r
367               if (av.getWrapAlignment())\r
368                 ex = getWrappedCanvasWidth(getWidth()) * av.charWidth;\r
369 \r
370             }\r
371           }\r
372           else if(ex>getWidth())\r
373             ex -= av.charWidth;\r
374 \r
375 \r
376           if (sx < getWidth()\r
377               && ex > 0\r
378               && group.sequences.contains(av.alignment.getSequenceAt(i)))\r
379           {\r
380             if (bottom == -1 &&\r
381                 !group.sequences.contains(av.alignment.getSequenceAt(i + 1)))\r
382               bottom = sy + av.charHeight ;\r
383 \r
384             if (!inGroup)\r
385             {\r
386               if (top == -1 && i==0 ||\r
387                   !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))\r
388                 top = sy;\r
389 \r
390 \r
391               oldY = sy;\r
392               inGroup = true;\r
393               if (group == av.getSelectionGroup())\r
394               {\r
395                 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,3f}, 0f ));\r
396                 g.setColor(Color.RED);\r
397               }\r
398               else\r
399               {\r
400                 g.setStroke(new BasicStroke());\r
401                 g.setColor(group.getOutlineColour());\r
402               }\r
403             }\r
404           }\r
405           else\r
406           {\r
407             if (inGroup)\r
408             {\r
409               g.drawLine(sx, oldY, sx, sy );\r
410               g.drawLine(sx+ex, oldY, sx+ex, sy );\r
411 \r
412               if (top != -1)\r
413               {\r
414                 g.drawLine(sx, top, sx + ex, top);\r
415                 top =-1;\r
416               }\r
417               if (bottom != -1)\r
418               {\r
419                 g.drawLine(sx, bottom, sx + ex, bottom);\r
420                 bottom = -1;\r
421               }\r
422 \r
423 \r
424               inGroup = false;\r
425             }\r
426           }\r
427         }\r
428 \r
429         if (inGroup)\r
430         {\r
431 \r
432           if(top!=-1)\r
433           {\r
434             g.drawLine(sx, top, sx + ex, top);\r
435             top =-1;\r
436           }\r
437           if(bottom!=-1)\r
438            {\r
439              g.drawLine(sx, bottom-1, sx + ex, bottom-1);\r
440              bottom = -1;\r
441 \r
442            }\r
443           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
444           g.drawLine(sx, oldY, sx, sy );\r
445           g.drawLine(sx+ex, oldY, sx+ex, sy );\r
446           inGroup = false;\r
447         }\r
448         groupIndex++;\r
449         if (groupIndex >= groups.size())\r
450           break;\r
451 \r
452         group = (SequenceGroup) groups.elementAt(groupIndex);\r
453 \r
454       }\r
455       while (groupIndex < groups.size());\r
456 \r
457 \r
458     /// Highlight search Results once all sequences have been drawn\r
459     //////////////////////////////////////////////////////////\r
460     if(displaySearch)\r
461     {\r
462       for(int r=0; r<searchResults.length; r+=3)\r
463       {\r
464         int searchSeq = searchResults[r];\r
465         int searchStart = searchResults[r+1];\r
466         int searchEnd = searchResults[r+2];\r
467 \r
468         if (searchSeq >= y1 && searchSeq < y2)\r
469         {\r
470           SequenceRenderer ssr = (SequenceRenderer) sr;\r
471           if(searchStart<x1)\r
472             searchStart = x1;\r
473           if(searchEnd > x2)\r
474             searchEnd = x2;\r
475           ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),\r
476                                   searchStart,\r
477                                   searchEnd,\r
478                                   (searchStart - startx) * av.charWidth,\r
479                                   offset +\r
480                                   AlignmentUtil.getPixelHeight(starty, searchSeq,\r
481               av.charHeight),\r
482                                   av.charWidth,\r
483                                   av.charHeight);\r
484         }\r
485       }\r
486     }\r
487 \r
488   }\r
489 \r
490 \r
491 \r
492   public void highlightSearchResults(int [] results)\r
493   {\r
494     // results are in the order sequence, startRes, endRes\r
495     if(results==null)\r
496       displaySearch = false;\r
497     else\r
498       displaySearch = true;\r
499 \r
500     searchResults = results;\r
501 \r
502     repaint();\r
503   }\r
504 \r
505 \r
506 }\r