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
40 boolean fastPaint = false;
\r
47 public SeqCanvas(AlignViewport av)
\r
50 fr = new FeatureRenderer(av);
\r
51 sr = new SequenceRenderer(av);
\r
52 PaintRefresher.Register(this, av.alignment);
\r
55 public AlignViewport getViewport()
\r
60 public FeatureRenderer getFeatureRenderer()
\r
65 MCview.AppletPDBCanvas pdbCanvas;
\r
66 public SequenceRenderer getSequenceRenderer()
\r
71 public void setPDBCanvas(MCview.AppletPDBCanvas pc)
\r
77 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
79 int scalestartx = startx - startx % 10 + 10;
\r
81 g.setColor(Color.black);
\r
84 for (int i = scalestartx; i < endx; i += 10)
\r
86 String string = String.valueOf(i);
\r
87 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
88 ypos - av.charHeight / 2);
\r
90 g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,
\r
91 ypos + 2 - av.charHeight / 2,
\r
92 (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);
\r
97 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
99 FontMetrics fm = getFontMetrics(av.getFont());
\r
100 ypos += av.charHeight;
\r
102 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
104 SequenceI seq = av.alignment.getSequenceAt(i);
\r
105 int index = startx;
\r
107 while (index < endx)
\r
109 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
115 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
120 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))-av.charWidth/2;
\r
121 g.drawString(value + "", x,
\r
122 ypos + i * av.charHeight - av.charHeight / 5);
\r
127 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
129 ypos += av.charHeight;
\r
131 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
133 SequenceI seq = av.alignment.getSequenceAt(i);
\r
136 while (index > startx)
\r
138 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
144 value = seq.findPosition(index);
\r
149 g.drawString(value + "", av.charWidth/2,
\r
150 ypos + i * av.charHeight - av.charHeight / 5);
\r
157 void fastPaint(int horizontal, int vertical)
\r
159 if ( fastPaint || gg == null)
\r
165 // Its possible on certain browsers that the call to fastpaint
\r
166 // is faster than it can paint, so this check here catches
\r
167 // this possibility
\r
168 if(lastsr + horizontal != av.startRes)
\r
170 horizontal = av.startRes - lastsr;
\r
173 lastsr = av.startRes;
\r
176 gg.copyArea(horizontal * av.charWidth,
\r
177 vertical * av.charHeight,
\r
178 imgWidth - horizontal * av.charWidth,
\r
179 imgHeight - vertical * av.charHeight,
\r
180 -horizontal * av.charWidth,
\r
181 -vertical * av.charHeight);
\r
185 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
186 transX = 0, transY = 0;
\r
188 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
190 transX = (er - sr - horizontal) * av.charWidth;
\r
191 sr = er - horizontal;
\r
193 else if (horizontal < 0)
\r
195 er = sr - horizontal;
\r
198 else if (vertical > 0) // scroll down
\r
200 ss = es - vertical;
\r
201 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
207 transY = imgHeight - vertical * av.charHeight;
\r
210 else if (vertical < 0)
\r
212 es = ss - vertical;
\r
213 if (es > av.endSeq)
\r
219 gg.translate(transX, transY);
\r
221 drawPanel(gg, sr, er, ss, es, 0);
\r
222 gg.translate( -transX, -transY);
\r
229 * Definitions of startx and endx (hopefully):
\r
230 * SMJS This is what I'm working towards!
\r
231 * startx is the first residue (starting at 0) to display.
\r
232 * endx is the last residue to display (starting at 0).
\r
233 * starty is the first sequence to display (starting at 0).
\r
234 * endy is the last sequence to display (starting at 0).
\r
235 * NOTE 1: The av limits are set in setFont in this class and
\r
236 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
238 public void update(Graphics g)
\r
243 public void paint(Graphics g)
\r
248 g.drawImage(img, 0, 0, this);
\r
253 // this draws the whole of the alignment
\r
254 imgWidth = this.getSize().width;
\r
255 imgHeight = this.getSize().height;
\r
257 imgWidth -= imgWidth % av.charWidth;
\r
258 imgHeight -= imgHeight % av.charHeight;
\r
260 if (imgWidth < 1 || imgHeight < 1)
\r
265 if (img == null || imgWidth != img.getWidth(this) ||
\r
266 imgHeight != img.getHeight(this))
\r
268 img = createImage(imgWidth, imgHeight);
\r
269 gg = img.getGraphics();
\r
270 gg.setFont(av.getFont());
\r
273 gg.setColor(Color.white);
\r
274 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
277 if (av.getWrapAlignment())
\r
279 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
\r
283 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
\r
286 g.drawImage(img, 0, 0, this);
\r
288 if (pdbCanvas != null)
\r
290 pdbCanvas.updateSeqColours();
\r
294 int LABEL_WEST, LABEL_EAST;
\r
295 public int getWrappedCanvasWidth(int cwidth)
\r
297 cwidth -= cwidth % av.charWidth;
\r
299 FontMetrics fm = getFontMetrics(av.getFont());
\r
304 if (av.scaleRightWrapped)
\r
306 LABEL_EAST = fm.stringWidth(getMask());
\r
309 if (av.scaleLeftWrapped)
\r
311 LABEL_WEST = fm.stringWidth(getMask());
\r
314 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
319 * Generates a string of zeroes.
\r
324 String mask = "00";
\r
325 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
332 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
335 AlignmentI al = av.getAlignment();
\r
337 FontMetrics fm = getFontMetrics(av.getFont());
\r
340 if (av.scaleRightWrapped)
\r
342 LABEL_EAST = fm.stringWidth(getMask());
\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 -1;
\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+1 );
\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
472 if (av.cursorMode && cursorY == i
\r
473 && cursorX >= startRes && cursorX <= endRes)
\r
475 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * av.charWidth,
\r
476 offset + ( (i - startSeq) * av.charHeight));
\r
482 /////////////////////////////////////
\r
483 // Now outline any areas if necessary
\r
484 /////////////////////////////////////
\r
485 SequenceGroup group = av.getSelectionGroup();
\r
490 int groupIndex = -1;
\r
492 if ((group == null) && (av.alignment.getGroups().size() > 0))
\r
494 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
\r
498 if ( group != null )
\r
504 boolean inGroup = false;
\r
507 int alHeight = av.alignment.getHeight()-1;
\r
509 for (i = startSeq; i < endSeq; i++)
\r
511 sx = (group.getStartRes() - startRes) * av.charWidth;
\r
512 sy = offset + ((i - startSeq) * av.charHeight);
\r
513 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
516 if(sx+ex<0 || sx>imgWidth)
\r
521 if ( (sx <= (endRes-startRes)*av.charWidth) &&
\r
522 group.getSequences(false).contains(av.alignment.getSequenceAt(
\r
525 if ( (bottom == -1) &&
\r
527 !group.getSequences(false).contains(
\r
528 av.alignment.getSequenceAt(i + 1))))
\r
530 bottom = sy + av.charHeight;
\r
535 if (((top == -1) && (i == 0)) ||
\r
536 !group.getSequences(false).contains(
\r
537 av.alignment.getSequenceAt(i - 1)))
\r
545 if (group == av.getSelectionGroup())
\r
547 g.setColor(Color.red);
\r
551 g.setColor(group.getOutlineColour());
\r
559 if (sx >= 0 && sx < imgWidth)
\r
560 g.drawLine(sx, oldY, sx, sy);
\r
562 if (sx + ex < imgWidth)
\r
563 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
571 if (sx + ex > imgWidth)
\r
574 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
\r
575 ex = (endRes - startRes + 1) * av.charWidth;
\r
579 g.drawLine(sx, top, sx + ex, top);
\r
585 g.drawLine(sx, bottom, sx + ex, bottom);
\r
596 sy = offset + ( (i - startSeq) * av.charHeight);
\r
597 if (sx >= 0 && sx < imgWidth)
\r
598 g.drawLine(sx, oldY, sx, sy);
\r
600 if (sx + ex < imgWidth)
\r
601 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
609 if (sx + ex > imgWidth)
\r
611 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
\r
612 ex = (endRes - startRes + 1) * av.charWidth;
\r
616 g.drawLine(sx, top, sx + ex, top);
\r
622 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
631 if (groupIndex >= av.alignment.getGroups().size())
\r
636 group = (SequenceGroup) av.alignment.getGroups().elementAt(groupIndex);
\r
638 while (groupIndex < av.alignment.getGroups().size());
\r
642 public void highlightSearchResults(SearchResults results)
\r
644 searchResults = results;
\r