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;
44 import javax.swing.JComponent;
52 public class SeqCanvas extends JComponent implements ViewportListenerI
54 final FeatureRenderer fr;
56 final SequenceRenderer sr;
68 boolean fastPaint = false;
79 * Creates a new SeqCanvas object.
84 public SeqCanvas(AlignmentPanel ap)
88 fr = new FeatureRenderer(ap);
89 sr = new SequenceRenderer(av);
90 setLayout(new BorderLayout());
91 PaintRefresher.Register(this, av.getSequenceSetId());
92 setBackground(Color.white);
94 av.getRanges().addPropertyChangeListener(this);
97 public SequenceRenderer getSequenceRenderer()
102 public FeatureRenderer getFeatureRenderer()
107 int charHeight = 0, charWidth = 0;
109 private void updateViewport()
111 charHeight = av.getCharHeight();
112 charWidth = av.getCharWidth();
127 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
130 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
133 int mpos = mark.column; // (i - startx - 1)
138 String mstring = mark.text;
144 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
146 g.drawLine((mpos * charWidth) + (charWidth / 2), (ypos + 2)
147 - (charHeight / 2), (mpos * charWidth) + (charWidth / 2),
165 void drawWestScale(Graphics g, int startx, int endx, int ypos)
167 FontMetrics fm = getFontMetrics(av.getFont());
170 if (av.hasHiddenColumns())
172 startx = av.getAlignment().getHiddenColumns()
173 .adjustForHiddenColumns(startx);
174 endx = av.getAlignment().getHiddenColumns()
175 .adjustForHiddenColumns(endx);
178 int maxwidth = av.getAlignment().getWidth();
179 if (av.hasHiddenColumns())
181 maxwidth = av.getAlignment().getHiddenColumns()
182 .findColumnPosition(maxwidth) - 1;
186 for (int i = 0; i < av.getAlignment().getHeight(); i++)
188 SequenceI seq = av.getAlignment().getSequenceAt(i);
194 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
201 value = av.getAlignment().getSequenceAt(i).findPosition(index);
208 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
210 g.drawString(value + "", x, (ypos + (i * charHeight))
228 void drawEastScale(Graphics g, int startx, int endx, int ypos)
232 if (av.hasHiddenColumns())
234 endx = av.getAlignment().getHiddenColumns()
235 .adjustForHiddenColumns(endx);
240 for (int i = 0; i < av.getAlignment().getHeight(); i++)
242 seq = av.getAlignment().getSequenceAt(i);
246 while (index > startx)
248 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
255 value = seq.findPosition(index);
262 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
268 boolean fastpainting = false;
271 * need to make this thread safe move alignment rendering in response to
277 * shift up or down in repaint
279 public void fastPaint(int horizontal, int vertical)
281 if (fastpainting || gg == null)
288 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
289 imgHeight, -horizontal * charWidth, -vertical * charHeight);
291 ViewportRanges ranges = av.getRanges();
292 int sr = ranges.getStartRes();
293 int er = ranges.getEndRes();
294 int ss = ranges.getStartSeq();
295 int es = ranges.getEndSeq();
299 if (horizontal > 0) // scrollbar pulled right, image to the left
301 transX = (er - sr - horizontal) * charWidth;
302 sr = er - horizontal;
304 else if (horizontal < 0)
306 er = sr - horizontal;
308 else if (vertical > 0) // scroll down
312 if (ss < ranges.getStartSeq())
313 { // ie scrolling too fast, more than a page at a time
314 ss = ranges.getStartSeq();
318 transY = imgHeight - ((vertical + 1) * charHeight);
321 else if (vertical < 0)
325 if (es > ranges.getEndSeq())
327 es = ranges.getEndSeq();
331 gg.translate(transX, transY);
332 drawPanel(gg, sr, er, ss, es, 0);
333 gg.translate(-transX, -transY);
336 fastpainting = false;
340 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
341 * towards! startx is the first residue (starting at 0) to display. endx is
342 * the last residue to display (starting at 0). starty is the first sequence
343 * to display (starting at 0). endy is the last sequence to display (starting
344 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
345 * adjustment listener in SeqPanel when the scrollbars move.
348 // Set this to false to force a full panel paint
350 public void paintComponent(Graphics g)
353 BufferedImage lcimg = img; // take reference since other threads may null
354 // img and call later.
355 super.paintComponent(g);
359 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
360 .getClipBounds().height)))
362 g.drawImage(lcimg, 0, 0, this);
367 // this draws the whole of the alignment
368 imgWidth = getWidth();
369 imgHeight = getHeight();
371 imgWidth -= (imgWidth % charWidth);
372 imgHeight -= (imgHeight % charHeight);
374 if ((imgWidth < 1) || (imgHeight < 1))
379 if (lcimg == null || imgWidth != lcimg.getWidth()
380 || imgHeight != lcimg.getHeight())
384 lcimg = img = new BufferedImage(imgWidth, imgHeight,
385 BufferedImage.TYPE_INT_RGB);
386 gg = (Graphics2D) img.getGraphics();
387 gg.setFont(av.getFont());
388 } catch (OutOfMemoryError er)
391 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
392 new OOMWarning("Creating alignment image for display", er);
400 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
401 RenderingHints.VALUE_ANTIALIAS_ON);
404 gg.setColor(Color.white);
405 gg.fillRect(0, 0, imgWidth, imgHeight);
407 ViewportRanges ranges = av.getRanges();
408 if (av.getWrapAlignment())
410 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
414 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
415 ranges.getStartSeq(), ranges.getEndSeq(), 0);
418 g.drawImage(lcimg, 0, 0, this);
428 * @return DOCUMENT ME!
430 public int getWrappedCanvasWidth(int cwidth)
432 FontMetrics fm = getFontMetrics(av.getFont());
437 if (av.getScaleRightWrapped())
439 LABEL_EAST = fm.stringWidth(getMask());
442 if (av.getScaleLeftWrapped())
444 LABEL_WEST = fm.stringWidth(getMask());
447 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
451 * Generates a string of zeroes.
460 for (int i = 0; i < av.getAlignment().getHeight(); i++)
462 tmp = av.getAlignment().getSequenceAt(i).getEnd();
469 for (int i = maxWidth; i > 0; i /= 10)
483 * @param canvasHeight
488 public void drawWrappedPanel(Graphics g, int canvasWidth,
489 int canvasHeight, int startRes)
492 AlignmentI al = av.getAlignment();
494 FontMetrics fm = getFontMetrics(av.getFont());
496 if (av.getScaleRightWrapped())
498 LABEL_EAST = fm.stringWidth(getMask());
501 if (av.getScaleLeftWrapped())
503 LABEL_WEST = fm.stringWidth(getMask());
506 int hgap = charHeight;
507 if (av.getScaleAboveWrapped())
512 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
513 int cHeight = av.getAlignment().getHeight() * charHeight;
515 av.setWrappedWidth(cWidth);
517 av.getRanges().setEndRes(av.getRanges().getStartRes() + cWidth - 1);
521 int maxwidth = av.getAlignment().getWidth() - 1;
523 if (av.hasHiddenColumns())
525 maxwidth = av.getAlignment().getHiddenColumns()
526 .findColumnPosition(maxwidth) - 1;
529 while ((ypos <= canvasHeight) && (startRes < maxwidth))
531 endx = startRes + cWidth - 1;
538 g.setFont(av.getFont());
539 g.setColor(Color.black);
541 if (av.getScaleLeftWrapped())
543 drawWestScale(g, startRes, endx, ypos);
546 if (av.getScaleRightWrapped())
548 g.translate(canvasWidth - LABEL_EAST, 0);
549 drawEastScale(g, startRes, endx, ypos);
550 g.translate(-(canvasWidth - LABEL_EAST), 0);
553 g.translate(LABEL_WEST, 0);
555 if (av.getScaleAboveWrapped())
557 drawNorthScale(g, startRes, endx, ypos);
560 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
562 g.setColor(Color.blue);
564 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
565 for (int i = 0; i < hidden.getHiddenRegions().size(); i++)
567 res = hidden.findHiddenRegionPosition(i) - startRes;
569 if (res < 0 || res > endx - startRes)
575 new int[] { res * charWidth - charHeight / 4,
576 res * charWidth + charHeight / 4, res * charWidth },
577 new int[] { ypos - (charHeight / 2),
578 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
584 // When printing we have an extra clipped region,
585 // the Printable page which we need to account for here
586 Shape clip = g.getClip();
590 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
594 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
595 (int) clip.getBounds().getHeight());
598 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
600 if (av.isShowAnnotation())
602 g.translate(0, cHeight + ypos + 3);
603 if (annotations == null)
605 annotations = new AnnotationPanel(av);
608 annotations.renderer.drawComponent(annotations, av, g, -1,
610 g.translate(0, -cHeight - ypos - 3);
613 g.translate(-LABEL_WEST, 0);
615 ypos += cHeight + getAnnotationHeight() + hgap;
621 AnnotationPanel annotations;
623 int getAnnotationHeight()
625 if (!av.isShowAnnotation())
630 if (annotations == null)
632 annotations = new AnnotationPanel(av);
635 return annotations.adjustPanelHeight();
654 public void drawPanel(Graphics g1, int startRes, int endRes,
655 int startSeq, int endSeq, int offset)
658 if (!av.hasHiddenColumns())
660 draw(g1, startRes, endRes, startSeq, endSeq, offset);
665 int blockStart = startRes;
666 int blockEnd = endRes;
668 for (int[] region : av.getAlignment().getHiddenColumns())
670 int hideStart = region[0];
671 int hideEnd = region[1];
673 if (hideStart <= blockStart)
675 blockStart += (hideEnd - hideStart) + 1;
679 blockEnd = hideStart - 1;
681 g1.translate(screenY * charWidth, 0);
683 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
685 if (av.getShowHiddenMarkers())
687 g1.setColor(Color.blue);
689 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
690 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
691 (endSeq - startSeq + 1) * charHeight + offset);
694 g1.translate(-screenY * charWidth, 0);
695 screenY += blockEnd - blockStart + 1;
696 blockStart = hideEnd + 1;
698 if (screenY > (endRes - startRes))
700 // already rendered last block
705 if (screenY <= (endRes - startRes))
707 // remaining visible region to render
708 blockEnd = blockStart + (endRes - startRes) - screenY;
709 g1.translate(screenY * charWidth, 0);
710 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
712 g1.translate(-screenY * charWidth, 0);
718 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
719 // int x1, int x2, int y1, int y2, int startx, int starty,
720 private void draw(Graphics g, int startRes, int endRes, int startSeq,
721 int endSeq, int offset)
723 g.setFont(av.getFont());
724 sr.prepare(g, av.isRenderGaps());
728 // / First draw the sequences
729 // ///////////////////////////
730 for (int i = startSeq; i <= endSeq; i++)
732 nextSeq = av.getAlignment().getSequenceAt(i);
735 // occasionally, a race condition occurs such that the alignment row is
739 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
740 startRes, endRes, offset + ((i - startSeq) * charHeight));
742 if (av.isShowSequenceFeatures())
744 fr.drawSequence(g, nextSeq, startRes, endRes, offset
745 + ((i - startSeq) * charHeight), false);
748 // / Highlight search Results once all sequences have been drawn
749 // ////////////////////////////////////////////////////////
750 if (av.hasSearchResults())
752 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
754 if (visibleResults != null)
756 for (int r = 0; r < visibleResults.length; r += 2)
758 sr.drawHighlightedText(nextSeq, visibleResults[r],
759 visibleResults[r + 1], (visibleResults[r] - startRes)
761 + ((i - startSeq) * charHeight));
766 if (av.cursorMode && cursorY == i && cursorX >= startRes
767 && cursorX <= endRes)
769 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
770 offset + ((i - startSeq) * charHeight));
774 if (av.getSelectionGroup() != null
775 || av.getAlignment().getGroups().size() > 0)
777 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
782 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
783 int startSeq, int endSeq, int offset)
785 Graphics2D g = (Graphics2D) g1;
787 // ///////////////////////////////////
788 // Now outline any areas if necessary
789 // ///////////////////////////////////
790 SequenceGroup group = av.getSelectionGroup();
796 int visWidth = (endRes - startRes + 1) * charWidth;
798 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
800 group = av.getAlignment().getGroups().get(0);
810 boolean inGroup = false;
814 for (i = startSeq; i <= endSeq; i++)
816 sx = (group.getStartRes() - startRes) * charWidth;
817 sy = offset + ((i - startSeq) * charHeight);
818 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
820 if (sx + ex < 0 || sx > visWidth)
825 if ((sx <= (endRes - startRes) * charWidth)
826 && group.getSequences(null).contains(
827 av.getAlignment().getSequenceAt(i)))
830 && !group.getSequences(null).contains(
831 av.getAlignment().getSequenceAt(i + 1)))
833 bottom = sy + charHeight;
838 if (((top == -1) && (i == 0))
839 || !group.getSequences(null).contains(
840 av.getAlignment().getSequenceAt(i - 1)))
848 if (group == av.getSelectionGroup())
850 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
851 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
853 g.setColor(Color.RED);
857 g.setStroke(new BasicStroke());
858 g.setColor(group.getOutlineColour());
866 if (sx >= 0 && sx < visWidth)
868 g.drawLine(sx, oldY, sx, sy);
871 if (sx + ex < visWidth)
873 g.drawLine(sx + ex, oldY, sx + ex, sy);
882 if (sx + ex > visWidth)
887 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
889 ex = (endRes - startRes + 1) * charWidth;
894 g.drawLine(sx, top, sx + ex, top);
900 g.drawLine(sx, bottom, sx + ex, bottom);
911 sy = offset + ((i - startSeq) * charHeight);
912 if (sx >= 0 && sx < visWidth)
914 g.drawLine(sx, oldY, sx, sy);
917 if (sx + ex < visWidth)
919 g.drawLine(sx + ex, oldY, sx + ex, sy);
928 if (sx + ex > visWidth)
932 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
934 ex = (endRes - startRes + 1) * charWidth;
939 g.drawLine(sx, top, sx + ex, top);
945 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
954 g.setStroke(new BasicStroke());
956 if (groupIndex >= av.getAlignment().getGroups().size())
961 group = av.getAlignment().getGroups().get(groupIndex);
963 } while (groupIndex < av.getAlignment().getGroups().size());
975 public void highlightSearchResults(SearchResultsI results)
979 av.setSearchResults(results);
985 public void propertyChange(PropertyChangeEvent evt)
987 if (!av.getWrapAlignment())
989 if (evt.getPropertyName().equals("startres")
990 || evt.getPropertyName().equals("endres"))
992 // Make sure we're not trying to draw a panel
993 // larger than the visible window
994 ViewportRanges vpRanges = av.getRanges();
995 int scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
996 if (scrollX > vpRanges.getEndRes() - vpRanges.getStartRes())
998 scrollX = vpRanges.getEndRes() - vpRanges.getStartRes();
1000 else if (scrollX < vpRanges.getStartRes() - vpRanges.getEndRes())
1002 scrollX = vpRanges.getStartRes() - vpRanges.getEndRes();
1004 fastPaint(scrollX, 0);
1006 else if (evt.getPropertyName().equals("startseq")
1007 || evt.getPropertyName().equals("endseq"))
1009 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());