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.SearchResults;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
28 import java.awt.BasicStroke;
29 import java.awt.BorderLayout;
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.RenderingHints;
35 import java.awt.Shape;
36 import java.awt.image.BufferedImage;
37 import java.util.List;
39 import javax.swing.JComponent;
47 public class SeqCanvas extends JComponent
49 final FeatureRenderer fr;
51 final SequenceRenderer sr;
63 SearchResults searchResults = null;
65 boolean fastPaint = false;
76 * Creates a new SeqCanvas object.
81 public SeqCanvas(AlignmentPanel ap)
85 fr = new FeatureRenderer(ap);
86 sr = new SequenceRenderer(av);
87 setLayout(new BorderLayout());
88 PaintRefresher.Register(this, av.getSequenceSetId());
89 setBackground(Color.white);
92 public SequenceRenderer getSequenceRenderer()
97 public FeatureRenderer getFeatureRenderer()
102 int charHeight = 0, charWidth = 0;
104 private void updateViewport()
106 charHeight = av.getCharHeight();
107 charWidth = av.getCharWidth();
122 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
125 int scalestartx = startx - (startx % 10) + 10;
127 g.setColor(Color.black);
129 for (int i = scalestartx; i < endx; i += 10)
132 if (av.hasHiddenColumns())
134 value = av.getColumnSelection().adjustForHiddenColumns(value);
137 g.drawString(String.valueOf(value), (i - startx - 1) * charWidth,
138 ypos - (charHeight / 2));
140 g.drawLine(((i - startx - 1) * charWidth) + (charWidth / 2),
141 (ypos + 2) - (charHeight / 2), ((i - startx - 1) * charWidth)
142 + (charWidth / 2), ypos - 2);
158 void drawWestScale(Graphics g, int startx, int endx, int ypos)
160 FontMetrics fm = getFontMetrics(av.getFont());
163 if (av.hasHiddenColumns())
165 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
166 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
169 int maxwidth = av.getAlignment().getWidth();
170 if (av.hasHiddenColumns())
172 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
176 for (int i = 0; i < av.getAlignment().getHeight(); i++)
178 SequenceI seq = av.getAlignment().getSequenceAt(i);
184 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
191 value = av.getAlignment().getSequenceAt(i).findPosition(index);
198 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
200 g.drawString(value + "", x, (ypos + (i * charHeight))
218 void drawEastScale(Graphics g, int startx, int endx, int ypos)
222 if (av.hasHiddenColumns())
224 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
229 for (int i = 0; i < av.getAlignment().getHeight(); i++)
231 seq = av.getAlignment().getSequenceAt(i);
235 while (index > startx)
237 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
244 value = seq.findPosition(index);
251 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
257 boolean fastpainting = false;
260 * need to make this thread safe move alignment rendering in response to
266 * shift up or down in repaint
268 public void fastPaint(int horizontal, int vertical)
270 if (fastpainting || gg == null)
277 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
278 imgHeight, -horizontal * charWidth, -vertical * charHeight);
280 int sr = av.startRes;
282 int ss = av.startSeq;
287 if (horizontal > 0) // scrollbar pulled right, image to the left
290 transX = (er - sr - horizontal) * charWidth;
291 sr = er - horizontal;
293 else if (horizontal < 0)
295 er = sr - horizontal - 1;
297 else if (vertical > 0) // scroll down
301 if (ss < av.startSeq)
302 { // ie scrolling too fast, more than a page at a time
307 transY = imgHeight - (vertical * charHeight);
310 else if (vertical < 0)
320 gg.translate(transX, transY);
321 drawPanel(gg, sr, er, ss, es, 0);
322 gg.translate(-transX, -transY);
325 fastpainting = false;
329 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
330 * towards! startx is the first residue (starting at 0) to display. endx is
331 * the last residue to display (starting at 0). starty is the first sequence
332 * to display (starting at 0). endy is the last sequence to display (starting
333 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
334 * adjustment listener in SeqPanel when the scrollbars move.
337 // Set this to false to force a full panel paint
339 public void paintComponent(Graphics g)
342 BufferedImage lcimg = img; // take reference since other threads may null
343 // img and call later.
344 super.paintComponent(g);
348 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
349 .getClipBounds().height)))
351 g.drawImage(lcimg, 0, 0, this);
356 // this draws the whole of the alignment
357 imgWidth = getWidth();
358 imgHeight = getHeight();
360 imgWidth -= (imgWidth % charWidth);
361 imgHeight -= (imgHeight % charHeight);
363 if ((imgWidth < 1) || (imgHeight < 1))
368 if (lcimg == null || imgWidth != lcimg.getWidth()
369 || imgHeight != lcimg.getHeight())
373 lcimg = img = new BufferedImage(imgWidth, imgHeight,
374 BufferedImage.TYPE_INT_RGB);
375 gg = (Graphics2D) img.getGraphics();
376 gg.setFont(av.getFont());
377 } catch (OutOfMemoryError er)
380 jalview.bin.Console.errPrintln("SeqCanvas OutOfMemory Redraw Error.\n" + er);
381 new OOMWarning("Creating alignment image for display", er);
389 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
390 RenderingHints.VALUE_ANTIALIAS_ON);
393 gg.setColor(Color.white);
394 gg.fillRect(0, 0, imgWidth, imgHeight);
396 if (av.getWrapAlignment())
398 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
402 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
405 g.drawImage(lcimg, 0, 0, this);
415 * @return DOCUMENT ME!
417 public int getWrappedCanvasWidth(int cwidth)
419 FontMetrics fm = getFontMetrics(av.getFont());
424 if (av.getScaleRightWrapped())
426 LABEL_EAST = fm.stringWidth(getMask());
429 if (av.getScaleLeftWrapped())
431 LABEL_WEST = fm.stringWidth(getMask());
434 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
438 * Generates a string of zeroes.
447 for (int i = 0; i < av.getAlignment().getHeight(); i++)
449 tmp = av.getAlignment().getSequenceAt(i).getEnd();
456 for (int i = maxWidth; i > 0; i /= 10)
470 * @param canvasHeight
475 public void drawWrappedPanel(Graphics g, int canvasWidth,
476 int canvasHeight, int startRes)
479 AlignmentI al = av.getAlignment();
481 FontMetrics fm = getFontMetrics(av.getFont());
483 if (av.getScaleRightWrapped())
485 LABEL_EAST = fm.stringWidth(getMask());
488 if (av.getScaleLeftWrapped())
490 LABEL_WEST = fm.stringWidth(getMask());
493 int hgap = charHeight;
494 if (av.getScaleAboveWrapped())
499 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
500 int cHeight = av.getAlignment().getHeight() * charHeight;
502 av.setWrappedWidth(cWidth);
504 av.endRes = av.startRes + cWidth;
508 int maxwidth = av.getAlignment().getWidth() - 1;
510 if (av.hasHiddenColumns())
512 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
515 while ((ypos <= canvasHeight) && (startRes < maxwidth))
517 endx = startRes + cWidth - 1;
524 g.setFont(av.getFont());
525 g.setColor(Color.black);
527 if (av.getScaleLeftWrapped())
529 drawWestScale(g, startRes, endx, ypos);
532 if (av.getScaleRightWrapped())
534 g.translate(canvasWidth - LABEL_EAST, 0);
535 drawEastScale(g, startRes, endx, ypos);
536 g.translate(-(canvasWidth - LABEL_EAST), 0);
539 g.translate(LABEL_WEST, 0);
541 if (av.getScaleAboveWrapped())
543 drawNorthScale(g, startRes, endx, ypos);
546 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
548 g.setColor(Color.blue);
550 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
553 res = av.getColumnSelection().findHiddenRegionPosition(i)
556 if (res < 0 || res > endx - startRes)
562 new int[] { res * charWidth - charHeight / 4,
563 res * charWidth + charHeight / 4, res * charWidth },
564 new int[] { ypos - (charHeight / 2),
565 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
571 // When printing we have an extra clipped region,
572 // the Printable page which we need to account for here
573 Shape clip = g.getClip();
577 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
581 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
582 (int) clip.getBounds().getHeight());
585 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
587 if (av.isShowAnnotation())
589 g.translate(0, cHeight + ypos + 3);
590 if (annotations == null)
592 annotations = new AnnotationPanel(av);
595 annotations.renderer.drawComponent(annotations, av, g, -1,
597 g.translate(0, -cHeight - ypos - 3);
600 g.translate(-LABEL_WEST, 0);
602 ypos += cHeight + getAnnotationHeight() + hgap;
608 AnnotationPanel annotations;
610 int getAnnotationHeight()
612 if (!av.isShowAnnotation())
617 if (annotations == null)
619 annotations = new AnnotationPanel(av);
622 return annotations.adjustPanelHeight();
641 public void drawPanel(Graphics g1, int startRes, int endRes,
642 int startSeq, int endSeq, int offset)
645 if (!av.hasHiddenColumns())
647 draw(g1, startRes, endRes, startSeq, endSeq, offset);
651 List<int[]> regions = av.getColumnSelection().getHiddenColumns();
654 int blockStart = startRes;
655 int blockEnd = endRes;
657 for (int[] region : regions)
659 int hideStart = region[0];
660 int hideEnd = region[1];
662 if (hideStart < blockStart)
664 blockStart += (hideEnd - hideStart) + 1;
667 blockEnd = hideStart - 1;
669 g1.translate(screenY * charWidth, 0);
671 // find end of this visible block
672 newY += blockEnd - blockStart + 1;
674 clip = newY - (endRes - startRes);
677 blockEnd = blockStart + (endRes - startRes) - screenY;
679 // TODO: JAL-1722 - does this block need clipping ?
680 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
681 // TODO: JAL-1722 - is this hidden marker visible ?
682 if (clip < -1 && av.getShowHiddenMarkers())
684 g1.setColor(Color.blue);
686 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
687 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
688 (endSeq - startSeq) * charHeight + offset);
691 g1.translate(-screenY * charWidth, 0);
694 blockStart = hideEnd + 1;
698 // already rendered last block
703 if (screenY <= (endRes - startRes))
705 // remaining visible region to render
706 blockEnd = blockStart + (endRes - startRes) - screenY;
707 g1.translate(screenY * charWidth, 0);
708 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
710 g1.translate(-screenY * charWidth, 0);
715 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
716 // int x1, int x2, int y1, int y2, int startx, int starty,
717 private void draw(Graphics g, int startRes, int endRes, int startSeq,
718 int endSeq, int offset)
720 g.setFont(av.getFont());
721 sr.prepare(g, av.isRenderGaps());
725 // / First draw the sequences
726 // ///////////////////////////
727 for (int i = startSeq; i < endSeq; i++)
729 nextSeq = av.getAlignment().getSequenceAt(i);
732 // occasionally, a race condition occurs such that the alignment row is
736 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
737 startRes, endRes, offset + ((i - startSeq) * charHeight));
739 if (av.isShowSequenceFeatures())
741 fr.drawSequence(g, nextSeq, startRes, endRes, offset
742 + ((i - startSeq) * charHeight));
745 // / Highlight search Results once all sequences have been drawn
746 // ////////////////////////////////////////////////////////
747 if (searchResults != null)
749 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
751 if (visibleResults != null)
753 for (int r = 0; r < visibleResults.length; r += 2)
755 sr.drawHighlightedText(nextSeq, visibleResults[r],
756 visibleResults[r + 1], (visibleResults[r] - startRes)
758 + ((i - startSeq) * charHeight));
763 if (av.cursorMode && cursorY == i && cursorX >= startRes
764 && cursorX <= endRes)
766 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
767 offset + ((i - startSeq) * charHeight));
771 if (av.getSelectionGroup() != null
772 || av.getAlignment().getGroups().size() > 0)
774 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
779 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
780 int startSeq, int endSeq, int offset)
782 Graphics2D g = (Graphics2D) g1;
784 // ///////////////////////////////////
785 // Now outline any areas if necessary
786 // ///////////////////////////////////
787 SequenceGroup group = av.getSelectionGroup();
793 int visWidth = (endRes - startRes + 1) * charWidth;
795 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
797 group = av.getAlignment().getGroups().get(0);
807 boolean inGroup = false;
811 for (i = startSeq; i < endSeq; i++)
813 sx = (group.getStartRes() - startRes) * charWidth;
814 sy = offset + ((i - startSeq) * charHeight);
815 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
817 if (sx + ex < 0 || sx > visWidth)
822 if ((sx <= (endRes - startRes) * charWidth)
823 && group.getSequences(null).contains(
824 av.getAlignment().getSequenceAt(i)))
827 && !group.getSequences(null).contains(
828 av.getAlignment().getSequenceAt(i + 1)))
830 bottom = sy + charHeight;
835 if (((top == -1) && (i == 0))
836 || !group.getSequences(null).contains(
837 av.getAlignment().getSequenceAt(i - 1)))
845 if (group == av.getSelectionGroup())
847 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
848 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
850 g.setColor(Color.RED);
854 g.setStroke(new BasicStroke());
855 g.setColor(group.getOutlineColour());
863 if (sx >= 0 && sx < visWidth)
865 g.drawLine(sx, oldY, sx, sy);
868 if (sx + ex < visWidth)
870 g.drawLine(sx + ex, oldY, sx + ex, sy);
879 if (sx + ex > visWidth)
884 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
886 ex = (endRes - startRes + 1) * charWidth;
891 g.drawLine(sx, top, sx + ex, top);
897 g.drawLine(sx, bottom, sx + ex, bottom);
908 sy = offset + ((i - startSeq) * charHeight);
909 if (sx >= 0 && sx < visWidth)
911 g.drawLine(sx, oldY, sx, sy);
914 if (sx + ex < visWidth)
916 g.drawLine(sx + ex, oldY, sx + ex, sy);
925 if (sx + ex > visWidth)
929 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
931 ex = (endRes - startRes + 1) * charWidth;
936 g.drawLine(sx, top, sx + ex, top);
942 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
951 g.setStroke(new BasicStroke());
953 if (groupIndex >= av.getAlignment().getGroups().size())
958 group = av.getAlignment().getGroups().get(groupIndex);
960 } while (groupIndex < av.getAlignment().getGroups().size());
972 public void highlightSearchResults(SearchResults results)
976 searchResults = results;