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 SearchResults searchResults = null;
\r
43 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
54 public AlignViewport getViewport()
\r
59 public FeatureRenderer getFeatureRenderer()
\r
64 MCview.AppletPDBCanvas pdbCanvas;
\r
65 public SequenceRenderer getSequenceRenderer()
\r
70 public void setPDBCanvas(MCview.AppletPDBCanvas pc)
\r
76 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
78 int scalestartx = startx - startx % 10 + 10;
\r
80 g.setColor(Color.black);
\r
83 for (int i = scalestartx; i < endx; i += 10)
\r
85 String string = String.valueOf(i);
\r
86 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
87 ypos - av.charHeight / 2);
\r
89 g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,
\r
90 ypos + 2 - av.charHeight / 2,
\r
91 (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);
\r
96 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
98 FontMetrics fm = getFontMetrics(av.getFont());
\r
99 ypos += av.charHeight;
\r
101 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
103 SequenceI seq = av.alignment.getSequenceAt(i);
\r
104 int index = startx;
\r
106 while (index < endx)
\r
108 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
114 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
119 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))-av.charWidth/2;
\r
120 g.drawString(value + "", x,
\r
121 ypos + i * av.charHeight - av.charHeight / 5);
\r
126 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
128 ypos += av.charHeight;
\r
130 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
132 SequenceI seq = av.alignment.getSequenceAt(i);
\r
135 while (index > startx)
\r
137 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
143 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
148 g.drawString(value + "", av.charWidth/2,
\r
149 ypos + i * av.charHeight - av.charHeight / 5);
\r
156 void fastPaint(int horizontal, int vertical)
\r
158 if ( fastPaint || gg == null)
\r
164 // Its possible on certain browsers that the call to fastpaint
\r
165 // is faster than it can paint, so this check here catches
\r
166 // this possibility
\r
167 if(lastsr + horizontal != av.startRes)
\r
169 horizontal = av.startRes - lastsr;
\r
172 lastsr = av.startRes;
\r
175 gg.copyArea(horizontal * av.charWidth,
\r
176 vertical * av.charHeight,
\r
177 imgWidth - horizontal * av.charWidth,
\r
178 imgHeight - vertical * av.charHeight,
\r
179 -horizontal * av.charWidth,
\r
180 -vertical * av.charHeight);
\r
184 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
185 transX = 0, transY = 0;
\r
187 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
189 transX = (er - sr - horizontal) * av.charWidth;
\r
190 sr = er - horizontal;
\r
192 else if (horizontal < 0)
\r
194 er = sr - horizontal;
\r
197 else if (vertical > 0) // scroll down
\r
199 ss = es - vertical;
\r
200 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
206 transY = imgHeight - vertical * av.charHeight;
\r
209 else if (vertical < 0)
\r
211 es = ss - vertical;
\r
212 if (es > av.endSeq)
\r
218 gg.translate(transX, transY);
\r
220 drawPanel(gg, sr, er, ss, es, 0);
\r
221 gg.translate( -transX, -transY);
\r
228 * Definitions of startx and endx (hopefully):
\r
229 * SMJS This is what I'm working towards!
\r
230 * startx is the first residue (starting at 0) to display.
\r
231 * endx is the last residue to display (starting at 0).
\r
232 * starty is the first sequence to display (starting at 0).
\r
233 * endy is the last sequence to display (starting at 0).
\r
234 * NOTE 1: The av limits are set in setFont in this class and
\r
235 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
237 public void update(Graphics g)
\r
242 public void paint(Graphics g)
\r
247 g.drawImage(img, 0, 0, this);
\r
252 // this draws the whole of the alignment
\r
253 imgWidth = this.getSize().width;
\r
254 imgHeight = this.getSize().height;
\r
256 imgWidth -= imgWidth % av.charWidth;
\r
257 imgHeight -= imgHeight % av.charHeight;
\r
259 if (imgWidth < 1 || imgHeight < 1)
\r
264 if (img == null || imgWidth != img.getWidth(this) ||
\r
265 imgHeight != img.getHeight(this))
\r
267 img = createImage(imgWidth, imgHeight);
\r
268 gg = img.getGraphics();
\r
269 gg.setFont(av.getFont());
\r
272 gg.setColor(Color.white);
\r
273 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
276 if (av.getWrapAlignment())
\r
278 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
\r
282 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
\r
285 g.drawImage(img, 0, 0, this);
\r
287 if (pdbCanvas != null)
\r
289 pdbCanvas.updateSeqColours();
\r
293 int LABEL_WEST, LABEL_EAST;
\r
294 public int getWrappedCanvasWidth(int cwidth)
\r
296 FontMetrics fm = getFontMetrics(av.getFont());
\r
301 if (av.scaleRightWrapped)
\r
303 LABEL_EAST = fm.stringWidth(getMask());
\r
306 if (av.scaleLeftWrapped)
\r
308 LABEL_WEST = fm.stringWidth(getMask());
\r
311 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
316 * Generates a string of zeroes.
\r
321 String mask = "00";
\r
322 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
329 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
332 AlignmentI al = av.getAlignment();
\r
334 FontMetrics fm = getFontMetrics(av.getFont());
\r
336 int LABEL_EAST = 0;
\r
338 if (av.scaleRightWrapped)
\r
340 LABEL_EAST = fm.stringWidth(getMask());
\r
343 int LABEL_WEST = 0;
\r
345 if (av.scaleLeftWrapped)
\r
347 LABEL_WEST = fm.stringWidth(getMask());
\r
350 int hgap = av.charHeight;
\r
351 if(av.scaleAboveWrapped)
\r
352 hgap += av.charHeight;
\r
354 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
355 int cHeight = av.getAlignment().getHeight() * av.charHeight;
\r
357 av.setWrappedWidth(cWidth);
\r
359 av.endRes = av.startRes + cWidth;
\r
366 while ((ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
368 endx = startRes + cWidth;
\r
370 if (endx > al.getWidth())
\r
372 endx = al.getWidth();
\r
375 g.setColor(Color.black);
\r
377 if (av.scaleLeftWrapped)
\r
379 drawWestScale(g, startRes, endx, ypos);
\r
382 if (av.scaleRightWrapped)
\r
384 g.translate(canvasWidth - LABEL_EAST, 0);
\r
385 drawEastScale(g, startRes, endx, ypos);
\r
386 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
389 g.translate(LABEL_WEST, 0);
\r
391 if (av.scaleAboveWrapped)
\r
393 drawNorthScale(g, startRes, endx, ypos);
\r
396 if(g.getClip()==null)
\r
397 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
399 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
\r
403 if(av.showAnnotation)
\r
405 g.translate(0, cHeight + ypos+4);
\r
406 if(annotations==null)
\r
407 annotations = new AnnotationPanel(av);
\r
409 annotations.drawComponent( g, startRes, endx );
\r
410 g.translate(0, -cHeight - ypos-4);
\r
412 g.translate(-LABEL_WEST, 0);
\r
414 ypos += cHeight+getAnnotationHeight()+hgap;
\r
417 startRes += cWidth;
\r
422 AnnotationPanel annotations;
\r
423 int getAnnotationHeight()
\r
425 if(!av.showAnnotation)
\r
428 if(annotations==null)
\r
429 annotations = new AnnotationPanel(av);
\r
431 return annotations.adjustPanelHeight();
\r
434 void drawPanel(Graphics g, int startRes, int endRes, int startSeq, int endSeq, int offset)
\r
437 g.setFont(av.getFont());
\r
438 sr.renderGaps(av.renderGaps);
\r
441 /// First draw the sequences
\r
442 /////////////////////////////
\r
443 for (int i = startSeq; i < endSeq; i++)
\r
445 nextSeq = av.alignment.getSequenceAt(i);
\r
447 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), startRes, endRes,
\r
448 offset + ( (i - startSeq) * av.charHeight));
\r
450 if (av.showSequenceFeatures)
\r
452 fr.drawSequence(g, nextSeq, startRes, endRes,
\r
453 offset + ((i - startSeq) * av.charHeight),
\r
454 av.charWidth, av.charHeight);
\r
456 /// Highlight search Results once all sequences have been drawn
\r
457 //////////////////////////////////////////////////////////
\r
458 if (searchResults != null)
\r
460 int[] visibleResults = searchResults.getResults(nextSeq, startRes, endRes);
\r
461 if (visibleResults != null)
\r
462 for (int r = 0; r < visibleResults.length; r += 2)
\r
464 sr.drawHighlightedText(nextSeq, visibleResults[r],
\r
465 visibleResults[r + 1],
\r
466 (visibleResults[r] - startRes) * av.charWidth,
\r
467 offset + ( (i - startSeq) * av.charHeight),
\r
468 av.charWidth, av.charHeight);
\r
474 /////////////////////////////////////
\r
475 // Now outline any areas if necessary
\r
476 /////////////////////////////////////
\r
477 SequenceGroup group = av.getSelectionGroup();
\r
482 int groupIndex = -1;
\r
484 if ((group == null) && (av.alignment.getGroups().size() > 0))
\r
486 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
\r
490 if ( group != null)
\r
496 boolean inGroup = false;
\r
500 for (i = startSeq; i < endSeq; i++)
\r
502 sx = (group.getStartRes() - startRes) * av.charWidth;
\r
503 sy = offset + ((i - startSeq) * av.charHeight);
\r
504 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
507 if(sx+ex<0 || sx>imgWidth)
\r
512 if ( (sx <= (endRes-startRes)*av.charWidth) &&
\r
513 group.sequences.contains(av.alignment.getSequenceAt(
\r
518 if(i == endSeq-1 || // Dont check for i+1 if on the bottom row
\r
519 !group.sequences.contains(av.alignment.getSequenceAt(i+1 )))
\r
521 bottom = sy + av.charHeight;
\r
526 if (((top == -1) && (i == 0)) ||
\r
527 !group.sequences.contains(
\r
528 av.alignment.getSequenceAt(i - 1)))
\r
536 if (group == av.getSelectionGroup())
\r
538 g.setColor(Color.red);
\r
542 g.setColor(group.getOutlineColour());
\r
550 if (sx >= 0 && sx < imgWidth)
\r
551 g.drawLine(sx, oldY, sx, sy);
\r
553 if (sx + ex < imgWidth)
\r
554 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
562 if (sx + ex > imgWidth)
\r
565 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
\r
566 ex = (endRes - startRes + 1) * av.charWidth;
\r
570 g.drawLine(sx, top, sx + ex, top);
\r
576 g.drawLine(sx, bottom, sx + ex, bottom);
\r
587 sy = offset + ( (i - startSeq) * av.charHeight);
\r
588 if (sx >= 0 && sx < imgWidth)
\r
589 g.drawLine(sx, oldY, sx, sy);
\r
591 if (sx + ex < imgWidth)
\r
592 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
600 if (sx + ex > imgWidth)
\r
602 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
\r
603 ex = (endRes - startRes + 1) * av.charWidth;
\r
607 g.drawLine(sx, top, sx + ex, top);
\r
613 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
622 if (groupIndex >= av.alignment.getGroups().size())
\r
627 group = (SequenceGroup) av.alignment.getGroups().elementAt(groupIndex);
\r
629 while (groupIndex < av.alignment.getGroups().size());
\r
633 public void highlightSearchResults(SearchResults results)
\r
635 searchResults = results;
\r