2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
23 import jalview.datamodel.*;
25 public class SeqCanvas extends Panel
41 SearchResults searchResults = null;
43 boolean fastPaint = false;
49 public SeqCanvas(AlignViewport av)
52 fr = new FeatureRenderer(av);
53 sr = new SequenceRenderer(av);
54 PaintRefresher.Register(this, av.getSequenceSetId());
57 public AlignViewport getViewport()
62 public FeatureRenderer getFeatureRenderer()
67 public SequenceRenderer getSequenceRenderer()
72 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
74 int scalestartx = startx - startx % 10 + 10;
76 g.setColor(Color.black);
79 for (int i = scalestartx; i < endx; i += 10)
82 if (av.hasHiddenColumns())
84 value = av.getColumnSelection().adjustForHiddenColumns(value);
87 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
88 ypos - (av.charHeight / 2));
90 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
91 (ypos + 2) - (av.charHeight / 2),
92 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
97 void drawWestScale(Graphics g, int startx, int endx, int ypos)
99 FontMetrics fm = getFontMetrics(av.getFont());
100 ypos += av.charHeight;
101 if (av.hasHiddenColumns())
103 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
104 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
107 int maxwidth = av.getAlignment().getWidth();
108 if (av.hasHiddenColumns())
110 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
114 for (int i = 0; i < av.getAlignment().getHeight(); i++)
116 SequenceI seq = av.getAlignment().getSequenceAt(i);
122 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
129 value = av.getAlignment().getSequenceAt(i).findPosition(index);
136 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
138 g.drawString(value + "", x, (ypos + (i * av.charHeight))
139 - (av.charHeight / 5));
144 void drawEastScale(Graphics g, int startx, int endx, int ypos)
146 ypos += av.charHeight;
148 if (av.hasHiddenColumns())
150 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
155 for (int i = 0; i < av.getAlignment().getHeight(); i++)
157 seq = av.getAlignment().getSequenceAt(i);
161 while (index > startx)
163 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
170 value = seq.findPosition(index);
177 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
178 - (av.charHeight / 5));
185 void fastPaint(int horizontal, int vertical)
187 if (fastPaint || gg == null)
192 // Its possible on certain browsers that the call to fastpaint
193 // is faster than it can paint, so this check here catches
195 if (lastsr + horizontal != av.startRes)
197 horizontal = av.startRes - lastsr;
200 lastsr = av.startRes;
203 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
204 imgWidth - horizontal * av.charWidth, imgHeight - vertical
205 * av.charHeight, -horizontal * av.charWidth, -vertical
208 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
210 if (horizontal > 0) // scrollbar pulled right, image to the left
212 transX = (er - sr - horizontal) * av.charWidth;
213 sr = er - horizontal;
215 else if (horizontal < 0)
217 er = sr - horizontal;
220 else if (vertical > 0) // scroll down
223 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
229 transY = imgHeight - vertical * av.charHeight;
232 else if (vertical < 0)
241 gg.translate(transX, transY);
243 drawPanel(gg, sr, er, ss, es, 0);
244 gg.translate(-transX, -transY);
251 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
252 * towards! startx is the first residue (starting at 0) to display. endx is
253 * the last residue to display (starting at 0). starty is the first sequence
254 * to display (starting at 0). endy is the last sequence to display (starting
255 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
256 * adjustment listener in SeqPanel when the scrollbars move.
258 public void update(Graphics g)
263 public void paint(Graphics g)
267 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
268 .getClipBounds().height)))
270 g.drawImage(img, 0, 0, this);
277 g.drawImage(img, 0, 0, this);
282 // this draws the whole of the alignment
283 imgWidth = this.getSize().width;
284 imgHeight = this.getSize().height;
286 imgWidth -= imgWidth % av.charWidth;
287 imgHeight -= imgHeight % av.charHeight;
289 if (imgWidth < 1 || imgHeight < 1)
294 if (img == null || imgWidth != img.getWidth(this)
295 || imgHeight != img.getHeight(this))
297 img = createImage(imgWidth, imgHeight);
298 gg = img.getGraphics();
299 gg.setFont(av.getFont());
302 gg.setColor(Color.white);
303 gg.fillRect(0, 0, imgWidth, imgHeight);
305 if (av.getWrapAlignment())
307 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
311 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
314 g.drawImage(img, 0, 0, this);
318 int LABEL_WEST, LABEL_EAST;
320 public int getWrappedCanvasWidth(int cwidth)
322 cwidth -= cwidth % av.charWidth;
324 FontMetrics fm = getFontMetrics(av.getFont());
329 if (av.scaleRightWrapped)
331 LABEL_EAST = fm.stringWidth(getMask());
334 if (av.scaleLeftWrapped)
336 LABEL_WEST = fm.stringWidth(getMask());
339 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
343 * Generates a string of zeroes.
352 AlignmentI alignment = av.getAlignment();
353 for (int i = 0; i < alignment.getHeight(); i++)
355 tmp = alignment.getSequenceAt(i).getEnd();
362 for (int i = maxWidth; i > 0; i /= 10)
369 public void drawWrappedPanel(Graphics g, int canvasWidth,
370 int canvasHeight, int startRes)
372 AlignmentI al = av.getAlignment();
374 FontMetrics fm = getFontMetrics(av.getFont());
376 if (av.scaleRightWrapped)
378 LABEL_EAST = fm.stringWidth(getMask());
381 if (av.scaleLeftWrapped)
383 LABEL_WEST = fm.stringWidth(getMask());
386 int hgap = av.charHeight;
387 if (av.scaleAboveWrapped)
389 hgap += av.charHeight;
392 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
393 int cHeight = av.getAlignment().getHeight() * av.charHeight;
395 av.setWrappedWidth(cWidth);
397 av.endRes = av.startRes + cWidth;
402 int maxwidth = av.getAlignment().getWidth() - 1;
404 if (av.hasHiddenColumns())
406 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
409 while ((ypos <= canvasHeight) && (startRes < maxwidth))
411 endx = startRes + cWidth - 1;
418 g.setColor(Color.black);
420 if (av.scaleLeftWrapped)
422 drawWestScale(g, startRes, endx, ypos);
425 if (av.scaleRightWrapped)
427 g.translate(canvasWidth - LABEL_EAST, 0);
428 drawEastScale(g, startRes, endx, ypos);
429 g.translate(-(canvasWidth - LABEL_EAST), 0);
432 g.translate(LABEL_WEST, 0);
434 if (av.scaleAboveWrapped)
436 drawNorthScale(g, startRes, endx, ypos);
438 if (av.hasHiddenColumns() && av.showHiddenMarkers)
440 g.setColor(Color.blue);
442 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
445 res = av.getColumnSelection().findHiddenRegionPosition(i)
448 if (res < 0 || res > endx - startRes)
453 gg.fillPolygon(new int[]
454 { res * av.charWidth - av.charHeight / 4,
455 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
457 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
458 ypos - (av.charHeight / 2) + 8 }, 3);
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;
493 int getAnnotationHeight()
495 if (!av.showAnnotation)
500 if (annotations == null)
502 annotations = new AnnotationPanel(av);
505 return annotations.adjustPanelHeight();
508 void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
509 int endSeq, int offset)
511 if (!av.hasHiddenColumns())
513 draw(g1, startRes, endRes, startSeq, endSeq, offset);
517 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
520 int blockStart = startRes;
521 int blockEnd = endRes;
523 for (int i = 0; i < regions.size(); i++)
525 int[] region = (int[]) regions.elementAt(i);
526 int hideStart = region[0];
527 int hideEnd = region[1];
529 if (hideStart <= blockStart)
531 blockStart += (hideEnd - hideStart) + 1;
535 blockEnd = hideStart - 1;
537 g1.translate(screenY * av.charWidth, 0);
539 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
541 if (av.getShowHiddenMarkers())
543 g1.setColor(Color.blue);
544 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
545 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
546 - 1, (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, int startRes, int endRes, int startSeq, int endSeq,
571 g.setFont(av.getFont());
572 sr.prepare(g, av.renderGaps);
576 // / First draw the sequences
577 // ///////////////////////////
578 for (int i = startSeq; i < endSeq; i++)
580 nextSeq = av.getAlignment().getSequenceAt(i);
587 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
588 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
590 if (av.showSequenceFeatures)
592 fr.drawSequence(g, nextSeq, startRes, endRes, offset
593 + ((i - startSeq) * av.charHeight));
596 // / Highlight search Results once all sequences have been drawn
597 // ////////////////////////////////////////////////////////
598 if (searchResults != null)
600 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
602 if (visibleResults != null)
604 for (int r = 0; r < visibleResults.length; r += 2)
606 sr.drawHighlightedText(nextSeq, visibleResults[r],
607 visibleResults[r + 1], (visibleResults[r] - startRes)
608 * av.charWidth, offset
609 + ((i - startSeq) * av.charHeight));
614 if (av.cursorMode && cursorY == i && cursorX >= startRes
615 && cursorX <= endRes)
617 sr.drawCursor(nextSeq, cursorX,
618 (cursorX - startRes) * av.charWidth, offset
619 + ((i - startSeq) * av.charHeight));
623 if (av.getSelectionGroup() != null
624 || av.getAlignment().getGroups().size() > 0)
626 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
631 void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
632 int startSeq, int endSeq, int offset)
635 // ///////////////////////////////////
636 // Now outline any areas if necessary
637 // ///////////////////////////////////
638 SequenceGroup group = av.getSelectionGroup();
645 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
647 group = (SequenceGroup) av.getAlignment().getGroups().get(0);
657 boolean inGroup = false;
660 int alHeight = av.getAlignment().getHeight() - 1;
662 for (i = startSeq; i < endSeq; i++)
664 sx = (group.getStartRes() - startRes) * av.charWidth;
665 sy = offset + ((i - startSeq) * av.charHeight);
666 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
668 if (sx + ex < 0 || sx > imgWidth)
673 if ((sx <= (endRes - startRes) * av.charWidth)
674 && group.getSequences(null).contains(
675 av.getAlignment().getSequenceAt(i)))
678 && (i >= alHeight || !group.getSequences(null)
680 av.getAlignment().getSequenceAt(i + 1))))
682 bottom = sy + av.charHeight;
687 if (((top == -1) && (i == 0))
688 || !group.getSequences(null).contains(
689 av.getAlignment().getSequenceAt(i - 1)))
697 if (group == av.getSelectionGroup())
699 g.setColor(Color.red);
703 g.setColor(group.getOutlineColour());
711 if (sx >= 0 && sx < imgWidth)
713 g.drawLine(sx, oldY, sx, sy);
716 if (sx + ex < imgWidth)
718 g.drawLine(sx + ex, oldY, sx + ex, sy);
727 if (sx + ex > imgWidth)
732 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
734 ex = (endRes - startRes + 1) * av.charWidth;
739 g.drawLine(sx, top, sx + ex, top);
745 g.drawLine(sx, bottom, sx + ex, bottom);
756 sy = offset + ((i - startSeq) * av.charHeight);
757 if (sx >= 0 && sx < imgWidth)
759 g.drawLine(sx, oldY, sx, sy);
762 if (sx + ex < imgWidth)
764 g.drawLine(sx + ex, oldY, sx + ex, sy);
773 if (sx + ex > imgWidth)
777 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
779 ex = (endRes - startRes + 1) * av.charWidth;
784 g.drawLine(sx, top, sx + ex, top);
790 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
799 if (groupIndex >= av.getAlignment().getGroups().size())
804 group = (SequenceGroup) av.getAlignment().getGroups()
806 } while (groupIndex < av.getAlignment().getGroups().size());
811 public void highlightSearchResults(SearchResults results)
813 searchResults = results;