wrap alignment box paints correctly
[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 JPanel\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 \r
257       while (ypos <= canvasHeight && startRes<av.alignment.getWidth() )\r
258       {\r
259         g.setColor(Color.black);\r
260 \r
261         if(av.scaleLeftWrapped)\r
262           drawWestScale(g, startRes, endx, ypos);\r
263 \r
264         if(av.scaleRightWrapped)\r
265         {\r
266           g.translate(canvasWidth - LABEL_EAST +av.charWidth, 0);\r
267           drawEastScale(g, startRes, endx, ypos);\r
268           g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);\r
269         }\r
270 \r
271         g.translate(LABEL_WEST,0);\r
272         if(av.scaleAboveWrapped)\r
273           drawNorthScale(g, startRes, endx, ypos);\r
274 \r
275         g.setClip(0, 0, cWidth*av.charWidth, getHeight());\r
276         drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);\r
277         g.setClip(null);\r
278         g.translate(-LABEL_WEST,0);\r
279 \r
280         ypos += cHeight;\r
281         startRes += cWidth;\r
282         endx = startRes + cWidth - 1;\r
283 \r
284         if (endx > al.getWidth())\r
285           endx = al.getWidth();\r
286       }\r
287 \r
288   }\r
289 \r
290 \r
291   public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
292 \r
293     Graphics2D g = (Graphics2D)g1;\r
294     g.setFont(av.getFont());\r
295     RendererI sr = av.getRenderer();\r
296 \r
297     SequenceI nextSeq;\r
298 \r
299     /// First draw the sequences\r
300     /////////////////////////////\r
301     for (int i = y1 ; i < y2 ;i++)\r
302     {\r
303      nextSeq = av.alignment.getSequenceAt(i);\r
304 \r
305      sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,\r
306                  (x1 - startx) * av.charWidth,\r
307                  offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
308                  av.charWidth,av.charHeight,null, i);\r
309 \r
310      if(av.showSequenceFeatures)\r
311      {\r
312        fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,\r
313                        (x1 - startx) * av.charWidth,\r
314                        offset +\r
315                        AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
316                        av.charWidth, av.charHeight, null, i);\r
317      }\r
318     }\r
319     //\r
320     /////////////////////////////////////\r
321 \r
322     // Now outline any areas if necessary\r
323     /////////////////////////////////////\r
324     SequenceGroup group = av.getSelectionGroup();\r
325     java.util.Vector groups = av.alignment.getGroups();\r
326 \r
327     int sx = -1, sy = -1, ex = -1;\r
328     int groupIndex = -1;\r
329     if (group == null && groups.size() > 0)\r
330     {\r
331       group = (SequenceGroup) groups.elementAt(0);\r
332       groupIndex = 0;\r
333     }\r
334 \r
335     if (group != null)\r
336       do\r
337       {\r
338         int oldY = -1;\r
339         int i = 0;\r
340         boolean inGroup = false;\r
341         int top=-1, bottom =-1;\r
342         for (i = y1; i < y2; i++)\r
343         {\r
344           sx = (group.getStartRes() - startx) * av.charWidth;\r
345           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
346           ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth -1;\r
347 \r
348           if (sx < getWidth()\r
349               && ex > 0\r
350               && group.sequences.contains(av.alignment.getSequenceAt(i)))\r
351           {\r
352             if (bottom == -1 &&\r
353                 !group.sequences.contains(av.alignment.getSequenceAt(i + 1)))\r
354               bottom = sy + av.charHeight ;\r
355 \r
356             if (!inGroup)\r
357             {\r
358               if (top == -1 && i==0 ||\r
359                   !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))\r
360                 top = sy;\r
361 \r
362 \r
363               oldY = sy;\r
364               inGroup = true;\r
365               if (group == av.getSelectionGroup())\r
366               {\r
367                 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,3f}, 0f ));\r
368                 g.setColor(Color.RED);\r
369               }\r
370               else\r
371               {\r
372                 g.setStroke(new BasicStroke());\r
373                 g.setColor(group.getOutlineColour());\r
374               }\r
375             }\r
376           }\r
377           else\r
378           {\r
379             if (inGroup)\r
380             {\r
381               g.drawLine(sx, oldY, sx, sy );\r
382               g.drawLine(sx+ex, oldY, sx+ex, sy );\r
383 \r
384               if (top != -1)\r
385               {\r
386                 g.drawLine(sx, top, sx + ex, top);\r
387                 top =-1;\r
388               }\r
389               if (bottom != -1)\r
390               {\r
391                 g.drawLine(sx, bottom, sx + ex, bottom);\r
392                 bottom = -1;\r
393               }\r
394 \r
395 \r
396               inGroup = false;\r
397             }\r
398           }\r
399         }\r
400 \r
401         if (inGroup)\r
402         {\r
403 \r
404           if(top!=-1)\r
405           {\r
406             g.drawLine(sx, top, sx + ex, top);\r
407             top =-1;\r
408           }\r
409           if(bottom!=-1)\r
410            {\r
411              g.drawLine(sx, bottom-1, sx + ex, bottom-1);\r
412              bottom = -1;\r
413 \r
414            }\r
415           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
416           g.drawLine(sx, oldY, sx, sy );\r
417           g.drawLine(sx+ex, oldY, sx+ex, sy );\r
418           inGroup = false;\r
419         }\r
420         groupIndex++;\r
421         if (groupIndex >= groups.size())\r
422           break;\r
423 \r
424         group = (SequenceGroup) groups.elementAt(groupIndex);\r
425 \r
426       }\r
427       while (groupIndex < groups.size());\r
428 \r
429 \r
430     /// Highlight search Results once all sequences have been drawn\r
431     //////////////////////////////////////////////////////////\r
432     if(displaySearch)\r
433     {\r
434       for(int r=0; r<searchResults.length; r+=3)\r
435       {\r
436         int searchSeq = searchResults[r];\r
437         int searchStart = searchResults[r+1];\r
438         int searchEnd = searchResults[r+2];\r
439 \r
440         if (searchSeq >= y1 && searchSeq < y2)\r
441         {\r
442           SequenceRenderer ssr = (SequenceRenderer) sr;\r
443           if(searchStart<x1)\r
444             searchStart = x1;\r
445           if(searchEnd > x2)\r
446             searchEnd = x2;\r
447           ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),\r
448                                   searchStart,\r
449                                   searchEnd,\r
450                                   (searchStart - startx) * av.charWidth,\r
451                                   offset +\r
452                                   AlignmentUtil.getPixelHeight(starty, searchSeq,\r
453               av.charHeight),\r
454                                   av.charWidth,\r
455                                   av.charHeight);\r
456         }\r
457       }\r
458     }\r
459 \r
460   }\r
461 \r
462 \r
463 \r
464   public void highlightSearchResults(int [] results)\r
465   {\r
466     // results are in the order sequence, startRes, endRes\r
467     if(results==null)\r
468       displaySearch = false;\r
469     else\r
470       displaySearch = true;\r
471 \r
472     searchResults = results;\r
473 \r
474     repaint();\r
475   }\r
476 \r
477 \r
478 }\r