JAL-2094 new classes ColorI, Colour added
[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.ColorUtils;
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   /**
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     int scalestartx = startx - (startx % 10) + 10;
127
128     g.setColor(Color.black);
129     // NORTH SCALE
130     for (int i = scalestartx; i < endx; i += 10)
131     {
132       int value = i;
133       if (av.hasHiddenColumns())
134       {
135         value = av.getColumnSelection().adjustForHiddenColumns(value);
136       }
137
138       g.drawString(String.valueOf(value), (i - startx - 1) * charWidth,
139               ypos - (charHeight / 2));
140
141       g.drawLine(((i - startx - 1) * charWidth) + (charWidth / 2),
142               (ypos + 2) - (charHeight / 2), ((i - startx - 1) * charWidth)
143                       + (charWidth / 2), 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 (jalview.util.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 (jalview.util.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   @Override
340   public void paintComponent(Graphics g)
341   {
342     updateViewport();
343     BufferedImage lcimg = img; // take reference since other threads may null
344     // img and call later.
345     super.paintComponent(g);
346
347     if (lcimg != null
348             && (fastPaint
349                     || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
350                     .getClipBounds().height)))
351     {
352       g.drawImage(lcimg, 0, 0, this);
353       fastPaint = false;
354       return;
355     }
356
357     // this draws the whole of the alignment
358     imgWidth = getWidth();
359     imgHeight = getHeight();
360
361     imgWidth -= (imgWidth % charWidth);
362     imgHeight -= (imgHeight % charHeight);
363
364     if ((imgWidth < 1) || (imgHeight < 1))
365     {
366       return;
367     }
368
369     if (lcimg == null || imgWidth != lcimg.getWidth()
370             || imgHeight != lcimg.getHeight())
371     {
372       try
373       {
374         lcimg = img = new BufferedImage(imgWidth, imgHeight,
375                 BufferedImage.TYPE_INT_RGB);
376         gg = (Graphics2D) img.getGraphics();
377         gg.setFont(av.getFont());
378       } catch (OutOfMemoryError er)
379       {
380         System.gc();
381         System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
382         new OOMWarning("Creating alignment image for display", er);
383
384         return;
385       }
386     }
387
388     if (av.antiAlias)
389     {
390       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
391               RenderingHints.VALUE_ANTIALIAS_ON);
392     }
393
394     gg.setColor(Color.white);
395     gg.fillRect(0, 0, imgWidth, imgHeight);
396
397     if (av.getWrapAlignment())
398     {
399       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
400     }
401     else
402     {
403       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
404     }
405
406     g.drawImage(lcimg, 0, 0, this);
407
408   }
409
410   /**
411    * DOCUMENT ME!
412    * 
413    * @param cwidth
414    *          DOCUMENT ME!
415    * 
416    * @return DOCUMENT ME!
417    */
418   public int getWrappedCanvasWidth(int cwidth)
419   {
420     FontMetrics fm = getFontMetrics(av.getFont());
421
422     LABEL_EAST = 0;
423     LABEL_WEST = 0;
424
425     if (av.getScaleRightWrapped())
426     {
427       LABEL_EAST = fm.stringWidth(getMask());
428     }
429
430     if (av.getScaleLeftWrapped())
431     {
432       LABEL_WEST = fm.stringWidth(getMask());
433     }
434
435     return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
436   }
437
438   /**
439    * Generates a string of zeroes.
440    * 
441    * @return String
442    */
443   String getMask()
444   {
445     String mask = "00";
446     int maxWidth = 0;
447     int tmp;
448     for (int i = 0; i < av.getAlignment().getHeight(); i++)
449     {
450       tmp = av.getAlignment().getSequenceAt(i).getEnd();
451       if (tmp > maxWidth)
452       {
453         maxWidth = tmp;
454       }
455     }
456
457     for (int i = maxWidth; i > 0; i /= 10)
458     {
459       mask += "0";
460     }
461     return mask;
462   }
463
464   /**
465    * DOCUMENT ME!
466    * 
467    * @param g
468    *          DOCUMENT ME!
469    * @param canvasWidth
470    *          DOCUMENT ME!
471    * @param canvasHeight
472    *          DOCUMENT ME!
473    * @param startRes
474    *          DOCUMENT ME!
475    */
476   public void drawWrappedPanel(Graphics g, int canvasWidth,
477           int canvasHeight, int startRes)
478   {
479     updateViewport();
480     AlignmentI al = av.getAlignment();
481
482     FontMetrics fm = getFontMetrics(av.getFont());
483
484     if (av.getScaleRightWrapped())
485     {
486       LABEL_EAST = fm.stringWidth(getMask());
487     }
488
489     if (av.getScaleLeftWrapped())
490     {
491       LABEL_WEST = fm.stringWidth(getMask());
492     }
493
494     int hgap = charHeight;
495     if (av.getScaleAboveWrapped())
496     {
497       hgap += charHeight;
498     }
499
500     int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
501     int cHeight = av.getAlignment().getHeight() * charHeight;
502
503     av.setWrappedWidth(cWidth);
504
505     av.endRes = av.startRes + cWidth;
506
507     int endx;
508     int ypos = hgap;
509     int maxwidth = av.getAlignment().getWidth() - 1;
510
511     if (av.hasHiddenColumns())
512     {
513       maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
514     }
515
516     while ((ypos <= canvasHeight) && (startRes < maxwidth))
517     {
518       endx = startRes + cWidth - 1;
519
520       if (endx > maxwidth)
521       {
522         endx = maxwidth;
523       }
524
525       g.setFont(av.getFont());
526       g.setColor(Color.black);
527
528       if (av.getScaleLeftWrapped())
529       {
530         drawWestScale(g, startRes, endx, ypos);
531       }
532
533       if (av.getScaleRightWrapped())
534       {
535         g.translate(canvasWidth - LABEL_EAST, 0);
536         drawEastScale(g, startRes, endx, ypos);
537         g.translate(-(canvasWidth - LABEL_EAST), 0);
538       }
539
540       g.translate(LABEL_WEST, 0);
541
542       if (av.getScaleAboveWrapped())
543       {
544         drawNorthScale(g, startRes, endx, ypos);
545       }
546
547       if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
548       {
549         g.setColor(Color.blue);
550         int res;
551         for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
552                 .size(); i++)
553         {
554           res = av.getColumnSelection().findHiddenRegionPosition(i)
555                   - startRes;
556
557           if (res < 0 || res > endx - startRes)
558           {
559             continue;
560           }
561
562           gg.fillPolygon(
563                   new int[] { res * charWidth - charHeight / 4,
564                       res * charWidth + charHeight / 4, res * charWidth },
565                   new int[] { ypos - (charHeight / 2),
566                       ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
567                   3);
568
569         }
570       }
571
572       // When printing we have an extra clipped region,
573       // the Printable page which we need to account for here
574       Shape clip = g.getClip();
575
576       if (clip == null)
577       {
578         g.setClip(0, 0, cWidth * charWidth, canvasHeight);
579       }
580       else
581       {
582         g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
583                 (int) clip.getBounds().getHeight());
584       }
585
586       drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
587
588       if (av.isShowAnnotation())
589       {
590         g.translate(0, cHeight + ypos + 3);
591         if (annotations == null)
592         {
593           annotations = new AnnotationPanel(av);
594         }
595
596         annotations.renderer.drawComponent(annotations, av, g, -1,
597                 startRes, endx + 1);
598         g.translate(0, -cHeight - ypos - 3);
599       }
600       g.setClip(clip);
601       g.translate(-LABEL_WEST, 0);
602
603       ypos += cHeight + getAnnotationHeight() + hgap;
604
605       startRes += cWidth;
606     }
607   }
608
609   AnnotationPanel annotations;
610
611   int getAnnotationHeight()
612   {
613     if (!av.isShowAnnotation())
614     {
615       return 0;
616     }
617
618     if (annotations == null)
619     {
620       annotations = new AnnotationPanel(av);
621     }
622
623     return annotations.adjustPanelHeight();
624   }
625
626   /**
627    * DOCUMENT ME!
628    * 
629    * @param g1
630    *          DOCUMENT ME!
631    * @param startRes
632    *          DOCUMENT ME!
633    * @param endRes
634    *          DOCUMENT ME!
635    * @param startSeq
636    *          DOCUMENT ME!
637    * @param endSeq
638    *          DOCUMENT ME!
639    * @param offset
640    *          DOCUMENT ME!
641    */
642   public void drawPanel(Graphics g1, int startRes, int endRes,
643           int startSeq, 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         if (screenY > (endRes - startRes))
689         {
690           // already rendered last block
691           return;
692         }
693       }
694
695       if (screenY <= (endRes - startRes))
696       {
697         // remaining visible region to render
698         blockEnd = blockStart + (endRes - startRes) - screenY;
699         g1.translate(screenY * charWidth, 0);
700         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
701
702         g1.translate(-screenY * charWidth, 0);
703       }
704     }
705
706   }
707
708   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
709   // int x1, int x2, int y1, int y2, int startx, int starty,
710   private void draw(Graphics g, int startRes, int endRes, int startSeq,
711           int endSeq, int offset)
712   {
713     g.setFont(av.getFont());
714     sr.prepare(g, av.isRenderGaps());
715
716     SequenceI nextSeq;
717
718     // / First draw the sequences
719     // ///////////////////////////
720     for (int i = startSeq; i < endSeq; i++)
721     {
722       nextSeq = av.getAlignment().getSequenceAt(i);
723       if (nextSeq == null)
724       {
725         // occasionally, a race condition occurs such that the alignment row is
726         // empty
727         continue;
728       }
729       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
730               startRes, endRes, offset + ((i - startSeq) * charHeight));
731
732       if (av.isShowSequenceFeatures())
733       {
734         fr.drawSequence(g, nextSeq, startRes, endRes, offset
735                 + ((i - startSeq) * charHeight));
736       }
737
738       // / Highlight search Results once all sequences have been drawn
739       // ////////////////////////////////////////////////////////
740       if (searchResults != null)
741       {
742         int[] visibleResults = searchResults.getResults(nextSeq, startRes,
743                 endRes);
744         if (visibleResults != null)
745         {
746           for (int r = 0; r < visibleResults.length; r += 2)
747           {
748             sr.drawHighlightedText(nextSeq, visibleResults[r],
749                     visibleResults[r + 1], (visibleResults[r] - startRes)
750                             * charWidth, offset
751                             + ((i - startSeq) * charHeight));
752           }
753         }
754       }
755
756       if (av.cursorMode && cursorY == i && cursorX >= startRes
757               && cursorX <= endRes)
758       {
759         sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
760                 offset + ((i - startSeq) * charHeight));
761       }
762     }
763
764     if (av.getSelectionGroup() != null
765             || av.getAlignment().getGroups().size() > 0)
766     {
767       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
768     }
769
770   }
771
772   void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
773           int startSeq, int endSeq, int offset)
774   {
775     Graphics2D g = (Graphics2D) g1;
776     //
777     // ///////////////////////////////////
778     // Now outline any areas if necessary
779     // ///////////////////////////////////
780     SequenceGroup group = av.getSelectionGroup();
781
782     int sx = -1;
783     int sy = -1;
784     int ex = -1;
785     int groupIndex = -1;
786     int visWidth = (endRes - startRes + 1) * charWidth;
787
788     if ((group == null) && (av.getAlignment().getGroups().size() > 0))
789     {
790       group = av.getAlignment().getGroups().get(0);
791       groupIndex = 0;
792     }
793
794     if (group != null)
795     {
796       do
797       {
798         int oldY = -1;
799         int i = 0;
800         boolean inGroup = false;
801         int top = -1;
802         int bottom = -1;
803
804         for (i = startSeq; i < endSeq; i++)
805         {
806           sx = (group.getStartRes() - startRes) * charWidth;
807           sy = offset + ((i - startSeq) * charHeight);
808           ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
809
810           if (sx + ex < 0 || sx > visWidth)
811           {
812             continue;
813           }
814
815           if ((sx <= (endRes - startRes) * charWidth)
816                   && group.getSequences(null).contains(
817                           av.getAlignment().getSequenceAt(i)))
818           {
819             if ((bottom == -1)
820                     && !group.getSequences(null).contains(
821                             av.getAlignment().getSequenceAt(i + 1)))
822             {
823               bottom = sy + charHeight;
824             }
825
826             if (!inGroup)
827             {
828               if (((top == -1) && (i == 0))
829                       || !group.getSequences(null).contains(
830                               av.getAlignment().getSequenceAt(i - 1)))
831               {
832                 top = sy;
833               }
834
835               oldY = sy;
836               inGroup = true;
837
838               if (group == av.getSelectionGroup())
839               {
840                 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
841                         BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
842                         0f));
843                 g.setColor(Color.RED);
844               }
845               else
846               {
847                 g.setStroke(new BasicStroke());
848                 g.setColor(ColorUtils.getColor(group.getOutlineColour()));
849               }
850             }
851           }
852           else
853           {
854             if (inGroup)
855             {
856               if (sx >= 0 && sx < visWidth)
857               {
858                 g.drawLine(sx, oldY, sx, sy);
859               }
860
861               if (sx + ex < visWidth)
862               {
863                 g.drawLine(sx + ex, oldY, sx + ex, sy);
864               }
865
866               if (sx < 0)
867               {
868                 ex += sx;
869                 sx = 0;
870               }
871
872               if (sx + ex > visWidth)
873               {
874                 ex = visWidth;
875               }
876
877               else if (sx + ex >= (endRes - startRes + 1) * charWidth)
878               {
879                 ex = (endRes - startRes + 1) * charWidth;
880               }
881
882               if (top != -1)
883               {
884                 g.drawLine(sx, top, sx + ex, top);
885                 top = -1;
886               }
887
888               if (bottom != -1)
889               {
890                 g.drawLine(sx, bottom, sx + ex, bottom);
891                 bottom = -1;
892               }
893
894               inGroup = false;
895             }
896           }
897         }
898
899         if (inGroup)
900         {
901           sy = offset + ((i - startSeq) * charHeight);
902           if (sx >= 0 && sx < visWidth)
903           {
904             g.drawLine(sx, oldY, sx, sy);
905           }
906
907           if (sx + ex < visWidth)
908           {
909             g.drawLine(sx + ex, oldY, sx + ex, sy);
910           }
911
912           if (sx < 0)
913           {
914             ex += sx;
915             sx = 0;
916           }
917
918           if (sx + ex > visWidth)
919           {
920             ex = visWidth;
921           }
922           else if (sx + ex >= (endRes - startRes + 1) * charWidth)
923           {
924             ex = (endRes - startRes + 1) * charWidth;
925           }
926
927           if (top != -1)
928           {
929             g.drawLine(sx, top, sx + ex, top);
930             top = -1;
931           }
932
933           if (bottom != -1)
934           {
935             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
936             bottom = -1;
937           }
938
939           inGroup = false;
940         }
941
942         groupIndex++;
943
944         g.setStroke(new BasicStroke());
945
946         if (groupIndex >= av.getAlignment().getGroups().size())
947         {
948           break;
949         }
950
951         group = av.getAlignment().getGroups().get(groupIndex);
952
953       } while (groupIndex < av.getAlignment().getGroups().size());
954
955     }
956
957   }
958
959   /**
960    * DOCUMENT ME!
961    * 
962    * @param results
963    *          DOCUMENT ME!
964    */
965   public void highlightSearchResults(SearchResults results)
966   {
967     img = null;
968
969     searchResults = results;
970
971     repaint();
972   }
973 }