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 showScores = false;
\r
45 boolean displaySearch = false;
\r
46 int[] searchResults = null;
\r
49 boolean fastPaint = false;
\r
54 * Creates a new SeqCanvas object.
\r
56 * @param av DOCUMENT ME!
\r
58 public SeqCanvas(AlignViewport av)
\r
61 fr = new FeatureRenderer(av);
\r
62 sr = new SequenceRenderer(av);
\r
63 setLayout(new BorderLayout());
\r
64 PaintRefresher.Register(this, av.alignment);
\r
65 setBackground(Color.white);
\r
71 * @param g DOCUMENT ME!
\r
72 * @param startx DOCUMENT ME!
\r
73 * @param endx DOCUMENT ME!
\r
74 * @param ypos DOCUMENT ME!
\r
76 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
78 int scalestartx = startx - (startx % 10) + 10;
\r
80 g.setColor(Color.black);
\r
83 for (int i = scalestartx; i < endx; i += 10)
\r
85 String string = String.valueOf(i);
\r
86 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
87 ypos - (av.charHeight / 2));
\r
89 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
\r
90 (ypos + 2) - (av.charHeight / 2),
\r
91 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2), ypos -
\r
99 * @param g DOCUMENT ME!
\r
100 * @param startx DOCUMENT ME!
\r
101 * @param endx DOCUMENT ME!
\r
102 * @param ypos DOCUMENT ME!
\r
104 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
106 FontMetrics fm = getFontMetrics(av.getFont());
\r
107 ypos += av.charHeight;
\r
110 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
112 SequenceI seq = av.alignment.getSequenceAt(i);
\r
113 int index = startx;
\r
116 while (index < endx)
\r
118 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
125 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
132 int x = LABEL_WEST - fm.stringWidth(value + "");
\r
133 g.drawString(value + "", x,
\r
134 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
142 * @param g DOCUMENT ME!
\r
143 * @param startx DOCUMENT ME!
\r
144 * @param endx DOCUMENT ME!
\r
145 * @param ypos DOCUMENT ME!
\r
147 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
149 ypos += av.charHeight;
\r
152 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
154 SequenceI seq = av.alignment.getSequenceAt(i);
\r
158 while (index > startx)
\r
160 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
167 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
174 g.drawString(value + "", 0,
\r
175 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
183 * @param horizontal DOCUMENT ME!
\r
184 * @param vertical DOCUMENT ME!
\r
186 public void fastPaint(int horizontal, int vertical)
\r
188 if (((horizontal == 0) && (vertical == 0)) || (gg == null))
\r
193 gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,
\r
194 -vertical * av.charHeight);
\r
196 int sr = av.startRes;
\r
197 int er = av.endRes;
\r
198 int ss = av.startSeq;
\r
199 int es = av.endSeq;
\r
203 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
205 transX = (er - sr - horizontal) * av.charWidth;
\r
206 sr = er - horizontal;
\r
208 else if (horizontal < 0)
\r
210 er = sr - horizontal;
\r
212 else if (vertical > 0) // scroll down
\r
214 ss = es - vertical;
\r
216 if (ss < av.startSeq)
\r
217 { // ie scrolling too fast, more than a page at a time
\r
222 transY = imgHeight - (vertical * av.charHeight);
\r
225 else if (vertical < 0)
\r
227 es = ss - vertical;
\r
229 if (es > av.endSeq)
\r
235 gg.translate(transX, transY);
\r
237 gg.setColor(Color.white);
\r
238 gg.fillRect(0, 0, (er - sr + 1) * av.charWidth,
\r
239 (es - ss) * av.charHeight);
\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 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
283 img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
\r
284 gg = (Graphics2D) img.getGraphics();
\r
285 gg.setFont(av.getFont());
\r
286 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
287 RenderingHints.VALUE_ANTIALIAS_ON);
\r
289 gg.setColor(Color.white);
\r
290 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
292 chunkWidth = getWrappedCanvasWidth(getWidth());
\r
293 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
295 av.setChunkHeight(chunkHeight);
\r
296 av.setChunkWidth(chunkWidth);
\r
298 if (av.getWrapAlignment())
\r
300 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
304 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq,
\r
305 av.startRes, av.startSeq, 0);
\r
308 g.drawImage(img, 0, 0, this);
\r
314 * @param cwidth DOCUMENT ME!
\r
316 * @return DOCUMENT ME!
\r
318 public int getWrappedCanvasWidth(int cwidth)
\r
320 FontMetrics fm = getFontMetrics(av.getFont());
\r
325 if (av.scaleRightWrapped)
\r
327 LABEL_EAST = fm.stringWidth(av.alignment.getWidth() + "000");
\r
330 if (av.scaleLeftWrapped)
\r
332 LABEL_WEST = fm.stringWidth(av.alignment.getWidth() + "");
\r
335 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
341 * @param g DOCUMENT ME!
\r
342 * @param canvasWidth DOCUMENT ME!
\r
343 * @param canvasHeight DOCUMENT ME!
\r
344 * @param startRes DOCUMENT ME!
\r
346 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
349 AlignmentI al = av.getAlignment();
\r
351 FontMetrics fm = getFontMetrics(av.getFont());
\r
353 int LABEL_EAST = 0;
\r
355 if (av.scaleRightWrapped)
\r
357 LABEL_EAST = fm.stringWidth(al.getWidth() + "000");
\r
360 int LABEL_WEST = 0;
\r
362 if (av.scaleLeftWrapped)
\r
364 LABEL_WEST = fm.stringWidth(al.getWidth() + "0");
\r
367 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
368 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
370 av.endRes = av.startRes + cWidth;
\r
372 int endx = (startRes + cWidth) - 1;
\r
373 int ypos = 2 * av.charHeight;
\r
375 while ((ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
377 g.setColor(Color.black);
\r
379 if (av.scaleLeftWrapped)
\r
381 drawWestScale(g, startRes, endx, ypos);
\r
384 if (av.scaleRightWrapped)
\r
386 g.translate(canvasWidth - LABEL_EAST + av.charWidth, 0);
\r
387 drawEastScale(g, startRes, endx, ypos);
\r
388 g.translate(-(canvasWidth - LABEL_EAST + av.charWidth), 0);
\r
391 g.translate(LABEL_WEST, 0);
\r
393 if (av.scaleAboveWrapped)
\r
395 drawNorthScale(g, startRes, endx, ypos);
\r
398 // When printing we have an extra clipped region,
\r
399 // the Printable page which we need to account for here
\r
400 Shape clip = g.getClip();
\r
404 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
408 g.setClip(0, (int) clip.getBounds().getY(),
\r
409 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
\r
412 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
414 g.translate(-LABEL_WEST, 0);
\r
417 startRes += cWidth;
\r
418 endx = (startRes + cWidth) - 1;
\r
420 if (endx > al.getWidth())
\r
422 endx = al.getWidth();
\r
430 * @param g1 DOCUMENT ME!
\r
431 * @param x1 DOCUMENT ME!
\r
432 * @param x2 DOCUMENT ME!
\r
433 * @param y1 DOCUMENT ME!
\r
434 * @param y2 DOCUMENT ME!
\r
435 * @param startx DOCUMENT ME!
\r
436 * @param starty DOCUMENT ME!
\r
437 * @param offset DOCUMENT ME!
\r
439 synchronized public void drawPanel(Graphics g1, int x1, int x2, int y1,
\r
440 int y2, int startx, int starty, int offset)
\r
442 Graphics2D g = (Graphics2D) g1;
\r
443 g.setFont(av.getFont());
\r
444 sr.renderGaps(av.renderGaps);
\r
448 /// First draw the sequences
\r
449 /////////////////////////////
\r
450 for (int i = y1; i < y2; i++)
\r
452 nextSeq = av.alignment.getSequenceAt(i);
\r
454 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq),
\r
455 x1, x2, (x1 - startx) * av.charWidth,
\r
456 offset + ((i - starty) * av.charHeight), av.charWidth,
\r
459 if (av.showSequenceFeatures)
\r
461 fr.drawSequence(g, nextSeq,
\r
462 av.alignment.findAllGroups(nextSeq), x1, x2,
\r
463 (x1 - startx) * av.charWidth,
\r
464 offset + ((i - starty) * av.charHeight), av.charWidth,
\r
470 /////////////////////////////////////
\r
471 // Now outline any areas if necessary
\r
472 /////////////////////////////////////
\r
473 SequenceGroup group = av.getSelectionGroup();
\r
474 java.util.Vector groups = av.alignment.getGroups();
\r
479 int groupIndex = -1;
\r
481 if ((group == null) && (groups.size() > 0))
\r
483 group = (SequenceGroup) groups.elementAt(0);
\r
494 boolean inGroup = false;
\r
498 for (i = y1; i < y2; i++)
\r
500 sx = (group.getStartRes() - startx) * av.charWidth;
\r
501 sy = offset + ((i - starty) * av.charHeight);
\r
502 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
506 if ((sx < (x2-x1)*av.charWidth) &&
\r
507 group.sequences.contains(av.alignment.getSequenceAt(
\r
510 if ((bottom == -1) &&
\r
511 !group.sequences.contains(
\r
512 av.alignment.getSequenceAt(i + 1)))
\r
514 bottom = sy + av.charHeight;
\r
519 if (((top == -1) && (i == 0)) ||
\r
520 !group.sequences.contains(
\r
521 av.alignment.getSequenceAt(i - 1)))
\r
529 if (group == av.getSelectionGroup())
\r
531 g.setStroke(new BasicStroke(1,
\r
532 BasicStroke.CAP_BUTT,
\r
533 BasicStroke.JOIN_ROUND, 3f,
\r
534 new float[] { 5f, 3f }, 0f));
\r
535 g.setColor(Color.RED);
\r
539 g.setStroke(new BasicStroke());
\r
540 g.setColor(group.getOutlineColour());
\r
548 g.drawLine(sx, oldY, sx, sy);
\r
549 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
553 g.drawLine(sx, top, sx + ex, top);
\r
559 g.drawLine(sx, bottom, sx + ex, bottom);
\r
573 g.drawLine(sx, top, sx + ex, top);
\r
579 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
583 sy = offset + ((i - starty) * av.charHeight);
\r
584 g.drawLine(sx, oldY, sx, sy);
\r
585 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
591 if (groupIndex >= groups.size())
\r
596 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
598 while (groupIndex < groups.size());
\r
601 /// Highlight search Results once all sequences have been drawn
\r
602 //////////////////////////////////////////////////////////
\r
605 for (int r = 0; r < searchResults.length; r += 3)
\r
607 int searchSeq = searchResults[r];
\r
609 if ((searchSeq >= y1) && (searchSeq < y2))
\r
611 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
613 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
614 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
616 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
618 if (searchStart < x1)
\r
623 if (searchEnd > x2)
\r
628 ssr.drawHighlightedText(seq, searchStart, searchEnd,
\r
629 (searchStart - startx) * av.charWidth,
\r
630 offset + ((searchSeq - starty) * av.charHeight),
\r
631 av.charWidth, av.charHeight);
\r
640 * @param results DOCUMENT ME!
\r
642 public void highlightSearchResults(int[] results)
\r
644 // results are in the order sequence, startRes, endRes
\r
645 if (results == null)
\r
647 displaySearch = false;
\r
651 displaySearch = true;
\r
654 searchResults = results;
\r