2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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
26 public class SeqCanvas
\r
30 SequenceRenderer sr;
\r
38 boolean displaySearch = false;
\r
39 int[] searchResults = null;
\r
44 boolean fastPaint = false;
\r
46 public SeqCanvas(AlignViewport av)
\r
49 fr = new FeatureRenderer(av);
\r
50 sr = new SequenceRenderer(av);
\r
51 PaintRefresher.Register(this, av.alignment);
\r
55 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
57 int scalestartx = startx - startx % 10 + 10;
\r
59 g.setColor(Color.black);
\r
62 for (int i = scalestartx; i < endx; i += 10)
\r
64 String string = String.valueOf(i);
\r
65 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
66 ypos - av.charHeight / 2);
\r
68 g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,
\r
69 ypos + 2 - av.charHeight / 2,
\r
70 (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);
\r
75 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
77 FontMetrics fm = getFontMetrics(av.getFont());
\r
78 ypos += av.charHeight;
\r
80 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
82 SequenceI seq = av.alignment.getSequenceAt(i);
\r
85 while (index < endx)
\r
87 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
93 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
98 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))-av.charWidth/2;
\r
99 g.drawString(value + "", x,
\r
100 ypos + i * av.charHeight - av.charHeight / 5);
\r
105 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
107 ypos += av.charHeight;
\r
109 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
111 SequenceI seq = av.alignment.getSequenceAt(i);
\r
114 while (index > startx)
\r
116 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
122 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
127 g.drawString(value + "", av.charWidth/2,
\r
128 ypos + i * av.charHeight - av.charHeight / 5);
\r
134 public void fastPaint(int horizontal, int vertical)
\r
141 gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,
\r
142 -vertical * av.charHeight);
\r
144 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
145 transX = 0, transY = 0;
\r
146 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
148 transX = (er - sr - horizontal) * av.charWidth;
\r
149 sr = er - horizontal;
\r
151 else if (horizontal < 0)
\r
153 er = sr - horizontal;
\r
156 else if (vertical > 0) // scroll down
\r
158 ss = es - vertical;
\r
159 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
165 transY = imgHeight - vertical * av.charHeight;
\r
168 else if (vertical < 0)
\r
170 es = ss - vertical;
\r
171 if (es > av.endSeq)
\r
177 gg.translate(transX, transY);
\r
179 gg.setColor(Color.white);
\r
180 gg.fillRect(0, 0, (er - sr + 1) * av.charWidth, (es - ss) * av.charHeight);
\r
181 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
182 gg.translate( -transX, -transY);
\r
190 * Definitions of startx and endx (hopefully):
\r
191 * SMJS This is what I'm working towards!
\r
192 * startx is the first residue (starting at 0) to display.
\r
193 * endx is the last residue to display (starting at 0).
\r
194 * starty is the first sequence to display (starting at 0).
\r
195 * endy is the last sequence to display (starting at 0).
\r
196 * NOTE 1: The av limits are set in setFont in this class and
\r
197 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
199 public void update(Graphics g)
\r
204 public void paint(Graphics g)
\r
208 g.drawImage(img, 0, 0, this);
\r
213 // this draws the whole of the alignment
\r
214 imgWidth = this.getSize().width;
\r
215 imgHeight = this.getSize().height;
\r
217 imgWidth -= imgWidth % av.charWidth;
\r
218 imgHeight -= imgHeight % av.charHeight;
\r
220 if (imgWidth < 1 || imgHeight < 1)
\r
225 if (img == null || imgWidth != img.getWidth(this) ||
\r
226 imgHeight != img.getHeight(this))
\r
228 img = createImage(imgWidth, imgHeight);
\r
229 gg = img.getGraphics();
\r
230 gg.setFont(av.getFont());
\r
233 gg.setColor(Color.white);
\r
234 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
236 chunkWidth = getWrappedCanvasWidth(getSize().width);
\r
237 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
239 av.setChunkHeight(chunkHeight);
\r
240 av.setChunkWidth(chunkWidth);
\r
242 if (av.getWrapAlignment())
\r
244 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
\r
248 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes,
\r
252 g.drawImage(img, 0, 0, this);
\r
256 int LABEL_WEST, LABEL_EAST;
\r
257 public int getWrappedCanvasWidth(int cwidth)
\r
259 FontMetrics fm = getFontMetrics(av.getFont());
\r
264 if (av.scaleRightWrapped)
\r
266 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
269 if (av.scaleLeftWrapped)
\r
271 LABEL_WEST = fm.stringWidth(getMask());
\r
274 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
279 * Generates a string of zeroes.
\r
285 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
292 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
295 AlignmentI al = av.getAlignment();
\r
297 FontMetrics fm = getFontMetrics(av.getFont());
\r
299 if (av.scaleRightWrapped)
\r
301 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
304 int LABEL_WEST = 0;
\r
306 if (av.scaleLeftWrapped)
\r
308 LABEL_WEST = fm.stringWidth(getMask());
\r
311 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
312 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
314 av.endRes = av.startRes + cWidth;
\r
316 int endx = startRes + cWidth - 1;
\r
317 int ypos = 2 * av.charHeight;
\r
319 while (ypos <= canvasHeight && startRes < av.alignment.getWidth())
\r
321 g.setColor(Color.black);
\r
323 if (av.scaleLeftWrapped)
\r
325 drawWestScale(g, startRes, endx, ypos);
\r
328 if (av.scaleRightWrapped)
\r
330 g.translate(canvasWidth - LABEL_EAST, 0);
\r
331 drawEastScale(g, startRes, endx, ypos);
\r
332 g.translate( - (canvasWidth - LABEL_EAST), 0);
\r
335 g.translate(LABEL_WEST, 0);
\r
336 if (av.scaleAboveWrapped)
\r
338 drawNorthScale(g, startRes, endx, ypos);
\r
342 if(g.getClip()==null)
\r
343 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
345 if (av.alignment.getWidth() >= av.vconsensus.size())
\r
347 endx = av.vconsensus.size() - 2;
\r
349 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
351 g.translate( -LABEL_WEST, 0);
\r
354 startRes += cWidth;
\r
355 endx = startRes + cWidth - 1;
\r
357 if (endx > al.getWidth())
\r
359 endx = al.getWidth();
\r
365 synchronized public void drawPanel(Graphics g, int x1, int x2, int y1, int y2,
\r
366 int startx, int starty, int offset)
\r
369 g.setFont(av.getFont());
\r
370 sr.renderGaps(av.renderGaps);
\r
374 /// First draw the sequences
\r
375 /////////////////////////////
\r
376 for (int i = y1; i < y2; i++)
\r
378 nextSeq = av.alignment.getSequenceAt(i);
\r
380 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
381 (x1 - startx) * av.charWidth,
\r
383 (i-starty)*av.charHeight,
\r
384 av.charWidth, av.charHeight);
\r
386 if (av.showSequenceFeatures)
\r
388 fr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
389 (x1 - startx) * av.charWidth,
\r
391 (i-starty)*av.charHeight,
\r
392 av.charWidth, av.charHeight);
\r
396 /////////////////////////////////////
\r
398 // Now outline any areas if necessary
\r
399 /////////////////////////////////////
\r
400 SequenceGroup group = av.getSelectionGroup();
\r
401 java.util.Vector groups = av.alignment.getGroups();
\r
403 int sx = -1, sy = -1, ex = -1;
\r
404 int groupIndex = -1;
\r
405 if (group == null && groups.size() > 0)
\r
407 group = (SequenceGroup) groups.elementAt(0);
\r
417 boolean inGroup = false;
\r
421 for (i = y1; i < y2; i++)
\r
423 sx = (group.getStartRes() - startx) * av.charWidth;
\r
424 sy = offset + ((i - starty) * av.charHeight);
\r
425 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
428 if(sx+ex<0 || sx>imgWidth)
\r
433 if ( (sx <= (x2-x1)*av.charWidth) &&
\r
434 group.sequences.contains(av.alignment.getSequenceAt(
\r
437 if ((bottom == -1) &&
\r
438 !group.sequences.contains(
\r
439 av.alignment.getSequenceAt(i + 1)))
\r
441 bottom = sy + av.charHeight;
\r
446 if (((top == -1) && (i == 0)) ||
\r
447 !group.sequences.contains(
\r
448 av.alignment.getSequenceAt(i - 1)))
\r
456 if (group == av.getSelectionGroup())
\r
458 g.setColor(Color.red);
\r
462 g.setColor(group.getOutlineColour());
\r
470 if (sx >= 0 && sx < imgWidth)
\r
471 g.drawLine(sx, oldY, sx, sy);
\r
473 if (sx + ex < imgWidth)
\r
474 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
482 if (sx + ex > imgWidth)
\r
485 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
486 ex = (x2 - x1 + 1) * av.charWidth;
\r
490 g.drawLine(sx, top, sx + ex, top);
\r
496 g.drawLine(sx, bottom, sx + ex, bottom);
\r
507 sy = offset + ( (i - starty) * av.charHeight);
\r
508 if (sx >= 0 && sx < imgWidth)
\r
509 g.drawLine(sx, oldY, sx, sy);
\r
511 if (sx + ex < imgWidth)
\r
512 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
520 if (sx + ex > imgWidth)
\r
522 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
523 ex = (x2 - x1 + 1) * av.charWidth;
\r
527 g.drawLine(sx, top, sx + ex, top);
\r
533 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
542 if (groupIndex >= groups.size())
\r
547 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
549 while (groupIndex < groups.size());
\r
552 /// Highlight search Results once all sequences have been drawn
\r
553 //////////////////////////////////////////////////////////
\r
556 for (int r = 0; r < searchResults.length; r += 3)
\r
558 int searchSeq = searchResults[r];
\r
560 if (searchSeq >= y1 && searchSeq < y2)
\r
562 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
564 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
565 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
567 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
568 if (searchStart < x1)
\r
572 if (searchEnd > x2)
\r
577 ssr.drawHighlightedText(seq,
\r
580 (searchStart - startx) * av.charWidth,
\r
582 (searchSeq-starty)*av.charHeight,
\r
591 public void highlightSearchResults(int[] results)
\r
593 // results are in the order sequence, startRes, endRes
\r
594 if (results == null)
\r
596 displaySearch = false;
\r
600 displaySearch = true;
\r
603 searchResults = results;
\r