54d2be76e2302b111b03ddd480d375b13c775f05
[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.getStartRes();
283     int er = av.getEndRes();
284     int ss = av.getStartSeq();
285     int es = av.getEndSeq();
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.getStartSeq())
304       { // ie scrolling too fast, more than a page at a time
305         ss = av.getStartSeq();
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.getEndSeq())
317       {
318         es = av.getEndSeq();
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.getStartRes());
401     }
402     else
403     {
404       drawPanel(gg, av.getStartRes(), av.getEndRes(), av.getStartSeq(),
405               av.getEndSeq(), 0);
406     }
407
408     g.drawImage(lcimg, 0, 0, this);
409
410   }
411
412   /**
413    * DOCUMENT ME!
414    * 
415    * @param cwidth
416    *          DOCUMENT ME!
417    * 
418    * @return DOCUMENT ME!
419    */
420   public int getWrappedCanvasWidth(int cwidth)
421   {
422     FontMetrics fm = getFontMetrics(av.getFont());
423
424     LABEL_EAST = 0;
425     LABEL_WEST = 0;
426
427     if (av.getScaleRightWrapped())
428     {
429       LABEL_EAST = fm.stringWidth(getMask());
430     }
431
432     if (av.getScaleLeftWrapped())
433     {
434       LABEL_WEST = fm.stringWidth(getMask());
435     }
436
437     return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
438   }
439
440   /**
441    * Generates a string of zeroes.
442    * 
443    * @return String
444    */
445   String getMask()
446   {
447     String mask = "00";
448     int maxWidth = 0;
449     int tmp;
450     for (int i = 0; i < av.getAlignment().getHeight(); i++)
451     {
452       tmp = av.getAlignment().getSequenceAt(i).getEnd();
453       if (tmp > maxWidth)
454       {
455         maxWidth = tmp;
456       }
457     }
458
459     for (int i = maxWidth; i > 0; i /= 10)
460     {
461       mask += "0";
462     }
463     return mask;
464   }
465
466   /**
467    * DOCUMENT ME!
468    * 
469    * @param g
470    *          DOCUMENT ME!
471    * @param canvasWidth
472    *          DOCUMENT ME!
473    * @param canvasHeight
474    *          DOCUMENT ME!
475    * @param startRes
476    *          DOCUMENT ME!
477    */
478   public void drawWrappedPanel(Graphics g, int canvasWidth,
479           int canvasHeight, int startRes)
480   {
481     updateViewport();
482     AlignmentI al = av.getAlignment();
483
484     FontMetrics fm = getFontMetrics(av.getFont());
485
486     if (av.getScaleRightWrapped())
487     {
488       LABEL_EAST = fm.stringWidth(getMask());
489     }
490
491     if (av.getScaleLeftWrapped())
492     {
493       LABEL_WEST = fm.stringWidth(getMask());
494     }
495
496     int hgap = charHeight;
497     if (av.getScaleAboveWrapped())
498     {
499       hgap += charHeight;
500     }
501
502     int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
503     int cHeight = av.getAlignment().getHeight() * charHeight;
504
505     av.setWrappedWidth(cWidth);
506
507     av.setEndRes(av.getStartRes() + cWidth);
508
509     int endx;
510     int ypos = hgap;
511     int maxwidth = av.getAlignment().getWidth() - 1;
512
513     if (av.hasHiddenColumns())
514     {
515       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
516     }
517
518     while ((ypos <= canvasHeight) && (startRes < maxwidth))
519     {
520       endx = startRes + cWidth - 1;
521
522       if (endx > maxwidth)
523       {
524         endx = maxwidth;
525       }
526
527       g.setFont(av.getFont());
528       g.setColor(Color.black);
529
530       if (av.getScaleLeftWrapped())
531       {
532         drawWestScale(g, startRes, endx, ypos);
533       }
534
535       if (av.getScaleRightWrapped())
536       {
537         g.translate(canvasWidth - LABEL_EAST, 0);
538         drawEastScale(g, startRes, endx, ypos);
539         g.translate(-(canvasWidth - LABEL_EAST), 0);
540       }
541
542       g.translate(LABEL_WEST, 0);
543
544       if (av.getScaleAboveWrapped())
545       {
546         drawNorthScale(g, startRes, endx, ypos);
547       }
548
549       if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
550       {
551         g.setColor(Color.blue);
552         int res;
553         for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
554                 .size(); i++)
555         {
556           res = av.getColumnSelection().findHiddenRegionPosition(i)
557                   - startRes;
558
559           if (res < 0 || res > endx - startRes)
560           {
561             continue;
562           }
563
564           gg.fillPolygon(
565                   new int[] { res * charWidth - charHeight / 4,
566                       res * charWidth + charHeight / 4, res * charWidth },
567                   new int[] { ypos - (charHeight / 2),
568                       ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
569                   3);
570
571         }
572       }
573
574       // When printing we have an extra clipped region,
575       // the Printable page which we need to account for here
576       Shape clip = g.getClip();
577
578       if (clip == null)
579       {
580         g.setClip(0, 0, cWidth * charWidth, canvasHeight);
581       }
582       else
583       {
584         g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
585                 (int) clip.getBounds().getHeight());
586       }
587
588       drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
589
590       if (av.isShowAnnotation())
591       {
592         g.translate(0, cHeight + ypos + 3);
593         if (annotations == null)
594         {
595           annotations = new AnnotationPanel(av);
596         }
597
598         annotations.renderer.drawComponent(annotations, av, g, -1,
599                 startRes, endx + 1);
600         g.translate(0, -cHeight - ypos - 3);
601       }
602       g.setClip(clip);
603       g.translate(-LABEL_WEST, 0);
604
605       ypos += cHeight + getAnnotationHeight() + hgap;
606
607       startRes += cWidth;
608     }
609   }
610
611   AnnotationPanel annotations;
612
613   int getAnnotationHeight()
614   {
615     if (!av.isShowAnnotation())
616     {
617       return 0;
618     }
619
620     if (annotations == null)
621     {
622       annotations = new AnnotationPanel(av);
623     }
624
625     return annotations.adjustPanelHeight();
626   }
627
628   /**
629    * DOCUMENT ME!
630    * 
631    * @param g1
632    *          DOCUMENT ME!
633    * @param startRes
634    *          DOCUMENT ME!
635    * @param endRes
636    *          DOCUMENT ME!
637    * @param startSeq
638    *          DOCUMENT ME!
639    * @param endSeq
640    *          DOCUMENT ME!
641    * @param offset
642    *          DOCUMENT ME!
643    */
644   public void drawPanel(Graphics g1, int startRes, int endRes,
645           int startSeq, int endSeq, int offset)
646   {
647     updateViewport();
648     if (!av.hasHiddenColumns())
649     {
650       draw(g1, startRes, endRes, startSeq, endSeq, offset);
651     }
652     else
653     {
654       List<int[]> regions = av.getColumnSelection().getHiddenColumns();
655
656       int screenY = 0;
657       int blockStart = startRes;
658       int blockEnd = endRes;
659
660       for (int[] region : regions)
661       {
662         int hideStart = region[0];
663         int hideEnd = region[1];
664
665         if (hideStart <= blockStart)
666         {
667           blockStart += (hideEnd - hideStart) + 1;
668           continue;
669         }
670
671         blockEnd = hideStart - 1;
672
673         g1.translate(screenY * charWidth, 0);
674
675         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
676
677         if (av.getShowHiddenMarkers())
678         {
679           g1.setColor(Color.blue);
680
681           g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
682                   0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
683                   (endSeq - startSeq) * charHeight + offset);
684         }
685
686         g1.translate(-screenY * charWidth, 0);
687         screenY += blockEnd - blockStart + 1;
688         blockStart = hideEnd + 1;
689
690         if (screenY > (endRes - startRes))
691         {
692           // already rendered last block
693           return;
694         }
695       }
696
697       if (screenY <= (endRes - startRes))
698       {
699         // remaining visible region to render
700         blockEnd = blockStart + (endRes - startRes) - screenY;
701         g1.translate(screenY * charWidth, 0);
702         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
703
704         g1.translate(-screenY * charWidth, 0);
705       }
706     }
707
708   }
709
710   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
711   // int x1, int x2, int y1, int y2, int startx, int starty,
712   private void draw(Graphics g, int startRes, int endRes, int startSeq,
713           int endSeq, int offset)
714   {
715     g.setFont(av.getFont());
716     sr.prepare(g, av.isRenderGaps());
717
718     SequenceI nextSeq;
719
720     // / First draw the sequences
721     // ///////////////////////////
722     for (int i = startSeq; i < endSeq; i++)
723     {
724       nextSeq = av.getAlignment().getSequenceAt(i);
725       if (nextSeq == null)
726       {
727         // occasionally, a race condition occurs such that the alignment row is
728         // empty
729         continue;
730       }
731       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
732               startRes, endRes, offset + ((i - startSeq) * charHeight));
733
734       if (av.isShowSequenceFeatures())
735       {
736         fr.drawSequence(g, nextSeq, startRes, endRes, offset
737                 + ((i - startSeq) * charHeight));
738       }
739
740       // / Highlight search Results once all sequences have been drawn
741       // ////////////////////////////////////////////////////////
742       if (av.hasSearchResults())
743       {
744         int[] visibleResults = av.getSearchResults().getResults(nextSeq,
745                 startRes, endRes);
746         if (visibleResults != null)
747         {
748           for (int r = 0; r < visibleResults.length; r += 2)
749           {
750             sr.drawHighlightedText(nextSeq, visibleResults[r],
751                     visibleResults[r + 1], (visibleResults[r] - startRes)
752                             * charWidth, offset
753                             + ((i - startSeq) * charHeight));
754           }
755         }
756       }
757
758       if (av.cursorMode && cursorY == i && cursorX >= startRes
759               && cursorX <= endRes)
760       {
761         sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
762                 offset + ((i - startSeq) * charHeight));
763       }
764     }
765
766     if (av.getSelectionGroup() != null
767             || av.getAlignment().getGroups().size() > 0)
768     {
769       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
770     }
771
772   }
773
774   void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
775           int startSeq, int endSeq, int offset)
776   {
777     Graphics2D g = (Graphics2D) g1;
778     //
779     // ///////////////////////////////////
780     // Now outline any areas if necessary
781     // ///////////////////////////////////
782     SequenceGroup group = av.getSelectionGroup();
783
784     int sx = -1;
785     int sy = -1;
786     int ex = -1;
787     int groupIndex = -1;
788     int visWidth = (endRes - startRes + 1) * charWidth;
789
790     if ((group == null) && (av.getAlignment().getGroups().size() > 0))
791     {
792       group = av.getAlignment().getGroups().get(0);
793       groupIndex = 0;
794     }
795
796     if (group != null)
797     {
798       do
799       {
800         int oldY = -1;
801         int i = 0;
802         boolean inGroup = false;
803         int top = -1;
804         int bottom = -1;
805
806         for (i = startSeq; i < endSeq; i++)
807         {
808           sx = (group.getStartRes() - startRes) * charWidth;
809           sy = offset + ((i - startSeq) * charHeight);
810           ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
811
812           if (sx + ex < 0 || sx > visWidth)
813           {
814             continue;
815           }
816
817           if ((sx <= (endRes - startRes) * charWidth)
818                   && group.getSequences(null).contains(
819                           av.getAlignment().getSequenceAt(i)))
820           {
821             if ((bottom == -1)
822                     && !group.getSequences(null).contains(
823                             av.getAlignment().getSequenceAt(i + 1)))
824             {
825               bottom = sy + charHeight;
826             }
827
828             if (!inGroup)
829             {
830               if (((top == -1) && (i == 0))
831                       || !group.getSequences(null).contains(
832                               av.getAlignment().getSequenceAt(i - 1)))
833               {
834                 top = sy;
835               }
836
837               oldY = sy;
838               inGroup = true;
839
840               if (group == av.getSelectionGroup())
841               {
842                 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
843                         BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
844                         0f));
845                 g.setColor(Color.RED);
846               }
847               else
848               {
849                 g.setStroke(new BasicStroke());
850                 g.setColor(group.getOutlineColour());
851               }
852             }
853           }
854           else
855           {
856             if (inGroup)
857             {
858               if (sx >= 0 && sx < visWidth)
859               {
860                 g.drawLine(sx, oldY, sx, sy);
861               }
862
863               if (sx + ex < visWidth)
864               {
865                 g.drawLine(sx + ex, oldY, sx + ex, sy);
866               }
867
868               if (sx < 0)
869               {
870                 ex += sx;
871                 sx = 0;
872               }
873
874               if (sx + ex > visWidth)
875               {
876                 ex = visWidth;
877               }
878
879               else if (sx + ex >= (endRes - startRes + 1) * charWidth)
880               {
881                 ex = (endRes - startRes + 1) * charWidth;
882               }
883
884               if (top != -1)
885               {
886                 g.drawLine(sx, top, sx + ex, top);
887                 top = -1;
888               }
889
890               if (bottom != -1)
891               {
892                 g.drawLine(sx, bottom, sx + ex, bottom);
893                 bottom = -1;
894               }
895
896               inGroup = false;
897             }
898           }
899         }
900
901         if (inGroup)
902         {
903           sy = offset + ((i - startSeq) * charHeight);
904           if (sx >= 0 && sx < visWidth)
905           {
906             g.drawLine(sx, oldY, sx, sy);
907           }
908
909           if (sx + ex < visWidth)
910           {
911             g.drawLine(sx + ex, oldY, sx + ex, sy);
912           }
913
914           if (sx < 0)
915           {
916             ex += sx;
917             sx = 0;
918           }
919
920           if (sx + ex > visWidth)
921           {
922             ex = visWidth;
923           }
924           else if (sx + ex >= (endRes - startRes + 1) * charWidth)
925           {
926             ex = (endRes - startRes + 1) * charWidth;
927           }
928
929           if (top != -1)
930           {
931             g.drawLine(sx, top, sx + ex, top);
932             top = -1;
933           }
934
935           if (bottom != -1)
936           {
937             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
938             bottom = -1;
939           }
940
941           inGroup = false;
942         }
943
944         groupIndex++;
945
946         g.setStroke(new BasicStroke());
947
948         if (groupIndex >= av.getAlignment().getGroups().size())
949         {
950           break;
951         }
952
953         group = av.getAlignment().getGroups().get(groupIndex);
954
955       } while (groupIndex < av.getAlignment().getGroups().size());
956
957     }
958
959   }
960
961   /**
962    * DOCUMENT ME!
963    * 
964    * @param results
965    *          DOCUMENT ME!
966    */
967   public void highlightSearchResults(SearchResultsI results)
968   {
969     img = null;
970
971     av.setSearchResults(results);
972
973     repaint();
974   }
975 }