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
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 = av.alignment.getSequenceAt(i).findPosition(index);
\r
149 g.drawString(value + "", av.charWidth/2,
\r
150 ypos + i * av.charHeight - av.charHeight / 5);
\r
156 public void fastPaint(int horizontal, int vertical)
\r
158 if (fastPaint || gg == null)
\r
165 gg.copyArea(horizontal * av.charWidth,
\r
166 vertical * av.charHeight,
\r
169 -horizontal * av.charWidth,
\r
170 -vertical * av.charHeight);
\r
172 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
173 transX = 0, transY = 0;
\r
175 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
177 transX = (er - sr - horizontal) * av.charWidth;
\r
178 sr = er - horizontal;
\r
180 else if (horizontal < 0)
\r
182 er = sr - horizontal;
\r
185 else if (vertical > 0) // scroll down
\r
187 ss = es - vertical;
\r
188 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
194 transY = imgHeight - vertical * av.charHeight;
\r
197 else if (vertical < 0)
\r
199 es = ss - vertical;
\r
200 if (es > av.endSeq)
\r
206 gg.translate(transX, transY);
\r
208 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
209 gg.translate( -transX, -transY);
\r
216 * Definitions of startx and endx (hopefully):
\r
217 * SMJS This is what I'm working towards!
\r
218 * startx is the first residue (starting at 0) to display.
\r
219 * endx is the last residue to display (starting at 0).
\r
220 * starty is the first sequence to display (starting at 0).
\r
221 * endy is the last sequence to display (starting at 0).
\r
222 * NOTE 1: The av limits are set in setFont in this class and
\r
223 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
225 public void update(Graphics g)
\r
230 public void paint(Graphics g)
\r
235 g.drawImage(img, 0, 0, this);
\r
240 // this draws the whole of the alignment
\r
241 imgWidth = this.getSize().width;
\r
242 imgHeight = this.getSize().height;
\r
244 imgWidth -= imgWidth % av.charWidth;
\r
245 imgHeight -= imgHeight % av.charHeight;
\r
247 if (imgWidth < 1 || imgHeight < 1)
\r
252 if (img == null || imgWidth != img.getWidth(this) ||
\r
253 imgHeight != img.getHeight(this))
\r
255 img = createImage(imgWidth, imgHeight);
\r
256 gg = img.getGraphics();
\r
257 gg.setFont(av.getFont());
\r
260 gg.setColor(Color.white);
\r
261 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
264 if (av.getWrapAlignment())
\r
266 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
\r
270 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes,
\r
274 g.drawImage(img, 0, 0, this);
\r
276 if (pdbCanvas != null)
\r
278 pdbCanvas.updateSeqColours();
\r
282 int LABEL_WEST, LABEL_EAST;
\r
283 public int getWrappedCanvasWidth(int cwidth)
\r
285 FontMetrics fm = getFontMetrics(av.getFont());
\r
290 if (av.scaleRightWrapped)
\r
292 LABEL_EAST = fm.stringWidth(getMask());
\r
295 if (av.scaleLeftWrapped)
\r
297 LABEL_WEST = fm.stringWidth(getMask());
\r
300 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
305 * Generates a string of zeroes.
\r
310 String mask = "00";
\r
311 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
318 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
321 AlignmentI al = av.getAlignment();
\r
323 FontMetrics fm = getFontMetrics(av.getFont());
\r
325 int LABEL_EAST = 0;
\r
327 if (av.scaleRightWrapped)
\r
329 LABEL_EAST = fm.stringWidth(getMask());
\r
332 int LABEL_WEST = 0;
\r
334 if (av.scaleLeftWrapped)
\r
336 LABEL_WEST = fm.stringWidth(getMask());
\r
339 int hgap = av.charHeight;
\r
340 if(av.scaleAboveWrapped)
\r
341 hgap += av.charHeight;
\r
343 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
344 int cHeight = av.getAlignment().getHeight() * av.charHeight;
\r
346 av.setWrappedWidth(cWidth);
\r
348 av.endRes = av.startRes + cWidth;
\r
351 int endx = (startRes + cWidth) - 1;
\r
355 while ((ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
357 g.setColor(Color.black);
\r
359 if (av.scaleLeftWrapped)
\r
361 drawWestScale(g, startRes, endx, ypos);
\r
364 if (av.scaleRightWrapped)
\r
366 g.translate(canvasWidth - LABEL_EAST, 0);
\r
367 drawEastScale(g, startRes, endx, ypos);
\r
368 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
371 g.translate(LABEL_WEST, 0);
\r
373 if (av.scaleAboveWrapped)
\r
375 drawNorthScale(g, startRes, endx, ypos);
\r
379 if (av.vconsensus!=null && av.alignment.getWidth() >= av.vconsensus.size())
\r
381 endx = av.vconsensus.size() - 2;
\r
385 if(g.getClip()==null)
\r
386 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
388 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
392 if(av.showAnnotation)
\r
394 g.translate(0, cHeight + ypos+4);
\r
395 if(annotations==null)
\r
396 annotations = new AnnotationPanel(av);
\r
398 annotations.drawComponent( g, startRes, endx + 1);
\r
399 g.translate(0, -cHeight - ypos-4);
\r
401 g.translate(-LABEL_WEST, 0);
\r
403 ypos += cHeight+getAnnotationHeight()+hgap;
\r
406 startRes += cWidth;
\r
407 endx = (startRes + cWidth) - 1;
\r
409 if (endx > al.getWidth())
\r
411 endx = al.getWidth();
\r
417 AnnotationPanel annotations;
\r
418 int getAnnotationHeight()
\r
420 if(!av.showAnnotation)
\r
423 if(annotations==null)
\r
424 annotations = new AnnotationPanel(av);
\r
426 return annotations.adjustPanelHeight();
\r
429 synchronized public void drawPanel(Graphics g, int x1, int x2, int y1, int y2,
\r
430 int startx, int starty, int offset)
\r
433 g.setFont(av.getFont());
\r
434 sr.renderGaps(av.renderGaps);
\r
437 /// First draw the sequences
\r
438 /////////////////////////////
\r
439 for (int i = y1; i < y2; i++)
\r
441 nextSeq = av.alignment.getSequenceAt(i);
\r
443 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
444 (x1 - startx) * av.charWidth,
\r
446 (i - starty) * av.charHeight,
\r
447 av.charWidth, av.charHeight);
\r
449 if (av.showSequenceFeatures)
\r
451 fr.drawSequence(g, nextSeq, x1, x2,
\r
452 (x1 - startx) * av.charWidth,
\r
454 (i - starty) * av.charHeight,
\r
455 av.charWidth, av.charHeight);
\r
459 /////////////////////////////////////
\r
461 // Now outline any areas if necessary
\r
462 /////////////////////////////////////
\r
463 SequenceGroup group = av.getSelectionGroup();
\r
464 java.util.Vector groups = av.alignment.getGroups();
\r
466 int sx = -1, sy = -1, ex = -1;
\r
467 int groupIndex = -1;
\r
468 if (group == null && groups.size() > 0)
\r
470 group = (SequenceGroup) groups.elementAt(0);
\r
479 boolean inGroup = false;
\r
483 for (i = y1; i < y2; i++)
\r
485 sx = (group.getStartRes() - startx) * av.charWidth;
\r
486 sy = offset + ((i - starty) * av.charHeight);
\r
487 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
490 if(sx+ex<0 || sx>imgWidth)
\r
495 if ( (sx <= (x2-x1)*av.charWidth) &&
\r
496 group.sequences.contains(av.alignment.getSequenceAt(
\r
502 if(i == y2-1 || // Dont check for i+1 if on the bottom row
\r
503 !group.sequences.contains(av.alignment.getSequenceAt(i+1 )))
\r
505 bottom = sy + av.charHeight;
\r
510 if (((top == -1) && (i == 0)) ||
\r
511 !group.sequences.contains(
\r
512 av.alignment.getSequenceAt(i - 1)))
\r
520 if (group == av.getSelectionGroup())
\r
523 g.setColor(Color.red);
\r
527 g.setColor(group.getOutlineColour());
\r
535 if (sx >= 0 && sx < imgWidth)
\r
536 g.drawLine(sx, oldY, sx, sy);
\r
538 if (sx + ex < imgWidth)
\r
539 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
547 if (sx + ex > imgWidth)
\r
550 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
551 ex = (x2 - x1 + 1) * av.charWidth;
\r
555 g.drawLine(sx, top, sx + ex, top);
\r
561 g.drawLine(sx, bottom, sx + ex, bottom);
\r
572 sy = offset + ( (i - starty) * av.charHeight);
\r
573 if (sx >= 0 && sx < imgWidth)
\r
574 g.drawLine(sx, oldY, sx, sy);
\r
576 if (sx + ex < imgWidth)
\r
577 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
585 if (sx + ex > imgWidth)
\r
587 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
588 ex = (x2 - x1 + 1) * av.charWidth;
\r
592 g.drawLine(sx, top, sx + ex, top);
\r
598 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
607 if (groupIndex >= groups.size())
\r
612 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
614 while (groupIndex < groups.size());
\r
617 /// Highlight search Results once all sequences have been drawn
\r
618 //////////////////////////////////////////////////////////
\r
621 for (int r = 0; r < searchResults.length; r += 3)
\r
623 int searchSeq = searchResults[r];
\r
625 if (searchSeq >= y1 && searchSeq < y2)
\r
627 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
629 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
630 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
632 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
633 if (searchStart < x1)
\r
637 if (searchEnd > x2)
\r
642 ssr.drawHighlightedText(seq,
\r
645 (searchStart - startx) * av.charWidth,
\r
647 (searchSeq-starty)*av.charHeight,
\r
656 public void highlightSearchResults(int[] results)
\r
658 // results are in the order sequence, startRes, endRes
\r
659 if (results == null)
\r
661 displaySearch = false;
\r
665 displaySearch = true;
\r
668 searchResults = results;
\r