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