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