896fda5a7c916879d9775e8f65371050c0e3c672
[jalview.git] / src / jalview / gui / 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 package jalview.gui;\r
20 \r
21 import java.awt.*;\r
22 \r
23 import jalview.datamodel.*;\r
24 import jalview.schemes.*;\r
25 \r
26 public class SequenceRenderer\r
27 {\r
28   AlignViewport av;\r
29   FontMetrics fm;\r
30   boolean renderGaps = true;\r
31   SequenceGroup currentSequenceGroup = null;\r
32   SequenceGroup[] allGroups = null;\r
33   Color resBoxColour;\r
34   Graphics graphics;\r
35 \r
36   public SequenceRenderer(AlignViewport av)\r
37   {\r
38     this.av = av;\r
39   }\r
40 \r
41   public void renderGaps(boolean b)\r
42   {\r
43     renderGaps = b;\r
44   }\r
45 \r
46   public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
47   {\r
48     getBoxColour(cs, seq, i);\r
49 \r
50     return resBoxColour;\r
51   }\r
52 \r
53   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
54   {\r
55     if (cs != null)\r
56     {\r
57       resBoxColour = cs.findColour(seq.getSequence(i, i + 1), i);\r
58     }\r
59     else\r
60     {\r
61       resBoxColour = Color.white;\r
62     }\r
63   }\r
64 \r
65   public void drawSequence(Graphics g, SequenceI seq, SequenceGroup[] sg,\r
66                            int start, int end, int x1, int y1, int width,\r
67                            int height)\r
68   {\r
69     allGroups = sg;\r
70 \r
71     graphics = g;\r
72 \r
73     drawBoxes(seq, start, end, x1, y1, (int) width, height);\r
74 \r
75     fm = g.getFontMetrics();\r
76     drawText(seq, start, end, x1, y1, (int) width, height);\r
77   }\r
78 \r
79   public void drawBoxes(SequenceI seq, int start, int end, int x1, int y1,\r
80                         int width, int height)\r
81   {\r
82     int i = start;\r
83     int length = seq.getLength();\r
84 \r
85     int curStart = -1;\r
86     int curWidth = width;\r
87 \r
88     Color tempColour = null;\r
89 \r
90     while ( (i <= end) && (i < length))\r
91     {\r
92       if (inCurrentSequenceGroup(i))\r
93       {\r
94         if (currentSequenceGroup.getDisplayBoxes())\r
95         {\r
96           getBoxColour(currentSequenceGroup.cs, seq, i);\r
97         }\r
98         else\r
99         {\r
100           resBoxColour = Color.white;\r
101         }\r
102       }\r
103       else if (av.getShowBoxes())\r
104       {\r
105         getBoxColour(av.getGlobalColourScheme(), seq, i);\r
106       }\r
107       else\r
108       {\r
109         resBoxColour = Color.white;\r
110       }\r
111 \r
112       if (resBoxColour != tempColour)\r
113       {\r
114         if (tempColour != null)\r
115         {\r
116           graphics.fillRect(x1 + (width * (curStart - start)), y1,\r
117                             curWidth, height);\r
118         }\r
119 \r
120         graphics.setColor(resBoxColour);\r
121 \r
122         curStart = i;\r
123         curWidth = width;\r
124         tempColour = resBoxColour;\r
125       }\r
126       else\r
127       {\r
128         curWidth += width;\r
129       }\r
130 \r
131       i++;\r
132     }\r
133 \r
134     graphics.fillRect(x1 + (width * (curStart - start)), y1, curWidth,\r
135                       height);\r
136   }\r
137 \r
138   public void drawText(SequenceI seq, int start, int end, int x1, int y1,\r
139                        int width, int height)\r
140   {\r
141     int pady = height / 5;\r
142     int charOffset = 0;\r
143     char s;\r
144 \r
145     // Need to find the sequence position here.\r
146     String sequence = seq.getSequence();\r
147 \r
148     for (int i = start; i <= end; i++)\r
149     {\r
150       graphics.setColor(Color.black);\r
151 \r
152       if (i < sequence.length())\r
153       {\r
154         s = sequence.charAt(i);\r
155       }\r
156       else\r
157       {\r
158         s = ' ';\r
159       }\r
160 \r
161       if (!renderGaps && jalview.util.Comparison.isGap(s))\r
162       {\r
163         continue;\r
164       }\r
165 \r
166       if (inCurrentSequenceGroup(i))\r
167       {\r
168         if (!currentSequenceGroup.getDisplayText())\r
169         {\r
170           continue;\r
171         }\r
172 \r
173         if (currentSequenceGroup.getColourText())\r
174         {\r
175           getBoxColour(currentSequenceGroup.cs, seq, i);\r
176           graphics.setColor(resBoxColour.darker());\r
177         }\r
178       }\r
179       else\r
180       {\r
181         if (!av.getShowText())\r
182         {\r
183           continue;\r
184         }\r
185 \r
186         if (av.getColourText())\r
187         {\r
188           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
189 \r
190           if (av.getShowBoxes())\r
191           {\r
192             graphics.setColor(resBoxColour.darker());\r
193           }\r
194           else\r
195           {\r
196             graphics.setColor(resBoxColour);\r
197           }\r
198         }\r
199       }\r
200 \r
201       charOffset = (width - fm.charWidth(s)) / 2;\r
202       graphics.drawString(String.valueOf(s),\r
203                           charOffset + x1 + (width * (i - start)),\r
204                           (y1 + height) - pady);\r
205     }\r
206   }\r
207 \r
208   boolean inCurrentSequenceGroup(int res)\r
209   {\r
210     if (allGroups == null)\r
211     {\r
212       return false;\r
213     }\r
214 \r
215     for (int i = 0; i < allGroups.length; i++)\r
216     {\r
217       if ( (allGroups[i].getStartRes() <= res) &&\r
218           (allGroups[i].getEndRes() >= res))\r
219       {\r
220         currentSequenceGroup = allGroups[i];\r
221 \r
222         return true;\r
223       }\r
224     }\r
225 \r
226     return false;\r
227   }\r
228 \r
229   public void drawHighlightedText(SequenceI seq, int start, int end, int x1,\r
230                                   int y1, int width, int height)\r
231   {\r
232     int pady = height / 5;\r
233     int charOffset = 0;\r
234     graphics.setColor(Color.BLACK);\r
235     graphics.fillRect(x1, y1, width * (end - start + 1), height);\r
236     graphics.setColor(Color.white);\r
237 \r
238     char s = '~';\r
239 \r
240     // Need to find the sequence position here.\r
241     for (int i = start; i <= end; i++)\r
242     {\r
243       if (i < seq.getLength())\r
244       {\r
245         s = seq.getSequence().charAt(i);\r
246       }\r
247 \r
248       charOffset = (width - fm.charWidth(s)) / 2;\r
249       graphics.drawString(String.valueOf(s),\r
250                           charOffset + x1 + (width * (i - start)),\r
251                           (y1 + height) - pady);\r
252     }\r
253   }\r
254 }\r