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 FontMetrics fm = getFontMetrics(av.getFont());
\r
302 if (av.scaleRightWrapped)
\r
304 LABEL_EAST = fm.stringWidth(getMask());
\r
307 if (av.scaleLeftWrapped)
\r
309 LABEL_WEST = fm.stringWidth(getMask());
\r
312 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
317 * Generates a string of zeroes.
\r
322 String mask = "00";
\r
323 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
330 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
333 AlignmentI al = av.getAlignment();
\r
335 FontMetrics fm = getFontMetrics(av.getFont());
\r
337 int LABEL_EAST = 0;
\r
339 if (av.scaleRightWrapped)
\r
341 LABEL_EAST = fm.stringWidth(getMask());
\r
344 int LABEL_WEST = 0;
\r
346 if (av.scaleLeftWrapped)
\r
348 LABEL_WEST = fm.stringWidth(getMask());
\r
351 int hgap = av.charHeight;
\r
352 if(av.scaleAboveWrapped)
\r
353 hgap += av.charHeight;
\r
355 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
356 int cHeight = av.getAlignment().getHeight() * av.charHeight;
\r
358 av.setWrappedWidth(cWidth);
\r
360 av.endRes = av.startRes + cWidth;
\r
367 while ((ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
369 endx = startRes + cWidth -1;
\r
371 if (endx > al.getWidth())
\r
373 endx = al.getWidth();
\r
376 g.setColor(Color.black);
\r
378 if (av.scaleLeftWrapped)
\r
380 drawWestScale(g, startRes, endx, ypos);
\r
383 if (av.scaleRightWrapped)
\r
385 g.translate(canvasWidth - LABEL_EAST, 0);
\r
386 drawEastScale(g, startRes, endx, ypos);
\r
387 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
390 g.translate(LABEL_WEST, 0);
\r
392 if (av.scaleAboveWrapped)
\r
394 drawNorthScale(g, startRes, endx, ypos);
\r
397 if(g.getClip()==null)
\r
398 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
400 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
\r
404 if(av.showAnnotation)
\r
406 g.translate(0, cHeight + ypos+4);
\r
407 if(annotations==null)
\r
408 annotations = new AnnotationPanel(av);
\r
410 annotations.drawComponent( g, startRes, endx+1 );
\r
411 g.translate(0, -cHeight - ypos-4);
\r
413 g.translate(-LABEL_WEST, 0);
\r
415 ypos += cHeight+getAnnotationHeight()+hgap;
\r
418 startRes += cWidth;
\r
423 AnnotationPanel annotations;
\r
424 int getAnnotationHeight()
\r
426 if(!av.showAnnotation)
\r
429 if(annotations==null)
\r
430 annotations = new AnnotationPanel(av);
\r
432 return annotations.adjustPanelHeight();
\r
435 void drawPanel(Graphics g, int startRes, int endRes, int startSeq, int endSeq, int offset)
\r
438 g.setFont(av.getFont());
\r
439 sr.renderGaps(av.renderGaps);
\r
442 /// First draw the sequences
\r
443 /////////////////////////////
\r
444 for (int i = startSeq; i < endSeq; i++)
\r
446 nextSeq = av.alignment.getSequenceAt(i);
\r
448 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), startRes, endRes,
\r
449 offset + ( (i - startSeq) * av.charHeight));
\r
451 if (av.showSequenceFeatures)
\r
453 fr.drawSequence(g, nextSeq, startRes, endRes,
\r
454 offset + ((i - startSeq) * av.charHeight),
\r
455 av.charWidth, av.charHeight);
\r
457 /// Highlight search Results once all sequences have been drawn
\r
458 //////////////////////////////////////////////////////////
\r
459 if (searchResults != null)
\r
461 int[] visibleResults = searchResults.getResults(nextSeq, startRes, endRes);
\r
462 if (visibleResults != null)
\r
463 for (int r = 0; r < visibleResults.length; r += 2)
\r
465 sr.drawHighlightedText(nextSeq, visibleResults[r],
\r
466 visibleResults[r + 1],
\r
467 (visibleResults[r] - startRes) * av.charWidth,
\r
468 offset + ( (i - startSeq) * av.charHeight),
\r
469 av.charWidth, av.charHeight);
\r
473 if (av.cursorMode && cursorY == i
\r
474 && cursorX >= startRes && cursorX <= endRes)
\r
476 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * av.charWidth,
\r
477 offset + ( (i - startSeq) * av.charHeight));
\r
483 /////////////////////////////////////
\r
484 // Now outline any areas if necessary
\r
485 /////////////////////////////////////
\r
486 SequenceGroup group = av.getSelectionGroup();
\r
491 int groupIndex = -1;
\r
493 if ((group == null) && (av.alignment.getGroups().size() > 0))
\r
495 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
\r
499 if ( group != null)
\r
505 boolean inGroup = false;
\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.sequences.contains(av.alignment.getSequenceAt(
\r
527 if(i == endSeq-1 || // Dont check for i+1 if on the bottom row
\r
528 !group.sequences.contains(av.alignment.getSequenceAt(i+1 )))
\r
530 bottom = sy + av.charHeight;
\r
535 if (((top == -1) && (i == 0)) ||
\r
536 !group.sequences.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