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