Drawing of group and rubberband corrected
[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 java.awt.event.*;\r
6 import jalview.datamodel.*;\r
7 import jalview.schemes.*;\r
8 import jalview.analysis.*;\r
9 \r
10 \r
11 public class SeqCanvas extends JPanel\r
12 {\r
13     Image             img;\r
14     Graphics          gg;\r
15     int               imgWidth;\r
16     int               imgHeight;\r
17 \r
18     AlignViewport     av;\r
19 \r
20     public boolean paintFlag = false;\r
21 \r
22     boolean showScores = false;\r
23     boolean displaySearch = false;\r
24     int [] searchResults = null;\r
25 \r
26     int chunkHeight;\r
27     int chunkWidth;\r
28 \r
29 \r
30     public SeqCanvas(AlignViewport av)\r
31     {\r
32         this.av         = av;\r
33        setLayout(new BorderLayout());\r
34 \r
35     }\r
36 \r
37   public void drawScale(int startx, int endx,int charWidth, int charHeight,int ypos) {\r
38       int scalestartx = startx - startx%10 + 10;\r
39 \r
40       gg.setColor(Color.black);\r
41 \r
42       for (int i=scalestartx;i < endx;i+= 10) {\r
43           String string = String.valueOf(i);\r
44           gg.drawString(string,(int)((i-startx-1)*charWidth),ypos+15 - charHeight*(2));\r
45       }\r
46   }\r
47 \r
48 \r
49 /**\r
50  * Definitions of startx and endx (hopefully):\r
51  * SMJS This is what I'm working towards!\r
52  *   startx is the first residue (starting at 0) to display.\r
53  *   endx   is the last residue to display (starting at 0).\r
54  *   starty is the first sequence to display (starting at 0).\r
55  *   endy   is the last sequence to display (starting at 0).\r
56  * NOTE 1: The av limits are set in setFont in this class and\r
57  * in the adjustment listener in SeqPanel when the scrollbars move.\r
58  */\r
59 \r
60   public void paintComponent(Graphics g) {\r
61 \r
62     AlignmentI da = av.getAlignment();\r
63 \r
64     if (img == null ||\r
65         imgWidth  !=  getWidth()  ||\r
66         imgHeight !=  getHeight()\r
67         || paintFlag)\r
68     {\r
69 \r
70       imgWidth  = getWidth();\r
71       imgHeight = getHeight();\r
72 \r
73       img = createImage(imgWidth,imgHeight);\r
74       gg  = img.getGraphics();\r
75 \r
76       gg.setFont(av.getFont());\r
77 \r
78       paintFlag = false;\r
79     }\r
80 \r
81     chunkWidth  =   getWidth()/av.charWidth;\r
82     chunkHeight =  (da.getHeight() + 2)*av.charHeight;\r
83 \r
84     av.setChunkHeight(chunkHeight);\r
85     av.setChunkWidth(chunkWidth);\r
86 \r
87 \r
88 \r
89     fillBackground(gg,Color.WHITE,0,0,imgWidth,imgHeight);\r
90 \r
91     /* if (av.getWrapAlignment()) {\r
92         int offy = av.getStartSeq();\r
93           startx = (int)(offy/chunkWidth)*chunkWidth;\r
94           endx   = startx + chunkWidth;\r
95           starty = offy%chunkHeight;\r
96           endy   = starty + da.getHeight();\r
97 \r
98           int ypos     = 0;\r
99           int rowstart = starty;\r
100 \r
101           if (starty == 0) {\r
102               ypos = 2*charHeight;\r
103           } else if (starty == 1) {\r
104               starty = 0;\r
105               ypos = charHeight;\r
106           }\r
107 \r
108           if (endy > da.getHeight()) {\r
109               endy = da.getHeight();\r
110           }\r
111 \r
112           if (endx > da.getWidth()) {\r
113               endx = da.getWidth();\r
114           }\r
115 \r
116           if (rowstart < 2) {\r
117               drawScale(startx,endx,charWidth,charHeight,ypos);\r
118           }\r
119 \r
120           drawPanel(gg,startx,endx,starty,endy,startx,starty,ypos);\r
121 \r
122           if (rowstart == 0) {\r
123               ypos = ypos + chunkHeight;\r
124           } else if (rowstart == 1) {\r
125               ypos = ypos + chunkHeight;\r
126           } else {\r
127               ypos   = ypos + chunkHeight - rowstart*charHeight;\r
128           }\r
129 \r
130           startx += chunkWidth;\r
131           endx   = startx + chunkWidth;\r
132           starty = 0;\r
133 \r
134           if (endx > da.getWidth()) {\r
135               endx = da.getWidth();\r
136           }\r
137           // Draw the rest of the panels\r
138 \r
139           while (ypos <= getHeight()) {\r
140               drawScale(startx,endx,charWidth,charHeight,ypos);\r
141               drawPanel(gg,startx,endx,0,da.getHeight(),startx,starty,ypos);\r
142 \r
143               ypos   += chunkHeight;\r
144               startx += chunkWidth;\r
145               endx   = startx + chunkWidth;\r
146 \r
147               if (endy > da.getHeight()) {\r
148                   endy = da.getHeight();\r
149               }\r
150 \r
151               if (endx > da.getWidth()) {\r
152                   endx = da.getWidth();\r
153               }\r
154 \r
155           }\r
156       }\r
157       else*/\r
158       {\r
159           drawPanel(gg,av.startRes,av.endRes,av.startSeq,av.endSeq,av.startRes,av.startSeq,0);\r
160       }\r
161 \r
162 \r
163   /*  if ((oldendy -oldstarty) > (getWidth() / av.getCharWidth())) {\r
164       System.out.println("LIMITS ERROR LIMITS ERROR");\r
165       System.out.println("Corrds " + (oldendy-oldstarty) + " " + (getWidth()/av.getCharWidth()) + " " + getWidth() + " " + av.getCharWidth());\r
166     }*/\r
167 \r
168     g.drawImage(img,0,0,this);\r
169 \r
170   }\r
171 \r
172 \r
173 \r
174 \r
175   public void drawPanel(Graphics g,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
176 \r
177 \r
178     g.setFont(av.getFont());\r
179     int            charWidth  = av.getCharWidth();\r
180     int            charHeight = av.getCharHeight();\r
181     RendererI sr = av.getRenderer();\r
182 \r
183     /*Vector    pid    = av.getConsensus(false);\r
184     Vector tmpseq = new Vector();\r
185     for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
186         if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))\r
187             tmpseq.addElement(av.getAlignment().getSequenceAt(i));\r
188 \r
189     if (sr instanceof SequenceRenderer)\r
190         pid    = AAFrequency.calculate(tmpseq,x1,x2);\r
191 \r
192     else if (sr instanceof GraphRenderer)\r
193         pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),\r
194                                        av.getAlignment().getSequences(),\r
195                                        av.getPIDWindow(),x1,x2);\r
196 \r
197 */\r
198 \r
199     if (y2 > starty && y1 < av.getEndSeq())\r
200     {\r
201        fillBackground(g,\r
202                    Color.red,\r
203                    (x1-startx)*charWidth,\r
204                    offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),\r
205                    (x2-x1+1)*charWidth,\r
206                    offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));\r
207     }\r
208 \r
209     SequenceI nextSeq;\r
210     SequenceGroup group=null;\r
211     boolean inGroup=false; // are we in a defined group?\r
212     boolean inRB=false; //Are we in a rubberband group?\r
213     int sx=-1, sy=-1, ex=-1, ey=-1;\r
214     int oldStartX=0,oldEndX=0,oldY=0;\r
215     int oldRStartX=0,oldREndX=0,oldRY=0;\r
216     for (int i = y1 ; i < y2 ;i++)\r
217     {\r
218      nextSeq = av.getAlignment().getSequenceAt(i);\r
219 \r
220      group = av.alignment.findGroup( nextSeq );\r
221 \r
222      sr.drawSequence(g, nextSeq, group,x1,x2,\r
223                  (x1 - startx) * charWidth,\r
224                  offset + AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight()),\r
225                  charWidth,charHeight,null, i);\r
226 \r
227 \r
228      if( group!=null )\r
229      {\r
230 \r
231         g.setColor(Color.lightGray);\r
232 \r
233         sx = (group.getStartRes()-startx)*charWidth;\r
234         sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight());\r
235         ex = (group.getEndRes()+1-startx)*charWidth;\r
236         ey = offset + AlignmentUtil.getPixelHeight(starty, i+1, av.getCharHeight());\r
237 \r
238 \r
239         if (!inGroup)\r
240         {\r
241           g.drawLine(sx, sy, ex, sy); // Horizontal, top of new box\r
242           inGroup=true;\r
243         }\r
244 \r
245         g.drawLine(sx, sy, sx, ey ); // vertical line, left hand side\r
246         g.drawLine( ex,sy,ex,ey);    // vertical line, right hand side\r
247         if (i == y2 - 1)// last line of alignment, seal the box\r
248           g.drawLine(sx, ey, ex, ey); // Horizontal, bottom of old box\r
249 \r
250         oldStartX = sx;\r
251         oldEndX = ex;\r
252         oldY = ey;\r
253 \r
254      }\r
255      else if(inGroup)\r
256      {\r
257        g.setColor(Color.lightGray);\r
258        g.drawLine(oldStartX, oldY, oldEndX, oldY); // Horizontal, bottom of old box\r
259      }\r
260      else\r
261        inGroup = false;\r
262 \r
263      group = av.getRubberbandGroup();\r
264      if( group != null && group.sequences.contains(nextSeq) )\r
265      {\r
266          g.setColor(Color.RED.brighter());\r
267          sx = (group.getStartRes() - startx) * charWidth;\r
268          sy = offset +\r
269              AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight());\r
270          ex = (group.getEndRes() + 1 - startx) * charWidth;\r
271          ey = offset +\r
272              AlignmentUtil.getPixelHeight(starty, i + 1, av.getCharHeight());\r
273 \r
274          if (!inRB)\r
275          {\r
276 \r
277            g.drawLine(sx, sy, ex, sy); // Horizontal, top of new box\r
278            inRB = true;\r
279          }\r
280 \r
281          g.drawLine(sx, sy, sx, ey); // vertical line, left hand side\r
282          g.drawLine(ex, sy, ex, ey); // vertical line, right hand side\r
283          if (i == y2 - 1) // last line of alignment, seal the box\r
284            g.drawLine(sx, ey, ex, ey); // Horizontal, bottom of old box\r
285 \r
286           oldRStartX = sx;\r
287           oldREndX = ex;\r
288           oldRY = ey;\r
289      }\r
290      else if(inRB)\r
291      {\r
292        g.setColor(Color.RED.brighter());\r
293        g.drawLine(oldRStartX, oldRY, oldREndX, oldRY); // Horizontal, bottom of old box\r
294        inRB = false;\r
295      }\r
296      else\r
297        inRB = false;\r
298 \r
299     }\r
300 \r
301     /// Highlight search Results once all sequences have been drawn\r
302     if(displaySearch)\r
303     {\r
304       for(int r=0; r<searchResults.length; r+=3)\r
305       {\r
306         int searchSeq = searchResults[r];\r
307         int searchStart = searchResults[r+1];\r
308         int searchEnd = searchResults[r+2];\r
309 \r
310         if (searchSeq >= y1 && searchSeq <= y2)\r
311         {\r
312           SequenceRenderer ssr = (SequenceRenderer) sr;\r
313           ssr.drawHighlightedText(g, av.getAlignment().getSequenceAt(searchSeq),\r
314                                   searchStart,\r
315                                   searchEnd,\r
316                                   (searchStart - startx) * charWidth,\r
317                                   offset +\r
318                                   AlignmentUtil.getPixelHeight(starty, searchSeq,\r
319               charHeight),\r
320                                   charWidth,\r
321                                   charHeight);\r
322         }\r
323       }\r
324     }\r
325 \r
326   }\r
327 \r
328   public void fillBackground(Graphics g,Color c, int x1,int y1,int width,int height) {\r
329     g.setColor(c);\r
330     g.fillRect(x1,y1,width,height);\r
331   }\r
332 \r
333   public int getChunkWidth() {\r
334     return chunkWidth;\r
335   }\r
336 \r
337   public void highlightSearchResults(int [] results)\r
338   {\r
339     // results are in the order sequence, startRes, endRes\r
340     if(results==null)\r
341       displaySearch = false;\r
342     else\r
343       displaySearch = true;\r
344 \r
345     searchResults = results;\r
346 \r
347     paintFlag = true;\r
348     repaint();\r
349   }\r
350 \r
351 \r
352 }\r