corrections for find when edit takes place
[jalview.git] / src / jalview / appletgui / SeqCanvas.java
1 package jalview.appletgui;\r
2 \r
3 import java.awt.*;\r
4 import jalview.datamodel.*;\r
5 import jalview.analysis.*;\r
6 \r
7 \r
8 public class SeqCanvas extends Panel\r
9 {\r
10     FeatureRenderer fr;\r
11     SequenceRenderer sr;\r
12     Image             img;\r
13     Graphics          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        sr = new SequenceRenderer(av);\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 = LABEL_WEST - 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 public void fastPaint(int horizontal, int vertical)\r
115 {\r
116     if (horizontal == 0 && vertical == 0 || gg==null)\r
117       return;\r
118 \r
119     gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,\r
120                 -vertical * av.charHeight);\r
121 \r
122     int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,\r
123         transX = 0, transY = 0;\r
124     if (horizontal > 0) // scrollbar pulled right, image to the left\r
125     {\r
126       transX = (er - sr - horizontal) * av.charWidth;\r
127       sr = er - horizontal;\r
128     }\r
129     else if (horizontal < 0)\r
130       er = sr - horizontal;\r
131 \r
132     else if (vertical > 0) // scroll down\r
133     {\r
134       ss = es - vertical;\r
135       if(ss<av.startSeq) // ie scrolling too fast, more than a page at a time\r
136         ss = av.startSeq;\r
137       else\r
138         transY = imgHeight - vertical * av.charHeight;\r
139     }\r
140     else if (vertical < 0)\r
141     {\r
142       es = ss - vertical;\r
143       if(es > av.endSeq)\r
144         es = av.endSeq;\r
145     }\r
146 \r
147 \r
148     gg.translate(transX, transY);\r
149 \r
150     gg.setColor(Color.white);\r
151     gg.fillRect(0,0, (er-sr+1)*av.charWidth, (es-ss)*av.charHeight);\r
152     drawPanel(gg, sr, er, ss, es, sr, ss, 0);\r
153     gg.translate( -transX, -transY);\r
154 \r
155     fastPaint = true;\r
156     repaint();\r
157 \r
158 }\r
159 \r
160 /**\r
161  * Definitions of startx and endx (hopefully):\r
162  * SMJS This is what I'm working towards!\r
163  *   startx is the first residue (starting at 0) to display.\r
164  *   endx   is the last residue to display (starting at 0).\r
165  *   starty is the first sequence to display (starting at 0).\r
166  *   endy   is the last sequence to display (starting at 0).\r
167  * NOTE 1: The av limits are set in setFont in this class and\r
168  * in the adjustment listener in SeqPanel when the scrollbars move.\r
169  */\r
170   public void update(Graphics g)\r
171   {\r
172     paint(g);\r
173   }\r
174 \r
175   public void paint(Graphics g)\r
176   {\r
177     if (fastPaint)\r
178     {\r
179       g.drawImage(img, 0, 0, this);\r
180       fastPaint = false;\r
181       return;\r
182     }\r
183 \r
184     // this draws the whole of the alignment\r
185       imgWidth  = this.getSize().width;\r
186       imgHeight = this.getSize().height;\r
187       if(imgWidth<1 || imgHeight<1)\r
188         return;\r
189 \r
190       imgWidth -= imgWidth%av.charWidth;\r
191       imgHeight-= imgHeight%av.charHeight;\r
192 \r
193       if(img==null || imgWidth!=img.getWidth(this) || imgHeight!=img.getHeight(this))\r
194       {\r
195         img = createImage(imgWidth, imgHeight);\r
196         gg = img.getGraphics();\r
197         gg.setFont(av.getFont());\r
198       }\r
199 \r
200       gg.setColor(Color.white);\r
201       gg.fillRect(0,0,imgWidth,imgHeight);\r
202 \r
203     chunkWidth  =   getWrappedCanvasWidth( getSize().width );\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, getSize().width, getSize().height, 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       av.endRes = av.startRes + cWidth;\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().y,\r
285                     cWidth*av.charWidth,\r
286                     (int)clip.getBounds().height\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 g,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
305 \r
306     g.setFont(av.getFont());\r
307     sr.renderGaps(av.renderGaps);\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);\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);\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 -1;\r
359 \r
360           if (sx < getSize().width\r
361               && ex > 0\r
362               && group.sequences.contains(av.alignment.getSequenceAt(i)))\r
363           {\r
364             if (bottom == -1 && (i==av.alignment.getHeight()-1 ||\r
365                 !group.sequences.contains(av.alignment.getSequenceAt(i + 1))))\r
366               bottom = sy + av.charHeight ;\r
367 \r
368             if (!inGroup)\r
369             {\r
370               if (top == -1 && i==0 ||\r
371                   !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))\r
372                 top = sy;\r
373 \r
374 \r
375               oldY = sy;\r
376               inGroup = true;\r
377               if (group == av.getSelectionGroup())\r
378               {\r
379                     g.setColor(new Color(255,0,0));\r
380               }\r
381               else\r
382               {\r
383                     g.setColor(group.getOutlineColour());\r
384               }\r
385             }\r
386           }\r
387           else\r
388           {\r
389             if (inGroup)\r
390             {\r
391               g.drawLine(sx, oldY, sx, sy );\r
392               g.drawLine(sx+ex, oldY, sx+ex, sy );\r
393 \r
394               if (top != -1)\r
395               {\r
396                 g.drawLine(sx, top, sx + ex, top);\r
397                 top =-1;\r
398               }\r
399               if (bottom != -1)\r
400               {\r
401                 g.drawLine(sx, bottom, sx + ex, bottom);\r
402                 bottom = -1;\r
403               }\r
404 \r
405 \r
406               inGroup = false;\r
407             }\r
408           }\r
409         }\r
410 \r
411         if (inGroup)\r
412         {\r
413 \r
414           if(top!=-1)\r
415           {\r
416             g.drawLine(sx, top, sx + ex, top);\r
417             top =-1;\r
418           }\r
419           if(bottom!=-1)\r
420            {\r
421              g.drawLine(sx, bottom-1, sx + ex, bottom-1);\r
422              bottom = -1;\r
423 \r
424            }\r
425           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
426           g.drawLine(sx, oldY, sx, sy );\r
427           g.drawLine(sx+ex, oldY, sx+ex, sy );\r
428           inGroup = false;\r
429         }\r
430         groupIndex++;\r
431         if (groupIndex >= groups.size())\r
432           break;\r
433 \r
434         group = (SequenceGroup) groups.elementAt(groupIndex);\r
435 \r
436       }\r
437       while (groupIndex < groups.size());\r
438 \r
439 \r
440     /// Highlight search Results once all sequences have been drawn\r
441     //////////////////////////////////////////////////////////\r
442     if(displaySearch)\r
443     {\r
444       for(int r=0; r<searchResults.length; r+=3)\r
445       {\r
446         int searchSeq = searchResults[r];\r
447 \r
448         if (searchSeq >= y1 && searchSeq < y2)\r
449         {\r
450           SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);\r
451 \r
452           int searchStart = seq.findIndex( searchResults[r+1] )-1;\r
453           int searchEnd =  seq.findIndex(  searchResults[r+2] )-1;\r
454 \r
455           SequenceRenderer ssr = (SequenceRenderer) sr;\r
456           if(searchStart<x1)\r
457             searchStart = x1;\r
458           if(searchEnd > x2)\r
459             searchEnd = x2;\r
460 \r
461           ssr.drawHighlightedText(seq,\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