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