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
155 public void fastPaint(int horizontal, int vertical)
\r
157 if (fastPaint || gg == null)
\r
164 gg.copyArea(horizontal * av.charWidth,
\r
165 vertical * av.charHeight,
\r
168 -horizontal * av.charWidth,
\r
169 -vertical * av.charHeight);
\r
171 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
172 transX = 0, transY = 0;
\r
174 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
176 transX = (er - sr - horizontal) * av.charWidth;
\r
177 sr = er - horizontal;
\r
179 else if (horizontal < 0)
\r
181 er = sr - horizontal;
\r
184 else if (vertical > 0) // scroll down
\r
186 ss = es - vertical;
\r
187 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
193 transY = imgHeight - vertical * av.charHeight;
\r
196 else if (vertical < 0)
\r
198 es = ss - vertical;
\r
199 if (es > av.endSeq)
\r
205 gg.translate(transX, transY);
\r
207 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
208 gg.translate( -transX, -transY);
\r
215 * Definitions of startx and endx (hopefully):
\r
216 * SMJS This is what I'm working towards!
\r
217 * startx is the first residue (starting at 0) to display.
\r
218 * endx is the last residue to display (starting at 0).
\r
219 * starty is the first sequence to display (starting at 0).
\r
220 * endy is the last sequence to display (starting at 0).
\r
221 * NOTE 1: The av limits are set in setFont in this class and
\r
222 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
224 public void update(Graphics g)
\r
229 public void paint(Graphics g)
\r
234 g.drawImage(img, 0, 0, this);
\r
239 // this draws the whole of the alignment
\r
240 imgWidth = this.getSize().width;
\r
241 imgHeight = this.getSize().height;
\r
243 imgWidth -= imgWidth % av.charWidth;
\r
244 imgHeight -= imgHeight % av.charHeight;
\r
246 if (imgWidth < 1 || imgHeight < 1)
\r
251 if (img == null || imgWidth != img.getWidth(this) ||
\r
252 imgHeight != img.getHeight(this))
\r
254 img = createImage(imgWidth, imgHeight);
\r
255 gg = img.getGraphics();
\r
256 gg.setFont(av.getFont());
\r
259 gg.setColor(Color.white);
\r
260 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
263 if (av.getWrapAlignment())
\r
265 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
\r
269 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes,
\r
273 g.drawImage(img, 0, 0, this);
\r
275 if (pdbCanvas != null)
\r
277 pdbCanvas.updateSeqColours();
\r
281 int LABEL_WEST, LABEL_EAST;
\r
282 public int getWrappedCanvasWidth(int cwidth)
\r
284 FontMetrics fm = getFontMetrics(av.getFont());
\r
289 if (av.scaleRightWrapped)
\r
291 LABEL_EAST = fm.stringWidth(getMask());
\r
294 if (av.scaleLeftWrapped)
\r
296 LABEL_WEST = fm.stringWidth(getMask());
\r
299 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
304 * Generates a string of zeroes.
\r
309 String mask = "00";
\r
310 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
317 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
320 AlignmentI al = av.getAlignment();
\r
322 FontMetrics fm = getFontMetrics(av.getFont());
\r
324 int LABEL_EAST = 0;
\r
326 if (av.scaleRightWrapped)
\r
328 LABEL_EAST = fm.stringWidth(getMask());
\r
331 int LABEL_WEST = 0;
\r
333 if (av.scaleLeftWrapped)
\r
335 LABEL_WEST = fm.stringWidth(getMask());
\r
338 int hgap = av.charHeight;
\r
339 if(av.scaleAboveWrapped)
\r
340 hgap += av.charHeight;
\r
342 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
343 int cHeight = av.getAlignment().getHeight() * av.charHeight;
\r
345 av.setWrappedWidth(cWidth);
\r
347 av.endRes = av.startRes + cWidth;
\r
350 int endx = (startRes + cWidth) - 1;
\r
354 while ((ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
356 g.setColor(Color.black);
\r
358 if (av.scaleLeftWrapped)
\r
360 drawWestScale(g, startRes, endx, ypos);
\r
363 if (av.scaleRightWrapped)
\r
365 g.translate(canvasWidth - LABEL_EAST, 0);
\r
366 drawEastScale(g, startRes, endx, ypos);
\r
367 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
370 g.translate(LABEL_WEST, 0);
\r
372 if (av.scaleAboveWrapped)
\r
374 drawNorthScale(g, startRes, endx, ypos);
\r
378 if (av.vconsensus!=null && av.alignment.getWidth() >= av.vconsensus.size())
\r
380 endx = av.vconsensus.size() - 2;
\r
384 if(g.getClip()==null)
\r
385 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
387 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
391 if(av.showAnnotation)
\r
393 g.translate(0, cHeight + ypos+4);
\r
394 if(annotations==null)
\r
395 annotations = new AnnotationPanel(av);
\r
397 annotations.drawComponent( g, startRes, endx + 1);
\r
398 g.translate(0, -cHeight - ypos-4);
\r
400 g.translate(-LABEL_WEST, 0);
\r
402 ypos += cHeight+getAnnotationHeight()+hgap;
\r
405 startRes += cWidth;
\r
406 endx = (startRes + cWidth) - 1;
\r
408 if (endx > al.getWidth())
\r
410 endx = al.getWidth();
\r
416 AnnotationPanel annotations;
\r
417 int getAnnotationHeight()
\r
419 if(!av.showAnnotation)
\r
422 if(annotations==null)
\r
423 annotations = new AnnotationPanel(av);
\r
425 return annotations.adjustPanelHeight();
\r
428 synchronized public void drawPanel(Graphics g, int x1, int x2, int y1, int y2,
\r
429 int startx, int starty, int offset)
\r
432 g.setFont(av.getFont());
\r
433 sr.renderGaps(av.renderGaps);
\r
436 /// First draw the sequences
\r
437 /////////////////////////////
\r
438 for (int i = y1; i < y2; i++)
\r
440 nextSeq = av.alignment.getSequenceAt(i);
\r
442 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
443 (x1 - startx) * av.charWidth,
\r
445 (i - starty) * av.charHeight,
\r
446 av.charWidth, av.charHeight);
\r
448 if (av.showSequenceFeatures)
\r
450 fr.drawSequence(g, nextSeq, x1, x2,
\r
451 (x1 - startx) * av.charWidth,
\r
453 (i - starty) * 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, x1, x2);
\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] - startx) * av.charWidth,
\r
467 offset + ( (i - starty) * av.charHeight),
\r
468 av.charWidth, av.charHeight);
\r
473 /////////////////////////////////////
\r
475 // Now outline any areas if necessary
\r
476 /////////////////////////////////////
\r
477 SequenceGroup group = av.getSelectionGroup();
\r
478 java.util.Vector groups = av.alignment.getGroups();
\r
480 int sx = -1, sy = -1, ex = -1;
\r
481 int groupIndex = -1;
\r
482 if (group == null && groups.size() > 0)
\r
484 group = (SequenceGroup) groups.elementAt(0);
\r
493 boolean inGroup = false;
\r
497 for (i = y1; i < y2; i++)
\r
499 sx = (group.getStartRes() - startx) * av.charWidth;
\r
500 sy = offset + ((i - starty) * av.charHeight);
\r
501 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
504 if(sx+ex<0 || sx>imgWidth)
\r
509 if ( (sx <= (x2-x1)*av.charWidth) &&
\r
510 group.sequences.contains(av.alignment.getSequenceAt(
\r
516 if(i == y2-1 || // Dont check for i+1 if on the bottom row
\r
517 !group.sequences.contains(av.alignment.getSequenceAt(i+1 )))
\r
519 bottom = sy + av.charHeight;
\r
524 if (((top == -1) && (i == 0)) ||
\r
525 !group.sequences.contains(
\r
526 av.alignment.getSequenceAt(i - 1)))
\r
534 if (group == av.getSelectionGroup())
\r
537 g.setColor(Color.red);
\r
541 g.setColor(group.getOutlineColour());
\r
549 if (sx >= 0 && sx < imgWidth)
\r
550 g.drawLine(sx, oldY, sx, sy);
\r
552 if (sx + ex < imgWidth)
\r
553 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
561 if (sx + ex > imgWidth)
\r
564 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
565 ex = (x2 - x1 + 1) * av.charWidth;
\r
569 g.drawLine(sx, top, sx + ex, top);
\r
575 g.drawLine(sx, bottom, sx + ex, bottom);
\r
586 sy = offset + ( (i - starty) * av.charHeight);
\r
587 if (sx >= 0 && sx < imgWidth)
\r
588 g.drawLine(sx, oldY, sx, sy);
\r
590 if (sx + ex < imgWidth)
\r
591 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
599 if (sx + ex > imgWidth)
\r
601 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
602 ex = (x2 - x1 + 1) * av.charWidth;
\r
606 g.drawLine(sx, top, sx + ex, top);
\r
612 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
621 if (groupIndex >= groups.size())
\r
626 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
628 while (groupIndex < groups.size());
\r
632 public void highlightSearchResults(SearchResults results)
\r
634 searchResults = results;
\r