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