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
20 package jalview.appletgui;
\r
24 import jalview.analysis.*;
\r
25 import jalview.datamodel.*;
\r
27 public class SeqCanvas
\r
31 SequenceRenderer sr;
\r
39 boolean showScores = false;
\r
40 boolean displaySearch = false;
\r
41 int[] searchResults = null;
\r
46 boolean fastPaint = false;
\r
48 public SeqCanvas(AlignViewport av)
\r
51 fr = new FeatureRenderer(av);
\r
52 sr = new SequenceRenderer(av);
\r
53 PaintRefresher.Register(this, av.alignment);
\r
57 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
59 int scalestartx = startx - startx % 10 + 10;
\r
61 g.setColor(Color.black);
\r
64 for (int i = scalestartx; i < endx; i += 10)
\r
66 String string = String.valueOf(i);
\r
67 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
68 ypos - av.charHeight / 2);
\r
70 g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,
\r
71 ypos + 2 - av.charHeight / 2,
\r
72 (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);
\r
77 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
79 FontMetrics fm = getFontMetrics(av.getFont());
\r
80 ypos += av.charHeight;
\r
82 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
84 SequenceI seq = av.alignment.getSequenceAt(i);
\r
87 while (index < endx)
\r
89 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
95 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
100 int x = LABEL_WEST - fm.stringWidth(value + "");
\r
101 g.drawString(value + "", x,
\r
102 ypos + i * av.charHeight - av.charHeight / 5);
\r
107 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
109 ypos += av.charHeight;
\r
111 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
113 SequenceI seq = av.alignment.getSequenceAt(i);
\r
116 while (index > startx)
\r
118 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
124 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
129 g.drawString(value + "", 0,
\r
130 ypos + i * av.charHeight - av.charHeight / 5);
\r
136 public void fastPaint(int horizontal, int vertical)
\r
138 if (horizontal == 0 && vertical == 0 || gg == null)
\r
143 gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,
\r
144 -vertical * av.charHeight);
\r
146 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
147 transX = 0, transY = 0;
\r
148 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
150 transX = (er - sr - horizontal) * av.charWidth;
\r
151 sr = er - horizontal;
\r
153 else if (horizontal < 0)
\r
155 er = sr - horizontal;
\r
158 else if (vertical > 0) // scroll down
\r
160 ss = es - vertical;
\r
161 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
167 transY = imgHeight - vertical * av.charHeight;
\r
170 else if (vertical < 0)
\r
172 es = ss - vertical;
\r
173 if (es > av.endSeq)
\r
179 gg.translate(transX, transY);
\r
181 gg.setColor(Color.white);
\r
182 gg.fillRect(0, 0, (er - sr + 1) * av.charWidth, (es - ss) * av.charHeight);
\r
183 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
184 gg.translate( -transX, -transY);
\r
192 * Definitions of startx and endx (hopefully):
\r
193 * SMJS This is what I'm working towards!
\r
194 * startx is the first residue (starting at 0) to display.
\r
195 * endx is the last residue to display (starting at 0).
\r
196 * starty is the first sequence to display (starting at 0).
\r
197 * endy is the last sequence to display (starting at 0).
\r
198 * NOTE 1: The av limits are set in setFont in this class and
\r
199 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
201 public void update(Graphics g)
\r
206 public void paint(Graphics g)
\r
210 g.drawImage(img, 0, 0, this);
\r
215 // this draws the whole of the alignment
\r
216 imgWidth = this.getSize().width;
\r
217 imgHeight = this.getSize().height;
\r
219 imgWidth -= imgWidth % av.charWidth;
\r
220 imgHeight -= imgHeight % av.charHeight;
\r
222 if (imgWidth < 1 || imgHeight < 1)
\r
227 if (img == null || imgWidth != img.getWidth(this) ||
\r
228 imgHeight != img.getHeight(this))
\r
230 img = createImage(imgWidth, imgHeight);
\r
231 gg = img.getGraphics();
\r
232 gg.setFont(av.getFont());
\r
235 gg.setColor(Color.white);
\r
236 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
238 chunkWidth = getWrappedCanvasWidth(getSize().width);
\r
239 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
241 av.setChunkHeight(chunkHeight);
\r
242 av.setChunkWidth(chunkWidth);
\r
244 if (av.getWrapAlignment())
\r
246 drawWrappedPanel(gg, getSize().width, getSize().height, av.startRes);
\r
250 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes,
\r
254 g.drawImage(img, 0, 0, this);
\r
258 int LABEL_WEST, LABEL_EAST;
\r
259 public int getWrappedCanvasWidth(int cwidth)
\r
261 FontMetrics fm = getFontMetrics(av.getFont());
\r
266 if (av.scaleRightWrapped)
\r
268 LABEL_EAST = fm.stringWidth(av.alignment.getWidth() + "000");
\r
271 if (av.scaleLeftWrapped)
\r
273 LABEL_WEST = fm.stringWidth(av.alignment.getWidth() + "");
\r
276 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
279 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
282 AlignmentI al = av.getAlignment();
\r
284 FontMetrics fm = getFontMetrics(av.getFont());
\r
286 int LABEL_EAST = 0;
\r
287 if (av.scaleRightWrapped)
\r
289 LABEL_EAST = fm.stringWidth(al.getWidth() + "000");
\r
291 int LABEL_WEST = 0;
\r
292 if (av.scaleLeftWrapped)
\r
294 LABEL_WEST = fm.stringWidth(al.getWidth() + "0");
\r
297 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
298 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
300 av.endRes = av.startRes + cWidth;
\r
302 int endx = startRes + cWidth - 1;
\r
303 int ypos = 2 * av.charHeight;
\r
305 while (ypos <= canvasHeight && startRes < av.alignment.getWidth())
\r
307 g.setColor(Color.black);
\r
309 if (av.scaleLeftWrapped)
\r
311 drawWestScale(g, startRes, endx, ypos);
\r
314 if (av.scaleRightWrapped)
\r
316 g.translate(canvasWidth - LABEL_EAST + av.charWidth, 0);
\r
317 drawEastScale(g, startRes, endx, ypos);
\r
318 g.translate( - (canvasWidth - LABEL_EAST + av.charWidth), 0);
\r
321 g.translate(LABEL_WEST, 0);
\r
322 if (av.scaleAboveWrapped)
\r
324 drawNorthScale(g, startRes, endx, ypos);
\r
327 // When printing we have an extra clipped region,
\r
328 // the Printable page which we need to account for here
\r
329 Shape clip = g.getClip();
\r
332 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
337 (int) clip.getBounds().y,
\r
338 cWidth * av.charWidth,
\r
339 (int) clip.getBounds().height
\r
343 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
345 g.translate( -LABEL_WEST, 0);
\r
348 startRes += cWidth;
\r
349 endx = startRes + cWidth - 1;
\r
351 if (endx > al.getWidth())
\r
353 endx = al.getWidth();
\r
359 synchronized public void drawPanel(Graphics g, int x1, int x2, int y1, int y2,
\r
360 int startx, int starty, int offset)
\r
363 g.setFont(av.getFont());
\r
364 sr.renderGaps(av.renderGaps);
\r
368 /// First draw the sequences
\r
369 /////////////////////////////
\r
370 for (int i = y1; i < y2; i++)
\r
372 nextSeq = av.alignment.getSequenceAt(i);
\r
374 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
375 (x1 - startx) * av.charWidth,
\r
377 (i-starty)*av.charHeight,
\r
378 av.charWidth, av.charHeight);
\r
380 if (av.showSequenceFeatures)
\r
382 fr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
383 (x1 - startx) * av.charWidth,
\r
385 (i-starty)*av.charHeight,
\r
386 av.charWidth, av.charHeight);
\r
390 /////////////////////////////////////
\r
392 // Now outline any areas if necessary
\r
393 /////////////////////////////////////
\r
394 SequenceGroup group = av.getSelectionGroup();
\r
395 java.util.Vector groups = av.alignment.getGroups();
\r
397 int sx = -1, sy = -1, ex = -1;
\r
398 int groupIndex = -1;
\r
399 if (group == null && groups.size() > 0)
\r
401 group = (SequenceGroup) groups.elementAt(0);
\r
411 boolean inGroup = false;
\r
412 int top = -1, bottom = -1;
\r
413 for (i = y1; i < y2; i++)
\r
415 sx = (group.getStartRes() - startx) * av.charWidth;
\r
416 sy = offset + (i-starty)*av.charHeight;
\r
417 ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth - 1;
\r
419 if (sx < getSize().width
\r
421 && group.sequences.contains(av.alignment.getSequenceAt(i)))
\r
423 if (bottom == -1 && (i == av.alignment.getHeight() - 1 ||
\r
424 !group.sequences.contains(av.alignment.
\r
425 getSequenceAt(i + 1))))
\r
427 bottom = sy + av.charHeight;
\r
432 if (top == -1 && i == 0 ||
\r
433 !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))
\r
440 if (group == av.getSelectionGroup())
\r
442 g.setColor(new Color(255, 0, 0));
\r
446 g.setColor(group.getOutlineColour());
\r
454 g.drawLine(sx, oldY, sx, sy);
\r
455 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
459 g.drawLine(sx, top, sx + ex, top);
\r
464 g.drawLine(sx, bottom, sx + ex, bottom);
\r
478 g.drawLine(sx, top, sx + ex, top);
\r
483 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
487 sy = offset + (i-starty)*av.charHeight;
\r
488 g.drawLine(sx, oldY, sx, sy);
\r
489 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
493 if (groupIndex >= groups.size())
\r
498 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
501 while (groupIndex < groups.size());
\r
504 /// Highlight search Results once all sequences have been drawn
\r
505 //////////////////////////////////////////////////////////
\r
508 for (int r = 0; r < searchResults.length; r += 3)
\r
510 int searchSeq = searchResults[r];
\r
512 if (searchSeq >= y1 && searchSeq < y2)
\r
514 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
516 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
517 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
519 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
520 if (searchStart < x1)
\r
524 if (searchEnd > x2)
\r
529 ssr.drawHighlightedText(seq,
\r
532 (searchStart - startx) * av.charWidth,
\r
534 (searchSeq-starty)*av.charHeight,
\r
543 public void highlightSearchResults(int[] results)
\r
545 // results are in the order sequence, startRes, endRes
\r
546 if (results == null)
\r
548 displaySearch = false;
\r
552 displaySearch = true;
\r
555 searchResults = results;
\r