AlignPanel RefreshPanels put into paintComponent
[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       ss = es - vertical;\r
137       if(ss<av.startSeq) // ie scrolling too fast, more than a page at a time\r
138         ss = av.startSeq;\r
139       else\r
140         transY = imgHeight - vertical * av.charHeight;\r
141     }\r
142     else if (vertical < 0)\r
143     {\r
144       es = ss - vertical;\r
145       if(es > av.endSeq)\r
146         es = av.endSeq;\r
147     }\r
148 \r
149 \r
150     gg.translate(transX, transY);\r
151 \r
152     gg.setColor(Color.white);\r
153     gg.fillRect(0,0, (er-sr)*av.charWidth, (es-ss)*av.charHeight);\r
154     drawPanel(gg, sr, er, ss, es, sr, ss, 0);\r
155     gg.translate( -transX, -transY);\r
156 \r
157     fastPaint = true;\r
158     repaint();\r
159 \r
160 }\r
161 \r
162 /**\r
163  * Definitions of startx and endx (hopefully):\r
164  * SMJS This is what I'm working towards!\r
165  *   startx is the first residue (starting at 0) to display.\r
166  *   endx   is the last residue to display (starting at 0).\r
167  *   starty is the first sequence to display (starting at 0).\r
168  *   endy   is the last sequence to display (starting at 0).\r
169  * NOTE 1: The av limits are set in setFont in this class and\r
170  * in the adjustment listener in SeqPanel when the scrollbars move.\r
171  */\r
172 \r
173   public void paintComponent(Graphics g)\r
174   {\r
175     g.setColor(Color.white);\r
176     g.fillRect(0, 0, getWidth(), getHeight());\r
177 \r
178     if (fastPaint)\r
179     {\r
180       g.drawImage(img, 0, 0, this);\r
181       fastPaint = false;\r
182       return;\r
183     }\r
184 \r
185     // this draws the whole of the alignment\r
186       imgWidth  = getWidth();\r
187       imgHeight = getHeight();\r
188       if(imgWidth==0 || imgHeight==0)\r
189         return;\r
190 \r
191       imgWidth -= imgWidth%av.charWidth;\r
192       imgHeight-= imgHeight%av.charHeight;\r
193 \r
194       img = createImage(imgWidth,imgHeight);\r
195       gg  = (Graphics2D)img.getGraphics();\r
196       gg.setFont(av.getFont());\r
197       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);\r
198 \r
199       gg.setColor(Color.white);\r
200       gg.fillRect(0,0,imgWidth,imgHeight);\r
201 \r
202     chunkWidth  =   getWrappedCanvasWidth( getWidth() );\r
203     chunkHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
204 \r
205     av.setChunkHeight(chunkHeight);\r
206     av.setChunkWidth(chunkWidth);\r
207 \r
208 \r
209     if (av.getWrapAlignment())\r
210       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);\r
211     else\r
212       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);\r
213 \r
214     g.drawImage(img, 0, 0, this);\r
215 \r
216   }\r
217 \r
218   public int getWrappedCanvasWidth(int cwidth)\r
219   {\r
220     FontMetrics fm = getFontMetrics(av.getFont());\r
221 \r
222     int LABEL_EAST = 0;\r
223     if(av.scaleRightWrapped)\r
224       LABEL_EAST = fm.stringWidth( av.alignment.getWidth()+"000" );\r
225     int LABEL_WEST = 0;\r
226     if(av.scaleLeftWrapped)\r
227       LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"0" );\r
228 \r
229     return  (cwidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
230   }\r
231 \r
232   public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)\r
233   {\r
234       AlignmentI al = av.getAlignment();\r
235 \r
236       FontMetrics fm = getFontMetrics(av.getFont());\r
237 \r
238       int LABEL_EAST = 0;\r
239       if(av.scaleRightWrapped)\r
240         LABEL_EAST = fm.stringWidth( al.getWidth()+"000" );\r
241       int LABEL_WEST = 0;\r
242       if(av.scaleLeftWrapped)\r
243         LABEL_WEST = fm.stringWidth( al.getWidth()+"0" );\r
244 \r
245       int cWidth  =   (canvasWidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
246       int cHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
247 \r
248       int  endx   = startRes+cWidth-1;\r
249       int  ypos  = 2*av.charHeight;\r
250 \r
251 \r
252       while (ypos <= canvasHeight && endx <al.getWidth())\r
253       {\r
254         g.setColor(Color.black);\r
255 \r
256         if(av.scaleLeftWrapped)\r
257           drawWestScale(g, startRes, endx, ypos);\r
258 \r
259         if(av.scaleRightWrapped)\r
260         {\r
261           g.translate(canvasWidth - LABEL_EAST +av.charWidth, 0);\r
262           drawEastScale(g, startRes, endx, ypos);\r
263           g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);\r
264         }\r
265 \r
266 \r
267         g.translate(LABEL_WEST,0);\r
268         if(av.scaleAboveWrapped)\r
269           drawNorthScale(g, startRes, endx, ypos);\r
270 \r
271         drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);\r
272         g.translate(-LABEL_WEST,0);\r
273 \r
274         ypos += cHeight;\r
275         startRes += cWidth;\r
276         endx = startRes + cWidth - 1;\r
277 \r
278         if (endx > al.getWidth())\r
279           endx = al.getWidth();\r
280       }\r
281 \r
282   }\r
283 \r
284 \r
285   public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
286 \r
287     Graphics2D g = (Graphics2D)g1;\r
288     g.setFont(av.getFont());\r
289     RendererI sr = av.getRenderer();\r
290 \r
291     /*Vector    pid    = av.getConsensus(false);\r
292     Vector tmpseq = new Vector();\r
293     for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
294         if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))\r
295             tmpseq.addElement(av.getAlignment().getSequenceAt(i));\r
296 \r
297     if (sr instanceof SequenceRenderer)\r
298         pid    = AAFrequency.calculate(tmpseq,x1,x2);\r
299 \r
300     else if (sr instanceof GraphRenderer)\r
301         pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),\r
302                                        av.getAlignment().getSequences(),\r
303                                        av.getPIDWindow(),x1,x2);\r
304 \r
305 */\r
306 \r
307    /* if (y2 > starty && y1 < av.getEndSeq())\r
308     {\r
309        fillBackground(g,\r
310                    Color.red,\r
311                    (x1-startx)*charWidth,\r
312                    offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),\r
313                    (x2-x1+1)*charWidth,\r
314                    offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));\r
315     }*/\r
316 \r
317     SequenceI nextSeq;\r
318 \r
319     /// First draw the sequences\r
320     /////////////////////////////\r
321     for (int i = y1 ; i < y2 ;i++)\r
322     {\r
323      nextSeq = av.alignment.getSequenceAt(i);\r
324 \r
325      sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,\r
326                  (x1 - startx) * av.charWidth,\r
327                  offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
328                  av.charWidth,av.charHeight,null, i);\r
329 \r
330      if(av.showSequenceFeatures)\r
331      {\r
332        fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,\r
333                        (x1 - startx) * av.charWidth,\r
334                        offset +\r
335                        AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
336                        av.charWidth, av.charHeight, null, i);\r
337      }\r
338     }\r
339     //\r
340     /////////////////////////////////////\r
341 \r
342     // Now outline any areas if necessary\r
343     /////////////////////////////////////\r
344     SequenceGroup group = av.getSelectionGroup();\r
345     java.util.Vector groups = av.alignment.getGroups();\r
346 \r
347     int sx = -1, sy = -1, ex = -1;\r
348     int groupIndex = -1;\r
349     if (group == null && groups.size() > 0)\r
350     {\r
351       group = (SequenceGroup) groups.elementAt(0);\r
352       groupIndex = 0;\r
353     }\r
354 \r
355     if (group != null)\r
356       do\r
357       {\r
358         int oldY = -1;\r
359         int i = 0;\r
360         boolean inGroup = false;\r
361         int top=-1, bottom =-1;\r
362         for (i = y1; i < y2; i++)\r
363         {\r
364           sx = (group.getStartRes() - startx) * av.charWidth;\r
365           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
366           ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth;\r
367 \r
368           if (av.getWrapAlignment())\r
369           {\r
370             if (sx < 0)\r
371               sx = 0;\r
372 \r
373             if (ex > getWidth())\r
374             {\r
375               if (av.getWrapAlignment())\r
376                 ex = getWrappedCanvasWidth(getWidth()) * av.charWidth;\r
377 \r
378             }\r
379           }\r
380           else if(ex>getWidth())\r
381             ex -= av.charWidth;\r
382 \r
383 \r
384           if (sx < getWidth()\r
385               && ex > 0\r
386               && group.sequences.contains(av.alignment.getSequenceAt(i)))\r
387           {\r
388             if (bottom == -1 &&\r
389                 !group.sequences.contains(av.alignment.getSequenceAt(i + 1)))\r
390               bottom = sy + av.charHeight ;\r
391 \r
392             if (!inGroup)\r
393             {\r
394               if (top == -1 && i==0 ||\r
395                   !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))\r
396                 top = sy;\r
397 \r
398 \r
399               oldY = sy;\r
400               inGroup = true;\r
401               if (group == av.getSelectionGroup())\r
402               {\r
403                 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,3f}, 0f ));\r
404                 g.setColor(Color.RED);\r
405               }\r
406               else\r
407               {\r
408                 g.setStroke(new BasicStroke());\r
409                 g.setColor(group.getOutlineColour());\r
410               }\r
411             }\r
412           }\r
413           else\r
414           {\r
415             if (inGroup)\r
416             {\r
417               g.drawLine(sx, oldY, sx, sy );\r
418               g.drawLine(sx+ex, oldY, sx+ex, sy );\r
419 \r
420               if (top != -1)\r
421               {\r
422                 g.drawLine(sx, top, sx + ex, top);\r
423                 top =-1;\r
424               }\r
425               if (bottom != -1)\r
426               {\r
427                 g.drawLine(sx, bottom, sx + ex, bottom);\r
428                 bottom = -1;\r
429               }\r
430 \r
431 \r
432               inGroup = false;\r
433             }\r
434           }\r
435         }\r
436 \r
437         if (inGroup)\r
438         {\r
439 \r
440           if(top!=-1)\r
441           {\r
442             g.drawLine(sx, top, sx + ex, top);\r
443             top =-1;\r
444           }\r
445           if(bottom!=-1)\r
446            {\r
447              g.drawLine(sx, bottom-1, sx + ex, bottom-1);\r
448              bottom = -1;\r
449 \r
450            }\r
451           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
452           g.drawLine(sx, oldY, sx, sy );\r
453           g.drawLine(sx+ex, oldY, sx+ex, sy );\r
454           inGroup = false;\r
455         }\r
456         groupIndex++;\r
457         if (groupIndex >= groups.size())\r
458           break;\r
459 \r
460         group = (SequenceGroup) groups.elementAt(groupIndex);\r
461 \r
462       }\r
463       while (groupIndex < groups.size());\r
464 \r
465 \r
466     /// Highlight search Results once all sequences have been drawn\r
467     //////////////////////////////////////////////////////////\r
468     if(displaySearch)\r
469     {\r
470       for(int r=0; r<searchResults.length; r+=3)\r
471       {\r
472         int searchSeq = searchResults[r];\r
473         int searchStart = searchResults[r+1];\r
474         int searchEnd = searchResults[r+2];\r
475 \r
476         if (searchSeq >= y1 && searchSeq < y2)\r
477         {\r
478           SequenceRenderer ssr = (SequenceRenderer) sr;\r
479           if(searchStart<x1)\r
480             searchStart = x1;\r
481           if(searchEnd > x2)\r
482             searchEnd = x2;\r
483           ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),\r
484                                   searchStart,\r
485                                   searchEnd,\r
486                                   (searchStart - startx) * av.charWidth,\r
487                                   offset +\r
488                                   AlignmentUtil.getPixelHeight(starty, searchSeq,\r
489               av.charHeight),\r
490                                   av.charWidth,\r
491                                   av.charHeight);\r
492         }\r
493       }\r
494     }\r
495 \r
496   }\r
497 \r
498 \r
499 \r
500   public void highlightSearchResults(int [] results)\r
501   {\r
502     // results are in the order sequence, startRes, endRes\r
503     if(results==null)\r
504       displaySearch = false;\r
505     else\r
506       displaySearch = true;\r
507 \r
508     searchResults = results;\r
509 \r
510     repaint();\r
511   }\r
512 \r
513 \r
514 }\r