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