2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.appletgui;
23 import jalview.datamodel.*;
25 public class SeqCanvas
37 SearchResults searchResults = null;
39 boolean fastPaint = false;
44 public SeqCanvas(AlignViewport av)
47 fr = new FeatureRenderer(av);
48 sr = new SequenceRenderer(av);
49 PaintRefresher.Register(this, av.getSequenceSetId());
52 public AlignViewport getViewport()
57 public FeatureRenderer getFeatureRenderer()
62 public SequenceRenderer getSequenceRenderer()
67 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
69 int scalestartx = startx - startx % 10 + 10;
71 g.setColor(Color.black);
74 for (int i = scalestartx; i < endx; i += 10)
77 if (av.hasHiddenColumns)
79 value = av.getColumnSelection().adjustForHiddenColumns(value);
82 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
83 ypos - (av.charHeight / 2));
85 g.drawLine( ( (i - startx - 1) * av.charWidth) + (av.charWidth / 2),
86 (ypos + 2) - (av.charHeight / 2),
87 ( (i - startx - 1) * av.charWidth) + (av.charWidth / 2), ypos -
92 void drawWestScale(Graphics g, int startx, int endx, int ypos)
94 FontMetrics fm = getFontMetrics(av.getFont());
95 ypos += av.charHeight;
96 if (av.hasHiddenColumns)
98 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
99 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
102 int maxwidth = av.alignment.getWidth();
103 if (av.hasHiddenColumns)
105 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
109 for (int i = 0; i < av.alignment.getHeight(); i++)
111 SequenceI seq = av.alignment.getSequenceAt(i);
117 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
124 value = av.alignment.getSequenceAt(i).findPosition(index);
131 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value)) -
133 g.drawString(value + "", x,
134 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
139 void drawEastScale(Graphics g, int startx, int endx, int ypos)
141 ypos += av.charHeight;
143 if (av.hasHiddenColumns)
145 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
150 for (int i = 0; i < av.alignment.getHeight(); i++)
152 seq = av.alignment.getSequenceAt(i);
156 while (index > startx)
158 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
165 value = seq.findPosition(index);
172 g.drawString(String.valueOf(value), 0,
173 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
179 void fastPaint(int horizontal, int vertical)
181 if (fastPaint || gg == null)
186 // Its possible on certain browsers that the call to fastpaint
187 // is faster than it can paint, so this check here catches
189 if (lastsr + horizontal != av.startRes)
191 horizontal = av.startRes - lastsr;
194 lastsr = av.startRes;
197 gg.copyArea(horizontal * av.charWidth,
198 vertical * av.charHeight,
199 imgWidth - horizontal * av.charWidth,
200 imgHeight - vertical * av.charHeight,
201 -horizontal * av.charWidth,
202 -vertical * av.charHeight);
204 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
205 transX = 0, transY = 0;
207 if (horizontal > 0) // scrollbar pulled right, image to the left
209 transX = (er - sr - horizontal) * av.charWidth;
210 sr = er - horizontal;
212 else if (horizontal < 0)
214 er = sr - horizontal;
217 else if (vertical > 0) // scroll down
220 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
226 transY = imgHeight - vertical * av.charHeight;
229 else if (vertical < 0)
238 gg.translate(transX, transY);
240 drawPanel(gg, sr, er, ss, es, 0);
241 gg.translate( -transX, -transY);
248 * Definitions of startx and endx (hopefully):
249 * SMJS This is what I'm working towards!
250 * startx is the first residue (starting at 0) to display.
251 * endx is the last residue to display (starting at 0).
252 * starty is the first sequence to display (starting at 0).
253 * endy is the last sequence to display (starting at 0).
254 * NOTE 1: The av limits are set in setFont in this class and
255 * in the adjustment listener in SeqPanel when the scrollbars move.
257 public void update(Graphics g)
262 public void paint(Graphics g)
265 if (img != null && (fastPaint
266 || (getSize().width != g.getClipBounds().width)
267 || (getSize().height != g.getClipBounds().height)))
269 g.drawImage(img, 0, 0, this);
276 g.drawImage(img, 0, 0, this);
281 // this draws the whole of the alignment
282 imgWidth = this.getSize().width;
283 imgHeight = this.getSize().height;
285 imgWidth -= imgWidth % av.charWidth;
286 imgHeight -= imgHeight % av.charHeight;
288 if (imgWidth < 1 || imgHeight < 1)
293 if (img == null || imgWidth != img.getWidth(this) ||
294 imgHeight != img.getHeight(this))
296 img = createImage(imgWidth, imgHeight);
297 gg = img.getGraphics();
298 gg.setFont(av.getFont());
301 gg.setColor(Color.white);
302 gg.fillRect(0, 0, imgWidth, imgHeight);
304 if (av.getWrapAlignment())
306 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
310 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
313 g.drawImage(img, 0, 0, this);
317 int LABEL_WEST, LABEL_EAST;
318 public int getWrappedCanvasWidth(int cwidth)
320 cwidth -= cwidth % av.charWidth;
322 FontMetrics fm = getFontMetrics(av.getFont());
327 if (av.scaleRightWrapped)
329 LABEL_EAST = fm.stringWidth(getMask());
332 if (av.scaleLeftWrapped)
334 LABEL_WEST = fm.stringWidth(getMask());
337 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
341 * Generates a string of zeroes.
349 for (int i = 0; i < av.alignment.getHeight(); i++)
351 tmp = av.alignment.getSequenceAt(i).getEnd();
358 for (int i = maxWidth; i > 0; i /= 10)
365 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
368 AlignmentI al = av.getAlignment();
370 FontMetrics fm = getFontMetrics(av.getFont());
372 if (av.scaleRightWrapped)
374 LABEL_EAST = fm.stringWidth(getMask());
377 if (av.scaleLeftWrapped)
379 LABEL_WEST = fm.stringWidth(getMask());
382 int hgap = av.charHeight;
383 if (av.scaleAboveWrapped)
385 hgap += av.charHeight;
388 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
389 int cHeight = av.getAlignment().getHeight() * av.charHeight;
391 av.setWrappedWidth(cWidth);
393 av.endRes = av.startRes + cWidth;
398 int maxwidth = av.alignment.getWidth() - 1;
400 if (av.hasHiddenColumns)
402 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
405 while ( (ypos <= canvasHeight) && (startRes < maxwidth))
407 endx = startRes + cWidth - 1;
414 g.setColor(Color.black);
416 if (av.scaleLeftWrapped)
418 drawWestScale(g, startRes, endx, ypos);
421 if (av.scaleRightWrapped)
423 g.translate(canvasWidth - LABEL_EAST, 0);
424 drawEastScale(g, startRes, endx, ypos);
425 g.translate( - (canvasWidth - LABEL_EAST), 0);
428 g.translate(LABEL_WEST, 0);
430 if (av.scaleAboveWrapped)
432 drawNorthScale(g, startRes, endx, ypos);
434 if (av.hasHiddenColumns && av.showHiddenMarkers)
436 g.setColor(Color.blue);
438 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size();
441 res = av.getColumnSelection().findHiddenRegionPosition(i) -
444 if (res < 0 || res > endx - startRes)
449 gg.fillPolygon(new int[]
450 {res * av.charWidth - av.charHeight / 4,
451 res * av.charWidth + av.charHeight / 4,
455 ypos - (av.charHeight / 2),
456 ypos - (av.charHeight / 2),
457 ypos - (av.charHeight / 2) + 8
463 if (g.getClip() == null)
465 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
468 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
471 if (av.showAnnotation)
473 g.translate(0, cHeight + ypos + 4);
474 if (annotations == null)
476 annotations = new AnnotationPanel(av);
479 annotations.drawComponent(g, startRes, endx + 1);
480 g.translate(0, -cHeight - ypos - 4);
482 g.translate( -LABEL_WEST, 0);
484 ypos += cHeight + getAnnotationHeight() + hgap;
491 AnnotationPanel annotations;
492 int getAnnotationHeight()
494 if (!av.showAnnotation)
499 if (annotations == null)
501 annotations = new AnnotationPanel(av);
504 return annotations.adjustPanelHeight();
507 void drawPanel(Graphics g1, int startRes, int endRes,
508 int startSeq, int endSeq, int offset)
510 if (!av.hasHiddenColumns)
512 draw(g1, startRes, endRes, startSeq, endSeq, offset);
516 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
519 int blockStart = startRes;
520 int blockEnd = endRes;
522 for (int i = 0; i < regions.size(); i++)
524 int[] region = (int[]) regions.elementAt(i);
525 int hideStart = region[0];
526 int hideEnd = region[1];
528 if (hideStart <= blockStart)
530 blockStart += (hideEnd - hideStart) + 1;
534 blockEnd = hideStart - 1;
536 g1.translate(screenY * av.charWidth, 0);
538 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
540 if (av.getShowHiddenMarkers())
542 g1.setColor(Color.blue);
543 g1.drawLine( (blockEnd - blockStart + 1) * av.charWidth - 1,
545 (blockEnd - blockStart + 1) * av.charWidth - 1,
546 (endSeq - startSeq) * av.charHeight + offset);
549 g1.translate( -screenY * av.charWidth, 0);
550 screenY += blockEnd - blockStart + 1;
551 blockStart = hideEnd + 1;
554 if (screenY <= (endRes - startRes))
556 blockEnd = blockStart + (endRes - startRes) - screenY;
557 g1.translate(screenY * av.charWidth, 0);
558 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
560 g1.translate( -screenY * av.charWidth, 0);
566 //int startRes, int endRes, int startSeq, int endSeq, int x, int y,
567 // int x1, int x2, int y1, int y2, int startx, int starty,
568 void draw(Graphics g,
569 int startRes, int endRes,
570 int startSeq, int endSeq,
573 g.setFont(av.getFont());
574 sr.prepare(g, av.renderGaps);
578 /// First draw the sequences
579 /////////////////////////////
580 for (int i = startSeq; i < endSeq; i++)
582 nextSeq = av.alignment.getSequenceAt(i);
589 sr.drawSequence(nextSeq, av.alignment.findAllGroups(nextSeq),
591 offset + ( (i - startSeq) * av.charHeight));
593 if (av.showSequenceFeatures)
595 fr.drawSequence(g, nextSeq, startRes, endRes,
596 offset + ( (i - startSeq) * av.charHeight));
599 /// Highlight search Results once all sequences have been drawn
600 //////////////////////////////////////////////////////////
601 if (searchResults != null)
603 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
605 if (visibleResults != null)
607 for (int r = 0; r < visibleResults.length; r += 2)
609 sr.drawHighlightedText(nextSeq, visibleResults[r],
610 visibleResults[r + 1],
611 (visibleResults[r] - startRes) *
613 offset + ( (i - startSeq) * av.charHeight));
618 if (av.cursorMode && cursorY == i
619 && cursorX >= startRes && cursorX <= endRes)
621 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * av.charWidth,
622 offset + ( (i - startSeq) * av.charHeight));
626 if (av.getSelectionGroup() != null || av.alignment.getGroups().size() > 0)
628 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
633 void drawGroupsBoundaries(Graphics g,
634 int startRes, int endRes,
635 int startSeq, int endSeq,
639 /////////////////////////////////////
640 // Now outline any areas if necessary
641 /////////////////////////////////////
642 SequenceGroup group = av.getSelectionGroup();
649 if ( (group == null) && (av.alignment.getGroups().size() > 0))
651 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
661 boolean inGroup = false;
664 int alHeight = av.alignment.getHeight() - 1;
666 for (i = startSeq; i < endSeq; i++)
668 sx = (group.getStartRes() - startRes) * av.charWidth;
669 sy = offset + ( (i - startSeq) * av.charHeight);
670 ex = ( ( (group.getEndRes() + 1) - group.getStartRes()) *
674 if (sx + ex < 0 || sx > imgWidth)
679 if ( (sx <= (endRes - startRes) * av.charWidth) &&
680 group.getSequences(null).
681 contains(av.alignment.getSequenceAt(i)))
683 if ( (bottom == -1) &&
685 !group.getSequences(null).contains(
686 av.alignment.getSequenceAt(i + 1))))
688 bottom = sy + av.charHeight;
693 if ( ( (top == -1) && (i == 0)) ||
694 !group.getSequences(null).contains(
695 av.alignment.getSequenceAt(i - 1)))
703 if (group == av.getSelectionGroup())
705 g.setColor(Color.red);
709 g.setColor(group.getOutlineColour());
717 if (sx >= 0 && sx < imgWidth)
719 g.drawLine(sx, oldY, sx, sy);
722 if (sx + ex < imgWidth)
724 g.drawLine(sx + ex, oldY, sx + ex, sy);
733 if (sx + ex > imgWidth)
738 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
740 ex = (endRes - startRes + 1) * av.charWidth;
745 g.drawLine(sx, top, sx + ex, top);
751 g.drawLine(sx, bottom, sx + ex, bottom);
762 sy = offset + ( (i - startSeq) * av.charHeight);
763 if (sx >= 0 && sx < imgWidth)
765 g.drawLine(sx, oldY, sx, sy);
768 if (sx + ex < imgWidth)
770 g.drawLine(sx + ex, oldY, sx + ex, sy);
779 if (sx + ex > imgWidth)
783 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
785 ex = (endRes - startRes + 1) * av.charWidth;
790 g.drawLine(sx, top, sx + ex, top);
796 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
805 if (groupIndex >= av.alignment.getGroups().size())
810 group = (SequenceGroup) av.alignment.getGroups().elementAt(groupIndex);
812 while (groupIndex < av.alignment.getGroups().size());
817 public void highlightSearchResults(SearchResults results)
819 searchResults = results;