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
48 boolean fastPaint = false;
\r
52 boolean isOverview = false;
\r
55 * Creates a new SeqCanvas object.
\r
57 * @param av DOCUMENT ME!
\r
59 public SeqCanvas(AlignViewport av)
\r
62 fr = new FeatureRenderer(av);
\r
63 sr = new SequenceRenderer(av);
\r
64 setLayout(new BorderLayout());
\r
65 PaintRefresher.Register(this, av.alignment);
\r
66 setBackground(Color.white);
\r
72 * @param g DOCUMENT ME!
\r
73 * @param startx DOCUMENT ME!
\r
74 * @param endx DOCUMENT ME!
\r
75 * @param ypos DOCUMENT ME!
\r
77 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
79 int scalestartx = startx - (startx % 10) + 10;
\r
81 g.setColor(Color.black);
\r
84 for (int i = scalestartx; i < endx; i += 10)
\r
86 String string = String.valueOf(i);
\r
87 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
88 ypos - (av.charHeight / 2));
\r
90 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
\r
91 (ypos + 2) - (av.charHeight / 2),
\r
92 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2), ypos -
\r
100 * @param g DOCUMENT ME!
\r
101 * @param startx DOCUMENT ME!
\r
102 * @param endx DOCUMENT ME!
\r
103 * @param ypos DOCUMENT ME!
\r
105 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
107 FontMetrics fm = getFontMetrics(av.getFont());
\r
108 ypos += av.charHeight;
\r
111 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
113 SequenceI seq = av.alignment.getSequenceAt(i);
\r
114 int index = startx;
\r
117 while (index < endx)
\r
119 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
126 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
133 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))-av.charWidth/2;
\r
134 g.drawString(value + "", x,
\r
135 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
143 * @param g DOCUMENT ME!
\r
144 * @param startx DOCUMENT ME!
\r
145 * @param endx DOCUMENT ME!
\r
146 * @param ypos DOCUMENT ME!
\r
148 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
150 ypos += av.charHeight;
\r
153 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
155 SequenceI seq = av.alignment.getSequenceAt(i);
\r
159 while (index > startx)
\r
161 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
168 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
175 g.drawString(String.valueOf(value), av.charWidth/2,
\r
176 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
184 * @param horizontal DOCUMENT ME!
\r
185 * @param vertical DOCUMENT ME!
\r
187 public void fastPaint(int horizontal, int vertical)
\r
194 gg.copyArea(horizontal * av.charWidth,
\r
195 vertical * av.charHeight,
\r
198 -horizontal * av.charWidth,
\r
199 -vertical * av.charHeight);
\r
201 int sr = av.startRes;
\r
202 int er = av.endRes;
\r
203 int ss = av.startSeq;
\r
204 int es = av.endSeq;
\r
208 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
211 transX = (er - sr - horizontal) * av.charWidth;
\r
212 sr = er - horizontal;
\r
214 else if (horizontal < 0)
\r
216 er = sr - horizontal-1;
\r
218 else if (vertical > 0) // scroll down
\r
220 ss = es - vertical;
\r
222 if (ss < av.startSeq)
\r
223 { // ie scrolling too fast, more than a page at a time
\r
228 transY = imgHeight - (vertical * av.charHeight);
\r
231 else if (vertical < 0)
\r
233 es = ss - vertical;
\r
235 if (es > av.endSeq)
\r
241 gg.translate(transX, transY);
\r
242 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
243 gg.translate(-transX, -transY);
\r
250 * Definitions of startx and endx (hopefully):
\r
251 * SMJS This is what I'm working towards!
\r
252 * startx is the first residue (starting at 0) to display.
\r
253 * endx is the last residue to display (starting at 0).
\r
254 * starty is the first sequence to display (starting at 0).
\r
255 * endy is the last sequence to display (starting at 0).
\r
256 * NOTE 1: The av limits are set in setFont in this class and
\r
257 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
260 // Set this to false to force a full panel paint
\r
261 public void paintComponent(Graphics g)
\r
263 sr.renderGaps(av.renderGaps);
\r
265 if ((img != null) &&
\r
266 (fastPaint || (getWidth() != g.getClipBounds().width) ||
\r
267 (getHeight() != g.getClipBounds().height)))
\r
269 g.drawImage(img, 0, 0, this);
\r
275 // this draws the whole of the alignment
\r
276 imgWidth = getWidth();
\r
277 imgHeight = getHeight();
\r
279 imgWidth -= (imgWidth % av.charWidth);
\r
280 imgHeight -= (imgHeight % av.charHeight);
\r
282 if ((imgWidth < 1) || (imgHeight < 1))
\r
288 img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
\r
289 gg = (Graphics2D) img.getGraphics();
\r
290 gg.setFont(av.getFont());
\r
291 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
292 RenderingHints.VALUE_ANTIALIAS_ON);
\r
294 gg.setColor(Color.white);
\r
295 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
297 chunkWidth = getWrappedCanvasWidth(getWidth());
\r
298 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
300 av.setChunkHeight(chunkHeight);
\r
301 av.setChunkWidth(chunkWidth);
\r
303 if (av.getWrapAlignment())
\r
305 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
309 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq,
\r
310 av.startRes, av.startSeq, 0);
\r
313 g.drawImage(img, 0, 0, this);
\r
319 * @param cwidth DOCUMENT ME!
\r
321 * @return DOCUMENT ME!
\r
323 public int getWrappedCanvasWidth(int cwidth)
\r
325 FontMetrics fm = getFontMetrics(av.getFont());
\r
330 if (av.scaleRightWrapped)
\r
332 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
335 if (av.scaleLeftWrapped)
\r
337 LABEL_WEST = fm.stringWidth(getMask());
\r
340 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
345 * Generates a string of zeroes.
\r
351 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
361 * @param g DOCUMENT ME!
\r
362 * @param canvasWidth DOCUMENT ME!
\r
363 * @param canvasHeight DOCUMENT ME!
\r
364 * @param startRes DOCUMENT ME!
\r
366 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
370 AlignmentI al = av.getAlignment();
\r
372 FontMetrics fm = getFontMetrics(av.getFont());
\r
374 int LABEL_EAST = 0;
\r
376 if (av.scaleRightWrapped)
\r
378 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
381 int LABEL_WEST = 0;
\r
383 if (av.scaleLeftWrapped)
\r
385 LABEL_WEST = fm.stringWidth(getMask());
\r
388 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
389 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
391 av.endRes = av.startRes + cWidth;
\r
393 int endx = (startRes + cWidth) - 1;
\r
394 int ypos = 2 * av.charHeight;
\r
396 while ((ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
398 g.setColor(Color.black);
\r
400 if (av.scaleLeftWrapped)
\r
402 drawWestScale(g, startRes, endx, ypos);
\r
405 if (av.scaleRightWrapped)
\r
407 g.translate(canvasWidth - LABEL_EAST, 0);
\r
408 drawEastScale(g, startRes, endx, ypos);
\r
409 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
412 g.translate(LABEL_WEST, 0);
\r
414 if (av.scaleAboveWrapped)
\r
416 drawNorthScale(g, startRes, endx, ypos);
\r
419 // When printing we have an extra clipped region,
\r
420 // the Printable page which we need to account for here
\r
421 Shape clip = g.getClip();
\r
425 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
429 g.setClip(0, (int) clip.getBounds().getY(),
\r
430 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
\r
433 if (av.vconsensus!=null && av.alignment.getWidth() >= av.vconsensus.size())
\r
435 endx = av.vconsensus.size() - 2;
\r
439 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
441 g.translate(-LABEL_WEST, 0);
\r
444 startRes += cWidth;
\r
445 endx = (startRes + cWidth) - 1;
\r
447 if (endx > al.getWidth())
\r
449 endx = al.getWidth();
\r
457 * @param g1 DOCUMENT ME!
\r
458 * @param x1 DOCUMENT ME!
\r
459 * @param x2 DOCUMENT ME!
\r
460 * @param y1 DOCUMENT ME!
\r
461 * @param y2 DOCUMENT ME!
\r
462 * @param startx DOCUMENT ME!
\r
463 * @param starty DOCUMENT ME!
\r
464 * @param offset DOCUMENT ME!
\r
466 synchronized public void drawPanel(Graphics g1, int x1, int x2, int y1,
\r
467 int y2, int startx, int starty, int offset)
\r
469 Graphics2D g = (Graphics2D) g1;
\r
470 g.setFont(av.getFont());
\r
474 /// First draw the sequences
\r
475 /////////////////////////////
\r
476 for (int i = y1; i < y2; i++)
\r
478 nextSeq = av.alignment.getSequenceAt(i);
\r
480 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq),
\r
481 x1, x2, (x1 - startx) * av.charWidth,
\r
482 offset + ((i - starty) * av.charHeight), av.charWidth,
\r
485 if (av.showSequenceFeatures)
\r
487 fr.drawSequence(g, nextSeq,
\r
488 av.alignment.findAllGroups(nextSeq), x1, x2,
\r
489 (x1 - startx) * av.charWidth,
\r
490 offset + ((i - starty) * av.charHeight), av.charWidth,
\r
496 /////////////////////////////////////
\r
497 // Now outline any areas if necessary
\r
498 /////////////////////////////////////
\r
499 SequenceGroup group = av.getSelectionGroup();
\r
500 java.util.Vector groups = av.alignment.getGroups();
\r
505 int groupIndex = -1;
\r
507 if ((group == null) && (groups.size() > 0))
\r
509 group = (SequenceGroup) groups.elementAt(0);
\r
514 if (group != null && !isOverview)
\r
520 boolean inGroup = false;
\r
524 for (i = y1; i < y2; i++)
\r
526 sx = (group.getStartRes() - startx) * av.charWidth;
\r
527 sy = offset + ((i - starty) * av.charHeight);
\r
528 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
531 if(sx+ex<0 || sx>imgWidth)
\r
536 if ( (sx <= (x2-x1)*av.charWidth) &&
\r
537 group.sequences.contains(av.alignment.getSequenceAt(
\r
540 if ((bottom == -1) &&
\r
541 !group.sequences.contains(
\r
542 av.alignment.getSequenceAt(i + 1)))
\r
544 bottom = sy + av.charHeight;
\r
549 if (((top == -1) && (i == 0)) ||
\r
550 !group.sequences.contains(
\r
551 av.alignment.getSequenceAt(i - 1)))
\r
559 if (group == av.getSelectionGroup())
\r
561 g.setStroke(new BasicStroke(1,
\r
562 BasicStroke.CAP_BUTT,
\r
563 BasicStroke.JOIN_ROUND, 3f,
\r
564 new float[] { 5f, 3f }, 0f));
\r
565 g.setColor(Color.RED);
\r
569 g.setStroke(new BasicStroke());
\r
570 g.setColor(group.getOutlineColour());
\r
578 if (sx >= 0 && sx < imgWidth)
\r
579 g.drawLine(sx, oldY, sx, sy);
\r
581 if (sx + ex < imgWidth)
\r
582 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
590 if (sx + ex > imgWidth)
\r
593 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
594 ex = (x2 - x1 + 1) * av.charWidth;
\r
598 g.drawLine(sx, top, sx + ex, top);
\r
604 g.drawLine(sx, bottom, sx + ex, bottom);
\r
615 sy = offset + ( (i - starty) * av.charHeight);
\r
616 if (sx >= 0 && sx < imgWidth)
\r
617 g.drawLine(sx, oldY, sx, sy);
\r
619 if (sx + ex < imgWidth)
\r
620 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
628 if (sx + ex > imgWidth)
\r
630 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
631 ex = (x2 - x1 + 1) * av.charWidth;
\r
635 g.drawLine(sx, top, sx + ex, top);
\r
641 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
650 if (groupIndex >= groups.size())
\r
655 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
657 while (groupIndex < groups.size());
\r
660 /// Highlight search Results once all sequences have been drawn
\r
661 //////////////////////////////////////////////////////////
\r
664 for (int r = 0; r < searchResults.length; r += 3)
\r
666 int searchSeq = searchResults[r];
\r
668 if ((searchSeq >= y1) && (searchSeq < y2))
\r
670 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
672 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
673 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
675 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
677 if (searchStart < x1)
\r
682 if (searchEnd > x2)
\r
687 ssr.drawHighlightedText(seq, searchStart, searchEnd,
\r
688 (searchStart - startx) * av.charWidth,
\r
689 offset + ((searchSeq - starty) * av.charHeight),
\r
690 av.charWidth, av.charHeight);
\r
699 * @param results DOCUMENT ME!
\r
701 public void highlightSearchResults(int[] results)
\r
703 // results are in the order sequence, startRes, endRes
\r
704 if (results == null)
\r
706 displaySearch = false;
\r
710 displaySearch = true;
\r
713 searchResults = results;
\r