2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
22 import jalview.datamodel.*;
24 public class SeqCanvas extends Panel
40 SearchResults searchResults = null;
42 boolean fastPaint = false;
48 public SeqCanvas(AlignViewport av)
51 fr = new FeatureRenderer(av);
52 sr = new SequenceRenderer(av);
53 PaintRefresher.Register(this, av.getSequenceSetId());
56 public AlignViewport getViewport()
61 public FeatureRenderer getFeatureRenderer()
66 public SequenceRenderer getSequenceRenderer()
71 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
73 int scalestartx = startx - startx % 10 + 10;
75 g.setColor(Color.black);
78 for (int i = scalestartx; i < endx; i += 10)
81 if (av.hasHiddenColumns)
83 value = av.getColumnSelection().adjustForHiddenColumns(value);
86 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
87 ypos - (av.charHeight / 2));
89 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
90 (ypos + 2) - (av.charHeight / 2),
91 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
96 void drawWestScale(Graphics g, int startx, int endx, int ypos)
98 FontMetrics fm = getFontMetrics(av.getFont());
99 ypos += av.charHeight;
100 if (av.hasHiddenColumns)
102 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
103 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
106 int maxwidth = av.alignment.getWidth();
107 if (av.hasHiddenColumns)
109 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
113 for (int i = 0; i < av.alignment.getHeight(); i++)
115 SequenceI seq = av.alignment.getSequenceAt(i);
121 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
128 value = av.alignment.getSequenceAt(i).findPosition(index);
135 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
137 g.drawString(value + "", x, (ypos + (i * av.charHeight))
138 - (av.charHeight / 5));
143 void drawEastScale(Graphics g, int startx, int endx, int ypos)
145 ypos += av.charHeight;
147 if (av.hasHiddenColumns)
149 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
154 for (int i = 0; i < av.alignment.getHeight(); i++)
156 seq = av.alignment.getSequenceAt(i);
160 while (index > startx)
162 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
169 value = seq.findPosition(index);
176 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
177 - (av.charHeight / 5));
184 void fastPaint(int horizontal, int vertical)
186 if (fastPaint || gg == null)
191 // Its possible on certain browsers that the call to fastpaint
192 // is faster than it can paint, so this check here catches
194 if (lastsr + horizontal != av.startRes)
196 horizontal = av.startRes - lastsr;
199 lastsr = av.startRes;
202 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
203 imgWidth - horizontal * av.charWidth, imgHeight - vertical
204 * av.charHeight, -horizontal * av.charWidth, -vertical
207 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
209 if (horizontal > 0) // scrollbar pulled right, image to the left
211 transX = (er - sr - horizontal) * av.charWidth;
212 sr = er - horizontal;
214 else if (horizontal < 0)
216 er = sr - horizontal;
219 else if (vertical > 0) // scroll down
222 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
228 transY = imgHeight - vertical * av.charHeight;
231 else if (vertical < 0)
240 gg.translate(transX, transY);
242 drawPanel(gg, sr, er, ss, es, 0);
243 gg.translate(-transX, -transY);
250 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
251 * towards! startx is the first residue (starting at 0) to display. endx is
252 * the last residue to display (starting at 0). starty is the first sequence
253 * to display (starting at 0). endy is the last sequence to display (starting
254 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
255 * adjustment listener in SeqPanel when the scrollbars move.
257 public void update(Graphics g)
262 public void paint(Graphics g)
266 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
267 .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;
319 public int getWrappedCanvasWidth(int cwidth)
321 cwidth -= cwidth % av.charWidth;
323 FontMetrics fm = getFontMetrics(av.getFont());
328 if (av.scaleRightWrapped)
330 LABEL_EAST = fm.stringWidth(getMask());
333 if (av.scaleLeftWrapped)
335 LABEL_WEST = fm.stringWidth(getMask());
338 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
342 * Generates a string of zeroes.
351 for (int i = 0; i < av.alignment.getHeight(); i++)
353 tmp = av.alignment.getSequenceAt(i).getEnd();
360 for (int i = maxWidth; i > 0; i /= 10)
367 public void drawWrappedPanel(Graphics g, int canvasWidth,
368 int canvasHeight, int startRes)
370 AlignmentI al = av.getAlignment();
372 FontMetrics fm = getFontMetrics(av.getFont());
374 if (av.scaleRightWrapped)
376 LABEL_EAST = fm.stringWidth(getMask());
379 if (av.scaleLeftWrapped)
381 LABEL_WEST = fm.stringWidth(getMask());
384 int hgap = av.charHeight;
385 if (av.scaleAboveWrapped)
387 hgap += av.charHeight;
390 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
391 int cHeight = av.getAlignment().getHeight() * av.charHeight;
393 av.setWrappedWidth(cWidth);
395 av.endRes = av.startRes + cWidth;
400 int maxwidth = av.alignment.getWidth() - 1;
402 if (av.hasHiddenColumns)
404 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
407 while ((ypos <= canvasHeight) && (startRes < maxwidth))
409 endx = startRes + cWidth - 1;
416 g.setColor(Color.black);
418 if (av.scaleLeftWrapped)
420 drawWestScale(g, startRes, endx, ypos);
423 if (av.scaleRightWrapped)
425 g.translate(canvasWidth - LABEL_EAST, 0);
426 drawEastScale(g, startRes, endx, ypos);
427 g.translate(-(canvasWidth - LABEL_EAST), 0);
430 g.translate(LABEL_WEST, 0);
432 if (av.scaleAboveWrapped)
434 drawNorthScale(g, startRes, endx, ypos);
436 if (av.hasHiddenColumns && av.showHiddenMarkers)
438 g.setColor(Color.blue);
440 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
443 res = av.getColumnSelection().findHiddenRegionPosition(i)
446 if (res < 0 || res > endx - startRes)
451 gg.fillPolygon(new int[]
452 { res * av.charWidth - av.charHeight / 4,
453 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
455 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
456 ypos - (av.charHeight / 2) + 8 }, 3);
461 if (g.getClip() == null)
463 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
466 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
469 if (av.showAnnotation)
471 g.translate(0, cHeight + ypos + 4);
472 if (annotations == null)
474 annotations = new AnnotationPanel(av);
477 annotations.drawComponent(g, startRes, endx + 1);
478 g.translate(0, -cHeight - ypos - 4);
480 g.translate(-LABEL_WEST, 0);
482 ypos += cHeight + getAnnotationHeight() + hgap;
489 AnnotationPanel annotations;
491 int getAnnotationHeight()
493 if (!av.showAnnotation)
498 if (annotations == null)
500 annotations = new AnnotationPanel(av);
503 return annotations.adjustPanelHeight();
506 void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
507 int endSeq, int offset)
509 if (!av.hasHiddenColumns)
511 draw(g1, startRes, endRes, startSeq, endSeq, offset);
515 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
518 int blockStart = startRes;
519 int blockEnd = endRes;
521 for (int i = 0; i < regions.size(); i++)
523 int[] region = (int[]) regions.elementAt(i);
524 int hideStart = region[0];
525 int hideEnd = region[1];
527 if (hideStart <= blockStart)
529 blockStart += (hideEnd - hideStart) + 1;
533 blockEnd = hideStart - 1;
535 g1.translate(screenY * av.charWidth, 0);
537 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
539 if (av.getShowHiddenMarkers())
541 g1.setColor(Color.blue);
542 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
543 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
544 - 1, (endSeq - startSeq) * av.charHeight + offset);
547 g1.translate(-screenY * av.charWidth, 0);
548 screenY += blockEnd - blockStart + 1;
549 blockStart = hideEnd + 1;
552 if (screenY <= (endRes - startRes))
554 blockEnd = blockStart + (endRes - startRes) - screenY;
555 g1.translate(screenY * av.charWidth, 0);
556 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
558 g1.translate(-screenY * av.charWidth, 0);
564 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
565 // int x1, int x2, int y1, int y2, int startx, int starty,
566 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
569 g.setFont(av.getFont());
570 sr.prepare(g, av.renderGaps);
574 // / First draw the sequences
575 // ///////////////////////////
576 for (int i = startSeq; i < endSeq; i++)
578 nextSeq = av.alignment.getSequenceAt(i);
585 sr.drawSequence(nextSeq, av.alignment.findAllGroups(nextSeq),
586 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
588 if (av.showSequenceFeatures)
590 fr.drawSequence(g, nextSeq, startRes, endRes, offset
591 + ((i - startSeq) * av.charHeight));
594 // / Highlight search Results once all sequences have been drawn
595 // ////////////////////////////////////////////////////////
596 if (searchResults != null)
598 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
600 if (visibleResults != null)
602 for (int r = 0; r < visibleResults.length; r += 2)
604 sr.drawHighlightedText(nextSeq, visibleResults[r],
605 visibleResults[r + 1], (visibleResults[r] - startRes)
606 * av.charWidth, offset
607 + ((i - startSeq) * av.charHeight));
612 if (av.cursorMode && cursorY == i && cursorX >= startRes
613 && cursorX <= endRes)
615 sr.drawCursor(nextSeq, cursorX,
616 (cursorX - startRes) * av.charWidth, offset
617 + ((i - startSeq) * av.charHeight));
621 if (av.getSelectionGroup() != null
622 || av.alignment.getGroups().size() > 0)
624 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
629 void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
630 int startSeq, int endSeq, int offset)
633 // ///////////////////////////////////
634 // Now outline any areas if necessary
635 // ///////////////////////////////////
636 SequenceGroup group = av.getSelectionGroup();
643 if ((group == null) && (av.alignment.getGroups().size() > 0))
645 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
655 boolean inGroup = false;
658 int alHeight = av.alignment.getHeight() - 1;
660 for (i = startSeq; i < endSeq; i++)
662 sx = (group.getStartRes() - startRes) * av.charWidth;
663 sy = offset + ((i - startSeq) * av.charHeight);
664 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
666 if (sx + ex < 0 || sx > imgWidth)
671 if ((sx <= (endRes - startRes) * av.charWidth)
672 && group.getSequences(null).contains(
673 av.alignment.getSequenceAt(i)))
676 && (i >= alHeight || !group.getSequences(null)
677 .contains(av.alignment.getSequenceAt(i + 1))))
679 bottom = sy + av.charHeight;
684 if (((top == -1) && (i == 0))
685 || !group.getSequences(null).contains(
686 av.alignment.getSequenceAt(i - 1)))
694 if (group == av.getSelectionGroup())
696 g.setColor(Color.red);
700 g.setColor(group.getOutlineColour());
708 if (sx >= 0 && sx < imgWidth)
710 g.drawLine(sx, oldY, sx, sy);
713 if (sx + ex < imgWidth)
715 g.drawLine(sx + ex, oldY, sx + ex, sy);
724 if (sx + ex > imgWidth)
729 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
731 ex = (endRes - startRes + 1) * av.charWidth;
736 g.drawLine(sx, top, sx + ex, top);
742 g.drawLine(sx, bottom, sx + ex, bottom);
753 sy = offset + ((i - startSeq) * av.charHeight);
754 if (sx >= 0 && sx < imgWidth)
756 g.drawLine(sx, oldY, sx, sy);
759 if (sx + ex < imgWidth)
761 g.drawLine(sx + ex, oldY, sx + ex, sy);
770 if (sx + ex > imgWidth)
774 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
776 ex = (endRes - startRes + 1) * av.charWidth;
781 g.drawLine(sx, top, sx + ex, top);
787 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
796 if (groupIndex >= av.alignment.getGroups().size())
801 group = (SequenceGroup) av.alignment.getGroups().elementAt(
803 } while (groupIndex < av.alignment.getGroups().size());
808 public void highlightSearchResults(SearchResults results)
810 searchResults = results;