GPL license added
[jalview.git] / src / jalview / gui / SeqCanvas.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 \r
20 package jalview.gui;\r
21 \r
22 import java.awt.*;\r
23 import java.awt.image.*;\r
24 import javax.swing.*;\r
25 import jalview.datamodel.*;\r
26 import jalview.analysis.*;\r
27 \r
28 \r
29 public class SeqCanvas extends JComponent\r
30 {\r
31      FeatureRenderer fr;\r
32      SequenceRenderer sr;\r
33     BufferedImage             img;\r
34     Graphics2D          gg;\r
35     int               imgWidth;\r
36     int               imgHeight;\r
37 \r
38     AlignViewport     av;\r
39 \r
40     boolean showScores = false;\r
41     boolean displaySearch = false;\r
42     int [] searchResults = null;\r
43 \r
44     int chunkHeight;\r
45     int chunkWidth;\r
46 \r
47     boolean fastPaint = false;\r
48 \r
49 \r
50     public SeqCanvas(AlignViewport av)\r
51     {\r
52         this.av         = av;\r
53        fr = new FeatureRenderer(av);\r
54        sr = new SequenceRenderer(av);\r
55        setLayout(new BorderLayout());\r
56        PaintRefresher.Register(this);\r
57 \r
58     }\r
59 \r
60   void drawNorthScale(Graphics g, int startx, int endx,int ypos) {\r
61     int scalestartx = startx - startx % 10 + 10;\r
62 \r
63     g.setColor(Color.black);\r
64 \r
65     // NORTH SCALE\r
66     for (int i = scalestartx; i < endx; i += 10)\r
67     {\r
68       String string = String.valueOf(i);\r
69       g.drawString(string, (i - startx - 1) * av.charWidth,\r
70                    ypos - av.charHeight / 2);\r
71 \r
72       g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,\r
73                  ypos + 2 - av.charHeight / 2,\r
74                  (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);\r
75 \r
76     }\r
77   }\r
78 \r
79   void drawWestScale(Graphics g, int startx, int endx, int ypos)\r
80   {\r
81     FontMetrics fm = getFontMetrics(av.getFont());\r
82     ypos+= av.charHeight;\r
83       // EAST SCALE\r
84     for (int i = 0; i < av.alignment.getHeight(); i++)\r
85     {\r
86       SequenceI seq = av.alignment.getSequenceAt(i);\r
87       int index = startx;\r
88       int value = -1;\r
89       while (index < endx)\r
90       {\r
91         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))\r
92         {\r
93           index++;\r
94           continue;\r
95         }\r
96 \r
97         value = av.alignment.getSequenceAt(i).findPosition(index);\r
98         break;\r
99       }\r
100       if(value!=-1)\r
101       {\r
102         int x = LABEL_WEST - fm.stringWidth(value+"");\r
103         g.drawString(value + "", x,  ypos +  i*av.charHeight - av.charHeight/5);\r
104       }\r
105     }\r
106   }\r
107 \r
108   void drawEastScale(Graphics g, int startx, int endx, int ypos)\r
109 {\r
110     ypos+= av.charHeight;\r
111     // EAST SCALE\r
112   for (int i = 0; i < av.alignment.getHeight(); i++)\r
113   {\r
114     SequenceI seq = av.alignment.getSequenceAt(i);\r
115     int index = endx;\r
116     int value = -1;\r
117     while (index > startx)\r
118     {\r
119       if (jalview.util.Comparison.isGap(seq.getCharAt(index)))\r
120       {\r
121         index--;\r
122         continue;\r
123       }\r
124 \r
125       value = av.alignment.getSequenceAt(i).findPosition(index);\r
126       break;\r
127     }\r
128     if(value!=-1)\r
129        g.drawString(value + "", 0,  ypos +  i*av.charHeight - av.charHeight/5);\r
130   }\r
131 \r
132 }\r
133 \r
134 \r
135 \r
136 \r
137 \r
138 public void fastPaint(int horizontal, int vertical)\r
139 {\r
140     if (horizontal == 0 && vertical == 0 || gg==null)\r
141       return;\r
142 \r
143     gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,\r
144                 -vertical * av.charHeight);\r
145 \r
146     int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,\r
147         transX = 0, transY = 0;\r
148     if (horizontal > 0) // scrollbar pulled right, image to the left\r
149     {\r
150       transX = (er - sr - horizontal) * av.charWidth;\r
151       sr = er - horizontal;\r
152     }\r
153     else if (horizontal < 0)\r
154       er = sr - horizontal;\r
155 \r
156     else if (vertical > 0) // scroll down\r
157     {\r
158       ss = es - vertical;\r
159       if(ss<av.startSeq) // ie scrolling too fast, more than a page at a time\r
160         ss = av.startSeq;\r
161       else\r
162         transY = imgHeight - vertical * av.charHeight;\r
163     }\r
164     else if (vertical < 0)\r
165     {\r
166       es = ss - vertical;\r
167       if(es > av.endSeq)\r
168         es = av.endSeq;\r
169     }\r
170 \r
171 \r
172     gg.translate(transX, transY);\r
173 \r
174     gg.setColor(Color.white);\r
175     gg.fillRect(0,0, (er-sr+1)*av.charWidth, (es-ss)*av.charHeight);\r
176     drawPanel(gg, sr, er, ss, es, sr, ss, 0);\r
177     gg.translate( -transX, -transY);\r
178 \r
179     fastPaint = true;\r
180     repaint();\r
181 \r
182 }\r
183 \r
184 /**\r
185  * Definitions of startx and endx (hopefully):\r
186  * SMJS This is what I'm working towards!\r
187  *   startx is the first residue (starting at 0) to display.\r
188  *   endx   is the last residue to display (starting at 0).\r
189  *   starty is the first sequence to display (starting at 0).\r
190  *   endy   is the last sequence to display (starting at 0).\r
191  * NOTE 1: The av limits are set in setFont in this class and\r
192  * in the adjustment listener in SeqPanel when the scrollbars move.\r
193  */\r
194 \r
195   public void paintComponent(Graphics g)\r
196   {\r
197     g.setColor(Color.white);\r
198     g.fillRect(0, 0, getWidth(), getHeight());\r
199 \r
200     if (fastPaint)\r
201     {\r
202       g.drawImage(img, 0, 0, this);\r
203       fastPaint = false;\r
204       return;\r
205     }\r
206 \r
207     // this draws the whole of the alignment\r
208       imgWidth  = getWidth();\r
209       imgHeight = getHeight();\r
210 \r
211       imgWidth -= imgWidth%av.charWidth;\r
212       imgHeight-= imgHeight%av.charHeight;\r
213 \r
214       if(imgWidth<1 || imgHeight<1)\r
215         return;\r
216 \r
217       img = new BufferedImage(imgWidth,imgHeight,BufferedImage.TYPE_INT_RGB);\r
218       gg  = (Graphics2D)img.getGraphics();\r
219       gg.setFont(av.getFont());\r
220       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);\r
221 \r
222       gg.setColor(Color.white);\r
223       gg.fillRect(0,0,imgWidth,imgHeight);\r
224 \r
225     chunkWidth  =   getWrappedCanvasWidth( getWidth() );\r
226     chunkHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
227 \r
228     av.setChunkHeight(chunkHeight);\r
229     av.setChunkWidth(chunkWidth);\r
230 \r
231 \r
232     if (av.getWrapAlignment())\r
233       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);\r
234     else\r
235       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);\r
236 \r
237     g.drawImage(img, 0, 0, this);\r
238 \r
239   }\r
240 \r
241   int LABEL_WEST, LABEL_EAST;\r
242   public int getWrappedCanvasWidth(int cwidth)\r
243   {\r
244     FontMetrics fm = getFontMetrics(av.getFont());\r
245 \r
246     LABEL_EAST = 0;\r
247     LABEL_WEST = 0;\r
248 \r
249     if(av.scaleRightWrapped)\r
250       LABEL_EAST = fm.stringWidth( av.alignment.getWidth()+"000" );\r
251 \r
252     if(av.scaleLeftWrapped)\r
253       LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"" );\r
254 \r
255     return  (cwidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
256   }\r
257 \r
258   public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)\r
259   {\r
260       AlignmentI al = av.getAlignment();\r
261 \r
262       FontMetrics fm = getFontMetrics(av.getFont());\r
263 \r
264       int LABEL_EAST = 0;\r
265       if(av.scaleRightWrapped)\r
266         LABEL_EAST = fm.stringWidth( al.getWidth()+"000" );\r
267       int LABEL_WEST = 0;\r
268       if(av.scaleLeftWrapped)\r
269         LABEL_WEST = fm.stringWidth(al.getWidth()+"0");\r
270 \r
271 \r
272       int cWidth  =   (canvasWidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
273       int cHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
274 \r
275       av.endRes = av.startRes + cWidth;\r
276 \r
277       int  endx   = startRes+cWidth-1;\r
278       int  ypos  = 2*av.charHeight;\r
279 \r
280       while (ypos <= canvasHeight && startRes<av.alignment.getWidth() )\r
281       {\r
282         g.setColor(Color.black);\r
283 \r
284         if(av.scaleLeftWrapped)\r
285           drawWestScale(g, startRes, endx, ypos);\r
286 \r
287         if(av.scaleRightWrapped)\r
288         {\r
289           g.translate(canvasWidth - LABEL_EAST +av.charWidth, 0);\r
290           drawEastScale(g, startRes, endx, ypos);\r
291           g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);\r
292         }\r
293 \r
294         g.translate(LABEL_WEST,0);\r
295         if(av.scaleAboveWrapped)\r
296           drawNorthScale(g, startRes, endx, ypos);\r
297 \r
298 \r
299         // When printing we have an extra clipped region,\r
300         // the Printable page which we need to account for here\r
301         Shape clip = g.getClip();\r
302         if(clip==null)\r
303           g.setClip(0, 0, cWidth*av.charWidth, canvasHeight);\r
304         else\r
305           g.setClip(0,\r
306                     (int)clip.getBounds().getY(),\r
307                     cWidth*av.charWidth,\r
308                     (int)clip.getBounds().getHeight()\r
309                     );\r
310 \r
311         drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);\r
312         g.setClip(clip);\r
313         g.translate(-LABEL_WEST,0);\r
314 \r
315         ypos += cHeight;\r
316         startRes += cWidth;\r
317         endx = startRes + cWidth - 1;\r
318 \r
319         if (endx > al.getWidth())\r
320           endx = al.getWidth();\r
321       }\r
322 \r
323   }\r
324 \r
325 \r
326   synchronized public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
327 \r
328     Graphics2D g = (Graphics2D)g1;\r
329     g.setFont(av.getFont());\r
330     sr.renderGaps(av.renderGaps);\r
331 \r
332 \r
333     SequenceI nextSeq;\r
334 \r
335     /// First draw the sequences\r
336     /////////////////////////////\r
337     for (int i = y1 ; i < y2 ;i++)\r
338     {\r
339      nextSeq = av.alignment.getSequenceAt(i);\r
340 \r
341      sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,\r
342                  (x1 - startx) * av.charWidth,\r
343                  offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
344                  av.charWidth,av.charHeight);\r
345 \r
346      if(av.showSequenceFeatures)\r
347      {\r
348        fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,\r
349                        (x1 - startx) * av.charWidth,\r
350                        offset +\r
351                        AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
352                        av.charWidth, av.charHeight);\r
353      }\r
354     }\r
355     //\r
356     /////////////////////////////////////\r
357 \r
358     // Now outline any areas if necessary\r
359     /////////////////////////////////////\r
360     SequenceGroup group = av.getSelectionGroup();\r
361     java.util.Vector groups = av.alignment.getGroups();\r
362 \r
363     int sx = -1, sy = -1, ex = -1;\r
364     int groupIndex = -1;\r
365     if (group == null && groups.size() > 0)\r
366     {\r
367       group = (SequenceGroup) groups.elementAt(0);\r
368       groupIndex = 0;\r
369     }\r
370 \r
371     if (group != null)\r
372       do\r
373       {\r
374         int oldY = -1;\r
375         int i = 0;\r
376         boolean inGroup = false;\r
377         int top=-1, bottom =-1;\r
378         for (i = y1; i < y2; i++)\r
379         {\r
380           sx = (group.getStartRes() - startx) * av.charWidth;\r
381           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
382           ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth -1;\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 \r
474         if (searchSeq >= y1 && searchSeq < y2)\r
475         {\r
476           SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);\r
477 \r
478           int searchStart = seq.findIndex( searchResults[r+1] )-1;\r
479           int searchEnd =  seq.findIndex(  searchResults[r+2] )-1;\r
480 \r
481           SequenceRenderer ssr = (SequenceRenderer) sr;\r
482           if(searchStart<x1)\r
483             searchStart = x1;\r
484           if(searchEnd > x2)\r
485             searchEnd = x2;\r
486 \r
487           ssr.drawHighlightedText(seq,\r
488                                   searchStart,\r
489                                   searchEnd,\r
490                                   (searchStart - startx) * av.charWidth,\r
491                                   offset +\r
492                                   AlignmentUtil.getPixelHeight(starty, searchSeq,\r
493               av.charHeight),\r
494                                   av.charWidth,\r
495                                   av.charHeight);\r
496         }\r
497       }\r
498     }\r
499 \r
500   }\r
501 \r
502 \r
503 \r
504   public void highlightSearchResults(int [] results)\r
505   {\r
506     // results are in the order sequence, startRes, endRes\r
507     if(results==null)\r
508       displaySearch = false;\r
509     else\r
510       displaySearch = true;\r
511 \r
512     searchResults = results;\r
513 \r
514     repaint();\r
515   }\r
516 \r
517 \r
518 }\r