2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
20 package jalview.appletgui;
24 import jalview.datamodel.*;
25 import jalview.schemes.*;
27 public class SequenceRenderer
31 boolean renderGaps = true;
32 SequenceGroup currentSequenceGroup = null;
33 SequenceGroup[] allGroups = null;
36 boolean forOverview = false;
38 public SequenceRenderer(AlignViewport av)
46 * @param b DOCUMENT ME!
48 public void prepare(Graphics g, boolean renderGaps)
51 fm = g.getFontMetrics();
53 this.renderGaps = renderGaps;
56 public Color getResidueBoxColour(SequenceI seq, int i)
58 allGroups = av.alignment.findAllGroups(seq);
60 if (inCurrentSequenceGroup(i))
62 if (currentSequenceGroup.getDisplayBoxes())
64 getBoxColour(currentSequenceGroup.cs, seq, i);
67 else if (av.getShowBoxes())
69 getBoxColour(av.globalColourScheme, seq, i);
75 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
79 resBoxColour = cs.findColour(seq.getSequence(i, i + 1), i);
81 else if(forOverview && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
83 resBoxColour = Color.lightGray;
87 resBoxColour = Color.white;
92 public Color findSequenceColour(SequenceI seq, int i)
94 allGroups = av.alignment.findAllGroups(seq);
95 drawBoxes(seq, i,i, 0);
99 public void drawSequence(SequenceI seq, SequenceGroup[] sg,
100 int start, int end, int y1)
104 drawBoxes(seq, start, end, y1);
106 if(av.validCharWidth)
108 drawText(seq, start, end, y1);
112 public void drawBoxes(SequenceI seq, int start, int end, int y1)
115 int length = seq.getLength();
118 int curWidth = av.charWidth;
120 Color tempColour = null;
123 resBoxColour = Color.white;
126 if (inCurrentSequenceGroup(i))
128 if (currentSequenceGroup.getDisplayBoxes())
130 getBoxColour(currentSequenceGroup.cs, seq, i);
133 else if (av.getShowBoxes())
135 getBoxColour(av.getGlobalColourScheme(), seq, i);
140 if (resBoxColour != tempColour)
142 if (tempColour != null)
144 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
147 graphics.setColor(resBoxColour);
150 curWidth = av.charWidth;
151 tempColour = resBoxColour;
156 curWidth += av.charWidth;
162 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth, av.charHeight);
165 public void drawText(SequenceI seq, int start, int end, int y1)
168 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
172 // Need to find the sequence position here.
173 if(end+1>=seq.getLength())
174 end = seq.getLength()-1;
178 for (int i = start; i <= end; i++)
180 graphics.setColor(Color.black);
182 s = seq.getCharAt(i);
183 if (!renderGaps && jalview.util.Comparison.isGap(s))
188 if (inCurrentSequenceGroup(i))
190 if (!currentSequenceGroup.getDisplayText())
195 if (currentSequenceGroup.getColourText())
197 getBoxColour(currentSequenceGroup.cs, seq, i);
198 graphics.setColor(resBoxColour.darker());
203 if (!av.getShowText())
208 if (av.getColourText())
210 getBoxColour(av.getGlobalColourScheme(), seq, i);
211 if (av.getShowBoxes())
213 graphics.setColor(resBoxColour.darker());
217 graphics.setColor(resBoxColour);
222 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
223 graphics.drawString(String.valueOf(s),
224 charOffset + av.charWidth * (i - start),
230 boolean inCurrentSequenceGroup(int res)
232 if (allGroups == null)
237 for (int i = 0; i < allGroups.length; i++)
239 if (allGroups[i].getStartRes() <= res && allGroups[i].getEndRes() >= res)
241 currentSequenceGroup = allGroups[i];
249 public void drawHighlightedText(SequenceI seq, int start, int end, int x1, int y1)
251 int pady = av.charHeight / 5;
253 graphics.setColor(Color.black);
254 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1), av.charHeight);
255 graphics.setColor(Color.white);
258 // Need to find the sequence position here.
259 if(av.validCharWidth)
261 for (int i = start; i <= end; i++)
263 if (i < seq.getLength())
265 s = seq.getSequence().charAt(i);
268 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
269 graphics.drawString(String.valueOf(s),
270 charOffset + x1 + av.charWidth * (i - start),
271 y1 + av.charHeight - pady);
276 public void drawCursor(SequenceI seq, int res, int x1, int y1)
278 int pady = av.charHeight / 5;
280 graphics.setColor(Color.black);
281 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
282 graphics.setColor(Color.white);
284 graphics.setColor(Color.white);
286 char s = seq.getCharAt(res);
287 if (av.validCharWidth)
290 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
291 graphics.drawString(String.valueOf(s),
293 (y1 + av.charHeight) - pady);