2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
\r
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3
\r
10 * of the License, or (at your option) any later version.
\r
12 * Jalview is distributed in the hope that it will be useful, but
\r
13 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
15 * PURPOSE. See the GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
\r
21 package jalview.appletgui;
\r
23 import jalview.datamodel.AlignmentI;
\r
24 import jalview.datamodel.SearchResults;
\r
25 import jalview.datamodel.SequenceGroup;
\r
26 import jalview.datamodel.SequenceI;
\r
27 import jalview.util.Comparison;
\r
28 import jalview.viewmodel.AlignmentViewport;
\r
30 import java.awt.Color;
\r
31 import java.awt.FontMetrics;
\r
32 import java.awt.Graphics;
\r
33 import java.awt.Image;
\r
35 import javax.swing.JPanel;
\r
37 public class SeqCanvas extends JPanel
\r
41 SequenceRenderer sr;
\r
53 SearchResults searchResults = null;
\r
55 boolean fastPaint = false;
\r
61 public SeqCanvas(AlignViewport av)
\r
64 fr = new FeatureRenderer(av);
\r
65 sr = new SequenceRenderer(av);
\r
66 PaintRefresher.Register(this, av.getSequenceSetId());
\r
70 int avcharHeight = 0, avcharWidth = 0;
\r
72 private void updateViewport()
\r
74 avcharHeight = av.getCharHeight();
\r
75 avcharWidth = av.getCharWidth();
\r
78 public AlignmentViewport getViewport()
\r
83 public FeatureRenderer getFeatureRenderer()
\r
88 public SequenceRenderer getSequenceRenderer()
\r
93 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
95 int scalestartx = startx - startx % 10 + 10;
\r
97 g.setColor(Color.black);
\r
100 for (int i = scalestartx; i < endx; i += 10)
\r
103 if (av.hasHiddenColumns())
\r
105 value = av.getColumnSelection().adjustForHiddenColumns(value);
\r
108 g.drawString(String.valueOf(value), (i - startx - 1) * avcharWidth,
\r
109 ypos - (avcharHeight / 2));
\r
111 g.drawLine(((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
\r
112 (ypos + 2) - (avcharHeight / 2),
\r
113 ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
\r
118 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
120 FontMetrics fm = getFontMetrics(av.getFont());
\r
121 ypos += avcharHeight;
\r
122 if (av.hasHiddenColumns())
\r
124 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
\r
125 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
\r
128 int maxwidth = av.getAlignment().getWidth();
\r
129 if (av.hasHiddenColumns())
\r
131 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
\r
135 for (int i = 0; i < av.getAlignment().getHeight(); i++)
\r
137 SequenceI seq = av.getAlignment().getSequenceAt(i);
\r
138 int index = startx;
\r
141 while (index < endx)
\r
143 if (Comparison.isGap(seq.getCharAt(index)))
\r
150 value = av.getAlignment().getSequenceAt(i).findPosition(index);
\r
157 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
\r
159 g.drawString(value + "", x, (ypos + (i * avcharHeight))
\r
160 - (avcharHeight / 5));
\r
165 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
167 ypos += avcharHeight;
\r
169 if (av.hasHiddenColumns())
\r
171 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
\r
176 for (int i = 0; i < av.getAlignment().getHeight(); i++)
\r
178 seq = av.getAlignment().getSequenceAt(i);
\r
182 while (index > startx)
\r
184 if (Comparison.isGap(seq.getCharAt(index)))
\r
191 value = seq.findPosition(index);
\r
198 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
\r
199 - (avcharHeight / 5));
\r
206 void fastPaint(int horizontal, int vertical)
\r
208 if (fastPaint || gg == null)
\r
215 // Its possible on certain browsers that the call to fastpaint
\r
216 // is faster than it can paint, so this check here catches
\r
217 // this possibility
\r
218 if (lastsr + horizontal != av.startRes)
\r
220 horizontal = av.startRes - lastsr;
\r
223 lastsr = av.startRes;
\r
226 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
\r
227 - horizontal * avcharWidth,
\r
228 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
\r
229 -vertical * avcharHeight);
\r
231 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
\r
233 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
235 transX = (er - sr - horizontal) * avcharWidth;
\r
236 sr = er - horizontal;
\r
238 else if (horizontal < 0)
\r
240 er = sr - horizontal;
\r
243 else if (vertical > 0) // scroll down
\r
245 ss = es - vertical;
\r
246 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
252 transY = imgHeight - vertical * avcharHeight;
\r
255 else if (vertical < 0)
\r
257 es = ss - vertical;
\r
258 if (es > av.endSeq)
\r
264 gg.translate(transX, transY);
\r
266 drawPanel(gg, sr, er, ss, es, 0);
\r
267 gg.translate(-transX, -transY);
\r
274 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
\r
275 * towards! startx is the first residue (starting at 0) to display. endx is
\r
276 * the last residue to display (starting at 0). starty is the first sequence
\r
277 * to display (starting at 0). endy is the last sequence to display (starting
\r
278 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
\r
279 * adjustment listener in SeqPanel when the scrollbars move.
\r
281 public void update(Graphics g)
\r
287 public void paint(Graphics g)
\r
291 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
\r
292 .getClipBounds().height)))
\r
294 g.drawImage(img, 0, 0, this);
\r
301 g.drawImage(img, 0, 0, this);
\r
307 // this draws the whole of the alignment
\r
308 imgWidth = this.getSize().width;
\r
309 imgHeight = this.getSize().height;
\r
311 imgWidth -= imgWidth % avcharWidth;
\r
312 imgHeight -= imgHeight % avcharHeight;
\r
314 if (imgWidth < 1 || imgHeight < 1)
\r
319 if (img == null || imgWidth != img.getWidth(this)
\r
320 || imgHeight != img.getHeight(this))
\r
322 img = createImage(imgWidth, imgHeight);
\r
323 gg = img.getGraphics();
\r
324 gg.setFont(av.getFont());
\r
327 gg.setColor(Color.white);
\r
328 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
330 if (av.getWrapAlignment())
\r
332 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
\r
336 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
\r
339 g.drawImage(img, 0, 0, this);
\r
343 int LABEL_WEST, LABEL_EAST;
\r
345 public int getWrappedCanvasWidth(int cwidth)
\r
347 cwidth -= cwidth % av.getCharWidth();
\r
349 FontMetrics fm = getFontMetrics(av.getFont());
\r
354 if (av.getScaleRightWrapped())
\r
356 LABEL_EAST = fm.stringWidth(getMask());
\r
359 if (av.getScaleLeftWrapped())
\r
361 LABEL_WEST = fm.stringWidth(getMask());
\r
364 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
\r
368 * Generates a string of zeroes.
\r
377 AlignmentI alignment = av.getAlignment();
\r
378 for (int i = 0; i < alignment.getHeight(); i++)
\r
380 tmp = alignment.getSequenceAt(i).getEnd();
\r
381 if (tmp > maxWidth)
\r
387 for (int i = maxWidth; i > 0; i /= 10)
\r
394 private void drawWrappedPanel(Graphics g, int canvasWidth,
\r
395 int canvasHeight, int startRes)
\r
397 AlignmentI al = av.getAlignment();
\r
399 FontMetrics fm = getFontMetrics(av.getFont());
\r
401 if (av.getScaleRightWrapped())
\r
403 LABEL_EAST = fm.stringWidth(getMask());
\r
406 if (av.getScaleLeftWrapped())
\r
408 LABEL_WEST = fm.stringWidth(getMask());
\r
411 int hgap = avcharHeight;
\r
412 if (av.getScaleAboveWrapped())
\r
414 hgap += avcharHeight;
\r
417 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
\r
418 int cHeight = av.getAlignment().getHeight() * avcharHeight;
\r
420 av.setWrappedWidth(cWidth);
\r
422 av.endRes = av.startRes + cWidth;
\r
427 int maxwidth = av.getAlignment().getWidth() - 1;
\r
429 if (av.hasHiddenColumns())
\r
431 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
\r
434 while ((ypos <= canvasHeight) && (startRes < maxwidth))
\r
436 endx = startRes + cWidth - 1;
\r
438 if (endx > maxwidth)
\r
443 g.setColor(Color.black);
\r
445 if (av.getScaleLeftWrapped())
\r
447 drawWestScale(g, startRes, endx, ypos);
\r
450 if (av.getScaleRightWrapped())
\r
452 g.translate(canvasWidth - LABEL_EAST, 0);
\r
453 drawEastScale(g, startRes, endx, ypos);
\r
454 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
457 g.translate(LABEL_WEST, 0);
\r
459 if (av.getScaleAboveWrapped())
\r
461 drawNorthScale(g, startRes, endx, ypos);
\r
463 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
\r
465 g.setColor(Color.blue);
\r
467 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
\r
470 res = av.getColumnSelection().findHiddenRegionPosition(i)
\r
473 if (res < 0 || res > endx - startRes)
\r
478 gg.fillPolygon(new int[]
\r
479 { res * avcharWidth - avcharHeight / 4,
\r
480 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
\r
482 { ypos - (avcharHeight / 2), ypos - (avcharHeight / 2),
\r
483 ypos - (avcharHeight / 2) + 8 }, 3);
\r
488 if (g.getClip() == null)
\r
490 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
\r
493 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
\r
496 if (av.isShowAnnotation())
\r
498 g.translate(0, cHeight + ypos + 4);
\r
499 if (annotations == null)
\r
501 annotations = new AnnotationPanel(av);
\r
504 annotations.drawComponent(g, startRes, endx + 1);
\r
505 g.translate(0, -cHeight - ypos - 4);
\r
507 g.translate(-LABEL_WEST, 0);
\r
509 ypos += cHeight + getAnnotationHeight() + hgap;
\r
511 startRes += cWidth;
\r
516 AnnotationPanel annotations;
\r
518 int getAnnotationHeight()
\r
520 if (!av.isShowAnnotation())
\r
525 if (annotations == null)
\r
527 annotations = new AnnotationPanel(av);
\r
530 return annotations.adjustPanelHeight();
\r
533 private void drawPanel(Graphics g1, int startRes, int endRes,
\r
535 int endSeq, int offset)
\r
539 if (!av.hasHiddenColumns())
\r
541 draw(g1, startRes, endRes, startSeq, endSeq, offset);
\r
547 int blockStart = startRes;
\r
548 int blockEnd = endRes;
\r
550 if (av.hasHiddenColumns())
\r
552 for (int[] region : av.getColumnSelection().getHiddenColumns())
\r
554 int hideStart = region[0];
\r
555 int hideEnd = region[1];
\r
557 if (hideStart <= blockStart)
\r
559 blockStart += (hideEnd - hideStart) + 1;
\r
563 blockEnd = hideStart - 1;
\r
565 g1.translate(screenY * avcharWidth, 0);
\r
567 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
\r
569 if (av.getShowHiddenMarkers())
\r
571 g1.setColor(Color.blue);
\r
572 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
\r
573 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
\r
574 - 1, (endSeq - startSeq) * avcharHeight
\r
578 g1.translate(-screenY * avcharWidth, 0);
\r
579 screenY += blockEnd - blockStart + 1;
\r
580 blockStart = hideEnd + 1;
\r
583 if (screenY <= (endRes - startRes))
\r
585 blockEnd = blockStart + (endRes - startRes) - screenY;
\r
586 g1.translate(screenY * avcharWidth, 0);
\r
587 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
\r
589 g1.translate(-screenY * avcharWidth, 0);
\r
595 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
\r
596 // int x1, int x2, int y1, int y2, int startx, int starty,
\r
597 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
\r
600 g.setFont(av.getFont());
\r
601 sr.prepare(g, av.isRenderGaps());
\r
605 // / First draw the sequences
\r
606 // ///////////////////////////
\r
607 for (int i = startSeq; i < endSeq; i++)
\r
609 nextSeq = av.getAlignment().getSequenceAt(i);
\r
611 if (nextSeq == null)
\r
616 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
\r
617 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
\r
619 if (av.isShowSequenceFeatures())
\r
621 fr.drawSequence(g, nextSeq, startRes, endRes, offset
\r
622 + ((i - startSeq) * avcharHeight));
\r
625 // / Highlight search Results once all sequences have been drawn
\r
626 // ////////////////////////////////////////////////////////
\r
627 if (searchResults != null)
\r
629 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
\r
631 if (visibleResults != null)
\r
633 for (int r = 0; r < visibleResults.length; r += 2)
\r
635 sr.drawHighlightedText(nextSeq, visibleResults[r],
\r
636 visibleResults[r + 1], (visibleResults[r] - startRes)
\r
637 * avcharWidth, offset
\r
638 + ((i - startSeq) * avcharHeight));
\r
643 if (av.cursorMode && cursorY == i && cursorX >= startRes
\r
644 && cursorX <= endRes)
\r
646 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
\r
647 offset + ((i - startSeq) * avcharHeight));
\r
651 if (av.getSelectionGroup() != null
\r
652 || av.getAlignment().getGroups().size() > 0)
\r
654 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
\r
659 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
\r
660 int startSeq, int endSeq, int offset)
\r
663 // ///////////////////////////////////
\r
664 // Now outline any areas if necessary
\r
665 // ///////////////////////////////////
\r
666 SequenceGroup group = av.getSelectionGroup();
\r
671 int groupIndex = -1;
\r
673 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
\r
675 group = av.getAlignment().getGroups().get(0);
\r
685 boolean inGroup = false;
\r
688 int alHeight = av.getAlignment().getHeight() - 1;
\r
690 for (i = startSeq; i < endSeq; i++)
\r
692 sx = (group.getStartRes() - startRes) * avcharWidth;
\r
693 sy = offset + ((i - startSeq) * avcharHeight);
\r
694 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
\r
696 if (sx + ex < 0 || sx > imgWidth)
\r
701 if ((sx <= (endRes - startRes) * avcharWidth)
\r
702 && group.getSequences(null).contains(
\r
703 av.getAlignment().getSequenceAt(i)))
\r
706 && (i >= alHeight || !group.getSequences(null)
\r
708 av.getAlignment().getSequenceAt(i + 1))))
\r
710 bottom = sy + avcharHeight;
\r
715 if (((top == -1) && (i == 0))
\r
716 || !group.getSequences(null).contains(
\r
717 av.getAlignment().getSequenceAt(i - 1)))
\r
725 if (group == av.getSelectionGroup())
\r
727 g.setColor(Color.red);
\r
731 g.setColor(group.getOutlineColour());
\r
739 if (sx >= 0 && sx < imgWidth)
\r
741 g.drawLine(sx, oldY, sx, sy);
\r
744 if (sx + ex < imgWidth)
\r
746 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
755 if (sx + ex > imgWidth)
\r
760 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
\r
762 ex = (endRes - startRes + 1) * avcharWidth;
\r
767 g.drawLine(sx, top, sx + ex, top);
\r
773 g.drawLine(sx, bottom, sx + ex, bottom);
\r
784 sy = offset + ((i - startSeq) * avcharHeight);
\r
785 if (sx >= 0 && sx < imgWidth)
\r
787 g.drawLine(sx, oldY, sx, sy);
\r
790 if (sx + ex < imgWidth)
\r
792 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
801 if (sx + ex > imgWidth)
\r
805 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
\r
807 ex = (endRes - startRes + 1) * avcharWidth;
\r
812 g.drawLine(sx, top, sx + ex, top);
\r
818 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
827 if (groupIndex >= av.getAlignment().getGroups().size())
\r
832 group = av.getAlignment().getGroups()
\r
834 } while (groupIndex < av.getAlignment().getGroups().size());
\r
839 public void highlightSearchResults(SearchResults results)
\r
841 searchResults = results;
\r