thread safety during repaint
[jalview.git] / src / jalview / gui / SeqCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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
213   /**
214    * DOCUMENT ME!
215    *
216    * @param horizontal DOCUMENT ME!
217    * @param vertical DOCUMENT ME!
218    */
219   public void fastPaint(int horizontal, int vertical)
220   {
221     if (gg == null)
222     {
223       return;
224     }
225
226     fastPaint = true;
227
228     gg.copyArea(horizontal * av.charWidth,
229                 vertical * av.charHeight,
230                 imgWidth,
231                 imgHeight,
232                 -horizontal * av.charWidth,
233                 -vertical * av.charHeight);
234
235     int sr = av.startRes;
236     int er = av.endRes;
237     int ss = av.startSeq;
238     int es = av.endSeq;
239     int transX = 0;
240     int transY = 0;
241
242     if (horizontal > 0) // scrollbar pulled right, image to the left
243     {
244       er++;
245       transX = (er - sr - horizontal) * av.charWidth;
246       sr = er - horizontal;
247     }
248     else if (horizontal < 0)
249     {
250       er = sr - horizontal - 1;
251     }
252     else if (vertical > 0) // scroll down
253     {
254       ss = es - vertical;
255
256       if (ss < av.startSeq)
257       { // ie scrolling too fast, more than a page at a time
258         ss = av.startSeq;
259       }
260       else
261       {
262         transY = imgHeight - (vertical * av.charHeight);
263       }
264     }
265     else if (vertical < 0)
266     {
267       es = ss - vertical;
268
269       if (es > av.endSeq)
270       {
271         es = av.endSeq;
272       }
273     }
274
275     gg.translate(transX, transY);
276     drawPanel(gg, sr, er, ss, es, 0);
277     gg.translate( -transX, -transY);
278
279     repaint();
280   }
281
282   /**
283    * Definitions of startx and endx (hopefully):
284    * SMJS This is what I'm working towards!
285    *   startx is the first residue (starting at 0) to display.
286    *   endx   is the last residue to display (starting at 0).
287    *   starty is the first sequence to display (starting at 0).
288    *   endy   is the last sequence to display (starting at 0).
289    * NOTE 1: The av limits are set in setFont in this class and
290    * in the adjustment listener in SeqPanel when the scrollbars move.
291    */
292
293   // Set this to false to force a full panel paint
294   public void paintComponent(Graphics g)
295   {
296     BufferedImage lcimg = img; // take reference since other threads may null img and call later.
297     super.paintComponent(g);
298
299     if (lcimg != null && (fastPaint
300                         || (getVisibleRect().width != g.getClipBounds().width)
301                         || (getVisibleRect().height != g.getClipBounds().height)))
302     {
303       g.drawImage(lcimg, 0, 0, this);
304       fastPaint = false;
305       return;
306     }
307
308     // this draws the whole of the alignment
309     imgWidth = getWidth();
310     imgHeight = getHeight();
311
312     imgWidth -= (imgWidth % av.charWidth);
313     imgHeight -= (imgHeight % av.charHeight);
314
315     if ( (imgWidth < 1) || (imgHeight < 1))
316     {
317       return;
318     }
319
320     if (lcimg == null || imgWidth != lcimg.getWidth() || imgHeight != lcimg.getHeight())
321     {
322       try
323       {
324         lcimg = img = new BufferedImage(imgWidth, imgHeight,
325                                 BufferedImage.TYPE_INT_RGB);
326         gg = (Graphics2D) img.getGraphics();
327         gg.setFont(av.getFont());
328       }
329       catch (OutOfMemoryError er)
330       {
331         System.gc();
332         System.out.println(er + " making image, SeqCanvas");
333         javax.swing.SwingUtilities.invokeLater(new Runnable()
334         {
335           public void run()
336           {
337             javax.swing.JOptionPane.showInternalMessageDialog(Desktop.
338                 desktop,
339                 "Out of memory creating alignment image!!"
340                 +
341                 "\nSee help files for increasing Java Virtual Machine memory."
342                 , "Out of memory",
343                 javax.swing.JOptionPane.WARNING_MESSAGE);
344           }
345         });
346
347         return;
348       }
349     }
350
351     if (av.antiAlias)
352     {
353       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
354                           RenderingHints.VALUE_ANTIALIAS_ON);
355     }
356
357     gg.setColor(Color.white);
358     gg.fillRect(0, 0, imgWidth, imgHeight);
359
360     if (av.getWrapAlignment())
361     {
362       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
363     }
364     else
365     {
366       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
367     }
368
369     g.drawImage(lcimg, 0, 0, this);
370
371
372   }
373
374   /**
375    * DOCUMENT ME!
376    *
377    * @param cwidth DOCUMENT ME!
378    *
379    * @return DOCUMENT ME!
380    */
381   public int getWrappedCanvasWidth(int cwidth)
382   {
383     FontMetrics fm = getFontMetrics(av.getFont());
384
385     LABEL_EAST = 0;
386     LABEL_WEST = 0;
387
388     if (av.scaleRightWrapped)
389     {
390       LABEL_EAST = fm.stringWidth(getMask());
391     }
392
393     if (av.scaleLeftWrapped)
394     {
395       LABEL_WEST = fm.stringWidth(getMask());
396     }
397
398     return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
399   }
400
401   /**
402    * Generates a string of zeroes.
403    * @return String
404    */
405   String getMask()
406   {
407     String mask = "00";
408     int maxWidth = 0;
409     int tmp;
410     for (int i = 0; i < av.alignment.getHeight(); i++)
411     {
412       tmp = av.alignment.getSequenceAt(i).getEnd();
413       if (tmp > maxWidth)
414       {
415         maxWidth = tmp;
416       }
417     }
418
419     for (int i = maxWidth; i > 0; i /= 10)
420     {
421       mask += "0";
422     }
423     return mask;
424   }
425
426   /**
427    * DOCUMENT ME!
428    *
429    * @param g DOCUMENT ME!
430    * @param canvasWidth DOCUMENT ME!
431    * @param canvasHeight DOCUMENT ME!
432    * @param startRes DOCUMENT ME!
433    */
434   public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
435                                int startRes)
436   {
437     AlignmentI al = av.getAlignment();
438
439     FontMetrics fm = getFontMetrics(av.getFont());
440
441     if (av.scaleRightWrapped)
442     {
443       LABEL_EAST = fm.stringWidth(getMask());
444     }
445
446     if (av.scaleLeftWrapped)
447     {
448       LABEL_WEST = fm.stringWidth(getMask());
449     }
450
451     int hgap = av.charHeight;
452     if (av.scaleAboveWrapped)
453     {
454       hgap += av.charHeight;
455     }
456
457     int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
458     int cHeight = av.getAlignment().getHeight() * av.charHeight;
459
460     av.setWrappedWidth(cWidth);
461
462     av.endRes = av.startRes + cWidth;
463
464     int endx;
465     int ypos = hgap;
466     int maxwidth = av.alignment.getWidth() - 1;
467
468     if (av.hasHiddenColumns)
469     {
470       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
471     }
472
473     while ( (ypos <= canvasHeight) && (startRes < maxwidth))
474     {
475       endx = startRes + cWidth - 1;
476
477       if (endx > maxwidth)
478       {
479         endx = maxwidth;
480       }
481
482       g.setFont(av.getFont());
483       g.setColor(Color.black);
484
485       if (av.scaleLeftWrapped)
486       {
487         drawWestScale(g, startRes, endx, ypos);
488       }
489
490       if (av.scaleRightWrapped)
491       {
492         g.translate(canvasWidth - LABEL_EAST, 0);
493         drawEastScale(g, startRes, endx, ypos);
494         g.translate( - (canvasWidth - LABEL_EAST), 0);
495       }
496
497       g.translate(LABEL_WEST, 0);
498
499       if (av.scaleAboveWrapped)
500       {
501         drawNorthScale(g, startRes, endx, ypos);
502       }
503
504       if (av.hasHiddenColumns && av.showHiddenMarkers)
505       {
506         g.setColor(Color.blue);
507         int res;
508         for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size();
509              i++)
510         {
511           res = av.getColumnSelection().findHiddenRegionPosition(i) -
512               startRes;
513
514           if (res < 0 || res > endx - startRes)
515           {
516             continue;
517           }
518
519           gg.fillPolygon(new int[]
520                          {res * av.charWidth - av.charHeight / 4,
521                          res * av.charWidth + av.charHeight / 4,
522                          res * av.charWidth},
523                          new int[]
524                          {
525                          ypos - (av.charHeight / 2),
526                          ypos - (av.charHeight / 2),
527                          ypos - (av.charHeight / 2) + 8
528           }, 3);
529
530         }
531       }
532
533       // When printing we have an extra clipped region,
534       // the Printable page which we need to account for here
535       Shape clip = g.getClip();
536
537       if (clip == null)
538       {
539         g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
540       }
541       else
542       {
543         g.setClip(0, (int) clip.getBounds().getY(),
544                   cWidth * av.charWidth, (int) clip.getBounds().getHeight());
545       }
546
547       drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
548
549       if (av.showAnnotation)
550       {
551         g.translate(0, cHeight + ypos + 3);
552         if (annotations == null)
553         {
554           annotations = new AnnotationPanel(av);
555         }
556
557         annotations.drawComponent( (Graphics2D) g, startRes, endx + 1);
558         g.translate(0, -cHeight - ypos - 3);
559       }
560       g.setClip(clip);
561       g.translate( -LABEL_WEST, 0);
562
563       ypos += cHeight + getAnnotationHeight() + hgap;
564
565       startRes += cWidth;
566     }
567   }
568
569   AnnotationPanel annotations;
570   int getAnnotationHeight()
571   {
572     if (!av.showAnnotation)
573     {
574       return 0;
575     }
576
577     if (annotations == null)
578     {
579       annotations = new AnnotationPanel(av);
580     }
581
582     return annotations.adjustPanelHeight();
583   }
584
585   /**
586    * DOCUMENT ME!
587    *
588    * @param g1 DOCUMENT ME!
589    * @param startRes DOCUMENT ME!
590    * @param endRes DOCUMENT ME!
591    * @param startSeq DOCUMENT ME!
592    * @param endSeq DOCUMENT ME!
593    * @param offset DOCUMENT ME!
594    */
595   void drawPanel(Graphics g1, int startRes, int endRes,
596                  int startSeq, int endSeq, int offset)
597   {
598     if (!av.hasHiddenColumns)
599     {
600       draw(g1, startRes, endRes, startSeq, endSeq, offset);
601     }
602     else
603     {
604       java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
605
606       int screenY = 0;
607       int blockStart = startRes;
608       int blockEnd = endRes;
609
610       for (int i = 0; i < regions.size(); i++)
611       {
612         int[] region = (int[]) regions.elementAt(i);
613         int hideStart = region[0];
614         int hideEnd = region[1];
615
616         if (hideStart <= blockStart)
617         {
618           blockStart += (hideEnd - hideStart) + 1;
619           continue;
620         }
621
622         blockEnd = hideStart - 1;
623
624         g1.translate(screenY * av.charWidth, 0);
625
626         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
627
628         if (av.getShowHiddenMarkers())
629         {
630           g1.setColor(Color.blue);
631
632           g1.drawLine( (blockEnd - blockStart + 1) * av.charWidth - 1,
633                       0 + offset,
634                       (blockEnd - blockStart + 1) * av.charWidth - 1,
635                       (endSeq - startSeq) * av.charHeight + offset);
636         }
637
638         g1.translate( -screenY * av.charWidth, 0);
639         screenY += blockEnd - blockStart + 1;
640         blockStart = hideEnd + 1;
641       }
642
643       if (screenY <= (endRes - startRes))
644       {
645         blockEnd = blockStart + (endRes - startRes) - screenY;
646         g1.translate(screenY * av.charWidth, 0);
647         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
648
649         g1.translate( -screenY * av.charWidth, 0);
650       }
651     }
652
653   }
654
655   //int startRes, int endRes, int startSeq, int endSeq, int x, int y,
656   // int x1, int x2, int y1, int y2, int startx, int starty,
657   void draw(Graphics g,
658             int startRes, int endRes,
659             int startSeq, int endSeq,
660             int offset)
661   {
662     g.setFont(av.getFont());
663     sr.prepare(g, av.renderGaps);
664
665     SequenceI nextSeq;
666
667     /// First draw the sequences
668     /////////////////////////////
669     for (int i = startSeq; i < endSeq; i++)
670     {
671       nextSeq = av.alignment.getSequenceAt(i);
672
673       sr.drawSequence(nextSeq, av.alignment.findAllGroups(nextSeq),
674                       startRes, endRes,
675                       offset + ( (i - startSeq) * av.charHeight));
676
677       if (av.showSequenceFeatures)
678       {
679         fr.drawSequence(g, nextSeq, startRes, endRes,
680                         offset + ( (i - startSeq) * av.charHeight));
681       }
682
683       /// Highlight search Results once all sequences have been drawn
684       //////////////////////////////////////////////////////////
685       if (searchResults != null)
686       {
687         int[] visibleResults = searchResults.getResults(nextSeq, startRes,
688             endRes);
689         if (visibleResults != null)
690         {
691           for (int r = 0; r < visibleResults.length; r += 2)
692           {
693             sr.drawHighlightedText(nextSeq, visibleResults[r],
694                                    visibleResults[r + 1],
695                                    (visibleResults[r] - startRes) *
696                                    av.charWidth,
697                                    offset + ( (i - startSeq) * av.charHeight));
698           }
699         }
700       }
701
702       if (av.cursorMode && cursorY == i
703           && cursorX >= startRes && cursorX <= endRes)
704       {
705         sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * av.charWidth,
706                       offset + ( (i - startSeq) * av.charHeight));
707       }
708     }
709
710     if (av.getSelectionGroup() != null || av.alignment.getGroups().size() > 0)
711     {
712       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
713     }
714
715   }
716
717   void drawGroupsBoundaries(Graphics g1,
718                             int startRes, int endRes,
719                             int startSeq, int endSeq,
720                             int offset)
721   {
722     Graphics2D g = (Graphics2D) g1;
723     //
724     /////////////////////////////////////
725     // Now outline any areas if necessary
726     /////////////////////////////////////
727     SequenceGroup group = av.getSelectionGroup();
728
729     int sx = -1;
730     int sy = -1;
731     int ex = -1;
732     int groupIndex = -1;
733     int visWidth = (endRes - startRes + 1) * av.charWidth;
734
735     if ( (group == null) && (av.alignment.getGroups().size() > 0))
736     {
737       group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
738       groupIndex = 0;
739     }
740
741     if (group != null)
742     {
743       do
744       {
745         int oldY = -1;
746         int i = 0;
747         boolean inGroup = false;
748         int top = -1;
749         int bottom = -1;
750
751         for (i = startSeq; i < endSeq; i++)
752         {
753           sx = (group.getStartRes() - startRes) * av.charWidth;
754           sy = offset + ( (i - startSeq) * av.charHeight);
755           ex = ( ( (group.getEndRes() + 1) - group.getStartRes()) *
756                 av.charWidth) -
757               1;
758
759           if (sx + ex < 0 || sx > visWidth)
760           {
761             continue;
762           }
763
764           if ( (sx <= (endRes - startRes) * av.charWidth) &&
765               group.getSequences(null).
766               contains(av.alignment.getSequenceAt(i)))
767           {
768             if ( (bottom == -1) &&
769                 !group.getSequences(null).contains(
770                     av.alignment.getSequenceAt(i + 1)))
771             {
772               bottom = sy + av.charHeight;
773             }
774
775             if (!inGroup)
776             {
777               if ( ( (top == -1) && (i == 0)) ||
778                   !group.getSequences(null).contains(
779                       av.alignment.getSequenceAt(i - 1)))
780               {
781                 top = sy;
782               }
783
784               oldY = sy;
785               inGroup = true;
786
787               if (group == av.getSelectionGroup())
788               {
789                 g.setStroke(new BasicStroke(1,
790                                             BasicStroke.CAP_BUTT,
791                                             BasicStroke.JOIN_ROUND, 3f,
792                                             new float[]
793                                             {5f, 3f}, 0f));
794                 g.setColor(Color.RED);
795               }
796               else
797               {
798                 g.setStroke(new BasicStroke());
799                 g.setColor(group.getOutlineColour());
800               }
801             }
802           }
803           else
804           {
805             if (inGroup)
806             {
807               if (sx >= 0 && sx < visWidth)
808               {
809                 g.drawLine(sx, oldY, sx, sy);
810               }
811
812               if (sx + ex < visWidth)
813               {
814                 g.drawLine(sx + ex, oldY, sx + ex, sy);
815               }
816
817               if (sx < 0)
818               {
819                 ex += sx;
820                 sx = 0;
821               }
822
823               if (sx + ex > visWidth)
824               {
825                 ex = visWidth;
826               }
827
828               else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
829               {
830                 ex = (endRes - startRes + 1) * av.charWidth;
831               }
832
833               if (top != -1)
834               {
835                 g.drawLine(sx, top, sx + ex, top);
836                 top = -1;
837               }
838
839               if (bottom != -1)
840               {
841                 g.drawLine(sx, bottom, sx + ex, bottom);
842                 bottom = -1;
843               }
844
845               inGroup = false;
846             }
847           }
848         }
849
850         if (inGroup)
851         {
852           sy = offset + ( (i - startSeq) * av.charHeight);
853           if (sx >= 0 && sx < visWidth)
854           {
855             g.drawLine(sx, oldY, sx, sy);
856           }
857
858           if (sx + ex < visWidth)
859           {
860             g.drawLine(sx + ex, oldY, sx + ex, sy);
861           }
862
863           if (sx < 0)
864           {
865             ex += sx;
866             sx = 0;
867           }
868
869           if (sx + ex > visWidth)
870           {
871             ex = visWidth;
872           }
873           else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
874           {
875             ex = (endRes - startRes + 1) * av.charWidth;
876           }
877
878           if (top != -1)
879           {
880             g.drawLine(sx, top, sx + ex, top);
881             top = -1;
882           }
883
884           if (bottom != -1)
885           {
886             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
887             bottom = -1;
888           }
889
890           inGroup = false;
891         }
892
893         groupIndex++;
894
895         g.setStroke(new BasicStroke());
896
897         if (groupIndex >= av.alignment.getGroups().size())
898         {
899           break;
900         }
901
902         group = (SequenceGroup) av.alignment.getGroups().elementAt(groupIndex);
903
904       }
905       while (groupIndex < av.alignment.getGroups().size());
906
907     }
908
909   }
910
911   /**
912    * DOCUMENT ME!
913    *
914    * @param results DOCUMENT ME!
915    */
916   public void highlightSearchResults(SearchResults results)
917   {
918     img = null;
919
920     searchResults = results;
921
922     repaint();
923   }
924 }