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