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