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)
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);
361 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
362 .getClipBounds().height)))
364 g.drawImage(lcimg, 0, 0, this);
369 // this draws the whole of the alignment
370 imgWidth = getWidth();
371 imgHeight = getHeight();
373 imgWidth -= (imgWidth % charWidth);
374 imgHeight -= (imgHeight % charHeight);
376 if ((imgWidth < 1) || (imgHeight < 1))
381 if (lcimg == null || imgWidth != lcimg.getWidth()
382 || imgHeight != lcimg.getHeight())
386 lcimg = img = new BufferedImage(imgWidth, imgHeight,
387 BufferedImage.TYPE_INT_RGB);
388 gg = (Graphics2D) img.getGraphics();
389 gg.setFont(av.getFont());
390 } catch (OutOfMemoryError er)
393 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
394 new OOMWarning("Creating alignment image for display", er);
402 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
403 RenderingHints.VALUE_ANTIALIAS_ON);
406 gg.setColor(Color.white);
407 gg.fillRect(0, 0, imgWidth, imgHeight);
409 ViewportRanges ranges = av.getRanges();
410 if (av.getWrapAlignment())
412 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
416 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
417 ranges.getStartSeq(), ranges.getEndSeq(), 0);
420 g.drawImage(lcimg, 0, 0, this);
430 * @return DOCUMENT ME!
432 public int getWrappedCanvasWidth(int cwidth)
434 FontMetrics fm = getFontMetrics(av.getFont());
439 if (av.getScaleRightWrapped())
441 LABEL_EAST = fm.stringWidth(getMask());
444 if (av.getScaleLeftWrapped())
446 LABEL_WEST = fm.stringWidth(getMask());
449 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
453 * Generates a string of zeroes.
462 for (int i = 0; i < av.getAlignment().getHeight(); i++)
464 tmp = av.getAlignment().getSequenceAt(i).getEnd();
471 for (int i = maxWidth; i > 0; i /= 10)
485 * @param canvasHeight
490 public void drawWrappedPanel(Graphics g, int canvasWidth,
491 int canvasHeight, int startRes)
494 AlignmentI al = av.getAlignment();
496 FontMetrics fm = getFontMetrics(av.getFont());
501 if (av.getScaleRightWrapped())
503 LABEL_EAST = fm.stringWidth(getMask());
506 if (av.getScaleLeftWrapped())
508 LABEL_WEST = fm.stringWidth(getMask());
511 int hgap = charHeight;
512 if (av.getScaleAboveWrapped())
517 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
518 int cHeight = av.getAlignment().getHeight() * charHeight;
520 av.setWrappedWidth(cWidth);
522 av.getRanges().setViewportStartAndWidth(startRes, cWidth);
526 int maxwidth = av.getAlignment().getWidth();
528 if (av.hasHiddenColumns())
530 maxwidth = av.getAlignment().getHiddenColumns()
531 .findColumnPosition(maxwidth);
534 while ((ypos <= canvasHeight) && (startRes < maxwidth))
536 endx = startRes + cWidth - 1;
543 g.setFont(av.getFont());
544 g.setColor(Color.black);
546 if (av.getScaleLeftWrapped())
548 drawWestScale(g, startRes, endx, ypos);
551 if (av.getScaleRightWrapped())
553 g.translate(canvasWidth - LABEL_EAST, 0);
554 drawEastScale(g, startRes, endx, ypos);
555 g.translate(-(canvasWidth - LABEL_EAST), 0);
558 g.translate(LABEL_WEST, 0);
560 if (av.getScaleAboveWrapped())
562 drawNorthScale(g, startRes, endx, ypos);
565 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
567 g.setColor(Color.blue);
569 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
570 List<Integer> positions = hidden.findHiddenRegionPositions();
571 for (int pos : positions)
573 res = pos - startRes;
575 if (res < 0 || res > endx - startRes)
581 new int[] { res * charWidth - charHeight / 4,
582 res * charWidth + charHeight / 4, res * charWidth },
583 new int[] { ypos - (charHeight / 2),
584 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
590 // When printing we have an extra clipped region,
591 // the Printable page which we need to account for here
592 Shape clip = g.getClip();
596 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
600 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
601 (int) clip.getBounds().getHeight());
604 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
606 if (av.isShowAnnotation())
608 g.translate(0, cHeight + ypos + 3);
609 if (annotations == null)
611 annotations = new AnnotationPanel(av);
614 annotations.renderer.drawComponent(annotations, av, g, -1,
616 g.translate(0, -cHeight - ypos - 3);
619 g.translate(-LABEL_WEST, 0);
621 ypos += cHeight + getAnnotationHeight() + hgap;
627 AnnotationPanel annotations;
629 int getAnnotationHeight()
631 if (!av.isShowAnnotation())
636 if (annotations == null)
638 annotations = new AnnotationPanel(av);
641 return annotations.adjustPanelHeight();
660 public void drawPanel(Graphics g1, int startRes, int endRes,
661 int startSeq, int endSeq, int offset)
664 if (!av.hasHiddenColumns())
666 draw(g1, startRes, endRes, startSeq, endSeq, offset);
671 int blockStart = startRes;
672 int blockEnd = endRes;
674 for (int[] region : av.getAlignment().getHiddenColumns()
675 .getHiddenColumnsCopy())
677 int hideStart = region[0];
678 int hideEnd = region[1];
680 if (hideStart <= blockStart)
682 blockStart += (hideEnd - hideStart) + 1;
686 blockEnd = hideStart - 1;
688 g1.translate(screenY * charWidth, 0);
690 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
692 if (av.getShowHiddenMarkers())
694 g1.setColor(Color.blue);
696 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
697 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
698 (endSeq - startSeq + 1) * charHeight + offset);
701 g1.translate(-screenY * charWidth, 0);
702 screenY += blockEnd - blockStart + 1;
703 blockStart = hideEnd + 1;
705 if (screenY > (endRes - startRes))
707 // already rendered last block
712 if (screenY <= (endRes - startRes))
714 // remaining visible region to render
715 blockEnd = blockStart + (endRes - startRes) - screenY;
716 g1.translate(screenY * charWidth, 0);
717 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
719 g1.translate(-screenY * charWidth, 0);
725 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
726 // int x1, int x2, int y1, int y2, int startx, int starty,
727 private void draw(Graphics g, int startRes, int endRes, int startSeq,
728 int endSeq, int offset)
730 g.setFont(av.getFont());
731 sr.prepare(g, av.isRenderGaps());
735 // / First draw the sequences
736 // ///////////////////////////
737 for (int i = startSeq; i <= endSeq; i++)
739 nextSeq = av.getAlignment().getSequenceAt(i);
742 // occasionally, a race condition occurs such that the alignment row is
746 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
747 startRes, endRes, offset + ((i - startSeq) * charHeight));
749 if (av.isShowSequenceFeatures())
751 fr.drawSequence(g, nextSeq, startRes, endRes, offset
752 + ((i - startSeq) * charHeight), false);
755 // / Highlight search Results once all sequences have been drawn
756 // ////////////////////////////////////////////////////////
757 if (av.hasSearchResults())
759 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
761 if (visibleResults != null)
763 for (int r = 0; r < visibleResults.length; r += 2)
765 sr.drawHighlightedText(nextSeq, visibleResults[r],
766 visibleResults[r + 1], (visibleResults[r] - startRes)
768 + ((i - startSeq) * charHeight));
773 if (av.cursorMode && cursorY == i && cursorX >= startRes
774 && cursorX <= endRes)
776 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
777 offset + ((i - startSeq) * charHeight));
781 if (av.getSelectionGroup() != null
782 || av.getAlignment().getGroups().size() > 0)
784 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
789 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
790 int startSeq, int endSeq, int offset)
792 Graphics2D g = (Graphics2D) g1;
794 // ///////////////////////////////////
795 // Now outline any areas if necessary
796 // ///////////////////////////////////
797 SequenceGroup group = av.getSelectionGroup();
803 int visWidth = (endRes - startRes + 1) * charWidth;
805 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
807 group = av.getAlignment().getGroups().get(0);
817 boolean inGroup = false;
821 for (i = startSeq; i <= endSeq; i++)
823 sx = (group.getStartRes() - startRes) * charWidth;
824 sy = offset + ((i - startSeq) * charHeight);
825 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
827 if (sx + ex < 0 || sx > visWidth)
832 if ((sx <= (endRes - startRes) * charWidth)
833 && group.getSequences(null).contains(
834 av.getAlignment().getSequenceAt(i)))
837 && !group.getSequences(null).contains(
838 av.getAlignment().getSequenceAt(i + 1)))
840 bottom = sy + charHeight;
845 if (((top == -1) && (i == 0))
846 || !group.getSequences(null).contains(
847 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[] { 5f, 3f },
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());