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 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.alignment.getWidth();
108 if (av.hasHiddenColumns)
110 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
114 for (int i = 0; i < av.alignment.getHeight(); i++)
116 SequenceI seq = av.alignment.getSequenceAt(i);
122 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
129 value = av.alignment.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.alignment.getHeight(); i++)
157 seq = av.alignment.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 for (int i = 0; i < av.alignment.getHeight(); i++)
354 tmp = av.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.alignment.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);
544 .drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
545 0 + offset, (blockEnd - blockStart + 1)
546 * av.charWidth - 1, (endSeq - startSeq)
547 * av.charHeight + offset);
550 g1.translate(-screenY * av.charWidth, 0);
551 screenY += blockEnd - blockStart + 1;
552 blockStart = hideEnd + 1;
555 if (screenY <= (endRes - startRes))
557 blockEnd = blockStart + (endRes - startRes) - screenY;
558 g1.translate(screenY * av.charWidth, 0);
559 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
561 g1.translate(-screenY * av.charWidth, 0);
567 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
568 // int x1, int x2, int y1, int y2, int startx, int starty,
569 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
572 g.setFont(av.getFont());
573 sr.prepare(g, av.renderGaps);
577 // / First draw the sequences
578 // ///////////////////////////
579 for (int i = startSeq; i < endSeq; i++)
581 nextSeq = av.alignment.getSequenceAt(i);
588 sr.drawSequence(nextSeq, av.alignment.findAllGroups(nextSeq),
589 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
591 if (av.showSequenceFeatures)
593 fr.drawSequence(g, nextSeq, startRes, endRes, offset
594 + ((i - startSeq) * av.charHeight));
597 // / Highlight search Results once all sequences have been drawn
598 // ////////////////////////////////////////////////////////
599 if (searchResults != null)
601 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
603 if (visibleResults != null)
605 for (int r = 0; r < visibleResults.length; r += 2)
607 sr.drawHighlightedText(nextSeq, visibleResults[r],
608 visibleResults[r + 1], (visibleResults[r] - startRes)
609 * av.charWidth, offset
610 + ((i - startSeq) * av.charHeight));
615 if (av.cursorMode && cursorY == i && cursorX >= startRes
616 && cursorX <= endRes)
618 sr.drawCursor(nextSeq, cursorX,
619 (cursorX - startRes) * av.charWidth, offset
620 + ((i - startSeq) * av.charHeight));
624 if (av.getSelectionGroup() != null
625 || av.alignment.getGroups().size() > 0)
627 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
632 void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
633 int startSeq, int endSeq, int offset)
636 // ///////////////////////////////////
637 // Now outline any areas if necessary
638 // ///////////////////////////////////
639 SequenceGroup group = av.getSelectionGroup();
646 if ((group == null) && (av.alignment.getGroups().size() > 0))
648 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
658 boolean inGroup = false;
661 int alHeight = av.alignment.getHeight() - 1;
663 for (i = startSeq; i < endSeq; i++)
665 sx = (group.getStartRes() - startRes) * av.charWidth;
666 sy = offset + ((i - startSeq) * av.charHeight);
667 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
669 if (sx + ex < 0 || sx > imgWidth)
674 if ((sx <= (endRes - startRes) * av.charWidth)
675 && group.getSequences(null).contains(
676 av.alignment.getSequenceAt(i)))
679 && (i >= alHeight || !group.getSequences(null)
680 .contains(av.alignment.getSequenceAt(i + 1))))
682 bottom = sy + av.charHeight;
687 if (((top == -1) && (i == 0))
688 || !group.getSequences(null).contains(
689 av.alignment.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.alignment.getGroups().size())
804 group = (SequenceGroup) av.alignment.getGroups().elementAt(
806 } while (groupIndex < av.alignment.getGroups().size());
811 public void highlightSearchResults(SearchResults results)
813 searchResults = results;