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