2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.appletgui;
23 import jalview.datamodel.*;
24 import jalview.schemes.*;
26 public class SequenceRenderer
30 boolean renderGaps = true;
31 SequenceGroup currentSequenceGroup = null;
32 SequenceGroup[] allGroups = null;
35 boolean forOverview = false;
37 public SequenceRenderer(AlignViewport av)
45 * @param b DOCUMENT ME!
47 public void prepare(Graphics g, boolean renderGaps)
50 fm = g.getFontMetrics();
52 this.renderGaps = renderGaps;
55 public Color getResidueBoxColour(SequenceI seq, int i)
57 allGroups = av.alignment.findAllGroups(seq);
59 if (inCurrentSequenceGroup(i))
61 if (currentSequenceGroup.getDisplayBoxes())
63 getBoxColour(currentSequenceGroup.cs, seq, i);
66 else if (av.getShowBoxes())
68 getBoxColour(av.globalColourScheme, seq, i);
74 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
78 resBoxColour = cs.findColour(seq.getCharAt(i), i);
80 else if (forOverview && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
82 resBoxColour = Color.lightGray;
86 resBoxColour = Color.white;
91 public Color findSequenceColour(SequenceI seq, int i)
93 allGroups = av.alignment.findAllGroups(seq);
94 drawBoxes(seq, i, i, 0);
98 public void drawSequence(SequenceI seq, SequenceGroup[] sg,
99 int start, int end, int y1)
108 drawBoxes(seq, start, end, y1);
110 if (av.validCharWidth)
112 drawText(seq, start, end, y1);
116 public void drawBoxes(SequenceI seq, int start, int end, int y1)
119 int length = seq.getLength();
122 int curWidth = av.charWidth;
124 Color tempColour = null;
127 resBoxColour = Color.white;
130 if (inCurrentSequenceGroup(i))
132 if (currentSequenceGroup.getDisplayBoxes())
134 getBoxColour(currentSequenceGroup.cs, seq, i);
137 else if (av.getShowBoxes())
139 getBoxColour(av.getGlobalColourScheme(), seq, i);
143 if (resBoxColour != tempColour)
145 if (tempColour != null)
147 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
150 graphics.setColor(resBoxColour);
153 curWidth = av.charWidth;
154 tempColour = resBoxColour;
159 curWidth += av.charWidth;
165 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
169 public void drawText(SequenceI seq, int start, int end, int y1)
171 Font boldFont = null;
172 boolean bold = false;
173 if (av.upperCasebold)
175 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
177 graphics.setFont(av.getFont());
180 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
184 // Need to find the sequence position here.
185 if (end + 1 >= seq.getLength())
187 end = seq.getLength() - 1;
192 for (int i = start; i <= end; i++)
194 graphics.setColor(Color.black);
196 s = seq.getCharAt(i);
197 if (!renderGaps && jalview.util.Comparison.isGap(s))
202 if (inCurrentSequenceGroup(i))
204 if (!currentSequenceGroup.getDisplayText())
209 if (currentSequenceGroup.getColourText())
211 getBoxColour(currentSequenceGroup.cs, seq, i);
212 graphics.setColor(resBoxColour.darker());
217 if (!av.getShowText())
222 if (av.getColourText())
224 getBoxColour(av.getGlobalColourScheme(), seq, i);
225 if (av.getShowBoxes())
227 graphics.setColor(resBoxColour.darker());
231 graphics.setColor(resBoxColour);
236 if (av.upperCasebold)
238 fm = graphics.getFontMetrics();
239 if ('A' <= s && s <= 'Z')
244 graphics.setFont(boldFont);
250 graphics.setFont(av.font);
256 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
257 graphics.drawString(String.valueOf(s),
258 charOffset + av.charWidth * (i - start),
264 boolean inCurrentSequenceGroup(int res)
266 if (allGroups == null)
271 for (int i = 0; i < allGroups.length; i++)
273 if (allGroups[i].getStartRes() <= res && allGroups[i].getEndRes() >= res)
275 currentSequenceGroup = allGroups[i];
283 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
286 int pady = av.charHeight / 5;
288 graphics.setColor(Color.black);
289 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1), av.charHeight);
290 graphics.setColor(Color.white);
293 // Need to find the sequence position here.
294 if (av.validCharWidth)
296 for (int i = start; i <= end; i++)
298 if (i < seq.getLength())
300 s = seq.getCharAt(i);
303 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
304 graphics.drawString(String.valueOf(s),
305 charOffset + x1 + av.charWidth * (i - start),
306 y1 + av.charHeight - pady);
311 public void drawCursor(SequenceI seq, int res, int x1, int y1)
313 int pady = av.charHeight / 5;
315 graphics.setColor(Color.black);
316 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
317 graphics.setColor(Color.white);
319 graphics.setColor(Color.white);
321 char s = seq.getCharAt(res);
322 if (av.validCharWidth)
325 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
326 graphics.drawString(String.valueOf(s),
328 (y1 + av.charHeight) - pady);