9a722f279ad171fa1c0305db395dc52f5629ceb9
[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 && 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, curWidth,\r
117                             height);\r
118         }\r
119         graphics.setColor(resBoxColour);\r
120 \r
121         curStart = i;\r
122         curWidth = width;\r
123         tempColour = resBoxColour;\r
124 \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, height);\r
135   }\r
136 \r
137   public void drawText(SequenceI seq, int start, int end, int x1, int y1,\r
138                        int width, int height)\r
139   {\r
140     int pady = height / 5;\r
141     int charOffset = 0;\r
142     char s;\r
143     // Need to find the sequence position here.\r
144 \r
145     String sequence = seq.getSequence();\r
146     for (int i = start; i <= end; i++)\r
147     {\r
148       graphics.setColor(Color.black);\r
149       if (i < sequence.length())\r
150       {\r
151         s = sequence.charAt(i);\r
152       }\r
153       else\r
154       {\r
155         s = ' ';\r
156       }\r
157 \r
158       if (!renderGaps && jalview.util.Comparison.isGap(s))\r
159       {\r
160         continue;\r
161       }\r
162 \r
163       if (inCurrentSequenceGroup(i))\r
164       {\r
165         if (!currentSequenceGroup.getDisplayText())\r
166         {\r
167           continue;\r
168         }\r
169 \r
170         if (currentSequenceGroup.getColourText())\r
171         {\r
172           getBoxColour(currentSequenceGroup.cs, seq, i);\r
173           graphics.setColor(resBoxColour.darker());\r
174         }\r
175       }\r
176       else\r
177       {\r
178         if (!av.getShowText())\r
179         {\r
180           continue;\r
181         }\r
182 \r
183         if (av.getColourText())\r
184         {\r
185           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
186           if (av.getShowBoxes())\r
187           {\r
188             graphics.setColor(resBoxColour.darker());\r
189           }\r
190           else\r
191           {\r
192             graphics.setColor(resBoxColour);\r
193           }\r
194         }\r
195       }\r
196 \r
197       charOffset = (width - fm.charWidth(s)) / 2;\r
198       graphics.drawString(String.valueOf(s),\r
199                           charOffset + x1 + width * (i - start),\r
200                           y1 + height - pady);\r
201     }\r
202 \r
203   }\r
204 \r
205   boolean inCurrentSequenceGroup(int res)\r
206   {\r
207     if (allGroups == null)\r
208     {\r
209       return false;\r
210     }\r
211 \r
212     for (int i = 0; i < allGroups.length; i++)\r
213     {\r
214       if (allGroups[i].getStartRes() <= res && allGroups[i].getEndRes() >= res)\r
215       {\r
216         currentSequenceGroup = allGroups[i];\r
217         return true;\r
218       }\r
219     }\r
220 \r
221     return false;\r
222   }\r
223 \r
224   public void drawHighlightedText(SequenceI seq, int start, int end, int x1,\r
225                                   int y1, int width, int height)\r
226   {\r
227     int pady = height / 5;\r
228     int charOffset = 0;\r
229     graphics.setColor(Color.black);\r
230     graphics.fillRect(x1, y1, width * (end - start + 1), height);\r
231     graphics.setColor(Color.white);\r
232 \r
233     char s = '~';\r
234     // Need to find the sequence position here.\r
235     for (int i = start; i <= end; i++)\r
236     {\r
237       if (i < seq.getLength())\r
238       {\r
239         s = seq.getSequence().charAt(i);\r
240       }\r
241 \r
242       charOffset = (width - fm.charWidth(s)) / 2;\r
243       graphics.drawString(String.valueOf(s),\r
244                           charOffset + x1 + width * (i - start),\r
245                           y1 + height - pady);\r
246     }\r
247   }\r
248 \r
249 }\r