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(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,
\r
195 -vertical * av.charHeight);
\r
197 int sr = av.startRes;
\r
198 int er = av.endRes;
\r
199 int ss = av.startSeq;
\r
200 int es = av.endSeq;
\r
204 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
207 transX = (er - sr - horizontal) * av.charWidth;
\r
208 sr = er - horizontal;
\r
210 else if (horizontal < 0)
\r
212 er = sr - horizontal-1;
\r
214 else if (vertical > 0) // scroll down
\r
216 ss = es - vertical;
\r
218 if (ss < av.startSeq)
\r
219 { // ie scrolling too fast, more than a page at a time
\r
224 transY = imgHeight - (vertical * av.charHeight);
\r
227 else if (vertical < 0)
\r
229 es = ss - vertical;
\r
231 if (es > av.endSeq)
\r
237 gg.translate(transX, transY);
\r
238 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
239 gg.translate(-transX, -transY);
\r
246 * Definitions of startx and endx (hopefully):
\r
247 * SMJS This is what I'm working towards!
\r
248 * startx is the first residue (starting at 0) to display.
\r
249 * endx is the last residue to display (starting at 0).
\r
250 * starty is the first sequence to display (starting at 0).
\r
251 * endy is the last sequence to display (starting at 0).
\r
252 * NOTE 1: The av limits are set in setFont in this class and
\r
253 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
256 // Set this to false to force a full panel paint
\r
257 public void paintComponent(Graphics g)
\r
259 sr.renderGaps(av.renderGaps);
\r
261 if ((img != null) &&
\r
262 (fastPaint || (getWidth() != g.getClipBounds().width) ||
\r
263 (getHeight() != g.getClipBounds().height)))
\r
265 g.drawImage(img, 0, 0, this);
\r
271 // this draws the whole of the alignment
\r
272 imgWidth = getWidth();
\r
273 imgHeight = getHeight();
\r
275 imgWidth -= (imgWidth % av.charWidth);
\r
276 imgHeight -= (imgHeight % av.charHeight);
\r
278 if ((imgWidth < 1) || (imgHeight < 1))
\r
284 img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
\r
285 gg = (Graphics2D) img.getGraphics();
\r
286 gg.setFont(av.getFont());
\r
287 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
288 RenderingHints.VALUE_ANTIALIAS_ON);
\r
290 gg.setColor(Color.white);
\r
291 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
293 chunkWidth = getWrappedCanvasWidth(getWidth());
\r
294 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
296 av.setChunkHeight(chunkHeight);
\r
297 av.setChunkWidth(chunkWidth);
\r
299 if (av.getWrapAlignment())
\r
301 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
305 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq,
\r
306 av.startRes, av.startSeq, 0);
\r
309 g.drawImage(img, 0, 0, this);
\r
315 * @param cwidth DOCUMENT ME!
\r
317 * @return DOCUMENT ME!
\r
319 public int getWrappedCanvasWidth(int cwidth)
\r
321 FontMetrics fm = getFontMetrics(av.getFont());
\r
326 if (av.scaleRightWrapped)
\r
328 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
331 if (av.scaleLeftWrapped)
\r
333 LABEL_WEST = fm.stringWidth(getMask());
\r
336 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
341 * Generates a string of zeroes.
\r
347 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
357 * @param g DOCUMENT ME!
\r
358 * @param canvasWidth DOCUMENT ME!
\r
359 * @param canvasHeight DOCUMENT ME!
\r
360 * @param startRes DOCUMENT ME!
\r
362 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
366 AlignmentI al = av.getAlignment();
\r
368 FontMetrics fm = getFontMetrics(av.getFont());
\r
370 int LABEL_EAST = 0;
\r
372 if (av.scaleRightWrapped)
\r
374 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
377 int LABEL_WEST = 0;
\r
379 if (av.scaleLeftWrapped)
\r
381 LABEL_WEST = fm.stringWidth(getMask());
\r
384 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
385 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
387 av.endRes = av.startRes + cWidth;
\r
389 int endx = (startRes + cWidth) - 1;
\r
390 int ypos = 2 * av.charHeight;
\r
392 while ((ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
394 g.setColor(Color.black);
\r
396 if (av.scaleLeftWrapped)
\r
398 drawWestScale(g, startRes, endx, ypos);
\r
401 if (av.scaleRightWrapped)
\r
403 g.translate(canvasWidth - LABEL_EAST, 0);
\r
404 drawEastScale(g, startRes, endx, ypos);
\r
405 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
408 g.translate(LABEL_WEST, 0);
\r
410 if (av.scaleAboveWrapped)
\r
412 drawNorthScale(g, startRes, endx, ypos);
\r
415 // When printing we have an extra clipped region,
\r
416 // the Printable page which we need to account for here
\r
417 Shape clip = g.getClip();
\r
421 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
425 g.setClip(0, (int) clip.getBounds().getY(),
\r
426 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
\r
429 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
431 g.translate(-LABEL_WEST, 0);
\r
434 startRes += cWidth;
\r
435 endx = (startRes + cWidth) - 1;
\r
437 if (endx > al.getWidth())
\r
439 endx = al.getWidth();
\r
447 * @param g1 DOCUMENT ME!
\r
448 * @param x1 DOCUMENT ME!
\r
449 * @param x2 DOCUMENT ME!
\r
450 * @param y1 DOCUMENT ME!
\r
451 * @param y2 DOCUMENT ME!
\r
452 * @param startx DOCUMENT ME!
\r
453 * @param starty DOCUMENT ME!
\r
454 * @param offset DOCUMENT ME!
\r
456 synchronized public void drawPanel(Graphics g1, int x1, int x2, int y1,
\r
457 int y2, int startx, int starty, int offset)
\r
459 Graphics2D g = (Graphics2D) g1;
\r
460 g.setFont(av.getFont());
\r
464 /// First draw the sequences
\r
465 /////////////////////////////
\r
466 for (int i = y1; i < y2; i++)
\r
468 nextSeq = av.alignment.getSequenceAt(i);
\r
470 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq),
\r
471 x1, x2, (x1 - startx) * av.charWidth,
\r
472 offset + ((i - starty) * av.charHeight), av.charWidth,
\r
475 if (av.showSequenceFeatures)
\r
477 fr.drawSequence(g, nextSeq,
\r
478 av.alignment.findAllGroups(nextSeq), x1, x2,
\r
479 (x1 - startx) * av.charWidth,
\r
480 offset + ((i - starty) * av.charHeight), av.charWidth,
\r
486 /////////////////////////////////////
\r
487 // Now outline any areas if necessary
\r
488 /////////////////////////////////////
\r
489 SequenceGroup group = av.getSelectionGroup();
\r
490 java.util.Vector groups = av.alignment.getGroups();
\r
495 int groupIndex = -1;
\r
497 if ((group == null) && (groups.size() > 0))
\r
499 group = (SequenceGroup) groups.elementAt(0);
\r
504 if (group != null && !isOverview)
\r
510 boolean inGroup = false;
\r
514 for (i = y1; i < y2; i++)
\r
516 sx = (group.getStartRes() - startx) * av.charWidth;
\r
517 sy = offset + ((i - starty) * av.charHeight);
\r
518 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
521 if(sx+ex<0 || sx>imgWidth)
\r
526 if ( (sx <= (x2-x1)*av.charWidth) &&
\r
527 group.sequences.contains(av.alignment.getSequenceAt(
\r
530 if ((bottom == -1) &&
\r
531 !group.sequences.contains(
\r
532 av.alignment.getSequenceAt(i + 1)))
\r
534 bottom = sy + av.charHeight;
\r
539 if (((top == -1) && (i == 0)) ||
\r
540 !group.sequences.contains(
\r
541 av.alignment.getSequenceAt(i - 1)))
\r
549 if (group == av.getSelectionGroup())
\r
551 g.setStroke(new BasicStroke(1,
\r
552 BasicStroke.CAP_BUTT,
\r
553 BasicStroke.JOIN_ROUND, 3f,
\r
554 new float[] { 5f, 3f }, 0f));
\r
555 g.setColor(Color.RED);
\r
559 g.setStroke(new BasicStroke());
\r
560 g.setColor(group.getOutlineColour());
\r
568 if (sx >= 0 && sx < imgWidth)
\r
569 g.drawLine(sx, oldY, sx, sy);
\r
571 if (sx + ex < imgWidth)
\r
572 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
580 if (sx + ex > imgWidth)
\r
583 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
584 ex = (x2 - x1 + 1) * av.charWidth;
\r
588 g.drawLine(sx, top, sx + ex, top);
\r
594 g.drawLine(sx, bottom, sx + ex, bottom);
\r
605 sy = offset + ( (i - starty) * av.charHeight);
\r
606 if (sx >= 0 && sx < imgWidth)
\r
607 g.drawLine(sx, oldY, sx, sy);
\r
609 if (sx + ex < imgWidth)
\r
610 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
618 if (sx + ex > imgWidth)
\r
620 else if (sx + ex >= (x2 - x1 + 1) * av.charWidth)
\r
621 ex = (x2 - x1 + 1) * av.charWidth;
\r
625 g.drawLine(sx, top, sx + ex, top);
\r
631 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
640 if (groupIndex >= groups.size())
\r
645 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
647 while (groupIndex < groups.size());
\r
650 /// Highlight search Results once all sequences have been drawn
\r
651 //////////////////////////////////////////////////////////
\r
654 for (int r = 0; r < searchResults.length; r += 3)
\r
656 int searchSeq = searchResults[r];
\r
658 if ((searchSeq >= y1) && (searchSeq < y2))
\r
660 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
662 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
663 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
665 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
667 if (searchStart < x1)
\r
672 if (searchEnd > x2)
\r
677 ssr.drawHighlightedText(seq, searchStart, searchEnd,
\r
678 (searchStart - startx) * av.charWidth,
\r
679 offset + ((searchSeq - starty) * av.charHeight),
\r
680 av.charWidth, av.charHeight);
\r
689 * @param results DOCUMENT ME!
\r
691 public void highlightSearchResults(int[] results)
\r
693 // results are in the order sequence, startRes, endRes
\r
694 if (results == null)
\r
696 displaySearch = false;
\r
700 displaySearch = true;
\r
703 searchResults = results;
\r