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