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.getSequence(i, i + 1), 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(Color.black);
\r
233 if(monospacedFont && av.showText && allGroups.length==0 && !av.getColourText())
\r
236 graphics.drawString(seq.getSequence(start, end + 1), 0, y1);
\r
239 char gap = av.getGapCharacter();
\r
240 graphics.drawString( seq.getSequence(start, end+1).replace(gap, ' '), 0, y1);
\r
245 for (int i = start; i <= end; i++)
\r
247 graphics.setColor(Color.black);
\r
248 s = seq.getCharAt(i);
\r
249 if (!renderGaps && jalview.util.Comparison.isGap(s))
\r
254 if (inCurrentSequenceGroup(i))
\r
256 if (!currentSequenceGroup.getDisplayText())
\r
261 if (currentSequenceGroup.getColourText())
\r
263 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
264 graphics.setColor(resBoxColour.darker());
\r
269 if (!av.getShowText())
\r
274 if (av.getColourText())
\r
276 getBoxColour(av.globalColourScheme, seq, i);
\r
278 if (av.getShowBoxes())
\r
280 graphics.setColor(resBoxColour.darker());
\r
284 graphics.setColor(resBoxColour);
\r
289 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
291 // System.out.print(s);
\r
292 graphics.drawString(String.valueOf(s),
\r
293 charOffset + av.charWidth * (i - start),
\r
297 // System.out.println("\n");
\r
304 * @param res DOCUMENT ME!
\r
306 * @return DOCUMENT ME!
\r
308 boolean inCurrentSequenceGroup(int res)
\r
310 if (allGroups == null)
\r
315 for (int i = 0; i < allGroups.length; i++)
\r
317 if ((allGroups[i].getStartRes() <= res) &&
\r
318 (allGroups[i].getEndRes() >= res))
\r
320 currentSequenceGroup = allGroups[i];
\r
332 * @param seq DOCUMENT ME!
\r
333 * @param start DOCUMENT ME!
\r
334 * @param end DOCUMENT ME!
\r
335 * @param x1 DOCUMENT ME!
\r
336 * @param y1 DOCUMENT ME!
\r
337 * @param width DOCUMENT ME!
\r
338 * @param height DOCUMENT ME!
\r
340 public void drawHighlightedText(SequenceI seq, int start, int end, int x1, int y1)
\r
342 int pady = av.charHeight / 5;
\r
343 int charOffset = 0;
\r
344 graphics.setColor(Color.BLACK);
\r
345 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1), av.charHeight);
\r
346 graphics.setColor(Color.white);
\r
350 // Need to find the sequence position here.
\r
351 if(av.validCharWidth)
\r
353 for (int i = start; i <= end; i++)
\r
355 if (i < seq.getLength())
\r
357 s = seq.getSequence().charAt(i);
\r
360 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
361 graphics.drawString(String.valueOf(s),
\r
362 charOffset + x1 + (av.charWidth * (i - start)),
\r
363 (y1 + av.charHeight) - pady);
\r
368 public void drawCursor(SequenceI seq, int res, int x1, int y1)
\r
370 int pady = av.charHeight / 5;
\r
371 int charOffset = 0;
\r
372 graphics.setColor(Color.black);
\r
373 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
\r
375 if(av.validCharWidth)
\r
377 graphics.setColor(Color.white);
\r
379 char s = seq.getCharAt(res);
\r
381 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
382 graphics.drawString(String.valueOf(s),
\r
384 (y1 + av.charHeight) - pady);
\r