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.
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.HiddenColumns;
25 import jalview.datamodel.SearchResultsI;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.ScaleRenderer;
29 import jalview.renderer.ScaleRenderer.ScaleMark;
30 import jalview.viewmodel.ViewportListenerI;
31 import jalview.viewmodel.ViewportRanges;
33 import java.awt.BasicStroke;
34 import java.awt.BorderLayout;
35 import java.awt.Color;
36 import java.awt.FontMetrics;
37 import java.awt.Graphics;
38 import java.awt.Graphics2D;
39 import java.awt.RenderingHints;
40 import java.awt.Shape;
41 import java.awt.image.BufferedImage;
42 import java.beans.PropertyChangeEvent;
43 import java.util.List;
45 import javax.swing.JComponent;
53 public class SeqCanvas extends JComponent implements ViewportListenerI
55 final FeatureRenderer fr;
57 final SequenceRenderer sr;
69 boolean fastPaint = false;
80 * Creates a new SeqCanvas object.
85 public SeqCanvas(AlignmentPanel ap)
89 fr = new FeatureRenderer(ap);
90 sr = new SequenceRenderer(av);
91 setLayout(new BorderLayout());
92 PaintRefresher.Register(this, av.getSequenceSetId());
93 setBackground(Color.white);
95 av.getRanges().addPropertyChangeListener(this);
98 public SequenceRenderer getSequenceRenderer()
103 public FeatureRenderer getFeatureRenderer()
108 int charHeight = 0, charWidth = 0;
110 private void updateViewport()
112 charHeight = av.getCharHeight();
113 charWidth = av.getCharWidth();
128 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
131 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
134 int mpos = mark.column; // (i - startx - 1)
139 String mstring = mark.text;
145 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
147 g.drawLine((mpos * charWidth) + (charWidth / 2),
148 (ypos + 2) - (charHeight / 2),
149 (mpos * charWidth) + (charWidth / 2), ypos - 2);
166 void drawWestScale(Graphics g, int startx, int endx, int ypos)
168 FontMetrics fm = getFontMetrics(av.getFont());
171 if (av.hasHiddenColumns())
173 startx = av.getAlignment().getHiddenColumns()
174 .adjustForHiddenColumns(startx);
175 endx = av.getAlignment().getHiddenColumns()
176 .adjustForHiddenColumns(endx);
179 int maxwidth = av.getAlignment().getWidth();
180 if (av.hasHiddenColumns())
182 maxwidth = av.getAlignment().getHiddenColumns()
183 .findColumnPosition(maxwidth) - 1;
187 for (int i = 0; i < av.getAlignment().getHeight(); i++)
189 SequenceI seq = av.getAlignment().getSequenceAt(i);
195 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
202 value = av.getAlignment().getSequenceAt(i).findPosition(index);
209 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
211 g.drawString(value + "", x,
212 (ypos + (i * charHeight)) - (charHeight / 5));
229 void drawEastScale(Graphics g, int startx, int endx, int ypos)
233 if (av.hasHiddenColumns())
235 endx = av.getAlignment().getHiddenColumns()
236 .adjustForHiddenColumns(endx);
241 for (int i = 0; i < av.getAlignment().getHeight(); i++)
243 seq = av.getAlignment().getSequenceAt(i);
247 while (index > startx)
249 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
256 value = seq.findPosition(index);
263 g.drawString(String.valueOf(value), 0,
264 (ypos + (i * charHeight)) - (charHeight / 5));
269 boolean fastpainting = false;
272 * need to make this thread safe move alignment rendering in response to
278 * shift up or down in repaint
280 public void fastPaint(int horizontal, int vertical)
282 if (fastpainting || gg == null)
290 ViewportRanges ranges = av.getRanges();
291 int sr = ranges.getStartRes();
292 int er = ranges.getEndRes();
293 int ss = ranges.getStartSeq();
294 int es = ranges.getEndSeq();
298 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
299 imgHeight, -horizontal * charWidth, -vertical * charHeight);
301 if (horizontal > 0) // scrollbar pulled right, image to the left
303 transX = (er - sr - horizontal) * charWidth;
304 sr = er - horizontal;
306 else if (horizontal < 0)
308 er = sr - horizontal;
310 else if (vertical > 0) // scroll down
314 if (ss < ranges.getStartSeq())
315 { // ie scrolling too fast, more than a page at a time
316 ss = ranges.getStartSeq();
320 transY = imgHeight - ((vertical + 1) * charHeight);
323 else if (vertical < 0)
327 if (es > ranges.getEndSeq())
329 es = ranges.getEndSeq();
333 gg.translate(transX, transY);
334 drawPanel(gg, sr, er, ss, es, 0);
335 gg.translate(-transX, -transY);
338 fastpainting = false;
342 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
343 * towards! startx is the first residue (starting at 0) to display. endx is
344 * the last residue to display (starting at 0). starty is the first sequence
345 * to display (starting at 0). endy is the last sequence to display (starting
346 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
347 * adjustment listener in SeqPanel when the scrollbars move.
350 // Set this to false to force a full panel paint
352 public void paintComponent(Graphics g)
355 BufferedImage lcimg = img; // take reference since other threads may null
356 // img and call later.
357 super.paintComponent(g);
359 if (lcimg != null && (fastPaint
360 || (getVisibleRect().width != g.getClipBounds().width)
361 || (getVisibleRect().height != g.getClipBounds().height)))
363 g.drawImage(lcimg, 0, 0, this);
368 // this draws the whole of the alignment
369 imgWidth = getWidth();
370 imgHeight = getHeight();
372 imgWidth -= (imgWidth % charWidth);
373 imgHeight -= (imgHeight % charHeight);
375 if ((imgWidth < 1) || (imgHeight < 1))
380 if (lcimg == null || imgWidth != lcimg.getWidth()
381 || imgHeight != lcimg.getHeight())
385 lcimg = img = new BufferedImage(imgWidth, imgHeight,
386 BufferedImage.TYPE_INT_RGB);
387 gg = (Graphics2D) img.getGraphics();
388 gg.setFont(av.getFont());
389 } catch (OutOfMemoryError er)
392 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
393 new OOMWarning("Creating alignment image for display", er);
401 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
402 RenderingHints.VALUE_ANTIALIAS_ON);
405 gg.setColor(Color.white);
406 gg.fillRect(0, 0, imgWidth, imgHeight);
408 ViewportRanges ranges = av.getRanges();
409 if (av.getWrapAlignment())
411 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
415 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
416 ranges.getStartSeq(), ranges.getEndSeq(), 0);
419 g.drawImage(lcimg, 0, 0, this);
429 * @return DOCUMENT ME!
431 public int getWrappedCanvasWidth(int cwidth)
433 FontMetrics fm = getFontMetrics(av.getFont());
438 if (av.getScaleRightWrapped())
440 LABEL_EAST = fm.stringWidth(getMask());
443 if (av.getScaleLeftWrapped())
445 LABEL_WEST = fm.stringWidth(getMask());
448 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
452 * Generates a string of zeroes.
461 for (int i = 0; i < av.getAlignment().getHeight(); i++)
463 tmp = av.getAlignment().getSequenceAt(i).getEnd();
470 for (int i = maxWidth; i > 0; i /= 10)
484 * @param canvasHeight
489 public void drawWrappedPanel(Graphics g, int canvasWidth,
490 int canvasHeight, int startRes)
493 AlignmentI al = av.getAlignment();
495 FontMetrics fm = getFontMetrics(av.getFont());
500 if (av.getScaleRightWrapped())
502 LABEL_EAST = fm.stringWidth(getMask());
505 if (av.getScaleLeftWrapped())
507 LABEL_WEST = fm.stringWidth(getMask());
510 int hgap = charHeight;
511 if (av.getScaleAboveWrapped())
516 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
517 int cHeight = av.getAlignment().getHeight() * charHeight;
519 av.setWrappedWidth(cWidth);
521 av.getRanges().setViewportStartAndWidth(startRes, cWidth);
525 int maxwidth = av.getAlignment().getWidth();
527 if (av.hasHiddenColumns())
529 maxwidth = av.getAlignment().getHiddenColumns()
530 .findColumnPosition(maxwidth);
533 while ((ypos <= canvasHeight) && (startRes < maxwidth))
535 endx = startRes + cWidth - 1;
542 g.setFont(av.getFont());
543 g.setColor(Color.black);
545 if (av.getScaleLeftWrapped())
547 drawWestScale(g, startRes, endx, ypos);
550 if (av.getScaleRightWrapped())
552 g.translate(canvasWidth - LABEL_EAST, 0);
553 drawEastScale(g, startRes, endx, ypos);
554 g.translate(-(canvasWidth - LABEL_EAST), 0);
557 g.translate(LABEL_WEST, 0);
559 if (av.getScaleAboveWrapped())
561 drawNorthScale(g, startRes, endx, ypos);
564 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
566 g.setColor(Color.blue);
568 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
569 List<Integer> positions = hidden.findHiddenRegionPositions();
570 for (int pos : positions)
572 res = pos - startRes;
574 if (res < 0 || res > endx - startRes)
581 { res * charWidth - charHeight / 4,
582 res * charWidth + charHeight / 4, res * charWidth },
584 { ypos - (charHeight / 2), ypos - (charHeight / 2),
585 ypos - (charHeight / 2) + 8 },
591 // When printing we have an extra clipped region,
592 // the Printable page which we need to account for here
593 Shape clip = g.getClip();
597 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
601 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
602 (int) clip.getBounds().getHeight());
605 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
607 if (av.isShowAnnotation())
609 g.translate(0, cHeight + ypos + 3);
610 if (annotations == null)
612 annotations = new AnnotationPanel(av);
615 annotations.renderer.drawComponent(annotations, av, g, -1, startRes,
617 g.translate(0, -cHeight - ypos - 3);
620 g.translate(-LABEL_WEST, 0);
622 ypos += cHeight + getAnnotationHeight() + hgap;
628 AnnotationPanel annotations;
630 int getAnnotationHeight()
632 if (!av.isShowAnnotation())
637 if (annotations == null)
639 annotations = new AnnotationPanel(av);
642 return annotations.adjustPanelHeight();
661 public void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
662 int endSeq, int offset)
665 if (!av.hasHiddenColumns())
667 draw(g1, startRes, endRes, startSeq, endSeq, offset);
672 int blockStart = startRes;
673 int blockEnd = endRes;
675 for (int[] region : av.getAlignment().getHiddenColumns()
676 .getHiddenColumnsCopy())
678 int hideStart = region[0];
679 int hideEnd = region[1];
681 if (hideStart <= blockStart)
683 blockStart += (hideEnd - hideStart) + 1;
687 blockEnd = hideStart - 1;
689 g1.translate(screenY * charWidth, 0);
691 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
693 if (av.getShowHiddenMarkers())
695 g1.setColor(Color.blue);
697 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
698 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
699 (endSeq - startSeq + 1) * charHeight + offset);
702 g1.translate(-screenY * charWidth, 0);
703 screenY += blockEnd - blockStart + 1;
704 blockStart = hideEnd + 1;
706 if (screenY > (endRes - startRes))
708 // already rendered last block
713 if (screenY <= (endRes - startRes))
715 // remaining visible region to render
716 blockEnd = blockStart + (endRes - startRes) - screenY;
717 g1.translate(screenY * charWidth, 0);
718 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
720 g1.translate(-screenY * charWidth, 0);
726 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
727 // int x1, int x2, int y1, int y2, int startx, int starty,
728 private void draw(Graphics g, int startRes, int endRes, int startSeq,
729 int endSeq, int offset)
731 g.setFont(av.getFont());
732 sr.prepare(g, av.isRenderGaps());
736 // / First draw the sequences
737 // ///////////////////////////
738 for (int i = startSeq; i <= endSeq; i++)
740 nextSeq = av.getAlignment().getSequenceAt(i);
743 // occasionally, a race condition occurs such that the alignment row is
747 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
748 startRes, endRes, offset + ((i - startSeq) * charHeight));
750 if (av.isShowSequenceFeatures())
752 fr.drawSequence(g, nextSeq, startRes, endRes,
753 offset + ((i - startSeq) * charHeight), false);
756 // / Highlight search Results once all sequences have been drawn
757 // ////////////////////////////////////////////////////////
758 if (av.hasSearchResults())
760 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
762 if (visibleResults != null)
764 for (int r = 0; r < visibleResults.length; r += 2)
766 sr.drawHighlightedText(nextSeq, visibleResults[r],
767 visibleResults[r + 1],
768 (visibleResults[r] - startRes) * charWidth,
769 offset + ((i - startSeq) * charHeight));
774 if (av.cursorMode && cursorY == i && cursorX >= startRes
775 && cursorX <= endRes)
777 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
778 offset + ((i - startSeq) * charHeight));
782 if (av.getSelectionGroup() != null
783 || av.getAlignment().getGroups().size() > 0)
785 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
790 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
791 int startSeq, int endSeq, int offset)
793 Graphics2D g = (Graphics2D) g1;
795 // ///////////////////////////////////
796 // Now outline any areas if necessary
797 // ///////////////////////////////////
798 SequenceGroup group = av.getSelectionGroup();
804 int visWidth = (endRes - startRes + 1) * charWidth;
806 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
808 group = av.getAlignment().getGroups().get(0);
818 boolean inGroup = false;
822 for (i = startSeq; i <= endSeq; i++)
824 sx = (group.getStartRes() - startRes) * charWidth;
825 sy = offset + ((i - startSeq) * charHeight);
826 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth)
829 if (sx + ex < 0 || sx > visWidth)
834 if ((sx <= (endRes - startRes) * charWidth)
835 && group.getSequences(null)
836 .contains(av.getAlignment().getSequenceAt(i)))
838 if ((bottom == -1) && !group.getSequences(null)
839 .contains(av.getAlignment().getSequenceAt(i + 1)))
841 bottom = sy + charHeight;
846 if (((top == -1) && (i == 0)) || !group.getSequences(null)
847 .contains(av.getAlignment().getSequenceAt(i - 1)))
855 if (group == av.getSelectionGroup())
857 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
858 BasicStroke.JOIN_ROUND, 3f, new float[]
860 g.setColor(Color.RED);
864 g.setStroke(new BasicStroke());
865 g.setColor(group.getOutlineColour());
873 if (sx >= 0 && sx < visWidth)
875 g.drawLine(sx, oldY, sx, sy);
878 if (sx + ex < visWidth)
880 g.drawLine(sx + ex, oldY, sx + ex, sy);
889 if (sx + ex > visWidth)
894 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
896 ex = (endRes - startRes + 1) * charWidth;
901 g.drawLine(sx, top, sx + ex, top);
907 g.drawLine(sx, bottom, sx + ex, bottom);
918 sy = offset + ((i - startSeq) * charHeight);
919 if (sx >= 0 && sx < visWidth)
921 g.drawLine(sx, oldY, sx, sy);
924 if (sx + ex < visWidth)
926 g.drawLine(sx + ex, oldY, sx + ex, sy);
935 if (sx + ex > visWidth)
939 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
941 ex = (endRes - startRes + 1) * charWidth;
946 g.drawLine(sx, top, sx + ex, top);
952 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
961 g.setStroke(new BasicStroke());
963 if (groupIndex >= av.getAlignment().getGroups().size())
968 group = av.getAlignment().getGroups().get(groupIndex);
970 } while (groupIndex < av.getAlignment().getGroups().size());
982 public void highlightSearchResults(SearchResultsI results)
986 av.setSearchResults(results);
992 public void propertyChange(PropertyChangeEvent evt)
994 String eventName = evt.getPropertyName();
996 if (av.getWrapAlignment())
998 if (eventName.equals(ViewportRanges.STARTRES))
1006 if (eventName.equals(ViewportRanges.STARTRES))
1008 // Make sure we're not trying to draw a panel
1009 // larger than the visible window
1010 ViewportRanges vpRanges = av.getRanges();
1011 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1012 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1013 if (scrollX > range)
1017 else if (scrollX < -range)
1023 // Both scrolling and resizing change viewport ranges: scrolling changes
1024 // both start and end points, but resize only changes end values.
1025 // Here we only want to fastpaint on a scroll, with resize using a normal
1026 // paint, so scroll events are identified as changes to the horizontal or
1027 // vertical start value.
1028 if (eventName.equals(ViewportRanges.STARTRES))
1030 // scroll - startres and endres both change
1031 fastPaint(scrollX, 0);
1033 else if (eventName.equals(ViewportRanges.STARTSEQ))
1036 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());