63c603388eb32a149d476d4d48dfcd215349e6b0
[jalview.git] / src / jalview / gui / SeqCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import javax.swing.*;
23
24 import jalview.datamodel.*;
25
26 /**
27  * DOCUMENT ME!
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class SeqCanvas extends JComponent
33 {
34   final FeatureRenderer fr;
35
36   final SequenceRenderer sr;
37
38   BufferedImage img;
39
40   Graphics2D gg;
41
42   int imgWidth;
43
44   int imgHeight;
45
46   AlignViewport av;
47
48   SearchResults searchResults = null;
49
50   boolean fastPaint = false;
51
52   int LABEL_WEST;
53
54   int LABEL_EAST;
55
56   int cursorX = 0;
57
58   int cursorY = 0;
59
60   /**
61    * Creates a new SeqCanvas object.
62    * 
63    * @param av
64    *          DOCUMENT ME!
65    */
66   public SeqCanvas(AlignmentPanel ap)
67   {
68     this.av = ap.av;
69     fr = new FeatureRenderer(ap);
70     sr = new SequenceRenderer(av);
71     setLayout(new BorderLayout());
72     PaintRefresher.Register(this, av.getSequenceSetId());
73     setBackground(Color.white);
74   }
75
76   public SequenceRenderer getSequenceRenderer()
77   {
78     return sr;
79   }
80
81   public FeatureRenderer getFeatureRenderer()
82   {
83     return fr;
84   }
85
86   /**
87    * DOCUMENT ME!
88    * 
89    * @param g
90    *          DOCUMENT ME!
91    * @param startx
92    *          DOCUMENT ME!
93    * @param endx
94    *          DOCUMENT ME!
95    * @param ypos
96    *          DOCUMENT ME!
97    */
98   void drawNorthScale(Graphics g, int startx, int endx, int ypos)
99   {
100     int scalestartx = startx - (startx % 10) + 10;
101
102     g.setColor(Color.black);
103
104     // NORTH SCALE
105     for (int i = scalestartx; i < endx; i += 10)
106     {
107       int value = i;
108       if (av.hasHiddenColumns())
109       {
110         value = av.getColumnSelection().adjustForHiddenColumns(value);
111       }
112
113       g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
114               ypos - (av.charHeight / 2));
115
116       g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
117               (ypos + 2) - (av.charHeight / 2),
118               ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
119               ypos - 2);
120     }
121   }
122
123   /**
124    * DOCUMENT ME!
125    * 
126    * @param g
127    *          DOCUMENT ME!
128    * @param startx
129    *          DOCUMENT ME!
130    * @param endx
131    *          DOCUMENT ME!
132    * @param ypos
133    *          DOCUMENT ME!
134    */
135   void drawWestScale(Graphics g, int startx, int endx, int ypos)
136   {
137     FontMetrics fm = getFontMetrics(av.getFont());
138     ypos += av.charHeight;
139
140     if (av.hasHiddenColumns())
141     {
142       startx = av.getColumnSelection().adjustForHiddenColumns(startx);
143       endx = av.getColumnSelection().adjustForHiddenColumns(endx);
144     }
145
146     int maxwidth = av.getAlignment().getWidth();
147     if (av.hasHiddenColumns())
148     {
149       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
150     }
151
152     // WEST SCALE
153     for (int i = 0; i < av.getAlignment().getHeight(); i++)
154     {
155       SequenceI seq = av.getAlignment().getSequenceAt(i);
156       int index = startx;
157       int value = -1;
158
159       while (index < endx)
160       {
161         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
162         {
163           index++;
164
165           continue;
166         }
167
168         value = av.getAlignment().getSequenceAt(i).findPosition(index);
169
170         break;
171       }
172
173       if (value != -1)
174       {
175         int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
176                 - av.charWidth / 2;
177         g.drawString(value + "", x, (ypos + (i * av.charHeight))
178                 - (av.charHeight / 5));
179       }
180     }
181   }
182
183   /**
184    * DOCUMENT ME!
185    * 
186    * @param g
187    *          DOCUMENT ME!
188    * @param startx
189    *          DOCUMENT ME!
190    * @param endx
191    *          DOCUMENT ME!
192    * @param ypos
193    *          DOCUMENT ME!
194    */
195   void drawEastScale(Graphics g, int startx, int endx, int ypos)
196   {
197     ypos += av.charHeight;
198
199     if (av.hasHiddenColumns())
200     {
201       endx = av.getColumnSelection().adjustForHiddenColumns(endx);
202     }
203
204     SequenceI seq;
205     // EAST SCALE
206     for (int i = 0; i < av.getAlignment().getHeight(); i++)
207     {
208       seq = av.getAlignment().getSequenceAt(i);
209       int index = endx;
210       int value = -1;
211
212       while (index > startx)
213       {
214         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
215         {
216           index--;
217
218           continue;
219         }
220
221         value = seq.findPosition(index);
222
223         break;
224       }
225
226       if (value != -1)
227       {
228         g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
229                 - (av.charHeight / 5));
230       }
231     }
232   }
233
234   boolean fastpainting = false;
235
236   /**
237    * need to make this thread safe move alignment rendering in response to
238    * slider adjustment
239    * 
240    * @param horizontal
241    *          shift along
242    * @param vertical
243    *          shift up or down in repaint
244    */
245   public void fastPaint(int horizontal, int vertical)
246   {
247     if (fastpainting)
248     {
249       return;
250     }
251     fastpainting = true;
252     if (gg == null)
253     {
254       return;
255     }
256
257     fastPaint = true;
258
259     gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
260             imgWidth, imgHeight, -horizontal * av.charWidth, -vertical
261                     * av.charHeight);
262
263     int sr = av.startRes;
264     int er = av.endRes;
265     int ss = av.startSeq;
266     int es = av.endSeq;
267     int transX = 0;
268     int transY = 0;
269
270     if (horizontal > 0) // scrollbar pulled right, image to the left
271     {
272       er++;
273       transX = (er - sr - horizontal) * av.charWidth;
274       sr = er - horizontal;
275     }
276     else if (horizontal < 0)
277     {
278       er = sr - horizontal - 1;
279     }
280     else if (vertical > 0) // scroll down
281     {
282       ss = es - vertical;
283
284       if (ss < av.startSeq)
285       { // ie scrolling too fast, more than a page at a time
286         ss = av.startSeq;
287       }
288       else
289       {
290         transY = imgHeight - (vertical * av.charHeight);
291       }
292     }
293     else if (vertical < 0)
294     {
295       es = ss - vertical;
296
297       if (es > av.endSeq)
298       {
299         es = av.endSeq;
300       }
301     }
302
303     gg.translate(transX, transY);
304     drawPanel(gg, sr, er, ss, es, 0);
305     gg.translate(-transX, -transY);
306
307     repaint();
308     fastpainting = false;
309   }
310
311   /**
312    * Definitions of startx and endx (hopefully): SMJS This is what I'm working
313    * towards! startx is the first residue (starting at 0) to display. endx is
314    * the last residue to display (starting at 0). starty is the first sequence
315    * to display (starting at 0). endy is the last sequence to display (starting
316    * at 0). NOTE 1: The av limits are set in setFont in this class and in the
317    * adjustment listener in SeqPanel when the scrollbars move.
318    */
319
320   // Set this to false to force a full panel paint
321   public void paintComponent(Graphics g)
322   {
323     BufferedImage lcimg = img; // take reference since other threads may null
324     // img and call later.
325     super.paintComponent(g);
326
327     if (lcimg != null
328             && (fastPaint
329                     || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
330                     .getClipBounds().height)))
331     {
332       g.drawImage(lcimg, 0, 0, this);
333       fastPaint = false;
334       return;
335     }
336
337     // this draws the whole of the alignment
338     imgWidth = getWidth();
339     imgHeight = getHeight();
340
341     imgWidth -= (imgWidth % av.charWidth);
342     imgHeight -= (imgHeight % av.charHeight);
343
344     if ((imgWidth < 1) || (imgHeight < 1))
345     {
346       return;
347     }
348
349     if (lcimg == null || imgWidth != lcimg.getWidth()
350             || imgHeight != lcimg.getHeight())
351     {
352       try
353       {
354         lcimg = img = new BufferedImage(imgWidth, imgHeight,
355                 BufferedImage.TYPE_INT_RGB);
356         gg = (Graphics2D) img.getGraphics();
357         gg.setFont(av.getFont());
358       } catch (OutOfMemoryError er)
359       {
360         System.gc();
361         System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
362         new OOMWarning("Creating alignment image for display", er);
363
364         return;
365       }
366     }
367
368     if (av.antiAlias)
369     {
370       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
371               RenderingHints.VALUE_ANTIALIAS_ON);
372     }
373
374     gg.setColor(Color.white);
375     gg.fillRect(0, 0, imgWidth, imgHeight);
376
377     if (av.getWrapAlignment())
378     {
379       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
380     }
381     else
382     {
383       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
384     }
385
386     g.drawImage(lcimg, 0, 0, this);
387
388   }
389
390   /**
391    * DOCUMENT ME!
392    * 
393    * @param cwidth
394    *          DOCUMENT ME!
395    * 
396    * @return DOCUMENT ME!
397    */
398   public int getWrappedCanvasWidth(int cwidth)
399   {
400     FontMetrics fm = getFontMetrics(av.getFont());
401
402     LABEL_EAST = 0;
403     LABEL_WEST = 0;
404
405     if (av.scaleRightWrapped)
406     {
407       LABEL_EAST = fm.stringWidth(getMask());
408     }
409
410     if (av.scaleLeftWrapped)
411     {
412       LABEL_WEST = fm.stringWidth(getMask());
413     }
414
415     return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
416   }
417
418   /**
419    * Generates a string of zeroes.
420    * 
421    * @return String
422    */
423   String getMask()
424   {
425     String mask = "00";
426     int maxWidth = 0;
427     int tmp;
428     for (int i = 0; i < av.getAlignment().getHeight(); i++)
429     {
430       tmp = av.getAlignment().getSequenceAt(i).getEnd();
431       if (tmp > maxWidth)
432       {
433         maxWidth = tmp;
434       }
435     }
436
437     for (int i = maxWidth; i > 0; i /= 10)
438     {
439       mask += "0";
440     }
441     return mask;
442   }
443
444   /**
445    * DOCUMENT ME!
446    * 
447    * @param g
448    *          DOCUMENT ME!
449    * @param canvasWidth
450    *          DOCUMENT ME!
451    * @param canvasHeight
452    *          DOCUMENT ME!
453    * @param startRes
454    *          DOCUMENT ME!
455    */
456   public void drawWrappedPanel(Graphics g, int canvasWidth,
457           int canvasHeight, int startRes)
458   {
459     AlignmentI al = av.getAlignment();
460
461     FontMetrics fm = getFontMetrics(av.getFont());
462
463     if (av.scaleRightWrapped)
464     {
465       LABEL_EAST = fm.stringWidth(getMask());
466     }
467
468     if (av.scaleLeftWrapped)
469     {
470       LABEL_WEST = fm.stringWidth(getMask());
471     }
472
473     int hgap = av.charHeight;
474     if (av.scaleAboveWrapped)
475     {
476       hgap += av.charHeight;
477     }
478
479     int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
480     int cHeight = av.getAlignment().getHeight() * av.charHeight;
481
482     av.setWrappedWidth(cWidth);
483
484     av.endRes = av.startRes + cWidth;
485
486     int endx;
487     int ypos = hgap;
488     int maxwidth = av.getAlignment().getWidth() - 1;
489
490     if (av.hasHiddenColumns())
491     {
492       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
493     }
494
495     while ((ypos <= canvasHeight) && (startRes < maxwidth))
496     {
497       endx = startRes + cWidth - 1;
498
499       if (endx > maxwidth)
500       {
501         endx = maxwidth;
502       }
503
504       g.setFont(av.getFont());
505       g.setColor(Color.black);
506
507       if (av.scaleLeftWrapped)
508       {
509         drawWestScale(g, startRes, endx, ypos);
510       }
511
512       if (av.scaleRightWrapped)
513       {
514         g.translate(canvasWidth - LABEL_EAST, 0);
515         drawEastScale(g, startRes, endx, ypos);
516         g.translate(-(canvasWidth - LABEL_EAST), 0);
517       }
518
519       g.translate(LABEL_WEST, 0);
520
521       if (av.scaleAboveWrapped)
522       {
523         drawNorthScale(g, startRes, endx, ypos);
524       }
525
526       if (av.hasHiddenColumns() && av.showHiddenMarkers)
527       {
528         g.setColor(Color.blue);
529         int res;
530         for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
531                 .size(); i++)
532         {
533           res = av.getColumnSelection().findHiddenRegionPosition(i)
534                   - startRes;
535
536           if (res < 0 || res > endx - startRes)
537           {
538             continue;
539           }
540
541           gg.fillPolygon(new int[]
542           { res * av.charWidth - av.charHeight / 4,
543               res * av.charWidth + av.charHeight / 4, res * av.charWidth },
544                   new int[]
545                   { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
546                       ypos - (av.charHeight / 2) + 8 }, 3);
547
548         }
549       }
550
551       // When printing we have an extra clipped region,
552       // the Printable page which we need to account for here
553       Shape clip = g.getClip();
554
555       if (clip == null)
556       {
557         g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
558       }
559       else
560       {
561         g.setClip(0, (int) clip.getBounds().getY(), cWidth * av.charWidth,
562                 (int) clip.getBounds().getHeight());
563       }
564
565       drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
566
567       if (av.showAnnotation)
568       {
569         g.translate(0, cHeight + ypos + 3);
570         if (annotations == null)
571         {
572           annotations = new AnnotationPanel(av);
573         }
574
575         annotations.renderer.drawComponent(annotations, av, (Graphics2D) g,
576                 -1, startRes, endx + 1);
577         g.translate(0, -cHeight - ypos - 3);
578       }
579       g.setClip(clip);
580       g.translate(-LABEL_WEST, 0);
581
582       ypos += cHeight + getAnnotationHeight() + hgap;
583
584       startRes += cWidth;
585     }
586   }
587
588   AnnotationPanel annotations;
589
590   int getAnnotationHeight()
591   {
592     if (!av.showAnnotation)
593     {
594       return 0;
595     }
596
597     if (annotations == null)
598     {
599       annotations = new AnnotationPanel(av);
600     }
601
602     return annotations.adjustPanelHeight();
603   }
604
605   /**
606    * DOCUMENT ME!
607    * 
608    * @param g1
609    *          DOCUMENT ME!
610    * @param startRes
611    *          DOCUMENT ME!
612    * @param endRes
613    *          DOCUMENT ME!
614    * @param startSeq
615    *          DOCUMENT ME!
616    * @param endSeq
617    *          DOCUMENT ME!
618    * @param offset
619    *          DOCUMENT ME!
620    */
621   void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
622           int endSeq, int offset)
623   {
624     if (!av.hasHiddenColumns())
625     {
626       draw(g1, startRes, endRes, startSeq, endSeq, offset);
627     }
628     else
629     {
630       java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
631
632       int screenY = 0;
633       int blockStart = startRes;
634       int blockEnd = endRes;
635
636       for (int i = 0; regions != null && i < regions.size(); i++)
637       {
638         int[] region = (int[]) regions.elementAt(i);
639         int hideStart = region[0];
640         int hideEnd = region[1];
641
642         if (hideStart <= blockStart)
643         {
644           blockStart += (hideEnd - hideStart) + 1;
645           continue;
646         }
647
648         blockEnd = hideStart - 1;
649
650         g1.translate(screenY * av.charWidth, 0);
651
652         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
653
654         if (av.getShowHiddenMarkers())
655         {
656           g1.setColor(Color.blue);
657
658           g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
659                   0 + offset, (blockEnd - blockStart + 1) * av.charWidth
660                           - 1, (endSeq - startSeq) * av.charHeight + offset);
661         }
662
663         g1.translate(-screenY * av.charWidth, 0);
664         screenY += blockEnd - blockStart + 1;
665         blockStart = hideEnd + 1;
666       }
667
668       if (screenY <= (endRes - startRes))
669       {
670         blockEnd = blockStart + (endRes - startRes) - screenY;
671         g1.translate(screenY * av.charWidth, 0);
672         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
673
674         g1.translate(-screenY * av.charWidth, 0);
675       }
676     }
677
678   }
679
680   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
681   // int x1, int x2, int y1, int y2, int startx, int starty,
682   void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
683           int offset)
684   {
685     g.setFont(av.getFont());
686     sr.prepare(g, av.renderGaps);
687
688     SequenceI nextSeq;
689
690     // / First draw the sequences
691     // ///////////////////////////
692     for (int i = startSeq; i < endSeq; i++)
693     {
694       nextSeq = av.getAlignment().getSequenceAt(i);
695       if (nextSeq == null)
696       {
697         // occasionally, a race condition occurs such that the alignment row is
698         // empty
699         continue;
700       }
701       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
702               startRes, endRes, offset + ((i - startSeq) * av.charHeight));
703
704       if (av.showSequenceFeatures)
705       {
706         fr.drawSequence(g, nextSeq, startRes, endRes, offset
707                 + ((i - startSeq) * av.charHeight));
708       }
709
710       // / Highlight search Results once all sequences have been drawn
711       // ////////////////////////////////////////////////////////
712       if (searchResults != null)
713       {
714         int[] visibleResults = searchResults.getResults(nextSeq, startRes,
715                 endRes);
716         if (visibleResults != null)
717         {
718           for (int r = 0; r < visibleResults.length; r += 2)
719           {
720             sr.drawHighlightedText(nextSeq, visibleResults[r],
721                     visibleResults[r + 1], (visibleResults[r] - startRes)
722                             * av.charWidth, offset
723                             + ((i - startSeq) * av.charHeight));
724           }
725         }
726       }
727
728       if (av.cursorMode && cursorY == i && cursorX >= startRes
729               && cursorX <= endRes)
730       {
731         sr.drawCursor(nextSeq, cursorX,
732                 (cursorX - startRes) * av.charWidth, offset
733                         + ((i - startSeq) * av.charHeight));
734       }
735     }
736
737     if (av.getSelectionGroup() != null
738             || av.getAlignment().getGroups().size() > 0)
739     {
740       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
741     }
742
743   }
744
745   void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
746           int startSeq, int endSeq, int offset)
747   {
748     Graphics2D g = (Graphics2D) g1;
749     //
750     // ///////////////////////////////////
751     // Now outline any areas if necessary
752     // ///////////////////////////////////
753     SequenceGroup group = av.getSelectionGroup();
754
755     int sx = -1;
756     int sy = -1;
757     int ex = -1;
758     int groupIndex = -1;
759     int visWidth = (endRes - startRes + 1) * av.charWidth;
760
761     if ((group == null) && (av.getAlignment().getGroups().size() > 0))
762     {
763       group = (SequenceGroup) av.getAlignment().getGroups().get(0);
764       groupIndex = 0;
765     }
766
767     if (group != null)
768     {
769       do
770       {
771         int oldY = -1;
772         int i = 0;
773         boolean inGroup = false;
774         int top = -1;
775         int bottom = -1;
776
777         for (i = startSeq; i < endSeq; i++)
778         {
779           sx = (group.getStartRes() - startRes) * av.charWidth;
780           sy = offset + ((i - startSeq) * av.charHeight);
781           ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
782
783           if (sx + ex < 0 || sx > visWidth)
784           {
785             continue;
786           }
787
788           if ((sx <= (endRes - startRes) * av.charWidth)
789                   && group.getSequences(null).contains(
790                           av.getAlignment().getSequenceAt(i)))
791           {
792             if ((bottom == -1)
793                     && !group.getSequences(null).contains(
794                             av.getAlignment().getSequenceAt(i + 1)))
795             {
796               bottom = sy + av.charHeight;
797             }
798
799             if (!inGroup)
800             {
801               if (((top == -1) && (i == 0))
802                       || !group.getSequences(null).contains(
803                               av.getAlignment().getSequenceAt(i - 1)))
804               {
805                 top = sy;
806               }
807
808               oldY = sy;
809               inGroup = true;
810
811               if (group == av.getSelectionGroup())
812               {
813                 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
814                         BasicStroke.JOIN_ROUND, 3f, new float[]
815                         { 5f, 3f }, 0f));
816                 g.setColor(Color.RED);
817               }
818               else
819               {
820                 g.setStroke(new BasicStroke());
821                 g.setColor(group.getOutlineColour());
822               }
823             }
824           }
825           else
826           {
827             if (inGroup)
828             {
829               if (sx >= 0 && sx < visWidth)
830               {
831                 g.drawLine(sx, oldY, sx, sy);
832               }
833
834               if (sx + ex < visWidth)
835               {
836                 g.drawLine(sx + ex, oldY, sx + ex, sy);
837               }
838
839               if (sx < 0)
840               {
841                 ex += sx;
842                 sx = 0;
843               }
844
845               if (sx + ex > visWidth)
846               {
847                 ex = visWidth;
848               }
849
850               else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
851               {
852                 ex = (endRes - startRes + 1) * av.charWidth;
853               }
854
855               if (top != -1)
856               {
857                 g.drawLine(sx, top, sx + ex, top);
858                 top = -1;
859               }
860
861               if (bottom != -1)
862               {
863                 g.drawLine(sx, bottom, sx + ex, bottom);
864                 bottom = -1;
865               }
866
867               inGroup = false;
868             }
869           }
870         }
871
872         if (inGroup)
873         {
874           sy = offset + ((i - startSeq) * av.charHeight);
875           if (sx >= 0 && sx < visWidth)
876           {
877             g.drawLine(sx, oldY, sx, sy);
878           }
879
880           if (sx + ex < visWidth)
881           {
882             g.drawLine(sx + ex, oldY, sx + ex, sy);
883           }
884
885           if (sx < 0)
886           {
887             ex += sx;
888             sx = 0;
889           }
890
891           if (sx + ex > visWidth)
892           {
893             ex = visWidth;
894           }
895           else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
896           {
897             ex = (endRes - startRes + 1) * av.charWidth;
898           }
899
900           if (top != -1)
901           {
902             g.drawLine(sx, top, sx + ex, top);
903             top = -1;
904           }
905
906           if (bottom != -1)
907           {
908             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
909             bottom = -1;
910           }
911
912           inGroup = false;
913         }
914
915         groupIndex++;
916
917         g.setStroke(new BasicStroke());
918
919         if (groupIndex >= av.getAlignment().getGroups().size())
920         {
921           break;
922         }
923
924         group = (SequenceGroup) av.getAlignment().getGroups()
925                 .get(groupIndex);
926
927       } while (groupIndex < av.getAlignment().getGroups().size());
928
929     }
930
931   }
932
933   /**
934    * DOCUMENT ME!
935    * 
936    * @param results
937    *          DOCUMENT ME!
938    */
939   public void highlightSearchResults(SearchResults results)
940   {
941     img = null;
942
943     searchResults = results;
944
945     repaint();
946   }
947 }