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 boolean isOverview = false;
\r
48 public SeqCanvas(AlignViewport av)
\r
51 fr = new FeatureRenderer(av);
\r
52 sr = new SequenceRenderer(av);
\r
53 PaintRefresher.Register(this, av.alignment);
\r
57 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
59 int scalestartx = startx - startx % 10 + 10;
\r
61 g.setColor(Color.black);
\r
64 for (int i = scalestartx; i < endx; i += 10)
\r
66 String string = String.valueOf(i);
\r
67 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
68 ypos - av.charHeight / 2);
\r
70 g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,
\r
71 ypos + 2 - av.charHeight / 2,
\r
72 (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);
\r
77 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
79 FontMetrics fm = getFontMetrics(av.getFont());
\r
80 ypos += av.charHeight;
\r
82 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
84 SequenceI seq = av.alignment.getSequenceAt(i);
\r
87 while (index < endx)
\r
89 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
95 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
100 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))-av.charWidth/2;
\r
101 g.drawString(value + "", x,
\r
102 ypos + i * av.charHeight - av.charHeight / 5);
\r
107 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
109 ypos += av.charHeight;
\r
111 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
113 SequenceI seq = av.alignment.getSequenceAt(i);
\r
116 while (index > startx)
\r
118 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
124 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
129 g.drawString(value + "", av.charWidth/2,
\r
130 ypos + i * av.charHeight - av.charHeight / 5);
\r
136 public void fastPaint(int horizontal, int vertical)
\r
143 gg.copyArea(horizontal * av.charWidth,
\r
144 vertical * av.charHeight,
\r
147 -horizontal * av.charWidth,
\r
148 -vertical * av.charHeight);
\r
150 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
151 transX = 0, transY = 0;
\r
152 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
154 transX = (er - sr - horizontal) * av.charWidth;
\r
155 sr = er - horizontal;
\r
157 else if (horizontal < 0)
\r
159 er = sr - horizontal;
\r
162 else if (vertical > 0) // scroll down
\r
164 ss = es - vertical;
\r
165 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
171 transY = imgHeight - vertical * av.charHeight;
\r
174 else if (vertical < 0)
\r
176 es = ss - vertical;
\r
177 if (es > av.endSeq)
\r
183 gg.translate(transX, transY);
\r
185 gg.setColor(Color.white);
\r
186 gg.fillRect(0, 0, (er - sr + 1) * av.charWidth, (es - ss) * av.charHeight);
\r
187 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
188 gg.translate( -transX, -transY);
\r
196 * Definitions of startx and endx (hopefully):
\r
197 * SMJS This is what I'm working towards!
\r
198 * startx is the first residue (starting at 0) to display.
\r
199 * endx is the last residue to display (starting at 0).
\r
200 * starty is the first sequence to display (starting at 0).
\r
201 * endy is the last sequence to display (starting at 0).
\r
202 * NOTE 1: The av limits are set in setFont in this class and
\r
203 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
205 public void update(Graphics g)
\r
210 public void paint(Graphics g)
\r
212 sr.renderGaps(av.renderGaps);
\r
216 g.drawImage(img, 0, 0, this);
\r
221 // this draws the whole of the alignment
\r
222 imgWidth = this.getSize().width;
\r
223 imgHeight = this.getSize().height;
\r
225 imgWidth -= imgWidth % av.charWidth;
\r
226 imgHeight -= imgHeight % av.charHeight;
\r
228 if (imgWidth < 1 || imgHeight < 1)
\r
233 if (img == null || imgWidth != img.getWidth(this) ||
\r
234 imgHeight != img.getHeight(this))
\r
236 img = createImage(imgWidth, imgHeight);
\r
237 gg = img.getGraphics();
\r
238 gg.setFont(av.getFont());
\r
241 gg.setColor(Color.white);
\r
242 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
244 chunkWidth = getWrappedCanvasWidth(getSize().width);
\r
245 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
247 av.setChunkHeight(chunkHeight);
\r
248 av.setChunkWidth(chunkWidth);
\r
250 if (av.getWrapAlignment())
\r
252 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
\r
256 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes,
\r
260 g.drawImage(img, 0, 0, this);
\r
264 int LABEL_WEST, LABEL_EAST;
\r
265 public int getWrappedCanvasWidth(int cwidth)
\r
267 FontMetrics fm = getFontMetrics(av.getFont());
\r
272 if (av.scaleRightWrapped)
\r
274 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
277 if (av.scaleLeftWrapped)
\r
279 LABEL_WEST = fm.stringWidth(getMask());
\r
282 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
287 * Generates a string of zeroes.
\r
293 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
300 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
303 AlignmentI al = av.getAlignment();
\r
305 FontMetrics fm = getFontMetrics(av.getFont());
\r
307 if (av.scaleRightWrapped)
\r
309 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
312 int LABEL_WEST = 0;
\r
314 if (av.scaleLeftWrapped)
\r
316 LABEL_WEST = fm.stringWidth(getMask());
\r
319 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
320 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
322 av.endRes = av.startRes + cWidth;
\r
324 int endx = startRes + cWidth - 1;
\r
325 int ypos = 2 * av.charHeight;
\r
327 while (ypos <= canvasHeight && startRes < av.alignment.getWidth())
\r
329 g.setColor(Color.black);
\r
331 if (av.scaleLeftWrapped)
\r
333 drawWestScale(g, startRes, endx, ypos);
\r
336 if (av.scaleRightWrapped)
\r
338 g.translate(canvasWidth - LABEL_EAST, 0);
\r
339 drawEastScale(g, startRes, endx, ypos);
\r
340 g.translate( - (canvasWidth - LABEL_EAST), 0);
\r
343 g.translate(LABEL_WEST, 0);
\r
344 if (av.scaleAboveWrapped)
\r
346 drawNorthScale(g, startRes, endx, ypos);
\r
350 if(g.getClip()==null)
\r
351 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
353 if (av.alignment.getWidth() >= av.vconsensus.size())
\r
355 endx = av.vconsensus.size() - 2;
\r
357 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
359 g.translate( -LABEL_WEST, 0);
\r
362 startRes += cWidth;
\r
363 endx = startRes + cWidth - 1;
\r
365 if (endx > al.getWidth())
\r
367 endx = al.getWidth();
\r
373 synchronized public void drawPanel(Graphics g, int x1, int x2, int y1, int y2,
\r
374 int startx, int starty, int offset)
\r
377 g.setFont(av.getFont());
\r
378 sr.renderGaps(av.renderGaps);
\r
382 /// First draw the sequences
\r
383 /////////////////////////////
\r
384 for (int i = y1; i < y2; i++)
\r
386 nextSeq = av.alignment.getSequenceAt(i);
\r
388 sr.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
394 if (av.showSequenceFeatures)
\r
396 fr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
397 (x1 - startx) * av.charWidth,
\r
399 (i-starty)*av.charHeight,
\r
400 av.charWidth, av.charHeight);
\r
404 /////////////////////////////////////
\r
406 // Now outline any areas if necessary
\r
407 /////////////////////////////////////
\r
408 SequenceGroup group = av.getSelectionGroup();
\r
409 java.util.Vector groups = av.alignment.getGroups();
\r
411 int sx = -1, sy = -1, ex = -1;
\r
412 int groupIndex = -1;
\r
413 if (group == null && groups.size() > 0)
\r
415 group = (SequenceGroup) groups.elementAt(0);
\r
419 if (group != null && !isOverview)
\r
425 boolean inGroup = false;
\r
429 for (i = y1; i < y2; i++)
\r
431 sx = (group.getStartRes() - startx) * av.charWidth;
\r
432 sy = offset + ((i - starty) * av.charHeight);
\r
433 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
436 if(sx+ex<0 || sx>imgWidth)
\r
441 if ( (sx <= (x2-x1)*av.charWidth) &&
\r
442 group.sequences.contains(av.alignment.getSequenceAt(
\r
445 if ((bottom == -1) &&
\r
446 !group.sequences.contains(
\r
447 av.alignment.getSequenceAt(i + 1)))
\r
449 bottom = sy + av.charHeight;
\r
454 if (((top == -1) && (i == 0)) ||
\r
455 !group.sequences.contains(
\r
456 av.alignment.getSequenceAt(i - 1)))
\r
464 if (group == av.getSelectionGroup())
\r
466 g.setColor(Color.red);
\r
470 g.setColor(group.getOutlineColour());
\r
478 if (sx >= 0 && sx < imgWidth)
\r
479 g.drawLine(sx, oldY, sx, sy);
\r
481 if (sx + ex < imgWidth)
\r
482 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
490 if (sx + ex > imgWidth)
\r
493 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
494 ex = (x2 - x1 + 1) * av.charWidth;
\r
498 g.drawLine(sx, top, sx + ex, top);
\r
504 g.drawLine(sx, bottom, sx + ex, bottom);
\r
515 sy = offset + ( (i - starty) * av.charHeight);
\r
516 if (sx >= 0 && sx < imgWidth)
\r
517 g.drawLine(sx, oldY, sx, sy);
\r
519 if (sx + ex < imgWidth)
\r
520 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
528 if (sx + ex > imgWidth)
\r
530 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
531 ex = (x2 - x1 + 1) * av.charWidth;
\r
535 g.drawLine(sx, top, sx + ex, top);
\r
541 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
550 if (groupIndex >= groups.size())
\r
555 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
557 while (groupIndex < groups.size());
\r
560 /// Highlight search Results once all sequences have been drawn
\r
561 //////////////////////////////////////////////////////////
\r
564 for (int r = 0; r < searchResults.length; r += 3)
\r
566 int searchSeq = searchResults[r];
\r
568 if (searchSeq >= y1 && searchSeq < y2)
\r
570 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
572 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
573 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
575 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
576 if (searchStart < x1)
\r
580 if (searchEnd > x2)
\r
585 ssr.drawHighlightedText(seq,
\r
588 (searchStart - startx) * av.charWidth,
\r
590 (searchSeq-starty)*av.charHeight,
\r
599 public void highlightSearchResults(int[] results)
\r
601 // results are in the order sequence, startRes, endRes
\r
602 if (results == null)
\r
604 displaySearch = false;
\r
608 displaySearch = true;
\r
611 searchResults = results;
\r