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