2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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.getAlignment().getWidth();
107 if (av.hasHiddenColumns())
109 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
113 for (int i = 0; i < av.getAlignment().getHeight(); i++)
115 SequenceI seq = av.getAlignment().getSequenceAt(i);
121 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
128 value = av.getAlignment().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.getAlignment().getHeight(); i++)
156 seq = av.getAlignment().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 AlignmentI alignment = av.getAlignment();
352 for (int i = 0; i < alignment.getHeight(); i++)
354 tmp = alignment.getSequenceAt(i).getEnd();
361 for (int i = maxWidth; i > 0; i /= 10)
368 public void drawWrappedPanel(Graphics g, int canvasWidth,
369 int canvasHeight, int startRes)
371 AlignmentI al = av.getAlignment();
373 FontMetrics fm = getFontMetrics(av.getFont());
375 if (av.scaleRightWrapped)
377 LABEL_EAST = fm.stringWidth(getMask());
380 if (av.scaleLeftWrapped)
382 LABEL_WEST = fm.stringWidth(getMask());
385 int hgap = av.charHeight;
386 if (av.scaleAboveWrapped)
388 hgap += av.charHeight;
391 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
392 int cHeight = av.getAlignment().getHeight() * av.charHeight;
394 av.setWrappedWidth(cWidth);
396 av.endRes = av.startRes + cWidth;
401 int maxwidth = av.getAlignment().getWidth() - 1;
403 if (av.hasHiddenColumns())
405 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
408 while ((ypos <= canvasHeight) && (startRes < maxwidth))
410 endx = startRes + cWidth - 1;
417 g.setColor(Color.black);
419 if (av.scaleLeftWrapped)
421 drawWestScale(g, startRes, endx, ypos);
424 if (av.scaleRightWrapped)
426 g.translate(canvasWidth - LABEL_EAST, 0);
427 drawEastScale(g, startRes, endx, ypos);
428 g.translate(-(canvasWidth - LABEL_EAST), 0);
431 g.translate(LABEL_WEST, 0);
433 if (av.scaleAboveWrapped)
435 drawNorthScale(g, startRes, endx, ypos);
437 if (av.hasHiddenColumns() && av.showHiddenMarkers)
439 g.setColor(Color.blue);
441 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
444 res = av.getColumnSelection().findHiddenRegionPosition(i)
447 if (res < 0 || res > endx - startRes)
452 gg.fillPolygon(new int[]
453 { res * av.charWidth - av.charHeight / 4,
454 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
456 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
457 ypos - (av.charHeight / 2) + 8 }, 3);
462 if (g.getClip() == null)
464 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
467 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
470 if (av.showAnnotation)
472 g.translate(0, cHeight + ypos + 4);
473 if (annotations == null)
475 annotations = new AnnotationPanel(av);
478 annotations.drawComponent(g, startRes, endx + 1);
479 g.translate(0, -cHeight - ypos - 4);
481 g.translate(-LABEL_WEST, 0);
483 ypos += cHeight + getAnnotationHeight() + hgap;
490 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, int startSeq,
508 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,
544 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
545 - 1, (endSeq - startSeq) * av.charHeight + offset);
548 g1.translate(-screenY * av.charWidth, 0);
549 screenY += blockEnd - blockStart + 1;
550 blockStart = hideEnd + 1;
553 if (screenY <= (endRes - startRes))
555 blockEnd = blockStart + (endRes - startRes) - screenY;
556 g1.translate(screenY * av.charWidth, 0);
557 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
559 g1.translate(-screenY * av.charWidth, 0);
565 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
566 // int x1, int x2, int y1, int y2, int startx, int starty,
567 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
570 g.setFont(av.getFont());
571 sr.prepare(g, av.renderGaps);
575 // / First draw the sequences
576 // ///////////////////////////
577 for (int i = startSeq; i < endSeq; i++)
579 nextSeq = av.getAlignment().getSequenceAt(i);
586 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
587 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
589 if (av.showSequenceFeatures)
591 fr.drawSequence(g, nextSeq, startRes, endRes, offset
592 + ((i - startSeq) * av.charHeight));
595 // / Highlight search Results once all sequences have been drawn
596 // ////////////////////////////////////////////////////////
597 if (searchResults != null)
599 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
601 if (visibleResults != null)
603 for (int r = 0; r < visibleResults.length; r += 2)
605 sr.drawHighlightedText(nextSeq, visibleResults[r],
606 visibleResults[r + 1], (visibleResults[r] - startRes)
607 * av.charWidth, offset
608 + ((i - startSeq) * av.charHeight));
613 if (av.cursorMode && cursorY == i && cursorX >= startRes
614 && cursorX <= endRes)
616 sr.drawCursor(nextSeq, cursorX,
617 (cursorX - startRes) * av.charWidth, offset
618 + ((i - startSeq) * av.charHeight));
622 if (av.getSelectionGroup() != null
623 || av.getAlignment().getGroups().size() > 0)
625 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
630 void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
631 int startSeq, int endSeq, int offset)
634 // ///////////////////////////////////
635 // Now outline any areas if necessary
636 // ///////////////////////////////////
637 SequenceGroup group = av.getSelectionGroup();
644 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
646 group = (SequenceGroup) av.getAlignment().getGroups().get(0);
656 boolean inGroup = false;
659 int alHeight = av.getAlignment().getHeight() - 1;
661 for (i = startSeq; i < endSeq; i++)
663 sx = (group.getStartRes() - startRes) * av.charWidth;
664 sy = offset + ((i - startSeq) * av.charHeight);
665 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
667 if (sx + ex < 0 || sx > imgWidth)
672 if ((sx <= (endRes - startRes) * av.charWidth)
673 && group.getSequences(null).contains(
674 av.getAlignment().getSequenceAt(i)))
677 && (i >= alHeight || !group.getSequences(null)
679 av.getAlignment().getSequenceAt(i + 1))))
681 bottom = sy + av.charHeight;
686 if (((top == -1) && (i == 0))
687 || !group.getSequences(null).contains(
688 av.getAlignment().getSequenceAt(i - 1)))
696 if (group == av.getSelectionGroup())
698 g.setColor(Color.red);
702 g.setColor(group.getOutlineColour());
710 if (sx >= 0 && sx < imgWidth)
712 g.drawLine(sx, oldY, sx, sy);
715 if (sx + ex < imgWidth)
717 g.drawLine(sx + ex, oldY, sx + ex, sy);
726 if (sx + ex > imgWidth)
731 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
733 ex = (endRes - startRes + 1) * av.charWidth;
738 g.drawLine(sx, top, sx + ex, top);
744 g.drawLine(sx, bottom, sx + ex, bottom);
755 sy = offset + ((i - startSeq) * av.charHeight);
756 if (sx >= 0 && sx < imgWidth)
758 g.drawLine(sx, oldY, sx, sy);
761 if (sx + ex < imgWidth)
763 g.drawLine(sx + ex, oldY, sx + ex, sy);
772 if (sx + ex > imgWidth)
776 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
778 ex = (endRes - startRes + 1) * av.charWidth;
783 g.drawLine(sx, top, sx + ex, top);
789 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
798 if (groupIndex >= av.getAlignment().getGroups().size())
803 group = (SequenceGroup) av.getAlignment().getGroups()
805 } while (groupIndex < av.getAlignment().getGroups().size());
810 public void highlightSearchResults(SearchResults results)
812 searchResults = results;