2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
20 package jalview.appletgui;
\r
24 import jalview.datamodel.*;
\r
25 import jalview.schemes.*;
\r
27 public class SequenceRenderer
\r
31 boolean renderGaps = true;
\r
32 SequenceGroup currentSequenceGroup = null;
\r
33 SequenceGroup[] allGroups = null;
\r
36 boolean forOverview = false;
\r
38 public SequenceRenderer(AlignViewport av)
\r
46 * @param b DOCUMENT ME!
\r
48 public void prepare(Graphics g, boolean renderGaps)
\r
51 fm = g.getFontMetrics();
\r
53 this.renderGaps = renderGaps;
\r
56 public Color getResidueBoxColour(SequenceI seq, int i)
\r
58 allGroups = av.alignment.findAllGroups(seq);
\r
60 if (inCurrentSequenceGroup(i))
\r
62 if (currentSequenceGroup.getDisplayBoxes())
\r
64 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
67 else if (av.getShowBoxes())
\r
69 getBoxColour(av.globalColourScheme, seq, i);
\r
72 return resBoxColour;
\r
75 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
\r
79 resBoxColour = cs.findColour(seq.getCharAt(i), i);
\r
81 else if (forOverview && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
\r
83 resBoxColour = Color.lightGray;
\r
87 resBoxColour = Color.white;
\r
92 public Color findSequenceColour(SequenceI seq, int i)
\r
94 allGroups = av.alignment.findAllGroups(seq);
\r
95 drawBoxes(seq, i, i, 0);
\r
96 return resBoxColour;
\r
99 public void drawSequence(SequenceI seq, SequenceGroup[] sg,
\r
100 int start, int end, int y1)
\r
109 drawBoxes(seq, start, end, y1);
\r
111 if (av.validCharWidth)
\r
113 drawText(seq, start, end, y1);
\r
117 public void drawBoxes(SequenceI seq, int start, int end, int y1)
\r
120 int length = seq.getLength();
\r
123 int curWidth = av.charWidth;
\r
125 Color tempColour = null;
\r
128 resBoxColour = Color.white;
\r
131 if (inCurrentSequenceGroup(i))
\r
133 if (currentSequenceGroup.getDisplayBoxes())
\r
135 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
138 else if (av.getShowBoxes())
\r
140 getBoxColour(av.getGlobalColourScheme(), seq, i);
\r
144 if (resBoxColour != tempColour)
\r
146 if (tempColour != null)
\r
148 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
\r
151 graphics.setColor(resBoxColour);
\r
154 curWidth = av.charWidth;
\r
155 tempColour = resBoxColour;
\r
160 curWidth += av.charWidth;
\r
166 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
\r
170 public void drawText(SequenceI seq, int start, int end, int y1)
\r
172 Font boldFont = null;
\r
173 boolean bold = false;
\r
174 if (av.upperCasebold)
\r
176 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
\r
178 graphics.setFont(av.getFont());
\r
181 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
\r
183 int charOffset = 0;
\r
185 // Need to find the sequence position here.
\r
186 if (end + 1 >= seq.getLength())
\r
188 end = seq.getLength() - 1;
\r
193 for (int i = start; i <= end; i++)
\r
195 graphics.setColor(Color.black);
\r
197 s = seq.getCharAt(i);
\r
198 if (!renderGaps && jalview.util.Comparison.isGap(s))
\r
203 if (inCurrentSequenceGroup(i))
\r
205 if (!currentSequenceGroup.getDisplayText())
\r
210 if (currentSequenceGroup.getColourText())
\r
212 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
213 graphics.setColor(resBoxColour.darker());
\r
218 if (!av.getShowText())
\r
223 if (av.getColourText())
\r
225 getBoxColour(av.getGlobalColourScheme(), seq, i);
\r
226 if (av.getShowBoxes())
\r
228 graphics.setColor(resBoxColour.darker());
\r
232 graphics.setColor(resBoxColour);
\r
237 if (av.upperCasebold)
\r
239 fm = graphics.getFontMetrics();
\r
240 if ('A' <= s && s <= 'Z')
\r
245 graphics.setFont(boldFont);
\r
251 graphics.setFont(av.font);
\r
257 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
258 graphics.drawString(String.valueOf(s),
\r
259 charOffset + av.charWidth * (i - start),
\r
265 boolean inCurrentSequenceGroup(int res)
\r
267 if (allGroups == null)
\r
272 for (int i = 0; i < allGroups.length; i++)
\r
274 if (allGroups[i].getStartRes() <= res && allGroups[i].getEndRes() >= res)
\r
276 currentSequenceGroup = allGroups[i];
\r
284 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
\r
287 int pady = av.charHeight / 5;
\r
288 int charOffset = 0;
\r
289 graphics.setColor(Color.black);
\r
290 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1), av.charHeight);
\r
291 graphics.setColor(Color.white);
\r
294 // Need to find the sequence position here.
\r
295 if (av.validCharWidth)
\r
297 for (int i = start; i <= end; i++)
\r
299 if (i < seq.getLength())
\r
301 s = seq.getCharAt(i);
\r
304 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
305 graphics.drawString(String.valueOf(s),
\r
306 charOffset + x1 + av.charWidth * (i - start),
\r
307 y1 + av.charHeight - pady);
\r
312 public void drawCursor(SequenceI seq, int res, int x1, int y1)
\r
314 int pady = av.charHeight / 5;
\r
315 int charOffset = 0;
\r
316 graphics.setColor(Color.black);
\r
317 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
\r
318 graphics.setColor(Color.white);
\r
320 graphics.setColor(Color.white);
\r
322 char s = seq.getCharAt(res);
\r
323 if (av.validCharWidth)
\r
326 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
327 graphics.drawString(String.valueOf(s),
\r
329 (y1 + av.charHeight) - pady);
\r