selected group draws last residue
[jalview.git] / src / jalview / gui / SeqCanvas.java
1 package jalview.gui;\r
2 \r
3 import java.awt.*;\r
4 import javax.swing.*;\r
5 import jalview.datamodel.*;\r
6 import jalview.analysis.*;\r
7 \r
8 \r
9 public class SeqCanvas extends JPanel\r
10 {\r
11      FeatureRenderer fr;\r
12     Image             img;\r
13     Graphics2D          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        setLayout(new BorderLayout());\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 = fm.stringWidth("000") - 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 \r
115 \r
116 public void fastPaint(int horizontal, int vertical)\r
117 {\r
118     if (horizontal == 0 && vertical == 0 || gg==null)\r
119       return;\r
120 \r
121     gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,\r
122                 -vertical * av.charHeight);\r
123 \r
124     int sr = av.startRes, er = av.endRes + 1, ss = av.startSeq, es = av.endSeq,\r
125         transX = 0, transY = 0;\r
126     if (horizontal > 0) // scrollbar pulled right, image to the left\r
127     {\r
128       transX = (er - sr - horizontal) * av.charWidth;\r
129       sr = er - horizontal;\r
130     }\r
131     else if (horizontal < 0)\r
132       er = sr - horizontal;\r
133 \r
134     else if (vertical > 0) // scroll down\r
135     {\r
136       ss = es - vertical;\r
137       if(ss<av.startSeq) // ie scrolling too fast, more than a page at a time\r
138         ss = av.startSeq;\r
139       else\r
140         transY = imgHeight - vertical * av.charHeight;\r
141     }\r
142     else if (vertical < 0)\r
143     {\r
144       es = ss - vertical;\r
145       if(es > av.endSeq)\r
146         es = av.endSeq;\r
147     }\r
148 \r
149 \r
150     gg.translate(transX, transY);\r
151 \r
152     gg.setColor(Color.white);\r
153     gg.fillRect(0,0, (er-sr)*av.charWidth, (es-ss)*av.charHeight);\r
154     drawPanel(gg, sr, er, ss, es, sr, ss, 0);\r
155     gg.translate( -transX, -transY);\r
156 \r
157     fastPaint = true;\r
158     repaint();\r
159 \r
160 }\r
161 \r
162 /**\r
163  * Definitions of startx and endx (hopefully):\r
164  * SMJS This is what I'm working towards!\r
165  *   startx is the first residue (starting at 0) to display.\r
166  *   endx   is the last residue to display (starting at 0).\r
167  *   starty is the first sequence to display (starting at 0).\r
168  *   endy   is the last sequence to display (starting at 0).\r
169  * NOTE 1: The av limits are set in setFont in this class and\r
170  * in the adjustment listener in SeqPanel when the scrollbars move.\r
171  */\r
172 \r
173   public void paintComponent(Graphics g)\r
174   {\r
175     g.setColor(Color.white);\r
176     g.fillRect(0, 0, getWidth(), getHeight());\r
177 \r
178     if (fastPaint)\r
179     {\r
180       g.drawImage(img, 0, 0, this);\r
181       fastPaint = false;\r
182       return;\r
183     }\r
184 \r
185     // this draws the whole of the alignment\r
186       imgWidth  = getWidth();\r
187       imgHeight = getHeight();\r
188       if(imgWidth==0 || imgHeight==0)\r
189         return;\r
190 \r
191       imgWidth -= imgWidth%av.charWidth;\r
192       imgHeight-= imgHeight%av.charHeight;\r
193 \r
194       img = createImage(imgWidth,imgHeight);\r
195       gg  = (Graphics2D)img.getGraphics();\r
196       gg.setFont(av.getFont());\r
197       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);\r
198 \r
199       gg.setColor(Color.white);\r
200       gg.fillRect(0,0,imgWidth,imgHeight);\r
201 \r
202     chunkWidth  =   getWrappedCanvasWidth( getWidth() );\r
203     chunkHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
204 \r
205     av.setChunkHeight(chunkHeight);\r
206     av.setChunkWidth(chunkWidth);\r
207 \r
208 \r
209     if (av.getWrapAlignment())\r
210       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);\r
211     else\r
212       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);\r
213 \r
214     g.drawImage(img, 0, 0, this);\r
215 \r
216   }\r
217 \r
218   public int getWrappedCanvasWidth(int cwidth)\r
219   {\r
220     FontMetrics fm = getFontMetrics(av.getFont());\r
221 \r
222     int LABEL_EAST = 0;\r
223     if(av.scaleRightWrapped)\r
224       LABEL_EAST = fm.stringWidth( av.alignment.getWidth()+"000" );\r
225     int LABEL_WEST = 0;\r
226     if(av.scaleLeftWrapped)\r
227       LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"0" );\r
228 \r
229     return  (cwidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
230   }\r
231 \r
232   public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)\r
233   {\r
234       AlignmentI al = av.getAlignment();\r
235 \r
236       FontMetrics fm = getFontMetrics(av.getFont());\r
237 \r
238       int LABEL_EAST = 0;\r
239       if(av.scaleRightWrapped)\r
240         LABEL_EAST = fm.stringWidth( al.getWidth()+"000" );\r
241       int LABEL_WEST = 0;\r
242       if(av.scaleLeftWrapped)\r
243         LABEL_WEST = fm.stringWidth( al.getWidth()+"0" );\r
244 \r
245       int cWidth  =   (canvasWidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
246       int cHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
247 \r
248       int  endx   = startRes+cWidth-1;\r
249       int  ypos  = 2*av.charHeight;\r
250 \r
251 \r
252       while (ypos <= canvasHeight)\r
253       {\r
254         g.setColor(Color.black);\r
255 \r
256         if(av.scaleLeftWrapped)\r
257           drawWestScale(g, startRes, endx, ypos);\r
258 \r
259         if(av.scaleRightWrapped)\r
260         {\r
261           g.translate(canvasWidth - LABEL_EAST +av.charWidth, 0);\r
262           drawEastScale(g, startRes, endx, ypos);\r
263           g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);\r
264         }\r
265 \r
266 \r
267         g.translate(LABEL_WEST,0);\r
268         if(av.scaleAboveWrapped)\r
269           drawNorthScale(g, startRes, endx, ypos);\r
270 \r
271         drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);\r
272         g.translate(-LABEL_WEST,0);\r
273 \r
274         ypos += cHeight;\r
275         startRes += cWidth;\r
276         endx = startRes + cWidth - 1;\r
277 \r
278         if (endx > al.getWidth())\r
279           endx = al.getWidth();\r
280       }\r
281 \r
282   }\r
283 \r
284 \r
285   public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
286 \r
287     Graphics2D g = (Graphics2D)g1;\r
288     g.setFont(av.getFont());\r
289     RendererI sr = av.getRenderer();\r
290 \r
291     /*Vector    pid    = av.getConsensus(false);\r
292     Vector tmpseq = new Vector();\r
293     for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
294         if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))\r
295             tmpseq.addElement(av.getAlignment().getSequenceAt(i));\r
296 \r
297     if (sr instanceof SequenceRenderer)\r
298         pid    = AAFrequency.calculate(tmpseq,x1,x2);\r
299 \r
300     else if (sr instanceof GraphRenderer)\r
301         pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),\r
302                                        av.getAlignment().getSequences(),\r
303                                        av.getPIDWindow(),x1,x2);\r
304 \r
305 */\r
306 \r
307    /* if (y2 > starty && y1 < av.getEndSeq())\r
308     {\r
309        fillBackground(g,\r
310                    Color.red,\r
311                    (x1-startx)*charWidth,\r
312                    offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),\r
313                    (x2-x1+1)*charWidth,\r
314                    offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));\r
315     }*/\r
316 \r
317     SequenceI nextSeq;\r
318 \r
319     /// First draw the sequences\r
320     /////////////////////////////\r
321     for (int i = y1 ; i < y2 ;i++)\r
322     {\r
323      nextSeq = av.alignment.getSequenceAt(i);\r
324 \r
325      sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,\r
326                  (x1 - startx) * av.charWidth,\r
327                  offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
328                  av.charWidth,av.charHeight,null, i);\r
329 \r
330      if(av.showSequenceFeatures)\r
331      {\r
332        fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,\r
333                        (x1 - startx) * av.charWidth,\r
334                        offset +\r
335                        AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
336                        av.charWidth, av.charHeight, null, i);\r
337      }\r
338     }\r
339     //\r
340     /////////////////////////////////////\r
341 \r
342     // Now outline any areas if necessary\r
343     /////////////////////////////////////\r
344     SequenceGroup group = av.getSelectionGroup();\r
345     java.util.Vector groups = av.alignment.getGroups();\r
346 \r
347     int sx = -1, sy = -1, ex = -1;\r
348     int groupIndex = -1;\r
349     if (group == null && groups.size() > 0)\r
350     {\r
351       group = (SequenceGroup) groups.elementAt(0);\r
352       groupIndex = 0;\r
353     }\r
354 \r
355     if (group != null)\r
356       do\r
357       {\r
358         int oldY = -1;\r
359         int i = 0;\r
360         boolean inGroup = false;\r
361         int top=-1, bottom =-1;\r
362         for (i = y1; i < y2; i++)\r
363         {\r
364           sx = (group.getStartRes() - startx) * av.charWidth;\r
365           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
366           ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth -1;\r
367 \r
368           if (av.getWrapAlignment())\r
369           {\r
370             if (sx < 0)\r
371               sx = 0;\r
372 \r
373             if (ex > getWidth())\r
374                 ex = getWrappedCanvasWidth(getWidth()) * av.charWidth;\r
375           }\r
376 \r
377           if (sx < getWidth()\r
378               && ex > 0\r
379               && group.sequences.contains(av.alignment.getSequenceAt(i)))\r
380           {\r
381             if (bottom == -1 &&\r
382                 !group.sequences.contains(av.alignment.getSequenceAt(i + 1)))\r
383               bottom = sy + av.charHeight ;\r
384 \r
385             if (!inGroup)\r
386             {\r
387               if (top == -1 && i==0 ||\r
388                   !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))\r
389                 top = sy;\r
390 \r
391 \r
392               oldY = sy;\r
393               inGroup = true;\r
394               if (group == av.getSelectionGroup())\r
395               {\r
396                 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,3f}, 0f ));\r
397                 g.setColor(Color.RED);\r
398               }\r
399               else\r
400               {\r
401                 g.setStroke(new BasicStroke());\r
402                 g.setColor(group.getOutlineColour());\r
403               }\r
404             }\r
405           }\r
406           else\r
407           {\r
408             if (inGroup)\r
409             {\r
410               g.drawLine(sx, oldY, sx, sy );\r
411               g.drawLine(sx+ex, oldY, sx+ex, sy );\r
412 \r
413               if (top != -1)\r
414               {\r
415                 g.drawLine(sx, top, sx + ex, top);\r
416                 top =-1;\r
417               }\r
418               if (bottom != -1)\r
419               {\r
420                 g.drawLine(sx, bottom, sx + ex, bottom);\r
421                 bottom = -1;\r
422               }\r
423 \r
424 \r
425               inGroup = false;\r
426             }\r
427           }\r
428         }\r
429 \r
430         if (inGroup)\r
431         {\r
432 \r
433           if(top!=-1)\r
434           {\r
435             g.drawLine(sx, top, sx + ex, top);\r
436             top =-1;\r
437           }\r
438           if(bottom!=-1)\r
439            {\r
440              g.drawLine(sx, bottom-1, sx + ex, bottom-1);\r
441              bottom = -1;\r
442 \r
443            }\r
444           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
445           g.drawLine(sx, oldY, sx, sy );\r
446           g.drawLine(sx+ex, oldY, sx+ex, sy );\r
447           inGroup = false;\r
448         }\r
449         groupIndex++;\r
450         if (groupIndex >= groups.size())\r
451           break;\r
452 \r
453         group = (SequenceGroup) groups.elementAt(groupIndex);\r
454 \r
455       }\r
456       while (groupIndex < groups.size());\r
457 \r
458 \r
459     /// Highlight search Results once all sequences have been drawn\r
460     //////////////////////////////////////////////////////////\r
461     if(displaySearch)\r
462     {\r
463       for(int r=0; r<searchResults.length; r+=3)\r
464       {\r
465         int searchSeq = searchResults[r];\r
466         int searchStart = searchResults[r+1];\r
467         int searchEnd = searchResults[r+2];\r
468 \r
469         if (searchSeq >= y1 && searchSeq < y2)\r
470         {\r
471           SequenceRenderer ssr = (SequenceRenderer) sr;\r
472           if(searchStart<x1)\r
473             searchStart = x1;\r
474           if(searchEnd > x2)\r
475             searchEnd = x2;\r
476           ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),\r
477                                   searchStart,\r
478                                   searchEnd,\r
479                                   (searchStart - startx) * av.charWidth,\r
480                                   offset +\r
481                                   AlignmentUtil.getPixelHeight(starty, searchSeq,\r
482               av.charHeight),\r
483                                   av.charWidth,\r
484                                   av.charHeight);\r
485         }\r
486       }\r
487     }\r
488 \r
489   }\r
490 \r
491 \r
492 \r
493   public void highlightSearchResults(int [] results)\r
494   {\r
495     // results are in the order sequence, startRes, endRes\r
496     if(results==null)\r
497       displaySearch = false;\r
498     else\r
499       displaySearch = true;\r
500 \r
501     searchResults = results;\r
502 \r
503     repaint();\r
504   }\r
505 \r
506 \r
507 }\r