2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SearchResultsI;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.renderer.ScaleRenderer;
28 import jalview.renderer.ScaleRenderer.ScaleMark;
29 import jalview.viewmodel.AlignmentViewport;
30 import jalview.viewmodel.ViewportListenerI;
31 import jalview.viewmodel.ViewportRanges;
33 import java.awt.Color;
34 import java.awt.FontMetrics;
35 import java.awt.Graphics;
36 import java.awt.Image;
37 import java.awt.Panel;
38 import java.beans.PropertyChangeEvent;
40 public class SeqCanvas extends Panel implements ViewportListenerI
56 boolean fastPaint = false;
62 public SeqCanvas(AlignViewport av)
65 fr = new FeatureRenderer(av);
66 sr = new SequenceRenderer(av);
67 PaintRefresher.Register(this, av.getSequenceSetId());
70 av.getRanges().addPropertyChangeListener(this);
73 int avcharHeight = 0, avcharWidth = 0;
75 private void updateViewport()
77 avcharHeight = av.getCharHeight();
78 avcharWidth = av.getCharWidth();
81 public AlignmentViewport getViewport()
86 public FeatureRenderer getFeatureRenderer()
91 public SequenceRenderer getSequenceRenderer()
96 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
99 g.setColor(Color.black);
100 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
103 int mpos = mark.column; // (i - startx - 1)
108 String mstring = mark.text;
114 g.drawString(mstring, mpos * avcharWidth, ypos
115 - (avcharHeight / 2));
117 g.drawLine((mpos * avcharWidth) + (avcharWidth / 2), (ypos + 2)
118 - (avcharHeight / 2), (mpos * avcharWidth)
119 + (avcharWidth / 2), ypos - 2);
124 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
126 FontMetrics fm = getFontMetrics(av.getFont());
127 ypos += avcharHeight;
128 if (av.hasHiddenColumns())
130 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
131 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
134 int maxwidth = av.getAlignment().getWidth();
135 if (av.hasHiddenColumns())
137 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
141 for (int i = 0; i < av.getAlignment().getHeight(); i++)
143 SequenceI seq = av.getAlignment().getSequenceAt(i);
149 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
156 value = av.getAlignment().getSequenceAt(i).findPosition(index);
163 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
165 g.drawString(value + "", x, (ypos + (i * avcharHeight))
166 - (avcharHeight / 5));
171 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
173 ypos += avcharHeight;
175 if (av.hasHiddenColumns())
177 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
182 for (int i = 0; i < av.getAlignment().getHeight(); i++)
184 seq = av.getAlignment().getSequenceAt(i);
188 while (index > startx)
190 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
197 value = seq.findPosition(index);
204 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
205 - (avcharHeight / 5));
212 void fastPaint(int horizontal, int vertical)
214 if (fastPaint || gg == null)
219 ViewportRanges ranges = av.getRanges();
223 // Its possible on certain browsers that the call to fastpaint
224 // is faster than it can paint, so this check here catches
226 if (lastsr + horizontal != ranges.getStartRes())
228 horizontal = ranges.getStartRes() - lastsr;
231 lastsr = ranges.getStartRes();
234 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
235 - horizontal * avcharWidth,
236 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
237 -vertical * avcharHeight);
239 int sr = ranges.getStartRes(), er = ranges.getEndRes(), ss = ranges
240 .getStartSeq(), es = ranges
241 .getEndSeq(), transX = 0, transY = 0;
243 if (horizontal > 0) // scrollbar pulled right, image to the left
245 transX = (er - sr - horizontal) * avcharWidth;
246 sr = er - horizontal;
248 else if (horizontal < 0)
250 er = sr - horizontal;
253 else if (vertical > 0) // scroll down
256 if (ss < ranges.getStartSeq()) // ie scrolling too fast, more than a page
260 ss = ranges.getStartSeq();
264 transY = imgHeight - ((vertical + 1) * avcharHeight);
267 else if (vertical < 0)
270 if (es > ranges.getEndSeq())
272 es = ranges.getEndSeq();
276 gg.translate(transX, transY);
278 drawPanel(gg, sr, er, ss, es, 0);
279 gg.translate(-transX, -transY);
286 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
287 * towards! startx is the first residue (starting at 0) to display. endx is
288 * the last residue to display (starting at 0). starty is the first sequence
289 * to display (starting at 0). endy is the last sequence to display (starting
290 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
291 * adjustment listener in SeqPanel when the scrollbars move.
294 public void update(Graphics g)
300 public void paint(Graphics g)
304 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
305 .getClipBounds().height)))
307 g.drawImage(img, 0, 0, this);
314 g.drawImage(img, 0, 0, this);
320 // this draws the whole of the alignment
321 imgWidth = this.getSize().width;
322 imgHeight = this.getSize().height;
324 imgWidth -= imgWidth % avcharWidth;
325 imgHeight -= imgHeight % avcharHeight;
327 if (imgWidth < 1 || imgHeight < 1)
332 if (img == null || imgWidth != img.getWidth(this)
333 || imgHeight != img.getHeight(this))
335 img = createImage(imgWidth, imgHeight);
336 gg = img.getGraphics();
337 gg.setFont(av.getFont());
340 gg.setColor(Color.white);
341 gg.fillRect(0, 0, imgWidth, imgHeight);
343 ViewportRanges ranges = av.getRanges();
345 if (av.getWrapAlignment())
347 drawWrappedPanel(gg, imgWidth, imgHeight, ranges.getStartRes());
351 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
352 ranges.getStartSeq(), ranges.getEndSeq(), 0);
355 g.drawImage(img, 0, 0, this);
359 int LABEL_WEST, LABEL_EAST;
361 public int getWrappedCanvasWidth(int cwidth)
363 cwidth -= cwidth % av.getCharWidth();
365 FontMetrics fm = getFontMetrics(av.getFont());
370 if (av.getScaleRightWrapped())
372 LABEL_EAST = fm.stringWidth(getMask());
375 if (av.getScaleLeftWrapped())
377 LABEL_WEST = fm.stringWidth(getMask());
380 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
384 * Generates a string of zeroes.
393 AlignmentI alignment = av.getAlignment();
394 for (int i = 0; i < alignment.getHeight(); i++)
396 tmp = alignment.getSequenceAt(i).getEnd();
403 for (int i = maxWidth; i > 0; i /= 10)
410 private void drawWrappedPanel(Graphics g, int canvasWidth,
411 int canvasHeight, int startRes)
413 AlignmentI al = av.getAlignment();
415 FontMetrics fm = getFontMetrics(av.getFont());
417 if (av.getScaleRightWrapped())
419 LABEL_EAST = fm.stringWidth(getMask());
422 if (av.getScaleLeftWrapped())
424 LABEL_WEST = fm.stringWidth(getMask());
427 int hgap = avcharHeight;
428 if (av.getScaleAboveWrapped())
430 hgap += avcharHeight;
433 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
434 int cHeight = av.getAlignment().getHeight() * avcharHeight;
436 av.setWrappedWidth(cWidth);
438 av.getRanges().setEndRes(av.getRanges().getStartRes() + cWidth - 1);
443 int maxwidth = av.getAlignment().getWidth() - 1;
445 if (av.hasHiddenColumns())
447 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
450 while ((ypos <= canvasHeight) && (startRes < maxwidth))
452 endx = startRes + cWidth - 1;
459 g.setColor(Color.black);
461 if (av.getScaleLeftWrapped())
463 drawWestScale(g, startRes, endx, ypos);
466 if (av.getScaleRightWrapped())
468 g.translate(canvasWidth - LABEL_EAST, 0);
469 drawEastScale(g, startRes, endx, ypos);
470 g.translate(-(canvasWidth - LABEL_EAST), 0);
473 g.translate(LABEL_WEST, 0);
475 if (av.getScaleAboveWrapped())
477 drawNorthScale(g, startRes, endx, ypos);
479 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
481 g.setColor(Color.blue);
483 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
486 res = av.getColumnSelection().findHiddenRegionPosition(i)
489 if (res < 0 || res > endx - startRes)
494 gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
495 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
496 new int[] { ypos - (avcharHeight / 2),
497 ypos - (avcharHeight / 2),
498 ypos - (avcharHeight / 2) + 8 }, 3);
503 if (g.getClip() == null)
505 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
508 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
511 if (av.isShowAnnotation())
513 g.translate(0, cHeight + ypos + 4);
514 if (annotations == null)
516 annotations = new AnnotationPanel(av);
519 annotations.drawComponent(g, startRes, endx + 1);
520 g.translate(0, -cHeight - ypos - 4);
522 g.translate(-LABEL_WEST, 0);
524 ypos += cHeight + getAnnotationHeight() + hgap;
531 AnnotationPanel annotations;
533 int getAnnotationHeight()
535 if (!av.isShowAnnotation())
540 if (annotations == null)
542 annotations = new AnnotationPanel(av);
545 return annotations.adjustPanelHeight();
548 private void drawPanel(Graphics g1, int startRes, int endRes,
549 int startSeq, int endSeq, int offset)
552 if (!av.hasHiddenColumns())
554 draw(g1, startRes, endRes, startSeq, endSeq, offset);
560 int blockStart = startRes;
561 int blockEnd = endRes;
563 if (av.hasHiddenColumns())
565 for (int[] region : av.getColumnSelection().getHiddenColumns())
567 int hideStart = region[0];
568 int hideEnd = region[1];
570 if (hideStart <= blockStart)
572 blockStart += (hideEnd - hideStart) + 1;
576 blockEnd = hideStart - 1;
578 g1.translate(screenY * avcharWidth, 0);
580 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
582 if (av.getShowHiddenMarkers())
584 g1.setColor(Color.blue);
585 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
586 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
587 - 1, (endSeq - startSeq + 1) * avcharHeight
591 g1.translate(-screenY * avcharWidth, 0);
592 screenY += blockEnd - blockStart + 1;
593 blockStart = hideEnd + 1;
595 if (screenY > (endRes - startRes))
597 // already rendered last block
602 if (screenY <= (endRes - startRes))
604 // remaining visible region to render
605 blockEnd = blockStart + (endRes - startRes) - screenY;
606 g1.translate(screenY * avcharWidth, 0);
607 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
609 g1.translate(-screenY * avcharWidth, 0);
615 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
616 // int x1, int x2, int y1, int y2, int startx, int starty,
617 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
620 g.setFont(av.getFont());
621 sr.prepare(g, av.isRenderGaps());
625 // / First draw the sequences
626 // ///////////////////////////
627 for (int i = startSeq; i <= endSeq; i++)
629 nextSeq = av.getAlignment().getSequenceAt(i);
636 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
637 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
639 if (av.isShowSequenceFeatures())
641 fr.drawSequence(g, nextSeq, startRes, endRes, offset
642 + ((i - startSeq) * avcharHeight), false);
645 // / Highlight search Results once all sequences have been drawn
646 // ////////////////////////////////////////////////////////
647 if (av.hasSearchResults())
649 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
652 if (visibleResults != null)
654 for (int r = 0; r < visibleResults.length; r += 2)
656 sr.drawHighlightedText(nextSeq, visibleResults[r],
657 visibleResults[r + 1], (visibleResults[r] - startRes)
658 * avcharWidth, offset
659 + ((i - startSeq) * avcharHeight));
664 if (av.cursorMode && cursorY == i && cursorX >= startRes
665 && cursorX <= endRes)
667 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
668 offset + ((i - startSeq) * avcharHeight));
672 if (av.getSelectionGroup() != null
673 || av.getAlignment().getGroups().size() > 0)
675 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
680 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
681 int startSeq, int endSeq, int offset)
684 // ///////////////////////////////////
685 // Now outline any areas if necessary
686 // ///////////////////////////////////
687 SequenceGroup group = av.getSelectionGroup();
694 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
696 group = av.getAlignment().getGroups().get(0);
706 boolean inGroup = false;
709 int alHeight = av.getAlignment().getHeight() - 1;
711 for (i = startSeq; i <= endSeq; i++)
713 sx = (group.getStartRes() - startRes) * avcharWidth;
714 sy = offset + ((i - startSeq) * avcharHeight);
715 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
717 if (sx + ex < 0 || sx > imgWidth)
722 if ((sx <= (endRes - startRes) * avcharWidth)
723 && group.getSequences(null).contains(
724 av.getAlignment().getSequenceAt(i)))
727 && (i >= alHeight || !group.getSequences(null)
729 av.getAlignment().getSequenceAt(i + 1))))
731 bottom = sy + avcharHeight;
736 if (((top == -1) && (i == 0))
737 || !group.getSequences(null).contains(
738 av.getAlignment().getSequenceAt(i - 1)))
746 if (group == av.getSelectionGroup())
748 g.setColor(Color.red);
752 g.setColor(group.getOutlineColour());
760 if (sx >= 0 && sx < imgWidth)
762 g.drawLine(sx, oldY, sx, sy);
765 if (sx + ex < imgWidth)
767 g.drawLine(sx + ex, oldY, sx + ex, sy);
776 if (sx + ex > imgWidth)
781 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
783 ex = (endRes - startRes + 1) * avcharWidth;
788 g.drawLine(sx, top, sx + ex, top);
794 g.drawLine(sx, bottom, sx + ex, bottom);
805 sy = offset + ((i - startSeq) * avcharHeight);
806 if (sx >= 0 && sx < imgWidth)
808 g.drawLine(sx, oldY, sx, sy);
811 if (sx + ex < imgWidth)
813 g.drawLine(sx + ex, oldY, sx + ex, sy);
822 if (sx + ex > imgWidth)
826 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
828 ex = (endRes - startRes + 1) * avcharWidth;
833 g.drawLine(sx, top, sx + ex, top);
839 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
848 if (groupIndex >= av.getAlignment().getGroups().size())
853 group = av.getAlignment().getGroups().get(groupIndex);
854 } while (groupIndex < av.getAlignment().getGroups().size());
859 public void highlightSearchResults(SearchResultsI results)
861 av.setSearchResults(results);
866 public void propertyChange(PropertyChangeEvent evt)
868 if (!av.getWrapAlignment())
870 if (evt.getPropertyName().equals("startres")
871 || evt.getPropertyName().equals("endres"))
873 // Make sure we're not trying to draw a panel
874 // larger than the visible window
875 ViewportRanges vpRanges = av.getRanges();
876 int scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
877 if (scrollX > vpRanges.getEndRes() - vpRanges.getStartRes())
879 scrollX = vpRanges.getEndRes() - vpRanges.getStartRes();
881 else if (scrollX < vpRanges.getStartRes() - vpRanges.getEndRes())
883 scrollX = vpRanges.getStartRes() - vpRanges.getEndRes();
885 fastPaint(scrollX, 0);
887 else if (evt.getPropertyName() == "startseq"
888 || evt.getPropertyName() == "endseq")
890 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());