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 displaySearch = false;
\r
40 int[] searchResults = null;
\r
45 boolean fastPaint = false;
\r
47 public SeqCanvas(AlignViewport av)
\r
50 fr = new FeatureRenderer(av);
\r
51 sr = new SequenceRenderer(av);
\r
52 PaintRefresher.Register(this, av.alignment);
\r
56 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
58 int scalestartx = startx - startx % 10 + 10;
\r
60 g.setColor(Color.black);
\r
63 for (int i = scalestartx; i < endx; i += 10)
\r
65 String string = String.valueOf(i);
\r
66 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
67 ypos - av.charHeight / 2);
\r
69 g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,
\r
70 ypos + 2 - av.charHeight / 2,
\r
71 (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);
\r
76 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
78 FontMetrics fm = getFontMetrics(av.getFont());
\r
79 ypos += av.charHeight;
\r
81 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
83 SequenceI seq = av.alignment.getSequenceAt(i);
\r
86 while (index < endx)
\r
88 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
94 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
99 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))-av.charWidth/2;
\r
100 g.drawString(value + "", x,
\r
101 ypos + i * av.charHeight - av.charHeight / 5);
\r
106 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
108 ypos += av.charHeight;
\r
110 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
112 SequenceI seq = av.alignment.getSequenceAt(i);
\r
115 while (index > startx)
\r
117 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
123 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
128 g.drawString(value + "", av.charWidth/2,
\r
129 ypos + i * av.charHeight - av.charHeight / 5);
\r
135 public void fastPaint(int horizontal, int vertical)
\r
137 if (horizontal == 0 && vertical == 0 || gg == null)
\r
142 gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,
\r
143 -vertical * av.charHeight);
\r
145 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
146 transX = 0, transY = 0;
\r
147 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
149 transX = (er - sr - horizontal) * av.charWidth;
\r
150 sr = er - horizontal;
\r
152 else if (horizontal < 0)
\r
154 er = sr - horizontal;
\r
157 else if (vertical > 0) // scroll down
\r
159 ss = es - vertical;
\r
160 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
\r
166 transY = imgHeight - vertical * av.charHeight;
\r
169 else if (vertical < 0)
\r
171 es = ss - vertical;
\r
172 if (es > av.endSeq)
\r
178 gg.translate(transX, transY);
\r
180 gg.setColor(Color.white);
\r
181 gg.fillRect(0, 0, (er - sr + 1) * av.charWidth, (es - ss) * av.charHeight);
\r
182 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
183 gg.translate( -transX, -transY);
\r
191 * Definitions of startx and endx (hopefully):
\r
192 * SMJS This is what I'm working towards!
\r
193 * startx is the first residue (starting at 0) to display.
\r
194 * endx is the last residue to display (starting at 0).
\r
195 * starty is the first sequence to display (starting at 0).
\r
196 * endy is the last sequence to display (starting at 0).
\r
197 * NOTE 1: The av limits are set in setFont in this class and
\r
198 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
200 public void update(Graphics g)
\r
205 public void paint(Graphics g)
\r
209 g.drawImage(img, 0, 0, this);
\r
214 // this draws the whole of the alignment
\r
215 imgWidth = this.getSize().width;
\r
216 imgHeight = this.getSize().height;
\r
218 imgWidth -= imgWidth % av.charWidth;
\r
219 imgHeight -= imgHeight % av.charHeight;
\r
221 if (imgWidth < 1 || imgHeight < 1)
\r
226 if (img == null || imgWidth != img.getWidth(this) ||
\r
227 imgHeight != img.getHeight(this))
\r
229 img = createImage(imgWidth, imgHeight);
\r
230 gg = img.getGraphics();
\r
231 gg.setFont(av.getFont());
\r
234 gg.setColor(Color.white);
\r
235 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
237 chunkWidth = getWrappedCanvasWidth(getSize().width);
\r
238 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
240 av.setChunkHeight(chunkHeight);
\r
241 av.setChunkWidth(chunkWidth);
\r
243 if (av.getWrapAlignment())
\r
245 drawWrappedPanel(gg, getSize().width, getSize().height, av.startRes);
\r
249 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes,
\r
253 g.drawImage(img, 0, 0, this);
\r
257 int LABEL_WEST, LABEL_EAST;
\r
258 public int getWrappedCanvasWidth(int cwidth)
\r
260 FontMetrics fm = getFontMetrics(av.getFont());
\r
265 if (av.scaleRightWrapped)
\r
267 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
270 if (av.scaleLeftWrapped)
\r
272 LABEL_WEST = fm.stringWidth(getMask());
\r
275 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
280 * Generates a string of zeroes.
\r
286 for (int i = av.alignment.getWidth(); i > 0; i /= 10)
\r
293 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
296 AlignmentI al = av.getAlignment();
\r
298 FontMetrics fm = getFontMetrics(av.getFont());
\r
300 if (av.scaleRightWrapped)
\r
302 LABEL_EAST = fm.stringWidth(getMask()+"0");
\r
305 int LABEL_WEST = 0;
\r
307 if (av.scaleLeftWrapped)
\r
309 LABEL_WEST = fm.stringWidth(getMask());
\r
312 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
313 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
315 av.endRes = av.startRes + cWidth;
\r
317 int endx = startRes + cWidth - 1;
\r
318 int ypos = 2 * av.charHeight;
\r
320 while (ypos <= canvasHeight && startRes < av.alignment.getWidth())
\r
322 g.setColor(Color.black);
\r
324 if (av.scaleLeftWrapped)
\r
326 drawWestScale(g, startRes, endx, ypos);
\r
329 if (av.scaleRightWrapped)
\r
331 g.translate(canvasWidth - LABEL_EAST, 0);
\r
332 drawEastScale(g, startRes, endx, ypos);
\r
333 g.translate( - (canvasWidth - LABEL_EAST), 0);
\r
336 g.translate(LABEL_WEST, 0);
\r
337 if (av.scaleAboveWrapped)
\r
339 drawNorthScale(g, startRes, endx, ypos);
\r
342 // When printing we have an extra clipped region,
\r
343 // the Printable page which we need to account for here
\r
344 Shape clip = g.getClip();
\r
347 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
352 (int) clip.getBounds().y,
\r
353 cWidth * av.charWidth,
\r
354 (int) clip.getBounds().height
\r
358 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
360 g.translate( -LABEL_WEST, 0);
\r
363 startRes += cWidth;
\r
364 endx = startRes + cWidth - 1;
\r
366 if (endx > al.getWidth())
\r
368 endx = al.getWidth();
\r
374 synchronized public void drawPanel(Graphics g, int x1, int x2, int y1, int y2,
\r
375 int startx, int starty, int offset)
\r
378 g.setFont(av.getFont());
\r
379 sr.renderGaps(av.renderGaps);
\r
383 /// First draw the sequences
\r
384 /////////////////////////////
\r
385 for (int i = y1; i < y2; i++)
\r
387 nextSeq = av.alignment.getSequenceAt(i);
\r
389 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
390 (x1 - startx) * av.charWidth,
\r
392 (i-starty)*av.charHeight,
\r
393 av.charWidth, av.charHeight);
\r
395 if (av.showSequenceFeatures)
\r
397 fr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq), x1, x2,
\r
398 (x1 - startx) * av.charWidth,
\r
400 (i-starty)*av.charHeight,
\r
401 av.charWidth, av.charHeight);
\r
405 /////////////////////////////////////
\r
407 // Now outline any areas if necessary
\r
408 /////////////////////////////////////
\r
409 SequenceGroup group = av.getSelectionGroup();
\r
410 java.util.Vector groups = av.alignment.getGroups();
\r
412 int sx = -1, sy = -1, ex = -1;
\r
413 int groupIndex = -1;
\r
414 if (group == null && groups.size() > 0)
\r
416 group = (SequenceGroup) groups.elementAt(0);
\r
426 boolean inGroup = false;
\r
427 int top = -1, bottom = -1;
\r
428 for (i = y1; i < y2; i++)
\r
430 sx = (group.getStartRes() - startx) * av.charWidth;
\r
431 sy = offset + (i-starty)*av.charHeight;
\r
432 ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth - 1;
\r
434 if (sx < getSize().width
\r
436 && group.sequences.contains(av.alignment.getSequenceAt(i)))
\r
438 if (bottom == -1 && (i == av.alignment.getHeight() - 1 ||
\r
439 !group.sequences.contains(av.alignment.
\r
440 getSequenceAt(i + 1))))
\r
442 bottom = sy + av.charHeight;
\r
447 if (top == -1 && i == 0 ||
\r
448 !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))
\r
455 if (group == av.getSelectionGroup())
\r
457 g.setColor(new Color(255, 0, 0));
\r
461 g.setColor(group.getOutlineColour());
\r
469 g.drawLine(sx, oldY, sx, sy);
\r
470 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
474 g.drawLine(sx, top, sx + ex, top);
\r
479 g.drawLine(sx, bottom, sx + ex, bottom);
\r
493 g.drawLine(sx, top, sx + ex, top);
\r
498 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
502 sy = offset + (i-starty)*av.charHeight;
\r
503 g.drawLine(sx, oldY, sx, sy);
\r
504 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
508 if (groupIndex >= groups.size())
\r
513 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
516 while (groupIndex < groups.size());
\r
519 /// Highlight search Results once all sequences have been drawn
\r
520 //////////////////////////////////////////////////////////
\r
523 for (int r = 0; r < searchResults.length; r += 3)
\r
525 int searchSeq = searchResults[r];
\r
527 if (searchSeq >= y1 && searchSeq < y2)
\r
529 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
531 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
532 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
534 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
535 if (searchStart < x1)
\r
539 if (searchEnd > x2)
\r
544 ssr.drawHighlightedText(seq,
\r
547 (searchStart - startx) * av.charWidth,
\r
549 (searchSeq-starty)*av.charHeight,
\r
558 public void highlightSearchResults(int[] results)
\r
560 // results are in the order sequence, startRes, endRes
\r
561 if (results == null)
\r
563 displaySearch = false;
\r
567 displaySearch = true;
\r
570 searchResults = results;
\r