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), (ypos + 2)
148 - (charHeight / 2), (mpos * charWidth) + (charWidth / 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, (ypos + (i * charHeight))
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, (ypos + (i * charHeight))
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)
289 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
290 imgHeight, -horizontal * charWidth, -vertical * charHeight);
292 ViewportRanges ranges = av.getRanges();
293 int sr = ranges.getStartRes();
294 int er = ranges.getEndRes();
295 int ss = ranges.getStartSeq();
296 int es = ranges.getEndSeq();
300 if (horizontal > 0) // scrollbar pulled right, image to the left
302 transX = (er - sr - horizontal) * charWidth;
303 sr = er - horizontal;
305 else if (horizontal < 0)
307 er = sr - horizontal;
309 else if (vertical > 0) // scroll down
313 if (ss < ranges.getStartSeq())
314 { // ie scrolling too fast, more than a page at a time
315 ss = ranges.getStartSeq();
319 transY = imgHeight - ((vertical + 1) * charHeight);
322 else if (vertical < 0)
326 if (es > ranges.getEndSeq())
328 es = ranges.getEndSeq();
332 gg.translate(transX, transY);
333 drawPanel(gg, sr, er, ss, es, 0);
334 gg.translate(-transX, -transY);
337 fastpainting = false;
341 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
342 * towards! startx is the first residue (starting at 0) to display. endx is
343 * the last residue to display (starting at 0). starty is the first sequence
344 * to display (starting at 0). endy is the last sequence to display (starting
345 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
346 * adjustment listener in SeqPanel when the scrollbars move.
349 // Set this to false to force a full panel paint
351 public void paintComponent(Graphics g)
354 BufferedImage lcimg = img; // take reference since other threads may null
355 // img and call later.
356 super.paintComponent(g);
360 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
361 .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());
497 if (av.getScaleRightWrapped())
499 LABEL_EAST = fm.stringWidth(getMask());
502 if (av.getScaleLeftWrapped())
504 LABEL_WEST = fm.stringWidth(getMask());
507 int hgap = charHeight;
508 if (av.getScaleAboveWrapped())
513 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
514 int cHeight = av.getAlignment().getHeight() * charHeight;
516 av.setWrappedWidth(cWidth);
518 av.getRanges().setEndRes(av.getRanges().getStartRes() + cWidth - 1);
522 int maxwidth = av.getAlignment().getWidth() - 1;
524 if (av.hasHiddenColumns())
526 maxwidth = av.getAlignment().getHiddenColumns()
527 .findColumnPosition(maxwidth) - 1;
530 while ((ypos <= canvasHeight) && (startRes < maxwidth))
532 endx = startRes + cWidth - 1;
539 g.setFont(av.getFont());
540 g.setColor(Color.black);
542 if (av.getScaleLeftWrapped())
544 drawWestScale(g, startRes, endx, ypos);
547 if (av.getScaleRightWrapped())
549 g.translate(canvasWidth - LABEL_EAST, 0);
550 drawEastScale(g, startRes, endx, ypos);
551 g.translate(-(canvasWidth - LABEL_EAST), 0);
554 g.translate(LABEL_WEST, 0);
556 if (av.getScaleAboveWrapped())
558 drawNorthScale(g, startRes, endx, ypos);
561 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
563 g.setColor(Color.blue);
565 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
566 List<Integer> positions = hidden.findHiddenRegionPositions();
567 for (int pos : positions)
569 res = pos - startRes;
571 if (res < 0 || res > endx - startRes)
577 new int[] { res * charWidth - charHeight / 4,
578 res * charWidth + charHeight / 4, res * charWidth },
579 new int[] { ypos - (charHeight / 2),
580 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
586 // When printing we have an extra clipped region,
587 // the Printable page which we need to account for here
588 Shape clip = g.getClip();
592 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
596 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
597 (int) clip.getBounds().getHeight());
600 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
602 if (av.isShowAnnotation())
604 g.translate(0, cHeight + ypos + 3);
605 if (annotations == null)
607 annotations = new AnnotationPanel(av);
610 annotations.renderer.drawComponent(annotations, av, g, -1,
612 g.translate(0, -cHeight - ypos - 3);
615 g.translate(-LABEL_WEST, 0);
617 ypos += cHeight + getAnnotationHeight() + hgap;
623 AnnotationPanel annotations;
625 int getAnnotationHeight()
627 if (!av.isShowAnnotation())
632 if (annotations == null)
634 annotations = new AnnotationPanel(av);
637 return annotations.adjustPanelHeight();
656 public void drawPanel(Graphics g1, int startRes, int endRes,
657 int startSeq, int endSeq, int offset)
660 if (!av.hasHiddenColumns())
662 draw(g1, startRes, endRes, startSeq, endSeq, offset);
667 int blockStart = startRes;
668 int blockEnd = endRes;
670 for (int[] region : av.getAlignment().getHiddenColumns()
671 .getHiddenColumnsCopy())
673 int hideStart = region[0];
674 int hideEnd = region[1];
676 if (hideStart <= blockStart)
678 blockStart += (hideEnd - hideStart) + 1;
682 blockEnd = hideStart - 1;
684 g1.translate(screenY * charWidth, 0);
686 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
688 if (av.getShowHiddenMarkers())
690 g1.setColor(Color.blue);
692 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
693 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
694 (endSeq - startSeq + 1) * charHeight + offset);
697 g1.translate(-screenY * charWidth, 0);
698 screenY += blockEnd - blockStart + 1;
699 blockStart = hideEnd + 1;
701 if (screenY > (endRes - startRes))
703 // already rendered last block
708 if (screenY <= (endRes - startRes))
710 // remaining visible region to render
711 blockEnd = blockStart + (endRes - startRes) - screenY;
712 g1.translate(screenY * charWidth, 0);
713 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
715 g1.translate(-screenY * charWidth, 0);
721 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
722 // int x1, int x2, int y1, int y2, int startx, int starty,
723 private void draw(Graphics g, int startRes, int endRes, int startSeq,
724 int endSeq, int offset)
726 g.setFont(av.getFont());
727 sr.prepare(g, av.isRenderGaps());
731 // / First draw the sequences
732 // ///////////////////////////
733 for (int i = startSeq; i <= endSeq; i++)
735 nextSeq = av.getAlignment().getSequenceAt(i);
738 // occasionally, a race condition occurs such that the alignment row is
742 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
743 startRes, endRes, offset + ((i - startSeq) * charHeight));
745 if (av.isShowSequenceFeatures())
747 fr.drawSequence(g, nextSeq, startRes, endRes, offset
748 + ((i - startSeq) * charHeight), false);
751 // / Highlight search Results once all sequences have been drawn
752 // ////////////////////////////////////////////////////////
753 if (av.hasSearchResults())
755 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
757 if (visibleResults != null)
759 for (int r = 0; r < visibleResults.length; r += 2)
761 sr.drawHighlightedText(nextSeq, visibleResults[r],
762 visibleResults[r + 1], (visibleResults[r] - startRes)
764 + ((i - startSeq) * charHeight));
769 if (av.cursorMode && cursorY == i && cursorX >= startRes
770 && cursorX <= endRes)
772 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
773 offset + ((i - startSeq) * charHeight));
777 if (av.getSelectionGroup() != null
778 || av.getAlignment().getGroups().size() > 0)
780 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
785 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
786 int startSeq, int endSeq, int offset)
788 Graphics2D g = (Graphics2D) g1;
790 // ///////////////////////////////////
791 // Now outline any areas if necessary
792 // ///////////////////////////////////
793 SequenceGroup group = av.getSelectionGroup();
799 int visWidth = (endRes - startRes + 1) * charWidth;
801 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
803 group = av.getAlignment().getGroups().get(0);
813 boolean inGroup = false;
817 for (i = startSeq; i <= endSeq; i++)
819 sx = (group.getStartRes() - startRes) * charWidth;
820 sy = offset + ((i - startSeq) * charHeight);
821 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
823 if (sx + ex < 0 || sx > visWidth)
828 if ((sx <= (endRes - startRes) * charWidth)
829 && group.getSequences(null).contains(
830 av.getAlignment().getSequenceAt(i)))
833 && !group.getSequences(null).contains(
834 av.getAlignment().getSequenceAt(i + 1)))
836 bottom = sy + charHeight;
841 if (((top == -1) && (i == 0))
842 || !group.getSequences(null).contains(
843 av.getAlignment().getSequenceAt(i - 1)))
851 if (group == av.getSelectionGroup())
853 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
854 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
856 g.setColor(Color.RED);
860 g.setStroke(new BasicStroke());
861 g.setColor(group.getOutlineColour());
869 if (sx >= 0 && sx < visWidth)
871 g.drawLine(sx, oldY, sx, sy);
874 if (sx + ex < visWidth)
876 g.drawLine(sx + ex, oldY, sx + ex, sy);
885 if (sx + ex > visWidth)
890 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
892 ex = (endRes - startRes + 1) * charWidth;
897 g.drawLine(sx, top, sx + ex, top);
903 g.drawLine(sx, bottom, sx + ex, bottom);
914 sy = offset + ((i - startSeq) * charHeight);
915 if (sx >= 0 && sx < visWidth)
917 g.drawLine(sx, oldY, sx, sy);
920 if (sx + ex < visWidth)
922 g.drawLine(sx + ex, oldY, sx + ex, sy);
931 if (sx + ex > visWidth)
935 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
937 ex = (endRes - startRes + 1) * charWidth;
942 g.drawLine(sx, top, sx + ex, top);
948 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
957 g.setStroke(new BasicStroke());
959 if (groupIndex >= av.getAlignment().getGroups().size())
964 group = av.getAlignment().getGroups().get(groupIndex);
966 } while (groupIndex < av.getAlignment().getGroups().size());
978 public void highlightSearchResults(SearchResultsI results)
982 av.setSearchResults(results);
988 public void propertyChange(PropertyChangeEvent evt)
990 if (!av.getWrapAlignment())
992 if (evt.getPropertyName().equals("startres")
993 || evt.getPropertyName().equals("endres"))
995 // Make sure we're not trying to draw a panel
996 // larger than the visible window
997 ViewportRanges vpRanges = av.getRanges();
998 int scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
999 if (scrollX > vpRanges.getEndRes() - vpRanges.getStartRes())
1001 scrollX = vpRanges.getEndRes() - vpRanges.getStartRes();
1003 else if (scrollX < vpRanges.getStartRes() - vpRanges.getEndRes())
1005 scrollX = vpRanges.getStartRes() - vpRanges.getEndRes();
1007 fastPaint(scrollX, 0);
1009 else if (evt.getPropertyName().equals("startseq")
1010 || evt.getPropertyName().equals("endseq"))
1012 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());