find top and bottom of group
[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     public boolean paintFlag = false;\r
20 \r
21     boolean showScores = false;\r
22     boolean displaySearch = false;\r
23     int [] searchResults = null;\r
24 \r
25     int chunkHeight;\r
26     int chunkWidth;\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   public void drawScale(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       for (int i=scalestartx;i < endx;i+= 10)\r
44       {\r
45           String string = String.valueOf(i);\r
46           g.drawString(string,(i-startx-1)*av.charWidth,ypos - av.charHeight/2);\r
47 \r
48           g.drawLine( (i-startx-1)*av.charWidth +av.charWidth/2, ypos+2 - av.charHeight/2,\r
49                     (i-startx-1)*av.charWidth +av.charWidth/2, ypos-2 );\r
50 \r
51       }\r
52 \r
53   }\r
54 \r
55 \r
56 public void fastPaint(int horizontal, int vertical)\r
57 {\r
58     if (horizontal == 0 && vertical == 0)\r
59       return;\r
60 \r
61     if (img == null || paintFlag)\r
62     {\r
63       repaint();\r
64       return;\r
65     }\r
66 \r
67     gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,\r
68                 -vertical * av.charHeight);\r
69 \r
70     int sr = av.startRes, er = av.endRes + 1, ss = av.startSeq, es = av.endSeq,\r
71         transX = 0, transY = 0;\r
72     if (horizontal > 0) // scrollbar pulled right, image to the left\r
73     {\r
74       transX = (er - sr - horizontal) * av.charWidth;\r
75       sr = er - horizontal;\r
76     }\r
77     else if (horizontal < 0)\r
78       er = sr - horizontal;\r
79 \r
80     else if (vertical > 0) // scroll down\r
81     {\r
82       transY = imgHeight - vertical * av.charHeight;\r
83       ss = es - vertical;\r
84     }\r
85     else if (vertical < 0)\r
86       es = ss - vertical;\r
87 \r
88     gg.translate(transX, transY);\r
89 \r
90     drawPanel(gg, sr, er, ss, es, sr, ss, 0);\r
91 \r
92     gg.translate( -transX, -transY);\r
93     getGraphics().drawImage(img, 0, 0, this);\r
94 \r
95 }\r
96 \r
97 /**\r
98  * Definitions of startx and endx (hopefully):\r
99  * SMJS This is what I'm working towards!\r
100  *   startx is the first residue (starting at 0) to display.\r
101  *   endx   is the last residue to display (starting at 0).\r
102  *   starty is the first sequence to display (starting at 0).\r
103  *   endy   is the last sequence to display (starting at 0).\r
104  * NOTE 1: The av limits are set in setFont in this class and\r
105  * in the adjustment listener in SeqPanel when the scrollbars move.\r
106  */\r
107 \r
108   public void paintComponent(Graphics g)\r
109   {\r
110     // this draws the whole of the alignment\r
111       imgWidth  = getWidth();\r
112       imgHeight = getHeight();\r
113 \r
114       imgWidth -= imgWidth%av.charWidth;\r
115       imgHeight-= imgHeight%av.charHeight;\r
116 \r
117       img = createImage(imgWidth,imgHeight);\r
118       gg  = (Graphics2D)img.getGraphics();\r
119       gg.setFont(av.getFont());\r
120       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);\r
121       paintFlag = false;\r
122 \r
123       gg.setColor(Color.white);\r
124       gg.fillRect(0,0,imgWidth,imgHeight);\r
125 \r
126     chunkWidth  =   getWidth()/av.charWidth;\r
127     chunkHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
128 \r
129     av.setChunkHeight(chunkHeight);\r
130     av.setChunkWidth(chunkWidth);\r
131 \r
132 \r
133     if (av.getWrapAlignment())\r
134       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);\r
135     else\r
136       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);\r
137 \r
138     g.setColor(Color.white);\r
139     g.fillRect(0,0,getWidth(),getHeight());\r
140     g.drawImage(img,0,0,this);\r
141 \r
142   }\r
143 \r
144   public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)\r
145   {\r
146       AlignmentI da = av.getAlignment();\r
147 \r
148       int cWidth  =   canvasWidth/av.charWidth;\r
149       int cHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
150 \r
151       int  endx   = startRes+cWidth-1;\r
152       int ypos  = 2*av.charHeight;\r
153 \r
154       while (ypos <= canvasHeight)\r
155       {\r
156         drawScale(g, startRes, endx, ypos);\r
157         drawPanel(g, startRes, endx, 0, da.getHeight(), startRes, 0, ypos);\r
158 \r
159         ypos += cHeight;\r
160         startRes += cWidth;\r
161         endx = startRes + cWidth - 1;\r
162 \r
163         if (endx > da.getWidth())\r
164           endx = da.getWidth();\r
165       }\r
166 \r
167   }\r
168 \r
169 \r
170   public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
171 \r
172 \r
173    Graphics2D g = (Graphics2D)g1;\r
174     g.setFont(av.getFont());\r
175     RendererI sr = av.getRenderer();\r
176 \r
177     /*Vector    pid    = av.getConsensus(false);\r
178     Vector tmpseq = new Vector();\r
179     for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
180         if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))\r
181             tmpseq.addElement(av.getAlignment().getSequenceAt(i));\r
182 \r
183     if (sr instanceof SequenceRenderer)\r
184         pid    = AAFrequency.calculate(tmpseq,x1,x2);\r
185 \r
186     else if (sr instanceof GraphRenderer)\r
187         pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),\r
188                                        av.getAlignment().getSequences(),\r
189                                        av.getPIDWindow(),x1,x2);\r
190 \r
191 */\r
192 \r
193    /* if (y2 > starty && y1 < av.getEndSeq())\r
194     {\r
195        fillBackground(g,\r
196                    Color.red,\r
197                    (x1-startx)*charWidth,\r
198                    offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),\r
199                    (x2-x1+1)*charWidth,\r
200                    offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));\r
201     }*/\r
202 \r
203     SequenceI nextSeq;\r
204 \r
205     /// First draw the sequences\r
206     /////////////////////////////\r
207     for (int i = y1 ; i < y2 ;i++)\r
208     {\r
209      nextSeq = av.getAlignment().getSequenceAt(i);\r
210 \r
211      sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,\r
212                  (x1 - startx) * av.charWidth,\r
213                  offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
214                  av.charWidth,av.charHeight,null, i);\r
215 \r
216      if(av.showSequenceFeatures)\r
217      {\r
218        fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,\r
219                        (x1 - startx) * av.charWidth,\r
220                        offset +\r
221                        AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
222                        av.charWidth, av.charHeight, null, i);\r
223      }\r
224     }\r
225     //\r
226     /////////////////////////////////////\r
227 \r
228     // Now outline any areas if necessary\r
229     /////////////////////////////////////\r
230     SequenceGroup group = av.getSelectionGroup();\r
231     java.util.Vector groups = av.alignment.getGroups();\r
232 \r
233     int sx = -1, sy = -1, ex = -1;\r
234     int groupIndex = -1;\r
235     if (group == null && groups.size() > 0)\r
236     {\r
237       group = (SequenceGroup) groups.elementAt(0);\r
238       groupIndex = 0;\r
239     }\r
240 \r
241     if (group != null)\r
242       do\r
243       {\r
244         int oldY = -1;\r
245         int i = 0;\r
246         boolean inGroup = false;\r
247         int top=-1, bottom =-1;\r
248         for (i = y1; i < y2; i++)\r
249         {\r
250           sx = (group.getStartRes() - startx) * av.charWidth;\r
251           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
252           ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth;\r
253 \r
254 \r
255           if (sx < getWidth()\r
256               && ex > 0\r
257               && group.sequences.contains(av.alignment.getSequenceAt(i)))\r
258           {\r
259 \r
260             if (bottom == -1 &&\r
261                 !group.sequences.contains(av.alignment.getSequenceAt(i + 1)))\r
262               bottom = sy + av.charHeight - 1;\r
263 \r
264             if (!inGroup)\r
265             {\r
266               if (top == -1 && i==0 ||\r
267                   !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))\r
268                 top = sy;\r
269 \r
270               oldY = sy;\r
271               inGroup = true;\r
272               if (group == av.getSelectionGroup())\r
273               {\r
274                 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,3f}, 0f ));\r
275                 g.setColor(Color.RED);\r
276               }\r
277               else\r
278               {\r
279                 g.setStroke(new BasicStroke());\r
280                 g.setColor(group.getOutlineColour());\r
281               }\r
282             }\r
283           }\r
284           else\r
285           {\r
286             if (inGroup)\r
287             {\r
288 \r
289               g.drawLine(sx, oldY, sx, sy );\r
290               g.drawLine(sx+ex, oldY, sx+ex, sy );\r
291               if (top != -1)\r
292                 g.drawLine(sx, top, sx + ex, top);\r
293               if (bottom != -1)\r
294                 g.drawLine(sx, bottom, sx + ex, bottom);\r
295 \r
296               inGroup = false;\r
297             }\r
298           }\r
299         }\r
300 \r
301         if (inGroup)\r
302         {\r
303 \r
304           if(top!=-1)\r
305              g.drawLine(sx,top, sx+ex, top);\r
306           if(bottom!=-1)\r
307              g.drawLine(sx,bottom, sx+ex, bottom);\r
308 \r
309           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
310           g.drawLine(sx, oldY, sx, sy );\r
311           g.drawLine(sx+ex, oldY, sx+ex, sy );\r
312           inGroup = false;\r
313         }\r
314         groupIndex++;\r
315         if (groupIndex >= groups.size())\r
316           break;\r
317 \r
318         group = (SequenceGroup) groups.elementAt(groupIndex);\r
319 \r
320       }\r
321       while (groupIndex < groups.size());\r
322 \r
323 \r
324     /// Highlight search Results once all sequences have been drawn\r
325     //////////////////////////////////////////////////////////\r
326     if(displaySearch)\r
327     {\r
328       for(int r=0; r<searchResults.length; r+=3)\r
329       {\r
330         int searchSeq = searchResults[r];\r
331         int searchStart = searchResults[r+1];\r
332         int searchEnd = searchResults[r+2];\r
333 \r
334         if (searchSeq >= y1 && searchSeq < y2)\r
335         {\r
336           SequenceRenderer ssr = (SequenceRenderer) sr;\r
337           ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),\r
338                                   searchStart,\r
339                                   searchEnd,\r
340                                   (searchStart - startx) * av.charWidth,\r
341                                   offset +\r
342                                   AlignmentUtil.getPixelHeight(starty, searchSeq,\r
343               av.charHeight),\r
344                                   av.charWidth,\r
345                                   av.charHeight);\r
346         }\r
347       }\r
348     }\r
349 \r
350   }\r
351 \r
352 \r
353   public int getChunkWidth() {\r
354     return chunkWidth;\r
355   }\r
356 \r
357   public void highlightSearchResults(int [] results)\r
358   {\r
359     // results are in the order sequence, startRes, endRes\r
360     if(results==null)\r
361       displaySearch = false;\r
362     else\r
363       displaySearch = true;\r
364 \r
365     searchResults = results;\r
366 \r
367     paintFlag = true;\r
368     repaint();\r
369   }\r
370 \r
371 \r
372 }\r