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