JAL-2674 2nd attempt to refactor seqcanvas drawing to iterator
[jalview.git] / src / jalview / appletgui / 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.appletgui;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.HiddenColumns;
25 import jalview.datamodel.SearchResultsI;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.ScaleRenderer;
29 import jalview.renderer.ScaleRenderer.ScaleMark;
30 import jalview.viewmodel.AlignmentViewport;
31 import jalview.viewmodel.ViewportListenerI;
32 import jalview.viewmodel.ViewportRanges;
33
34 import java.awt.Color;
35 import java.awt.FontMetrics;
36 import java.awt.Graphics;
37 import java.awt.Image;
38 import java.awt.Panel;
39 import java.beans.PropertyChangeEvent;
40 import java.util.Iterator;
41
42 public class SeqCanvas extends Panel implements ViewportListenerI
43 {
44   FeatureRenderer fr;
45
46   SequenceRenderer sr;
47
48   Image img;
49
50   Graphics gg;
51
52   int imgWidth;
53
54   int imgHeight;
55
56   AlignViewport av;
57
58   boolean fastPaint = false;
59
60   int cursorX = 0;
61
62   int cursorY = 0;
63
64   public SeqCanvas(AlignViewport av)
65   {
66     this.av = av;
67     fr = new FeatureRenderer(av);
68     sr = new SequenceRenderer(av);
69     PaintRefresher.Register(this, av.getSequenceSetId());
70     updateViewport();
71
72     av.getRanges().addPropertyChangeListener(this);
73   }
74
75   int avcharHeight = 0, avcharWidth = 0;
76
77   private void updateViewport()
78   {
79     avcharHeight = av.getCharHeight();
80     avcharWidth = av.getCharWidth();
81   }
82
83   public AlignmentViewport getViewport()
84   {
85     return av;
86   }
87
88   public FeatureRenderer getFeatureRenderer()
89   {
90     return fr;
91   }
92
93   public SequenceRenderer getSequenceRenderer()
94   {
95     return sr;
96   }
97
98   private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
99   {
100     updateViewport();
101     g.setColor(Color.black);
102     for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
103             endx))
104     {
105       int mpos = mark.column; // (i - startx - 1)
106       if (mpos < 0)
107       {
108         continue;
109       }
110       String mstring = mark.text;
111
112       if (mark.major)
113       {
114         if (mstring != null)
115         {
116           g.drawString(mstring, mpos * avcharWidth,
117                   ypos - (avcharHeight / 2));
118         }
119         g.drawLine((mpos * avcharWidth) + (avcharWidth / 2),
120                 (ypos + 2) - (avcharHeight / 2),
121                 (mpos * avcharWidth) + (avcharWidth / 2), ypos - 2);
122       }
123     }
124   }
125
126   private void drawWestScale(Graphics g, int startx, int endx, int ypos)
127   {
128     FontMetrics fm = getFontMetrics(av.getFont());
129     ypos += avcharHeight;
130     if (av.hasHiddenColumns())
131     {
132       startx = av.getAlignment().getHiddenColumns()
133               .adjustForHiddenColumns(startx);
134       endx = av.getAlignment().getHiddenColumns()
135               .adjustForHiddenColumns(endx);
136     }
137
138     int maxwidth = av.getAlignment().getWidth();
139     if (av.hasHiddenColumns())
140     {
141       maxwidth = av.getAlignment().getHiddenColumns()
142               .findColumnPosition(maxwidth) - 1;
143     }
144
145     // WEST SCALE
146     for (int i = 0; i < av.getAlignment().getHeight(); i++)
147     {
148       SequenceI seq = av.getAlignment().getSequenceAt(i);
149       int index = startx;
150       int value = -1;
151
152       while (index < endx)
153       {
154         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
155         {
156           index++;
157
158           continue;
159         }
160
161         value = av.getAlignment().getSequenceAt(i).findPosition(index);
162
163         break;
164       }
165
166       if (value != -1)
167       {
168         int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
169                 - avcharWidth / 2;
170         g.drawString(value + "", x,
171                 (ypos + (i * avcharHeight)) - (avcharHeight / 5));
172       }
173     }
174   }
175
176   private void drawEastScale(Graphics g, int startx, int endx, int ypos)
177   {
178     ypos += avcharHeight;
179
180     if (av.hasHiddenColumns())
181     {
182       endx = av.getAlignment().getHiddenColumns()
183               .adjustForHiddenColumns(endx);
184     }
185
186     SequenceI seq;
187     // EAST SCALE
188     for (int i = 0; i < av.getAlignment().getHeight(); i++)
189     {
190       seq = av.getAlignment().getSequenceAt(i);
191       int index = endx;
192       int value = -1;
193
194       while (index > startx)
195       {
196         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
197         {
198           index--;
199
200           continue;
201         }
202
203         value = seq.findPosition(index);
204
205         break;
206       }
207
208       if (value != -1)
209       {
210         g.drawString(String.valueOf(value), 0,
211                 (ypos + (i * avcharHeight)) - (avcharHeight / 5));
212       }
213     }
214   }
215
216   int lastsr = 0;
217
218   void fastPaint(int horizontal, int vertical)
219   {
220     if (fastPaint || gg == null)
221     {
222       return;
223     }
224
225     ViewportRanges ranges = av.getRanges();
226
227     updateViewport();
228
229     // Its possible on certain browsers that the call to fastpaint
230     // is faster than it can paint, so this check here catches
231     // this possibility
232     if (lastsr + horizontal != ranges.getStartRes())
233     {
234       horizontal = ranges.getStartRes() - lastsr;
235     }
236
237     lastsr = ranges.getStartRes();
238
239     fastPaint = true;
240     gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight,
241             imgWidth - horizontal * avcharWidth,
242             imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
243             -vertical * avcharHeight);
244
245     int sr = ranges.getStartRes(), er = ranges.getEndRes(),
246             ss = ranges.getStartSeq(), es = ranges.getEndSeq(), transX = 0,
247             transY = 0;
248
249     if (horizontal > 0) // scrollbar pulled right, image to the left
250     {
251       transX = (er - sr - horizontal) * avcharWidth;
252       sr = er - horizontal;
253     }
254     else if (horizontal < 0)
255     {
256       er = sr - horizontal;
257     }
258
259     else if (vertical > 0) // scroll down
260     {
261       ss = es - vertical;
262       if (ss < ranges.getStartSeq()) // ie scrolling too fast, more than a page
263                                      // at a
264       // time
265       {
266         ss = ranges.getStartSeq();
267       }
268       else
269       {
270         transY = imgHeight - ((vertical + 1) * avcharHeight);
271       }
272     }
273     else if (vertical < 0)
274     {
275       es = ss - vertical;
276       if (es > ranges.getEndSeq())
277       {
278         es = ranges.getEndSeq();
279       }
280     }
281
282     gg.translate(transX, transY);
283
284     drawPanel(gg, sr, er, ss, es, 0);
285     gg.translate(-transX, -transY);
286
287     repaint();
288
289   }
290
291   /**
292    * Definitions of startx and endx (hopefully): SMJS This is what I'm working
293    * towards! startx is the first residue (starting at 0) to display. endx is
294    * the last residue to display (starting at 0). starty is the first sequence
295    * to display (starting at 0). endy is the last sequence to display (starting
296    * at 0). NOTE 1: The av limits are set in setFont in this class and in the
297    * adjustment listener in SeqPanel when the scrollbars move.
298    */
299   @Override
300   public void update(Graphics g)
301   {
302     paint(g);
303   }
304
305   @Override
306   public void paint(Graphics g)
307   {
308
309     if (img != null
310             && (fastPaint || (getSize().width != g.getClipBounds().width)
311                     || (getSize().height != g.getClipBounds().height)))
312     {
313       g.drawImage(img, 0, 0, this);
314       fastPaint = false;
315       return;
316     }
317
318     if (fastPaint)
319     {
320       g.drawImage(img, 0, 0, this);
321       fastPaint = false;
322       return;
323     }
324
325     updateViewport();
326     // this draws the whole of the alignment
327     imgWidth = this.getSize().width;
328     imgHeight = this.getSize().height;
329
330     imgWidth -= imgWidth % avcharWidth;
331     imgHeight -= imgHeight % avcharHeight;
332
333     if (imgWidth < 1 || imgHeight < 1)
334     {
335       return;
336     }
337
338     if (img == null || imgWidth != img.getWidth(this)
339             || imgHeight != img.getHeight(this))
340     {
341       img = createImage(imgWidth, imgHeight);
342       gg = img.getGraphics();
343       gg.setFont(av.getFont());
344     }
345
346     gg.setColor(Color.white);
347     gg.fillRect(0, 0, imgWidth, imgHeight);
348
349     ViewportRanges ranges = av.getRanges();
350
351     if (av.getWrapAlignment())
352     {
353       drawWrappedPanel(gg, imgWidth, imgHeight, ranges.getStartRes());
354     }
355     else
356     {
357       drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
358               ranges.getStartSeq(), ranges.getEndSeq(), 0);
359     }
360
361     g.drawImage(img, 0, 0, this);
362
363   }
364
365   int LABEL_WEST, LABEL_EAST;
366
367   public int getWrappedCanvasWidth(int cwidth)
368   {
369     cwidth -= cwidth % av.getCharWidth();
370
371     FontMetrics fm = getFontMetrics(av.getFont());
372
373     LABEL_EAST = 0;
374     LABEL_WEST = 0;
375
376     if (av.getScaleRightWrapped())
377     {
378       LABEL_EAST = fm.stringWidth(getMask());
379     }
380
381     if (av.getScaleLeftWrapped())
382     {
383       LABEL_WEST = fm.stringWidth(getMask());
384     }
385
386     return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
387   }
388
389   /**
390    * Generates a string of zeroes.
391    * 
392    * @return String
393    */
394   String getMask()
395   {
396     String mask = "0";
397     int maxWidth = 0;
398     int tmp;
399     AlignmentI alignment = av.getAlignment();
400     for (int i = 0; i < alignment.getHeight(); i++)
401     {
402       tmp = alignment.getSequenceAt(i).getEnd();
403       if (tmp > maxWidth)
404       {
405         maxWidth = tmp;
406       }
407     }
408
409     for (int i = maxWidth; i > 0; i /= 10)
410     {
411       mask += "0";
412     }
413     return mask;
414   }
415
416   private void drawWrappedPanel(Graphics g, int canvasWidth,
417           int canvasHeight, int startRes)
418   {
419     AlignmentI al = av.getAlignment();
420   
421     FontMetrics fm = getFontMetrics(av.getFont());
422   
423     LABEL_EAST = 0;
424     LABEL_WEST = 0;
425   
426     if (av.getScaleRightWrapped())
427     {
428       LABEL_EAST = fm.stringWidth(getMask());
429     }
430   
431     if (av.getScaleLeftWrapped())
432     {
433       LABEL_WEST = fm.stringWidth(getMask());
434     }
435   
436     int hgap = avcharHeight;
437     if (av.getScaleAboveWrapped())
438     {
439       hgap += avcharHeight;
440     }
441   
442     int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
443     int cHeight = av.getAlignment().getHeight() * avcharHeight;
444   
445     av.setWrappedWidth(cWidth);
446   
447     av.getRanges().setViewportStartAndWidth(startRes, cWidth);
448   
449     int endx;
450     int ypos = hgap;
451   
452     int maxwidth = av.getAlignment().getWidth();
453   
454     if (av.hasHiddenColumns())
455     {
456       maxwidth = av.getAlignment().getHiddenColumns()
457               .findColumnPosition(maxwidth);
458     }
459   
460     while ((ypos <= canvasHeight) && (startRes < maxwidth))
461     {
462       endx = startRes + cWidth - 1;
463   
464       if (endx > maxwidth)
465       {
466         endx = maxwidth;
467       }
468   
469       g.setColor(Color.black);
470   
471       if (av.getScaleLeftWrapped())
472       {
473         drawWestScale(g, startRes, endx, ypos);
474       }
475   
476       if (av.getScaleRightWrapped())
477       {
478         g.translate(canvasWidth - LABEL_EAST, 0);
479         drawEastScale(g, startRes, endx, ypos);
480         g.translate(-(canvasWidth - LABEL_EAST), 0);
481       }
482   
483       g.translate(LABEL_WEST, 0);
484   
485       if (av.getScaleAboveWrapped())
486       {
487         drawNorthScale(g, startRes, endx, ypos);
488       }
489       if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
490       {
491         HiddenColumns hidden = av.getAlignment().getHiddenColumns();
492         g.setColor(Color.blue);
493         int res;
494         Iterator<Integer> it = hidden.getBoundedStartIterator(startRes,
495                 endx + 1);
496         while (it.hasNext())
497         {
498           res = it.next() - startRes;
499           gg.fillPolygon(
500                   new int[]
501                   { res * avcharWidth - avcharHeight / 4, res * avcharWidth + avcharHeight / 4, res * avcharWidth },
502                   new int[]
503                   { ypos - (avcharHeight / 2), ypos - (avcharHeight / 2), ypos - (avcharHeight / 2) + 8 }, 3);
504         }
505       }
506   
507       if (g.getClip() == null)
508       {
509         g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
510       }
511   
512       drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
513       g.setClip(null);
514   
515       if (av.isShowAnnotation())
516       {
517         g.translate(0, cHeight + ypos + 4);
518         if (annotations == null)
519         {
520           annotations = new AnnotationPanel(av);
521         }
522   
523         annotations.drawComponent(g, startRes, endx + 1);
524         g.translate(0, -cHeight - ypos - 4);
525       }
526       g.translate(-LABEL_WEST, 0);
527   
528       ypos += cHeight + getAnnotationHeight() + hgap;
529   
530       startRes += cWidth;
531     }
532   
533   }
534
535   AnnotationPanel annotations;
536
537   int getAnnotationHeight()
538   {
539     if (!av.isShowAnnotation())
540     {
541       return 0;
542     }
543
544     if (annotations == null)
545     {
546       annotations = new AnnotationPanel(av);
547     }
548
549     return annotations.adjustPanelHeight();
550   }
551
552   private void drawPanel(Graphics g1, final int startRes, final int endRes,
553           final int startSeq, final int endSeq, final int offset)
554   {
555
556     if (!av.hasHiddenColumns())
557     {
558       draw(g1, startRes, endRes, startSeq, endSeq, offset);
559     }
560     else
561     {
562       int screenY = 0;
563       final int screenYMax = endRes - startRes;
564       int blockStart = startRes;
565       int blockEnd = endRes; // equals blockStart + screenYMax - screenY;
566
567       HiddenColumns hidden = av.getAlignment().getHiddenColumns();
568       Iterator<int[]> regions = hidden.iterator();
569       while (regions.hasNext())
570       {
571         int[] region = regions.next();
572         int hideStart = region[0];
573         int hideEnd = region[1];
574
575         if (hideStart <= blockStart)
576         {
577           blockStart += (hideEnd - hideStart) + 1;
578           blockEnd += (hideEnd - hideStart) + 1;
579           continue;
580         }
581
582         /*
583          * draw up to just before the next hidden region, or the end of
584          * the visible region, whichever comes first
585          */
586         blockEnd = Math.min(blockEnd, hideStart - 1);
587         g1.translate(screenY * avcharWidth, 0);
588
589         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
590
591         /*
592          * draw the downline of the hidden column marker (ScalePanel draws the
593          * triangle on top) if we reached it
594          */
595         if (av.getShowHiddenMarkers() && blockEnd == hideStart - 1)
596         {
597           g1.setColor(Color.blue);
598           g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
599                   0 + offset, (blockEnd - blockStart + 1) * avcharWidth - 1,
600                   (endSeq - startSeq + 1) * avcharHeight + offset);
601         }
602
603         g1.translate(-screenY * avcharWidth, 0);
604         screenY += blockEnd - blockStart + 1;
605         blockStart = hideEnd + 1;
606         blockEnd = blockStart + screenYMax - screenY;
607
608         if (screenY > screenYMax)
609         {
610           // already rendered last block
611           return;
612         }
613       }
614
615       if (screenY <= screenYMax)
616       {
617         // remaining visible region to render
618         g1.translate(screenY * avcharWidth, 0);
619         draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
620         g1.translate(-screenY * avcharWidth, 0);
621       }
622     }
623   }
624
625   // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
626   // int x1, int x2, int y1, int y2, int startx, int starty,
627   void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
628           int offset)
629   {
630     g.setFont(av.getFont());
631     sr.prepare(g, av.isRenderGaps());
632     updateViewport();
633     SequenceI nextSeq;
634
635     // / First draw the sequences
636     // ///////////////////////////
637     for (int i = startSeq; i <= endSeq; i++)
638     {
639       nextSeq = av.getAlignment().getSequenceAt(i);
640
641       if (nextSeq == null)
642       {
643         continue;
644       }
645
646       sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
647               startRes, endRes, offset + ((i - startSeq) * avcharHeight));
648
649       if (av.isShowSequenceFeatures())
650       {
651         fr.drawSequence(g, nextSeq, startRes, endRes,
652                 offset + ((i - startSeq) * avcharHeight), false);
653       }
654
655       // / Highlight search Results once all sequences have been drawn
656       // ////////////////////////////////////////////////////////
657       if (av.hasSearchResults())
658       {
659         int[] visibleResults = av.getSearchResults().getResults(nextSeq,
660                 startRes, endRes);
661         if (visibleResults != null)
662         {
663           for (int r = 0; r < visibleResults.length; r += 2)
664           {
665             sr.drawHighlightedText(nextSeq, visibleResults[r],
666                     visibleResults[r + 1],
667                     (visibleResults[r] - startRes) * avcharWidth,
668                     offset + ((i - startSeq) * avcharHeight));
669           }
670         }
671       }
672
673       if (av.cursorMode && cursorY == i && cursorX >= startRes
674               && cursorX <= endRes)
675       {
676         sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
677                 offset + ((i - startSeq) * avcharHeight));
678       }
679     }
680
681     if (av.getSelectionGroup() != null
682             || av.getAlignment().getGroups().size() > 0)
683     {
684       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
685     }
686
687   }
688
689   private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
690           int startSeq, int endSeq, int offset)
691   {
692     //
693     // ///////////////////////////////////
694     // Now outline any areas if necessary
695     // ///////////////////////////////////
696     SequenceGroup group = av.getSelectionGroup();
697
698     int sx = -1;
699     int sy = -1;
700     int ex = -1;
701     int groupIndex = -1;
702
703     if ((group == null) && (av.getAlignment().getGroups().size() > 0))
704     {
705       group = av.getAlignment().getGroups().get(0);
706       groupIndex = 0;
707     }
708
709     if (group != null)
710     {
711       do
712       {
713         int oldY = -1;
714         int i = 0;
715         boolean inGroup = false;
716         int top = -1;
717         int bottom = -1;
718         int alHeight = av.getAlignment().getHeight() - 1;
719
720         for (i = startSeq; i <= endSeq; i++)
721         {
722           sx = (group.getStartRes() - startRes) * avcharWidth;
723           sy = offset + ((i - startSeq) * avcharHeight);
724           ex = (((group.getEndRes() + 1) - group.getStartRes())
725                   * avcharWidth) - 1;
726
727           if (sx + ex < 0 || sx > imgWidth)
728           {
729             continue;
730           }
731
732           if ((sx <= (endRes - startRes) * avcharWidth)
733                   && group.getSequences(null)
734                           .contains(av.getAlignment().getSequenceAt(i)))
735           {
736             if ((bottom == -1)
737                     && (i >= alHeight || !group.getSequences(null).contains(
738                             av.getAlignment().getSequenceAt(i + 1))))
739             {
740               bottom = sy + avcharHeight;
741             }
742
743             if (!inGroup)
744             {
745               if (((top == -1) && (i == 0)) || !group.getSequences(null)
746                       .contains(av.getAlignment().getSequenceAt(i - 1)))
747               {
748                 top = sy;
749               }
750
751               oldY = sy;
752               inGroup = true;
753
754               if (group == av.getSelectionGroup())
755               {
756                 g.setColor(Color.red);
757               }
758               else
759               {
760                 g.setColor(group.getOutlineColour());
761               }
762             }
763           }
764           else
765           {
766             if (inGroup)
767             {
768               if (sx >= 0 && sx < imgWidth)
769               {
770                 g.drawLine(sx, oldY, sx, sy);
771               }
772
773               if (sx + ex < imgWidth)
774               {
775                 g.drawLine(sx + ex, oldY, sx + ex, sy);
776               }
777
778               if (sx < 0)
779               {
780                 ex += sx;
781                 sx = 0;
782               }
783
784               if (sx + ex > imgWidth)
785               {
786                 ex = imgWidth;
787               }
788
789               else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
790               {
791                 ex = (endRes - startRes + 1) * avcharWidth;
792               }
793
794               if (top != -1)
795               {
796                 g.drawLine(sx, top, sx + ex, top);
797                 top = -1;
798               }
799
800               if (bottom != -1)
801               {
802                 g.drawLine(sx, bottom, sx + ex, bottom);
803                 bottom = -1;
804               }
805
806               inGroup = false;
807             }
808           }
809         }
810
811         if (inGroup)
812         {
813           sy = offset + ((i - startSeq) * avcharHeight);
814           if (sx >= 0 && sx < imgWidth)
815           {
816             g.drawLine(sx, oldY, sx, sy);
817           }
818
819           if (sx + ex < imgWidth)
820           {
821             g.drawLine(sx + ex, oldY, sx + ex, sy);
822           }
823
824           if (sx < 0)
825           {
826             ex += sx;
827             sx = 0;
828           }
829
830           if (sx + ex > imgWidth)
831           {
832             ex = imgWidth;
833           }
834           else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
835           {
836             ex = (endRes - startRes + 1) * avcharWidth;
837           }
838
839           if (top != -1)
840           {
841             g.drawLine(sx, top, sx + ex, top);
842             top = -1;
843           }
844
845           if (bottom != -1)
846           {
847             g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
848             bottom = -1;
849           }
850
851           inGroup = false;
852         }
853
854         groupIndex++;
855
856         if (groupIndex >= av.getAlignment().getGroups().size())
857         {
858           break;
859         }
860
861         group = av.getAlignment().getGroups().get(groupIndex);
862       } while (groupIndex < av.getAlignment().getGroups().size());
863
864     }
865   }
866
867   public void highlightSearchResults(SearchResultsI results)
868   {
869     av.setSearchResults(results);
870     repaint();
871   }
872
873   @Override
874   public void propertyChange(PropertyChangeEvent evt)
875   {
876     String eventName = evt.getPropertyName();
877
878     if (!av.getWrapAlignment())
879     {
880       int scrollX = 0;
881       if (eventName.equals(ViewportRanges.STARTRES))
882       {
883         // Make sure we're not trying to draw a panel
884         // larger than the visible window
885         ViewportRanges vpRanges = av.getRanges();
886         scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
887         int range = vpRanges.getEndRes() - vpRanges.getStartRes();
888         if (scrollX > range)
889         {
890           scrollX = range;
891         }
892         else if (scrollX < -range)
893         {
894           scrollX = -range;
895         }
896       }
897
898       // Both scrolling and resizing change viewport ranges: scrolling changes
899       // both start and end points, but resize only changes end values.
900       // Here we only want to fastpaint on a scroll, with resize using a normal
901       // paint, so scroll events are identified as changes to the horizontal or
902       // vertical start value.
903       if (eventName.equals(ViewportRanges.STARTRES))
904       {
905         // scroll - startres and endres both change
906         fastPaint(scrollX, 0);
907       }
908       else if (eventName.equals(ViewportRanges.STARTSEQ))
909       {
910         // scroll
911         fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
912       }
913     }
914   }
915
916 }