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 public FeatureRenderer getFeatureRenderer()
\r
62 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
64 int scalestartx = startx - startx % 10 + 10;
\r
66 g.setColor(Color.black);
\r
69 for (int i = scalestartx; i < endx; i += 10)
\r
71 String string = String.valueOf(i);
\r
72 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
73 ypos - av.charHeight / 2);
\r
75 g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,
\r
76 ypos + 2 - av.charHeight / 2,
\r
77 (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);
\r
82 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
84 FontMetrics fm = getFontMetrics(av.getFont());
\r
85 ypos += av.charHeight;
\r
87 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
89 SequenceI seq = av.alignment.getSequenceAt(i);
\r
92 while (index < endx)
\r
94 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
100 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
105 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))-av.charWidth/2;
\r
106 g.drawString(value + "", x,
\r
107 ypos + i * av.charHeight - av.charHeight / 5);
\r
112 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
114 ypos += av.charHeight;
\r
116 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
118 SequenceI seq = av.alignment.getSequenceAt(i);
\r
121 while (index > startx)
\r
123 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
129 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
134 g.drawString(value + "", av.charWidth/2,
\r
135 ypos + i * av.charHeight - av.charHeight / 5);
\r
141 public void fastPaint(int horizontal, int vertical)
\r
148 gg.copyArea(horizontal * av.charWidth,
\r
149 vertical * av.charHeight,
\r
152 -horizontal * av.charWidth,
\r
153 -vertical * av.charHeight);
\r
155 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
156 transX = 0, transY = 0;
\r
157 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
159 transX = (er - sr - horizontal) * av.charWidth;
\r
160 sr = er - horizontal;
\r
162 else if (horizontal < 0)
\r
164 er = sr - horizontal;
\r
167 else if (vertical > 0) // scroll down
\r
169 ss = es - vertical;
\r
170 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
176 transY = imgHeight - vertical * av.charHeight;
\r
179 else if (vertical < 0)
\r
181 es = ss - vertical;
\r
182 if (es > av.endSeq)
\r
188 gg.translate(transX, transY);
\r
190 gg.setColor(Color.white);
\r
191 gg.fillRect(0, 0, (er - sr + 1) * av.charWidth, (es - ss) * av.charHeight);
\r
192 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
193 gg.translate( -transX, -transY);
\r
201 * Definitions of startx and endx (hopefully):
\r
202 * SMJS This is what I'm working towards!
\r
203 * startx is the first residue (starting at 0) to display.
\r
204 * endx is the last residue to display (starting at 0).
\r
205 * starty is the first sequence to display (starting at 0).
\r
206 * endy is the last sequence to display (starting at 0).
\r
207 * NOTE 1: The av limits are set in setFont in this class and
\r
208 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
210 public void update(Graphics g)
\r
215 public void paint(Graphics g)
\r
217 sr.renderGaps(av.renderGaps);
\r
221 g.drawImage(img, 0, 0, this);
\r
226 // this draws the whole of the alignment
\r
227 imgWidth = this.getSize().width;
\r
228 imgHeight = this.getSize().height;
\r
230 imgWidth -= imgWidth % av.charWidth;
\r
231 imgHeight -= imgHeight % av.charHeight;
\r
233 if (imgWidth < 1 || imgHeight < 1)
\r
238 if (img == null || imgWidth != img.getWidth(this) ||
\r
239 imgHeight != img.getHeight(this))
\r
241 img = createImage(imgWidth, imgHeight);
\r
242 gg = img.getGraphics();
\r
243 if(!jalview.bin.JalviewLite.AWT1)
\r
245 MyGraphics.AntiAlias(gg);
\r
247 gg.setFont(av.getFont());
\r
250 gg.setColor(Color.white);
\r
251 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
254 if (av.getWrapAlignment())
\r
256 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
\r
260 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes,
\r
264 g.drawImage(img, 0, 0, this);
\r
268 int LABEL_WEST, LABEL_EAST;
\r
269 public int getWrappedCanvasWidth(int cwidth)
\r
271 FontMetrics fm = getFontMetrics(av.getFont());
\r
276 if (av.scaleRightWrapped)
\r
278 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
281 if (av.scaleLeftWrapped)
\r
283 LABEL_WEST = fm.stringWidth(getMask());
\r
286 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
291 * Generates a string of zeroes.
\r
297 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
304 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
307 AlignmentI al = av.getAlignment();
\r
309 FontMetrics fm = getFontMetrics(av.getFont());
\r
311 int LABEL_EAST = 0;
\r
313 if (av.scaleRightWrapped)
\r
315 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
318 int LABEL_WEST = 0;
\r
320 if (av.scaleLeftWrapped)
\r
322 LABEL_WEST = fm.stringWidth(getMask());
\r
325 int hgap = av.charHeight;
\r
326 if(av.scaleAboveWrapped)
\r
327 hgap += av.charHeight;
\r
329 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
330 int cHeight = av.getAlignment().getHeight() * av.charHeight;
\r
332 av.setWrappedWidth(cWidth);
\r
334 av.endRes = av.startRes + cWidth;
\r
337 int endx = (startRes + cWidth) - 1;
\r
341 while ((ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
343 g.setColor(Color.black);
\r
345 if (av.scaleLeftWrapped)
\r
347 drawWestScale(g, startRes, endx, ypos);
\r
350 if (av.scaleRightWrapped)
\r
352 g.translate(canvasWidth - LABEL_EAST, 0);
\r
353 drawEastScale(g, startRes, endx, ypos);
\r
354 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
357 g.translate(LABEL_WEST, 0);
\r
359 if (av.scaleAboveWrapped)
\r
361 drawNorthScale(g, startRes, endx, ypos);
\r
365 if (av.vconsensus!=null && av.alignment.getWidth() >= av.vconsensus.size())
\r
367 endx = av.vconsensus.size() - 2;
\r
371 if(g.getClip()==null)
\r
372 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
374 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
378 if(av.showAnnotation)
\r
380 g.translate(0, cHeight + ypos+4);
\r
381 if(annotations==null)
\r
382 annotations = new AnnotationPanel(av);
\r
384 annotations.drawComponent( g, startRes, endx + 1);
\r
385 g.translate(0, -cHeight - ypos-4);
\r
387 g.translate(-LABEL_WEST, 0);
\r
389 ypos += cHeight+getAnnotationHeight()+hgap;
\r
392 startRes += cWidth;
\r
393 endx = (startRes + cWidth) - 1;
\r
395 if (endx > al.getWidth())
\r
397 endx = al.getWidth();
\r
403 AnnotationPanel annotations;
\r
404 int getAnnotationHeight()
\r
406 if(!av.showAnnotation)
\r
409 if(annotations==null)
\r
410 annotations = new AnnotationPanel(av);
\r
412 return annotations.adjustPanelHeight();
\r
415 synchronized public void drawPanel(Graphics g, int x1, int x2, int y1, int y2,
\r
416 int startx, int starty, int offset)
\r
419 g.setFont(av.getFont());
\r
420 sr.renderGaps(av.renderGaps);
\r
423 /// First draw the sequences
\r
424 /////////////////////////////
\r
425 for (int i = y1; i < y2; i++)
\r
427 nextSeq = av.alignment.getSequenceAt(i);
\r
429 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
430 (x1 - startx) * av.charWidth,
\r
432 (i - starty) * av.charHeight,
\r
433 av.charWidth, av.charHeight);
\r
435 if (av.showSequenceFeatures)
\r
437 fr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
438 (x1 - startx) * av.charWidth,
\r
440 (i - starty) * av.charHeight,
\r
441 av.charWidth, av.charHeight);
\r
445 /////////////////////////////////////
\r
447 // Now outline any areas if necessary
\r
448 /////////////////////////////////////
\r
449 SequenceGroup group = av.getSelectionGroup();
\r
450 java.util.Vector groups = av.alignment.getGroups();
\r
452 int sx = -1, sy = -1, ex = -1;
\r
453 int groupIndex = -1;
\r
454 if (group == null && groups.size() > 0)
\r
456 group = (SequenceGroup) groups.elementAt(0);
\r
459 if (group != null && !isOverview)
\r
465 boolean inGroup = false;
\r
469 for (i = y1; i < y2; i++)
\r
471 sx = (group.getStartRes() - startx) * av.charWidth;
\r
472 sy = offset + ((i - starty) * av.charHeight);
\r
473 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
476 if(sx+ex<0 || sx>imgWidth)
\r
481 if ( (sx <= (x2-x1)*av.charWidth) &&
\r
482 group.sequences.contains(av.alignment.getSequenceAt(
\r
485 if ((bottom == -1) &&
\r
486 !group.sequences.contains(
\r
487 av.alignment.getSequenceAt(i + 1)))
\r
489 bottom = sy + av.charHeight;
\r
494 if (((top == -1) && (i == 0)) ||
\r
495 !group.sequences.contains(
\r
496 av.alignment.getSequenceAt(i - 1)))
\r
504 if (group == av.getSelectionGroup())
\r
507 g.setColor(Color.red);
\r
511 g.setColor(group.getOutlineColour());
\r
519 if (sx >= 0 && sx < imgWidth)
\r
520 g.drawLine(sx, oldY, sx, sy);
\r
522 if (sx + ex < imgWidth)
\r
523 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
531 if (sx + ex > imgWidth)
\r
534 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
535 ex = (x2 - x1 + 1) * av.charWidth;
\r
539 g.drawLine(sx, top, sx + ex, top);
\r
545 g.drawLine(sx, bottom, sx + ex, bottom);
\r
556 sy = offset + ( (i - starty) * av.charHeight);
\r
557 if (sx >= 0 && sx < imgWidth)
\r
558 g.drawLine(sx, oldY, sx, sy);
\r
560 if (sx + ex < imgWidth)
\r
561 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
569 if (sx + ex > imgWidth)
\r
571 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
572 ex = (x2 - x1 + 1) * av.charWidth;
\r
576 g.drawLine(sx, top, sx + ex, top);
\r
582 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
591 if (groupIndex >= groups.size())
\r
596 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
598 while (groupIndex < groups.size());
\r
601 /// Highlight search Results once all sequences have been drawn
\r
602 //////////////////////////////////////////////////////////
\r
605 for (int r = 0; r < searchResults.length; r += 3)
\r
607 int searchSeq = searchResults[r];
\r
609 if (searchSeq >= y1 && searchSeq < y2)
\r
611 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
613 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
614 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
616 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
617 if (searchStart < x1)
\r
621 if (searchEnd > x2)
\r
626 ssr.drawHighlightedText(seq,
\r
629 (searchStart - startx) * av.charWidth,
\r
631 (searchSeq-starty)*av.charHeight,
\r
640 public void highlightSearchResults(int[] results)
\r
642 // results are in the order sequence, startRes, endRes
\r
643 if (results == null)
\r
645 displaySearch = false;
\r
649 displaySearch = true;
\r
652 searchResults = results;
\r