Merge: 497958b 68dcaa7
[jalview.git] / src / jalview / gui / SeqCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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.isShowAnnotation())
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.isShowAnnotation())
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   public void drawPanel(Graphics g1, int startRes, int endRes,
631           int startSeq,
632           int endSeq, int offset)
633   {
634     if (!av.hasHiddenColumns())
635     {
636       draw(g1, startRes, endRes, startSeq, endSeq, offset);
637     }
638     else
639     {
640       java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
641
642       int screenY = 0;
643       int blockStart = startRes;
644       int blockEnd = endRes;
645
646       for (int i = 0; regions != null && i < regions.size(); i++)
647       {
648         int[] region = (int[]) regions.elementAt(i);
649         int hideStart = region[0];
650         int hideEnd = region[1];
651
652         if (hideStart <= blockStart)
653         {
654           blockStart += (hideEnd - hideStart) + 1;
655           continue;
656         }
657
658         blockEnd = hideStart - 1;
659
660         g1.translate(screenY * av.charWidth, 0);
661
662         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
663
664         if (av.getShowHiddenMarkers())
665         {
666           g1.setColor(Color.blue);
667
668           g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
669                   0 + offset, (blockEnd - blockStart + 1) * av.charWidth
670                           - 1, (endSeq - startSeq) * av.charHeight + offset);
671         }
672
673         g1.translate(-screenY * av.charWidth, 0);
674         screenY += blockEnd - blockStart + 1;
675         blockStart = hideEnd + 1;
676       }
677
678       if (screenY <= (endRes - startRes))
679       {
680         blockEnd = blockStart + (endRes - startRes) - screenY;
681         g1.translate(screenY * av.charWidth, 0);
682         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
683
684         g1.translate(-screenY * av.charWidth, 0);
685       }
686     }
687
688   }
689
690   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
691   // int x1, int x2, int y1, int y2, int startx, int starty,
692   void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
693           int offset)
694   {
695     g.setFont(av.getFont());
696     sr.prepare(g, av.renderGaps);
697
698     SequenceI nextSeq;
699
700     // / First draw the sequences
701     // ///////////////////////////
702     for (int i = startSeq; i < endSeq; i++)
703     {
704       nextSeq = av.getAlignment().getSequenceAt(i);
705       if (nextSeq == null)
706       {
707         // occasionally, a race condition occurs such that the alignment row is
708         // empty
709         continue;
710       }
711       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
712               startRes, endRes, offset + ((i - startSeq) * av.charHeight));
713
714       if (av.isShowSequenceFeatures())
715       {
716         fr.drawSequence(g, nextSeq, startRes, endRes, offset
717                 + ((i - startSeq) * av.charHeight));
718       }
719
720       // / Highlight search Results once all sequences have been drawn
721       // ////////////////////////////////////////////////////////
722       if (searchResults != null)
723       {
724         int[] visibleResults = searchResults.getResults(nextSeq, startRes,
725                 endRes);
726         if (visibleResults != null)
727         {
728           for (int r = 0; r < visibleResults.length; r += 2)
729           {
730             sr.drawHighlightedText(nextSeq, visibleResults[r],
731                     visibleResults[r + 1], (visibleResults[r] - startRes)
732                             * av.charWidth, offset
733                             + ((i - startSeq) * av.charHeight));
734           }
735         }
736       }
737
738       if (av.cursorMode && cursorY == i && cursorX >= startRes
739               && cursorX <= endRes)
740       {
741         sr.drawCursor(nextSeq, cursorX,
742                 (cursorX - startRes) * av.charWidth, offset
743                         + ((i - startSeq) * av.charHeight));
744       }
745     }
746
747     if (av.getSelectionGroup() != null
748             || av.getAlignment().getGroups().size() > 0)
749     {
750       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
751     }
752
753   }
754
755   void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
756           int startSeq, int endSeq, int offset)
757   {
758     Graphics2D g = (Graphics2D) g1;
759     //
760     // ///////////////////////////////////
761     // Now outline any areas if necessary
762     // ///////////////////////////////////
763     SequenceGroup group = av.getSelectionGroup();
764
765     int sx = -1;
766     int sy = -1;
767     int ex = -1;
768     int groupIndex = -1;
769     int visWidth = (endRes - startRes + 1) * av.charWidth;
770
771     if ((group == null) && (av.getAlignment().getGroups().size() > 0))
772     {
773       group = av.getAlignment().getGroups().get(0);
774       groupIndex = 0;
775     }
776
777     if (group != null)
778     {
779       do
780       {
781         int oldY = -1;
782         int i = 0;
783         boolean inGroup = false;
784         int top = -1;
785         int bottom = -1;
786
787         for (i = startSeq; i < endSeq; i++)
788         {
789           sx = (group.getStartRes() - startRes) * av.charWidth;
790           sy = offset + ((i - startSeq) * av.charHeight);
791           ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
792
793           if (sx + ex < 0 || sx > visWidth)
794           {
795             continue;
796           }
797
798           if ((sx <= (endRes - startRes) * av.charWidth)
799                   && group.getSequences(null).contains(
800                           av.getAlignment().getSequenceAt(i)))
801           {
802             if ((bottom == -1)
803                     && !group.getSequences(null).contains(
804                             av.getAlignment().getSequenceAt(i + 1)))
805             {
806               bottom = sy + av.charHeight;
807             }
808
809             if (!inGroup)
810             {
811               if (((top == -1) && (i == 0))
812                       || !group.getSequences(null).contains(
813                               av.getAlignment().getSequenceAt(i - 1)))
814               {
815                 top = sy;
816               }
817
818               oldY = sy;
819               inGroup = true;
820
821               if (group == av.getSelectionGroup())
822               {
823                 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
824                         BasicStroke.JOIN_ROUND, 3f, new float[]
825                         { 5f, 3f }, 0f));
826                 g.setColor(Color.RED);
827               }
828               else
829               {
830                 g.setStroke(new BasicStroke());
831                 g.setColor(group.getOutlineColour());
832               }
833             }
834           }
835           else
836           {
837             if (inGroup)
838             {
839               if (sx >= 0 && sx < visWidth)
840               {
841                 g.drawLine(sx, oldY, sx, sy);
842               }
843
844               if (sx + ex < visWidth)
845               {
846                 g.drawLine(sx + ex, oldY, sx + ex, sy);
847               }
848
849               if (sx < 0)
850               {
851                 ex += sx;
852                 sx = 0;
853               }
854
855               if (sx + ex > visWidth)
856               {
857                 ex = visWidth;
858               }
859
860               else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
861               {
862                 ex = (endRes - startRes + 1) * av.charWidth;
863               }
864
865               if (top != -1)
866               {
867                 g.drawLine(sx, top, sx + ex, top);
868                 top = -1;
869               }
870
871               if (bottom != -1)
872               {
873                 g.drawLine(sx, bottom, sx + ex, bottom);
874                 bottom = -1;
875               }
876
877               inGroup = false;
878             }
879           }
880         }
881
882         if (inGroup)
883         {
884           sy = offset + ((i - startSeq) * av.charHeight);
885           if (sx >= 0 && sx < visWidth)
886           {
887             g.drawLine(sx, oldY, sx, sy);
888           }
889
890           if (sx + ex < visWidth)
891           {
892             g.drawLine(sx + ex, oldY, sx + ex, sy);
893           }
894
895           if (sx < 0)
896           {
897             ex += sx;
898             sx = 0;
899           }
900
901           if (sx + ex > visWidth)
902           {
903             ex = visWidth;
904           }
905           else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
906           {
907             ex = (endRes - startRes + 1) * av.charWidth;
908           }
909
910           if (top != -1)
911           {
912             g.drawLine(sx, top, sx + ex, top);
913             top = -1;
914           }
915
916           if (bottom != -1)
917           {
918             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
919             bottom = -1;
920           }
921
922           inGroup = false;
923         }
924
925         groupIndex++;
926
927         g.setStroke(new BasicStroke());
928
929         if (groupIndex >= av.getAlignment().getGroups().size())
930         {
931           break;
932         }
933
934         group = av.getAlignment().getGroups()
935                 .get(groupIndex);
936
937       } while (groupIndex < av.getAlignment().getGroups().size());
938
939     }
940
941   }
942
943   /**
944    * DOCUMENT ME!
945    * 
946    * @param results
947    *          DOCUMENT ME!
948    */
949   public void highlightSearchResults(SearchResults results)
950   {
951     img = null;
952
953     searchResults = results;
954
955     repaint();
956   }
957 }