after merge
[jalview.git] / src / jalview / appletgui / SequenceRenderer.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.awt.*;\r
23 \r
24 import jalview.datamodel.*;\r
25 import jalview.schemes.*;\r
26 \r
27 public class SequenceRenderer\r
28 {\r
29   AlignViewport av;\r
30   FontMetrics fm;\r
31   boolean renderGaps = true;\r
32   SequenceGroup currentSequenceGroup = null;\r
33   SequenceGroup[] allGroups = null;\r
34   Color resBoxColour;\r
35   Graphics graphics;\r
36 \r
37   public SequenceRenderer(AlignViewport av)\r
38   {\r
39     this.av = av;\r
40   }\r
41 \r
42   public void renderGaps(boolean b)\r
43   {\r
44     renderGaps = b;\r
45   }\r
46 \r
47   public Color getResidueBoxColour(SequenceI seq, int i)\r
48   {\r
49     allGroups = av.alignment.findAllGroups(seq);\r
50 \r
51     if (inCurrentSequenceGroup(i))\r
52     {\r
53       if (currentSequenceGroup.getDisplayBoxes())\r
54       {\r
55         getBoxColour(currentSequenceGroup.cs, seq, i);\r
56       }\r
57     }\r
58     else if (av.getShowBoxes())\r
59     {\r
60         getBoxColour(av.globalColourScheme, seq, i);\r
61     }\r
62 \r
63     return resBoxColour;\r
64     }\r
65 \r
66   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
67   {\r
68     if (cs != null)\r
69     {\r
70       resBoxColour = cs.findColour(seq.getSequence(i, i + 1), i);\r
71     }\r
72     else\r
73     {\r
74       resBoxColour = Color.white;\r
75     }\r
76   }\r
77 \r
78   Image offscreen;\r
79   public Color findSequenceColour(SequenceI seq, int i)\r
80   {\r
81     allGroups = av.alignment.findAllGroups(seq);\r
82     drawBoxes(seq, i,i, 0, 0, 1,1);\r
83     return resBoxColour;\r
84   }\r
85 \r
86   public void drawSequence(Graphics g, SequenceI seq, SequenceGroup[] sg,\r
87                            int start, int end, int x1, int y1, int width,\r
88                            int height)\r
89   {\r
90     allGroups = sg;\r
91 \r
92     graphics = g;\r
93 \r
94     drawBoxes(seq, start, end, x1, y1, (int) width, height);\r
95 \r
96     fm = g.getFontMetrics();\r
97     drawText(seq, start, end, x1, y1, (int) width, height);\r
98 \r
99   }\r
100 \r
101   public void drawBoxes(SequenceI seq, int start, int end, int x1, int y1,\r
102                         int width, int height)\r
103   {\r
104     int i = start;\r
105     int length = seq.getLength();\r
106 \r
107     int curStart = -1;\r
108     int curWidth = width;\r
109 \r
110     Color tempColour = null;\r
111     while (i <= end)\r
112     {\r
113       resBoxColour = Color.white;\r
114       if(i < length)\r
115       {\r
116         if (inCurrentSequenceGroup(i))\r
117         {\r
118           if (currentSequenceGroup.getDisplayBoxes())\r
119           {\r
120             getBoxColour(currentSequenceGroup.cs, seq, i);\r
121           }\r
122         }\r
123         else if (av.getShowBoxes())\r
124         {\r
125           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
126         }\r
127       }\r
128 \r
129 \r
130       if (resBoxColour != tempColour)\r
131       {\r
132         if (tempColour != null)\r
133         {\r
134           graphics.fillRect(x1 + width * (curStart - start), y1, curWidth,\r
135                             height);\r
136         }\r
137         graphics.setColor(resBoxColour);\r
138 \r
139         curStart = i;\r
140         curWidth = width;\r
141         tempColour = resBoxColour;\r
142 \r
143       }\r
144       else\r
145       {\r
146         curWidth += width;\r
147       }\r
148 \r
149       i++;\r
150     }\r
151 \r
152     graphics.fillRect(x1 + width * (curStart - start), y1, curWidth, height);\r
153   }\r
154 \r
155   public void drawText(SequenceI seq, int start, int end, int x1, int y1,\r
156                        int width, int height)\r
157   {\r
158     int pady = height / 5;\r
159     int charOffset = 0;\r
160     char s=' ';\r
161     // Need to find the sequence position here.\r
162 \r
163     String sequence = seq.getSequence();\r
164 \r
165     if(end+1>=seq.getLength())\r
166           end = seq.getLength()-1;\r
167 \r
168     for (int i = start; i <= end; i++)\r
169     {\r
170       graphics.setColor(Color.black);\r
171 \r
172       s = sequence.charAt(i);\r
173 \r
174       if (!renderGaps && jalview.util.Comparison.isGap(s))\r
175       {\r
176         continue;\r
177       }\r
178 \r
179       if (inCurrentSequenceGroup(i))\r
180       {\r
181         if (!currentSequenceGroup.getDisplayText())\r
182         {\r
183           continue;\r
184         }\r
185 \r
186         if (currentSequenceGroup.getColourText())\r
187         {\r
188           getBoxColour(currentSequenceGroup.cs, seq, i);\r
189           graphics.setColor(resBoxColour.darker());\r
190         }\r
191       }\r
192       else\r
193       {\r
194         if (!av.getShowText())\r
195         {\r
196           continue;\r
197         }\r
198 \r
199         if (av.getColourText())\r
200         {\r
201           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
202           if (av.getShowBoxes())\r
203           {\r
204             graphics.setColor(resBoxColour.darker());\r
205           }\r
206           else\r
207           {\r
208             graphics.setColor(resBoxColour);\r
209           }\r
210         }\r
211       }\r
212 \r
213       charOffset = (width - fm.charWidth(s)) / 2;\r
214       graphics.drawString(String.valueOf(s),\r
215                           charOffset + x1 + width * (i - start),\r
216                           y1 + height - pady);\r
217     }\r
218 \r
219   }\r
220 \r
221   boolean inCurrentSequenceGroup(int res)\r
222   {\r
223     if (allGroups == null)\r
224     {\r
225       return false;\r
226     }\r
227 \r
228     for (int i = 0; i < allGroups.length; i++)\r
229     {\r
230       if (allGroups[i].getStartRes() <= res && allGroups[i].getEndRes() >= res)\r
231       {\r
232         currentSequenceGroup = allGroups[i];\r
233         return true;\r
234       }\r
235     }\r
236 \r
237     return false;\r
238   }\r
239 \r
240   public void drawHighlightedText(SequenceI seq, int start, int end, int x1,\r
241                                   int y1, int width, int height)\r
242   {\r
243     int pady = height / 5;\r
244     int charOffset = 0;\r
245     graphics.setColor(Color.black);\r
246     graphics.fillRect(x1, y1, width * (end - start + 1), height);\r
247     graphics.setColor(Color.white);\r
248 \r
249     char s = '~';\r
250     // Need to find the sequence position here.\r
251     for (int i = start; i <= end; i++)\r
252     {\r
253       if (i < seq.getLength())\r
254       {\r
255         s = seq.getSequence().charAt(i);\r
256       }\r
257 \r
258       charOffset = (width - fm.charWidth(s)) / 2;\r
259       graphics.drawString(String.valueOf(s),\r
260                           charOffset + x1 + width * (i - start),\r
261                           y1 + height - pady);\r
262     }\r
263   }\r
264 \r
265 }\r