2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
19 package jalview.gui;
\r
21 import jalview.datamodel.*;
\r
23 import jalview.schemes.*;
\r
31 * @version $Revision$
\r
33 public class SequenceRenderer
\r
37 boolean renderGaps = true;
\r
38 SequenceGroup currentSequenceGroup = null;
\r
39 SequenceGroup[] allGroups = null;
\r
42 boolean monospacedFont;
\r
43 boolean forOverview = false;
\r
46 * Creates a new SequenceRenderer object.
\r
48 * @param av DOCUMENT ME!
\r
50 public SequenceRenderer(AlignViewport av)
\r
58 * @param b DOCUMENT ME!
\r
60 public void prepare(Graphics g, boolean renderGaps)
\r
63 fm = g.getFontMetrics();
\r
65 // If EPS graphics, stringWidth will be a double, not an int
\r
66 double dwidth = fm.getStringBounds("M", g).getWidth();
\r
69 (dwidth == fm.getStringBounds("|",g).getWidth()
\r
70 && (float)av.charWidth == dwidth);
\r
72 this.renderGaps = renderGaps;
\r
76 public Color getResidueBoxColour(SequenceI seq, int i)
\r
78 allGroups = av.alignment.findAllGroups(seq);
\r
80 if (inCurrentSequenceGroup(i))
\r
82 if (currentSequenceGroup.getDisplayBoxes())
\r
84 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
87 else if (av.getShowBoxes())
\r
89 getBoxColour(av.globalColourScheme, seq, i);
\r
92 return resBoxColour;
\r
99 * @param cs DOCUMENT ME!
\r
100 * @param seq DOCUMENT ME!
\r
101 * @param i DOCUMENT ME!
\r
103 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
\r
107 resBoxColour = cs.findColour(seq.getCharAt(i), i);
\r
109 else if(forOverview && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
\r
111 resBoxColour = Color.lightGray;
\r
115 resBoxColour = Color.white;
\r
122 * @param g DOCUMENT ME!
\r
123 * @param seq DOCUMENT ME!
\r
124 * @param sg DOCUMENT ME!
\r
125 * @param start DOCUMENT ME!
\r
126 * @param end DOCUMENT ME!
\r
127 * @param x1 DOCUMENT ME!
\r
128 * @param y1 DOCUMENT ME!
\r
129 * @param width DOCUMENT ME!
\r
130 * @param height DOCUMENT ME!
\r
132 public void drawSequence(SequenceI seq, SequenceGroup[] sg,
\r
133 int start, int end, int y1)
\r
137 drawBoxes(seq, start, end, y1);
\r
139 if (av.validCharWidth)
\r
140 drawText(seq, start, end, y1);
\r
146 * @param seq DOCUMENT ME!
\r
147 * @param start DOCUMENT ME!
\r
148 * @param end DOCUMENT ME!
\r
149 * @param x1 DOCUMENT ME!
\r
150 * @param y1 DOCUMENT ME!
\r
151 * @param width DOCUMENT ME!
\r
152 * @param height DOCUMENT ME!
\r
154 public synchronized void drawBoxes(SequenceI seq, int start, int end, int y1)
\r
157 int length = seq.getLength();
\r
160 int curWidth = av.charWidth;
\r
162 Color tempColour = null;
\r
166 resBoxColour = Color.white;
\r
170 if (inCurrentSequenceGroup(i))
\r
172 if (currentSequenceGroup.getDisplayBoxes())
\r
174 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
177 else if (av.getShowBoxes())
\r
179 getBoxColour(av.globalColourScheme, seq, i);
\r
184 if (resBoxColour != tempColour)
\r
186 if (tempColour != null)
\r
188 graphics.fillRect( av.charWidth * (curStart - start), y1,
\r
189 curWidth, av.charHeight);
\r
192 graphics.setColor(resBoxColour);
\r
195 curWidth = av.charWidth;
\r
196 tempColour = resBoxColour;
\r
200 curWidth += av.charWidth;
\r
206 graphics.fillRect( av.charWidth * (curStart - start), y1, curWidth,
\r
214 * @param seq DOCUMENT ME!
\r
215 * @param start DOCUMENT ME!
\r
216 * @param end DOCUMENT ME!
\r
217 * @param x1 DOCUMENT ME!
\r
218 * @param y1 DOCUMENT ME!
\r
219 * @param width DOCUMENT ME!
\r
220 * @param height DOCUMENT ME!
\r
222 public void drawText(SequenceI seq, int start, int end, int y1)
\r
224 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
\r
225 int charOffset = 0;
\r
228 if(end+1>=seq.getLength())
\r
229 end = seq.getLength()-1;
\r
230 graphics.setColor(av.textColour);
\r
235 && allGroups.length==0
\r
236 && !av.getColourText()
\r
237 && av.thresholdTextColour==0)
\r
240 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
\r
243 char gap = av.getGapCharacter();
\r
244 graphics.drawString( seq.getSequenceAsString(start, end+1).replace(gap, ' '), 0, y1);
\r
249 boolean getboxColour = false;
\r
250 for (int i = start; i <= end; i++)
\r
252 graphics.setColor(av.textColour);
\r
253 getboxColour = false;
\r
254 s = seq.getCharAt(i);
\r
255 if (!renderGaps && jalview.util.Comparison.isGap(s))
\r
260 if (inCurrentSequenceGroup(i))
\r
262 if (!currentSequenceGroup.getDisplayText())
\r
267 if (currentSequenceGroup.thresholdTextColour > 0
\r
268 || currentSequenceGroup.getColourText())
\r
270 getboxColour = true;
\r
271 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
273 if (currentSequenceGroup.getColourText())
\r
274 graphics.setColor(resBoxColour.darker());
\r
276 if (currentSequenceGroup.thresholdTextColour > 0)
\r
278 if (resBoxColour.getRed() +
\r
279 resBoxColour.getBlue() +
\r
280 resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
\r
281 graphics.setColor(currentSequenceGroup.textColour2);
\r
285 graphics.setColor(currentSequenceGroup.textColour);
\r
290 if (!av.getShowText())
\r
295 if (av.getColourText())
\r
297 getboxColour = true;
\r
298 getBoxColour(av.globalColourScheme, seq, i);
\r
300 if (av.getShowBoxes())
\r
302 graphics.setColor(resBoxColour.darker());
\r
306 graphics.setColor(resBoxColour);
\r
310 if (av.thresholdTextColour > 0)
\r
313 getBoxColour(av.globalColourScheme, seq, i);
\r
315 if (resBoxColour.getRed() +
\r
316 resBoxColour.getBlue() +
\r
317 resBoxColour.getGreen() < av.thresholdTextColour)
\r
318 graphics.setColor(av.textColour2);
\r
325 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
326 graphics.drawString(String.valueOf(s),
\r
327 charOffset + av.charWidth * (i - start),
\r
337 * @param res DOCUMENT ME!
\r
339 * @return DOCUMENT ME!
\r
341 boolean inCurrentSequenceGroup(int res)
\r
343 if (allGroups == null)
\r
348 for (int i = 0; i < allGroups.length; i++)
\r
350 if ((allGroups[i].getStartRes() <= res) &&
\r
351 (allGroups[i].getEndRes() >= res))
\r
353 currentSequenceGroup = allGroups[i];
\r
365 * @param seq DOCUMENT ME!
\r
366 * @param start DOCUMENT ME!
\r
367 * @param end DOCUMENT ME!
\r
368 * @param x1 DOCUMENT ME!
\r
369 * @param y1 DOCUMENT ME!
\r
370 * @param width DOCUMENT ME!
\r
371 * @param height DOCUMENT ME!
\r
373 public void drawHighlightedText(SequenceI seq, int start, int end, int x1, int y1)
\r
375 int pady = av.charHeight / 5;
\r
376 int charOffset = 0;
\r
377 graphics.setColor(Color.BLACK);
\r
378 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1), av.charHeight);
\r
379 graphics.setColor(Color.white);
\r
383 // Need to find the sequence position here.
\r
384 if(av.validCharWidth)
\r
386 for (int i = start; i <= end; i++)
\r
388 if (i < seq.getLength())
\r
390 s = seq.getCharAt(i);
\r
393 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
394 graphics.drawString(String.valueOf(s),
\r
395 charOffset + x1 + (av.charWidth * (i - start)),
\r
396 (y1 + av.charHeight) - pady);
\r
401 public void drawCursor(SequenceI seq, int res, int x1, int y1)
\r
403 int pady = av.charHeight / 5;
\r
404 int charOffset = 0;
\r
405 graphics.setColor(Color.black);
\r
406 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
\r
408 if(av.validCharWidth)
\r
410 graphics.setColor(Color.white);
\r
412 char s = seq.getCharAt(res);
\r
414 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
415 graphics.drawString(String.valueOf(s),
\r
417 (y1 + av.charHeight) - pady);
\r