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