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