BoldFont upperCase
[jalview.git] / src / jalview / appletgui / SequenceRenderer.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 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   boolean forOverview = false;\r
37 \r
38   public SequenceRenderer(AlignViewport av)\r
39   {\r
40     this.av = av;\r
41   }\r
42 \r
43   /**\r
44    * DOCUMENT ME!\r
45    *\r
46    * @param b DOCUMENT ME!\r
47    */\r
48   public void prepare(Graphics g, boolean renderGaps)\r
49   {\r
50       graphics = g;\r
51       fm = g.getFontMetrics();\r
52 \r
53       this.renderGaps = renderGaps;\r
54     }\r
55 \r
56   public Color getResidueBoxColour(SequenceI seq, int i)\r
57   {\r
58     allGroups = av.alignment.findAllGroups(seq);\r
59 \r
60     if (inCurrentSequenceGroup(i))\r
61     {\r
62       if (currentSequenceGroup.getDisplayBoxes())\r
63       {\r
64         getBoxColour(currentSequenceGroup.cs, seq, i);\r
65       }\r
66     }\r
67     else if (av.getShowBoxes())\r
68     {\r
69         getBoxColour(av.globalColourScheme, seq, i);\r
70     }\r
71 \r
72     return resBoxColour;\r
73     }\r
74 \r
75   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
76   {\r
77     if (cs != null)\r
78     {\r
79       resBoxColour = cs.findColour(seq.getSequence(i, i + 1), i);\r
80     }\r
81     else if(forOverview && !jalview.util.Comparison.isGap(seq.getCharAt(i)))\r
82     {\r
83         resBoxColour = Color.lightGray;\r
84     }\r
85     else\r
86     {\r
87       resBoxColour = Color.white;\r
88     }\r
89 \r
90   }\r
91 \r
92   public Color findSequenceColour(SequenceI seq, int i)\r
93   {\r
94     allGroups = av.alignment.findAllGroups(seq);\r
95     drawBoxes(seq, i,i, 0);\r
96     return resBoxColour;\r
97   }\r
98 \r
99   public void drawSequence(SequenceI seq, SequenceGroup[] sg,\r
100                            int start, int end,  int y1)\r
101   {\r
102     allGroups = sg;\r
103 \r
104     drawBoxes(seq, start, end,  y1);\r
105 \r
106     if(av.validCharWidth)\r
107     {\r
108       drawText(seq, start, end, y1);\r
109     }\r
110   }\r
111 \r
112   public void drawBoxes(SequenceI seq, int start, int end,  int y1)\r
113   {\r
114     int i = start;\r
115     int length = seq.getLength();\r
116 \r
117     int curStart = -1;\r
118     int curWidth = av.charWidth;\r
119 \r
120     Color tempColour = null;\r
121     while (i <= end)\r
122     {\r
123       resBoxColour = Color.white;\r
124       if(i < length)\r
125       {\r
126         if (inCurrentSequenceGroup(i))\r
127         {\r
128           if (currentSequenceGroup.getDisplayBoxes())\r
129           {\r
130             getBoxColour(currentSequenceGroup.cs, seq, i);\r
131           }\r
132         }\r
133         else if (av.getShowBoxes())\r
134         {\r
135           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
136         }\r
137       }\r
138 \r
139 \r
140       if (resBoxColour != tempColour)\r
141       {\r
142         if (tempColour != null)\r
143         {\r
144           graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,\r
145                             av.charHeight);\r
146         }\r
147         graphics.setColor(resBoxColour);\r
148 \r
149         curStart = i;\r
150         curWidth = av.charWidth;\r
151         tempColour = resBoxColour;\r
152 \r
153       }\r
154       else\r
155       {\r
156         curWidth += av.charWidth;\r
157       }\r
158 \r
159       i++;\r
160     }\r
161 \r
162     graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth, av.charHeight);\r
163   }\r
164 \r
165   public void drawText(SequenceI seq, int start, int end, int y1)\r
166   {\r
167    Font boldFont = null;\r
168    boolean bold = false;\r
169    if(av.upperCasebold)\r
170    {\r
171      boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);\r
172 \r
173      graphics.setFont(av.getFont());\r
174    }\r
175 \r
176     y1 += av.charHeight - av.charHeight / 5;  // height/5 replaces pady\r
177 \r
178     int charOffset = 0;\r
179 \r
180     // Need to find the sequence position here.\r
181     if(end+1>=seq.getLength())\r
182           end = seq.getLength()-1;\r
183 \r
184     char s = ' ';\r
185 \r
186     for (int i = start; i <= end; i++)\r
187     {\r
188       graphics.setColor(Color.black);\r
189 \r
190       s = seq.getCharAt(i);\r
191       if (!renderGaps && jalview.util.Comparison.isGap(s))\r
192       {\r
193         continue;\r
194       }\r
195 \r
196       if (inCurrentSequenceGroup(i))\r
197       {\r
198         if (!currentSequenceGroup.getDisplayText())\r
199         {\r
200           continue;\r
201         }\r
202 \r
203         if (currentSequenceGroup.getColourText())\r
204         {\r
205           getBoxColour(currentSequenceGroup.cs, seq, i);\r
206           graphics.setColor(resBoxColour.darker());\r
207         }\r
208       }\r
209       else\r
210       {\r
211         if (!av.getShowText())\r
212         {\r
213           continue;\r
214         }\r
215 \r
216         if (av.getColourText())\r
217         {\r
218           getBoxColour(av.getGlobalColourScheme(), seq, i);\r
219           if (av.getShowBoxes())\r
220           {\r
221             graphics.setColor(resBoxColour.darker());\r
222           }\r
223           else\r
224           {\r
225             graphics.setColor(resBoxColour);\r
226           }\r
227         }\r
228       }\r
229 \r
230       if (av.upperCasebold)\r
231       {\r
232         fm = graphics.getFontMetrics();\r
233         if ('A' <= s && s <= 'Z')\r
234         {\r
235           if(!bold)\r
236           {\r
237 \r
238             graphics.setFont(boldFont);\r
239           }\r
240           bold = true;\r
241         }\r
242         else if(bold)\r
243         {\r
244           graphics.setFont(av.font);\r
245           bold = false;\r
246         }\r
247 \r
248       }\r
249 \r
250       charOffset = (av.charWidth - fm.charWidth(s)) / 2;\r
251       graphics.drawString(String.valueOf(s),\r
252                          charOffset + av.charWidth * (i - start),\r
253                         y1 );\r
254     }\r
255 \r
256   }\r
257 \r
258   boolean inCurrentSequenceGroup(int res)\r
259   {\r
260     if (allGroups == null)\r
261     {\r
262       return false;\r
263     }\r
264 \r
265     for (int i = 0; i < allGroups.length; i++)\r
266     {\r
267       if (allGroups[i].getStartRes() <= res && allGroups[i].getEndRes() >= res)\r
268       {\r
269         currentSequenceGroup = allGroups[i];\r
270         return true;\r
271       }\r
272     }\r
273 \r
274     return false;\r
275   }\r
276 \r
277   public void drawHighlightedText(SequenceI seq, int start, int end, int x1, int y1)\r
278   {\r
279     int pady = av.charHeight / 5;\r
280     int charOffset = 0;\r
281     graphics.setColor(Color.black);\r
282     graphics.fillRect(x1, y1, av.charWidth * (end - start + 1), av.charHeight);\r
283     graphics.setColor(Color.white);\r
284 \r
285     char s = '~';\r
286     // Need to find the sequence position here.\r
287     if(av.validCharWidth)\r
288     {\r
289       for (int i = start; i <= end; i++)\r
290       {\r
291         if (i < seq.getLength())\r
292         {\r
293           s = seq.getSequence().charAt(i);\r
294         }\r
295 \r
296         charOffset = (av.charWidth - fm.charWidth(s)) / 2;\r
297         graphics.drawString(String.valueOf(s),\r
298                             charOffset + x1 + av.charWidth * (i - start),\r
299                             y1 + av.charHeight - pady);\r
300       }\r
301     }\r
302   }\r
303 \r
304   public void drawCursor(SequenceI seq, int res, int x1, int y1)\r
305   {\r
306     int pady = av.charHeight / 5;\r
307     int charOffset = 0;\r
308     graphics.setColor(Color.black);\r
309     graphics.fillRect(x1, y1, av.charWidth, av.charHeight);\r
310     graphics.setColor(Color.white);\r
311 \r
312     graphics.setColor(Color.white);\r
313 \r
314     char s = seq.getCharAt(res);\r
315     if (av.validCharWidth)\r
316     {\r
317 \r
318       charOffset = (av.charWidth - fm.charWidth(s)) / 2;\r
319       graphics.drawString(String.valueOf(s),\r
320                           charOffset + x1,\r
321                           (y1 + av.charHeight) - pady);\r
322     }\r
323     }\r
324 \r
325 }\r