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