Checks end of sequence before drawing text
[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(ColourSchemeI cs, SequenceI seq, int i)\r
48   {\r
49     getBoxColour(cs, seq, i);\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 \r
80   public void drawBoxes(SequenceI seq, int start, int end, int x1, int y1,\r
81                         int width, int height)\r
82   {\r
83     int i = start;\r
84     int length = seq.getLength();\r
85 \r
86     int curStart = -1;\r
87     int curWidth = width;\r
88 \r
89     Color tempColour = null;\r
90     while (i <= end)\r
91     {\r
92       resBoxColour = Color.white;\r
93       if(i < length)\r
94       {\r
95         if (inCurrentSequenceGroup(i))\r
96         {\r
97           if (currentSequenceGroup.getDisplayBoxes())\r
98           {\r
99             getBoxColour(currentSequenceGroup.cs, seq, i);\r
100           }\r
101         }\r
102         else if (av.getShowBoxes())\r
103         {\r
104           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
105         }\r
106       }\r
107 \r
108 \r
109       if (resBoxColour != tempColour)\r
110       {\r
111         if (tempColour != null)\r
112         {\r
113           graphics.fillRect(x1 + width * (curStart - start), y1, curWidth,\r
114                             height);\r
115         }\r
116         graphics.setColor(resBoxColour);\r
117 \r
118         curStart = i;\r
119         curWidth = width;\r
120         tempColour = resBoxColour;\r
121 \r
122       }\r
123       else\r
124       {\r
125         curWidth += width;\r
126       }\r
127 \r
128       i++;\r
129     }\r
130 \r
131     graphics.fillRect(x1 + width * (curStart - start), y1, curWidth, height);\r
132   }\r
133 \r
134   public void drawText(SequenceI seq, int start, int end, int x1, int y1,\r
135                        int width, int height)\r
136   {\r
137     int pady = height / 5;\r
138     int charOffset = 0;\r
139     char s=' ';\r
140     // Need to find the sequence position here.\r
141 \r
142     String sequence = seq.getSequence();\r
143 \r
144     if(end+1>=seq.getLength())\r
145           end = seq.getLength()-1;\r
146 \r
147     for (int i = start; i <= end; i++)\r
148     {\r
149       graphics.setColor(Color.black);\r
150 \r
151       s = sequence.charAt(i);\r
152 \r
153       if (!renderGaps && jalview.util.Comparison.isGap(s))\r
154       {\r
155         continue;\r
156       }\r
157 \r
158       if (inCurrentSequenceGroup(i))\r
159       {\r
160         if (!currentSequenceGroup.getDisplayText())\r
161         {\r
162           continue;\r
163         }\r
164 \r
165         if (currentSequenceGroup.getColourText())\r
166         {\r
167           getBoxColour(currentSequenceGroup.cs, seq, i);\r
168           graphics.setColor(resBoxColour.darker());\r
169         }\r
170       }\r
171       else\r
172       {\r
173         if (!av.getShowText())\r
174         {\r
175           continue;\r
176         }\r
177 \r
178         if (av.getColourText())\r
179         {\r
180           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
181           if (av.getShowBoxes())\r
182           {\r
183             graphics.setColor(resBoxColour.darker());\r
184           }\r
185           else\r
186           {\r
187             graphics.setColor(resBoxColour);\r
188           }\r
189         }\r
190       }\r
191 \r
192       charOffset = (width - fm.charWidth(s)) / 2;\r
193       graphics.drawString(String.valueOf(s),\r
194                           charOffset + x1 + width * (i - start),\r
195                           y1 + height - pady);\r
196     }\r
197 \r
198   }\r
199 \r
200   boolean inCurrentSequenceGroup(int res)\r
201   {\r
202     if (allGroups == null)\r
203     {\r
204       return false;\r
205     }\r
206 \r
207     for (int i = 0; i < allGroups.length; i++)\r
208     {\r
209       if (allGroups[i].getStartRes() <= res && allGroups[i].getEndRes() >= res)\r
210       {\r
211         currentSequenceGroup = allGroups[i];\r
212         return true;\r
213       }\r
214     }\r
215 \r
216     return false;\r
217   }\r
218 \r
219   public void drawHighlightedText(SequenceI seq, int start, int end, int x1,\r
220                                   int y1, int width, int height)\r
221   {\r
222     int pady = height / 5;\r
223     int charOffset = 0;\r
224     graphics.setColor(Color.black);\r
225     graphics.fillRect(x1, y1, width * (end - start + 1), height);\r
226     graphics.setColor(Color.white);\r
227 \r
228     char s = '~';\r
229     // Need to find the sequence position here.\r
230     for (int i = start; i <= end; i++)\r
231     {\r
232       if (i < seq.getLength())\r
233       {\r
234         s = seq.getSequence().charAt(i);\r
235       }\r
236 \r
237       charOffset = (width - fm.charWidth(s)) / 2;\r
238       graphics.drawString(String.valueOf(s),\r
239                           charOffset + x1 + width * (i - start),\r
240                           y1 + height - pady);\r
241     }\r
242   }\r
243 \r
244 }\r