2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
21 import jalview.datamodel.*;
\r
24 import java.awt.image.*;
\r
26 import javax.swing.*;
\r
33 * @version $Revision$
\r
35 public class SeqCanvas extends JComponent
\r
38 SequenceRenderer sr;
\r
44 boolean displaySearch = false;
\r
45 int[] searchResults = null;
\r
46 boolean fastPaint = false;
\r
50 boolean isOverview = false;
\r
53 * Creates a new SeqCanvas object.
\r
55 * @param av DOCUMENT ME!
\r
57 public SeqCanvas(AlignViewport av)
\r
60 fr = new FeatureRenderer(av);
\r
61 sr = new SequenceRenderer(av);
\r
62 setLayout(new BorderLayout());
\r
63 PaintRefresher.Register(this, av.alignment);
\r
64 setBackground(Color.white);
\r
70 * @param g DOCUMENT ME!
\r
71 * @param startx DOCUMENT ME!
\r
72 * @param endx DOCUMENT ME!
\r
73 * @param ypos DOCUMENT ME!
\r
75 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
77 int scalestartx = startx - (startx % 10) + 10;
\r
79 g.setColor(Color.black);
\r
82 for (int i = scalestartx; i < endx; i += 10)
\r
84 String string = String.valueOf(i);
\r
85 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
86 ypos - (av.charHeight / 2));
\r
88 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
\r
89 (ypos + 2) - (av.charHeight / 2),
\r
90 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2), ypos -
\r
98 * @param g DOCUMENT ME!
\r
99 * @param startx DOCUMENT ME!
\r
100 * @param endx DOCUMENT ME!
\r
101 * @param ypos DOCUMENT ME!
\r
103 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
105 FontMetrics fm = getFontMetrics(av.getFont());
\r
106 ypos += av.charHeight;
\r
109 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
111 SequenceI seq = av.alignment.getSequenceAt(i);
\r
112 int index = startx;
\r
115 while (index < endx)
\r
117 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
124 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
131 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))-av.charWidth/2;
\r
132 g.drawString(value + "", x,
\r
133 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
141 * @param g DOCUMENT ME!
\r
142 * @param startx DOCUMENT ME!
\r
143 * @param endx DOCUMENT ME!
\r
144 * @param ypos DOCUMENT ME!
\r
146 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
148 ypos += av.charHeight;
\r
151 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
153 SequenceI seq = av.alignment.getSequenceAt(i);
\r
157 while (index > startx)
\r
159 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
166 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
173 g.drawString(String.valueOf(value), av.charWidth/2,
\r
174 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
182 * @param horizontal DOCUMENT ME!
\r
183 * @param vertical DOCUMENT ME!
\r
185 public void fastPaint(int horizontal, int vertical)
\r
192 gg.copyArea(horizontal * av.charWidth,
\r
193 vertical * av.charHeight,
\r
196 -horizontal * av.charWidth,
\r
197 -vertical * av.charHeight);
\r
199 int sr = av.startRes;
\r
200 int er = av.endRes;
\r
201 int ss = av.startSeq;
\r
202 int es = av.endSeq;
\r
206 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
209 transX = (er - sr - horizontal) * av.charWidth;
\r
210 sr = er - horizontal;
\r
212 else if (horizontal < 0)
\r
214 er = sr - horizontal-1;
\r
216 else if (vertical > 0) // scroll down
\r
218 ss = es - vertical;
\r
220 if (ss < av.startSeq)
\r
221 { // ie scrolling too fast, more than a page at a time
\r
226 transY = imgHeight - (vertical * av.charHeight);
\r
229 else if (vertical < 0)
\r
231 es = ss - vertical;
\r
233 if (es > av.endSeq)
\r
239 gg.translate(transX, transY);
\r
240 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
241 gg.translate(-transX, -transY);
\r
248 * Definitions of startx and endx (hopefully):
\r
249 * SMJS This is what I'm working towards!
\r
250 * startx is the first residue (starting at 0) to display.
\r
251 * endx is the last residue to display (starting at 0).
\r
252 * starty is the first sequence to display (starting at 0).
\r
253 * endy is the last sequence to display (starting at 0).
\r
254 * NOTE 1: The av limits are set in setFont in this class and
\r
255 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
258 // Set this to false to force a full panel paint
\r
259 public void paintComponent(Graphics g)
\r
261 sr.renderGaps(av.renderGaps);
\r
263 if ((img != null) &&
\r
264 (fastPaint || (getWidth() != g.getClipBounds().width) ||
\r
265 (getHeight() != g.getClipBounds().height)))
\r
267 g.drawImage(img, 0, 0, this);
\r
273 // this draws the whole of the alignment
\r
274 imgWidth = getWidth();
\r
275 imgHeight = getHeight();
\r
277 imgWidth -= (imgWidth % av.charWidth);
\r
278 imgHeight -= (imgHeight % av.charHeight);
\r
280 if ((imgWidth < 1) || (imgHeight < 1))
\r
286 img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
\r
287 gg = (Graphics2D) img.getGraphics();
\r
288 gg.setFont(av.getFont());
\r
289 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
290 RenderingHints.VALUE_ANTIALIAS_ON);
\r
292 gg.setColor(Color.white);
\r
293 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
296 if (av.getWrapAlignment())
\r
298 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
302 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq,
\r
303 av.startRes, av.startSeq, 0);
\r
306 g.drawImage(img, 0, 0, this);
\r
312 * @param cwidth DOCUMENT ME!
\r
314 * @return DOCUMENT ME!
\r
316 public int getWrappedCanvasWidth(int cwidth)
\r
318 FontMetrics fm = getFontMetrics(av.getFont());
\r
323 if (av.scaleRightWrapped)
\r
325 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
328 if (av.scaleLeftWrapped)
\r
330 LABEL_WEST = fm.stringWidth(getMask());
\r
333 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
338 * Generates a string of zeroes.
\r
344 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
354 * @param g DOCUMENT ME!
\r
355 * @param canvasWidth DOCUMENT ME!
\r
356 * @param canvasHeight DOCUMENT ME!
\r
357 * @param startRes DOCUMENT ME!
\r
359 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
363 AlignmentI al = av.getAlignment();
\r
365 FontMetrics fm = getFontMetrics(av.getFont());
\r
367 int LABEL_EAST = 0;
\r
369 if (av.scaleRightWrapped)
\r
371 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
374 int LABEL_WEST = 0;
\r
376 if (av.scaleLeftWrapped)
\r
378 LABEL_WEST = fm.stringWidth(getMask());
\r
381 int hgap = av.charHeight;
\r
382 if(av.scaleAboveWrapped)
\r
383 hgap += av.charHeight;
\r
385 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
386 int cHeight = av.getAlignment().getHeight() * av.charHeight;
\r
388 av.setWrappedWidth(cWidth);
\r
390 av.endRes = av.startRes + cWidth;
\r
393 int endx = (startRes + cWidth) - 1;
\r
397 while ((ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
399 g.setColor(Color.black);
\r
401 if (av.scaleLeftWrapped)
\r
403 drawWestScale(g, startRes, endx, ypos);
\r
406 if (av.scaleRightWrapped)
\r
408 g.translate(canvasWidth - LABEL_EAST, 0);
\r
409 drawEastScale(g, startRes, endx, ypos);
\r
410 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
413 g.translate(LABEL_WEST, 0);
\r
415 if (av.scaleAboveWrapped)
\r
417 drawNorthScale(g, startRes, endx, ypos);
\r
420 // When printing we have an extra clipped region,
\r
421 // the Printable page which we need to account for here
\r
422 Shape clip = g.getClip();
\r
426 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
430 g.setClip(0, (int) clip.getBounds().getY(),
\r
431 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
\r
434 if (av.vconsensus!=null && av.alignment.getWidth() >= av.vconsensus.size())
\r
436 endx = av.vconsensus.size() - 2;
\r
440 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
442 if(av.showAnnotation)
\r
444 g.translate(0, cHeight + ypos + 3);
\r
445 if(annotations==null)
\r
446 annotations = new AnnotationPanel(av);
\r
448 annotations.drawComponent( (Graphics2D) g, startRes, endx + 1);
\r
449 g.translate(0, -cHeight - ypos);
\r
452 g.translate(-LABEL_WEST, 0);
\r
454 ypos += cHeight+getAnnotationHeight()+hgap;
\r
455 if(av.showAnnotation)
\r
458 startRes += cWidth;
\r
459 endx = (startRes + cWidth) - 1;
\r
461 if (endx > al.getWidth())
\r
463 endx = al.getWidth();
\r
468 AnnotationPanel annotations;
\r
469 int getAnnotationHeight()
\r
471 if(!av.showAnnotation)
\r
474 if(annotations==null)
\r
475 annotations = new AnnotationPanel(av);
\r
477 return annotations.adjustPanelHeight();
\r
483 * @param g1 DOCUMENT ME!
\r
484 * @param x1 DOCUMENT ME!
\r
485 * @param x2 DOCUMENT ME!
\r
486 * @param y1 DOCUMENT ME!
\r
487 * @param y2 DOCUMENT ME!
\r
488 * @param startx DOCUMENT ME!
\r
489 * @param starty DOCUMENT ME!
\r
490 * @param offset DOCUMENT ME!
\r
492 synchronized public void drawPanel(Graphics g1, int x1, int x2, int y1,
\r
493 int y2, int startx, int starty, int offset)
\r
495 Graphics2D g = (Graphics2D) g1;
\r
496 g.setFont(av.getFont());
\r
500 /// First draw the sequences
\r
501 /////////////////////////////
\r
502 for (int i = y1; i < y2; i++)
\r
504 nextSeq = av.alignment.getSequenceAt(i);
\r
506 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq),
\r
507 x1, x2, (x1 - startx) * av.charWidth,
\r
508 offset + ((i - starty) * av.charHeight), av.charWidth,
\r
511 if (av.showSequenceFeatures)
\r
513 fr.drawSequence(g, nextSeq,
\r
514 av.alignment.findAllGroups(nextSeq), x1, x2,
\r
515 (x1 - startx) * av.charWidth,
\r
516 offset + ((i - starty) * av.charHeight), av.charWidth,
\r
522 /////////////////////////////////////
\r
523 // Now outline any areas if necessary
\r
524 /////////////////////////////////////
\r
525 SequenceGroup group = av.getSelectionGroup();
\r
526 java.util.Vector groups = av.alignment.getGroups();
\r
531 int groupIndex = -1;
\r
533 if ((group == null) && (groups.size() > 0))
\r
535 group = (SequenceGroup) groups.elementAt(0);
\r
540 if (group != null && !isOverview)
\r
546 boolean inGroup = false;
\r
550 for (i = y1; i < y2; i++)
\r
552 sx = (group.getStartRes() - startx) * av.charWidth;
\r
553 sy = offset + ((i - starty) * av.charHeight);
\r
554 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
557 if(sx+ex<0 || sx>imgWidth)
\r
562 if ( (sx <= (x2-x1)*av.charWidth) &&
\r
563 group.sequences.contains(av.alignment.getSequenceAt(
\r
566 if ((bottom == -1) &&
\r
567 !group.sequences.contains(
\r
568 av.alignment.getSequenceAt(i + 1)))
\r
570 bottom = sy + av.charHeight;
\r
575 if (((top == -1) && (i == 0)) ||
\r
576 !group.sequences.contains(
\r
577 av.alignment.getSequenceAt(i - 1)))
\r
585 if (group == av.getSelectionGroup())
\r
587 g.setStroke(new BasicStroke(1,
\r
588 BasicStroke.CAP_BUTT,
\r
589 BasicStroke.JOIN_ROUND, 3f,
\r
590 new float[] { 5f, 3f }, 0f));
\r
591 g.setColor(Color.RED);
\r
595 g.setStroke(new BasicStroke());
\r
596 g.setColor(group.getOutlineColour());
\r
604 if (sx >= 0 && sx < imgWidth)
\r
605 g.drawLine(sx, oldY, sx, sy);
\r
607 if (sx + ex < imgWidth)
\r
608 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
616 if (sx + ex > imgWidth)
\r
619 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
620 ex = (x2 - x1 + 1) * av.charWidth;
\r
624 g.drawLine(sx, top, sx + ex, top);
\r
630 g.drawLine(sx, bottom, sx + ex, bottom);
\r
641 sy = offset + ( (i - starty) * av.charHeight);
\r
642 if (sx >= 0 && sx < imgWidth)
\r
643 g.drawLine(sx, oldY, sx, sy);
\r
645 if (sx + ex < imgWidth)
\r
646 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
654 if (sx + ex > imgWidth)
\r
656 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
657 ex = (x2 - x1 + 1) * av.charWidth;
\r
661 g.drawLine(sx, top, sx + ex, top);
\r
667 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
676 if (groupIndex >= groups.size())
\r
681 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
683 while (groupIndex < groups.size());
\r
686 /// Highlight search Results once all sequences have been drawn
\r
687 //////////////////////////////////////////////////////////
\r
690 for (int r = 0; r < searchResults.length; r += 3)
\r
692 int searchSeq = searchResults[r];
\r
694 if ((searchSeq >= y1) && (searchSeq < y2))
\r
696 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
698 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
699 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
701 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
703 if (searchStart < x1)
\r
708 if (searchEnd > x2)
\r
713 ssr.drawHighlightedText(seq, searchStart, searchEnd,
\r
714 (searchStart - startx) * av.charWidth,
\r
715 offset + ((searchSeq - starty) * av.charHeight),
\r
716 av.charWidth, av.charHeight);
\r
725 * @param results DOCUMENT ME!
\r
727 public void highlightSearchResults(int[] results)
\r
729 // results are in the order sequence, startRes, endRes
\r
730 if (results == null)
\r
732 displaySearch = false;
\r
736 displaySearch = true;
\r
739 searchResults = results;
\r