04ffc0909b617540f3a3cd6078b677c7245a582c
[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.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.ViewportListenerI;
31 import jalview.viewmodel.ViewportRanges;
32
33 import java.awt.AlphaComposite;
34 import java.awt.BasicStroke;
35 import java.awt.BorderLayout;
36 import java.awt.Color;
37 import java.awt.FontMetrics;
38 import java.awt.Graphics;
39 import java.awt.Graphics2D;
40 import java.awt.RenderingHints;
41 import java.awt.Shape;
42 import java.awt.image.BufferedImage;
43 import java.beans.PropertyChangeEvent;
44 import java.util.List;
45
46 import javax.swing.JComponent;
47
48 /**
49  * DOCUMENT ME!
50  * 
51  * @author $author$
52  * @version $Revision$
53  */
54 public class SeqCanvas extends JComponent implements ViewportListenerI
55 {
56   private static String ZEROS = "0000000000";
57
58   final FeatureRenderer fr;
59
60   final SequenceRenderer seqRdr;
61
62   BufferedImage img;
63
64   Graphics2D gg;
65
66   AlignViewport av;
67
68   boolean fastPaint = false;
69
70   int labelWidthWest;
71
72   int labelWidthEast;
73
74   int cursorX = 0;
75
76   int cursorY = 0;
77
78   int charHeight = 0;
79
80   int charWidth = 0;
81
82   boolean fastpainting = false;
83
84   AnnotationPanel annotations;
85
86   /**
87    * Creates a new SeqCanvas object.
88    * 
89    * @param av
90    *          DOCUMENT ME!
91    */
92   public SeqCanvas(AlignmentPanel ap)
93   {
94     this.av = ap.av;
95     updateViewport();
96     fr = new FeatureRenderer(ap);
97     seqRdr = new SequenceRenderer(av);
98     setLayout(new BorderLayout());
99     PaintRefresher.Register(this, av.getSequenceSetId());
100     setBackground(Color.white);
101
102     av.getRanges().addPropertyChangeListener(this);
103   }
104
105   public SequenceRenderer getSequenceRenderer()
106   {
107     return seqRdr;
108   }
109
110   public FeatureRenderer getFeatureRenderer()
111   {
112     return fr;
113   }
114
115   private void updateViewport()
116   {
117     charHeight = av.getCharHeight();
118     charWidth = av.getCharWidth();
119   }
120
121   /**
122    * DOCUMENT ME!
123    * 
124    * @param g
125    *          DOCUMENT ME!
126    * @param startx
127    *          DOCUMENT ME!
128    * @param endx
129    *          DOCUMENT ME!
130    * @param ypos
131    *          DOCUMENT ME!
132    */
133   private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
134   {
135     updateViewport();
136     for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
137             endx))
138     {
139       int mpos = mark.column; // (i - startx - 1)
140       if (mpos < 0)
141       {
142         continue;
143       }
144       String mstring = mark.text;
145
146       if (mark.major)
147       {
148         if (mstring != null)
149         {
150           g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
151         }
152         g.drawLine((mpos * charWidth) + (charWidth / 2),
153                 (ypos + 2) - (charHeight / 2),
154                 (mpos * charWidth) + (charWidth / 2), ypos - 2);
155       }
156     }
157   }
158
159   /**
160    * DOCUMENT ME!
161    * 
162    * @param g
163    *          DOCUMENT ME!
164    * @param startx
165    *          DOCUMENT ME!
166    * @param endx
167    *          DOCUMENT ME!
168    * @param ypos
169    *          DOCUMENT ME!
170    */
171   void drawWestScale(Graphics g, int startx, int endx, int ypos)
172   {
173     FontMetrics fm = getFontMetrics(av.getFont());
174     ypos += charHeight;
175
176     if (av.hasHiddenColumns())
177     {
178       startx = av.getAlignment().getHiddenColumns()
179               .adjustForHiddenColumns(startx);
180       endx = av.getAlignment().getHiddenColumns()
181               .adjustForHiddenColumns(endx);
182     }
183
184     int maxwidth = av.getAlignment().getWidth();
185     if (av.hasHiddenColumns())
186     {
187       maxwidth = av.getAlignment().getHiddenColumns()
188               .findColumnPosition(maxwidth) - 1;
189     }
190
191     // WEST SCALE
192     for (int i = 0; i < av.getAlignment().getHeight(); i++)
193     {
194       SequenceI seq = av.getAlignment().getSequenceAt(i);
195       int index = startx;
196       int value = -1;
197
198       while (index < endx)
199       {
200         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
201         {
202           index++;
203
204           continue;
205         }
206
207         value = av.getAlignment().getSequenceAt(i).findPosition(index);
208
209         break;
210       }
211
212       if (value != -1)
213       {
214         int x = labelWidthWest - fm.stringWidth(String.valueOf(value))
215                 - charWidth / 2;
216         g.drawString(value + "", x,
217                 (ypos + (i * charHeight)) - (charHeight / 5));
218       }
219     }
220   }
221
222   /**
223    * DOCUMENT ME!
224    * 
225    * @param g
226    *          DOCUMENT ME!
227    * @param startx
228    *          DOCUMENT ME!
229    * @param endx
230    *          DOCUMENT ME!
231    * @param ypos
232    *          DOCUMENT ME!
233    */
234   void drawEastScale(Graphics g, int startx, int endx, int ypos)
235   {
236     ypos += charHeight;
237
238     if (av.hasHiddenColumns())
239     {
240       endx = av.getAlignment().getHiddenColumns()
241               .adjustForHiddenColumns(endx);
242     }
243
244     SequenceI seq;
245     // EAST SCALE
246     for (int i = 0; i < av.getAlignment().getHeight(); i++)
247     {
248       seq = av.getAlignment().getSequenceAt(i);
249       int index = endx;
250       int value = -1;
251
252       while (index > startx)
253       {
254         if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
255         {
256           index--;
257
258           continue;
259         }
260
261         value = seq.findPosition(index);
262
263         break;
264       }
265
266       if (value != -1)
267       {
268         g.drawString(String.valueOf(value), 0,
269                 (ypos + (i * charHeight)) - (charHeight / 5));
270       }
271     }
272   }
273
274
275   /**
276    * need to make this thread safe move alignment rendering in response to
277    * slider adjustment
278    * 
279    * @param horizontal
280    *          shift along
281    * @param vertical
282    *          shift up or down in repaint
283    */
284   public void fastPaint(int horizontal, int vertical)
285   {
286     if (fastpainting || gg == null)
287     {
288       return;
289     }
290     fastpainting = true;
291     fastPaint = true;
292     updateViewport();
293
294     ViewportRanges ranges = av.getRanges();
295     int startRes = ranges.getStartRes();
296     int endRes = ranges.getEndRes();
297     int startSeq = ranges.getStartSeq();
298     int endSeq = ranges.getEndSeq();
299     int transX = 0;
300     int transY = 0;
301
302     gg.copyArea(horizontal * charWidth, vertical * charHeight,
303             img.getWidth(), img.getHeight(), -horizontal * charWidth,
304             -vertical * charHeight);
305
306     if (horizontal > 0) // scrollbar pulled right, image to the left
307     {
308       transX = (endRes - startRes - horizontal) * charWidth;
309       startRes = endRes - horizontal;
310     }
311     else if (horizontal < 0)
312     {
313       endRes = startRes - horizontal;
314     }
315     else if (vertical > 0) // scroll down
316     {
317       startSeq = endSeq - vertical;
318
319       if (startSeq < ranges.getStartSeq())
320       { // ie scrolling too fast, more than a page at a time
321         startSeq = ranges.getStartSeq();
322       }
323       else
324       {
325         transY = img.getHeight() - ((vertical + 1) * charHeight);
326       }
327     }
328     else if (vertical < 0)
329     {
330       endSeq = startSeq - vertical;
331
332       if (endSeq > ranges.getEndSeq())
333       {
334         endSeq = ranges.getEndSeq();
335       }
336     }
337
338     gg.translate(transX, transY);
339     drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
340     gg.translate(-transX, -transY);
341
342     repaint();
343     fastpainting = false;
344   }
345
346   @Override
347   public void paintComponent(Graphics g)
348   {
349     super.paintComponent(g);
350
351     updateViewport();
352
353     ViewportRanges ranges = av.getRanges();
354
355     int width = getWidth();
356     int height = getHeight();
357
358     width -= (width % charWidth);
359     height -= (height % charHeight);
360
361     // selectImage is the selection group outline image
362     BufferedImage selectImage = drawSelectionGroup(
363             ranges.getStartRes(), ranges.getEndRes(),
364             ranges.getStartSeq(), ranges.getEndSeq());
365
366     if ((img != null) && (fastPaint
367             || (getVisibleRect().width != g.getClipBounds().width)
368             || (getVisibleRect().height != g.getClipBounds().height)))
369     {
370       BufferedImage lcimg = buildLocalImage(selectImage);
371       g.drawImage(lcimg, 0, 0, this);
372       fastPaint = false;
373     }
374     else if ((width > 0) && (height > 0))
375     {
376       // img is a cached version of the last view we drew, if any
377       // if we have no img or the size has changed, make a new one
378       if (img == null || width != img.getWidth()
379               || height != img.getHeight())
380       {
381         img = setupImage();
382         if (img == null)
383         {
384           return;
385         }
386         gg = (Graphics2D) img.getGraphics();
387         gg.setFont(av.getFont());
388       }
389
390       if (av.antiAlias)
391       {
392         gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
393                 RenderingHints.VALUE_ANTIALIAS_ON);
394       }
395
396       gg.setColor(Color.white);
397       gg.fillRect(0, 0, img.getWidth(), img.getHeight());
398
399       if (av.getWrapAlignment())
400       {
401         drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
402       }
403       else
404       {
405         drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
406                 ranges.getStartSeq(), ranges.getEndSeq(), 0);
407       }
408
409       // lcimg is a local *copy* of img which we'll draw selectImage on top of
410       BufferedImage lcimg = buildLocalImage(selectImage);
411       g.drawImage(lcimg, 0, 0, this);
412     }
413   }
414
415   /**
416    * Draw an alignment panel for printing
417    * 
418    * @param g1
419    *          Graphics object to draw with
420    * @param startRes
421    *          start residue of print area
422    * @param endRes
423    *          end residue of print area
424    * @param startSeq
425    *          start sequence of print area
426    * @param endSeq
427    *          end sequence of print area
428    */
429   public void drawPanelForPrint(Graphics g1, int startRes, int endRes,
430           int startSeq, int endSeq)
431   {
432     BufferedImage selectImage = drawSelectionGroup(startRes, endRes,
433             startSeq, endSeq);
434     drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
435     ((Graphics2D) g1).setComposite(
436             AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
437     g1.drawImage(selectImage, 0, 0, this);
438   }
439
440   /*
441    * Make a local image by combining the cached image img
442    * with any selection
443    */
444   private BufferedImage buildLocalImage(BufferedImage selectImage)
445   {
446     // clone the cached image
447     BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
448             img.getType());
449     Graphics2D g2d = lcimg.createGraphics();
450     g2d.drawImage(img, 0, 0, null);
451
452     // overlay selection group on lcimg
453     if (selectImage != null)
454     {
455       g2d.setComposite(
456               AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
457       g2d.drawImage(selectImage, 0, 0, this);
458     }
459     g2d.dispose();
460
461     return lcimg;
462   }
463
464   /*
465    * Set up a buffered image of the correct height and size for the sequence canvas
466    */
467   private BufferedImage setupImage()
468   {
469     BufferedImage lcimg = null;
470
471     int width = getWidth();
472     int height = getHeight();
473
474     width -= (width % charWidth);
475     height -= (height % charHeight);
476
477     if ((width < 1) || (height < 1))
478     {
479       return null;
480     }
481
482     try
483     {
484       lcimg = new BufferedImage(width, height,
485               BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
486     } catch (OutOfMemoryError er)
487     {
488       System.gc();
489       System.err.println(
490               "Group image OutOfMemory Redraw Error.\n" + er);
491       new OOMWarning("Creating alignment image for display", er);
492
493       return null;
494     }
495
496     return lcimg;
497   }
498
499   /**
500    * Returns the visible width of the canvas in residues, after allowing for
501    * East or West scales (if shown)
502    * 
503    * @param canvasWidth
504    *          the width in pixels (possibly including scales)
505    * 
506    * @return
507    */
508   public int getWrappedCanvasWidth(int canvasWidth)
509   {
510     FontMetrics fm = getFontMetrics(av.getFont());
511
512     labelWidthEast = 0;
513     labelWidthWest = 0;
514
515     if (av.getScaleRightWrapped())
516     {
517       labelWidthEast = getLabelWidth(fm);
518     }
519
520     if (av.getScaleLeftWrapped())
521     {
522       labelWidthWest = labelWidthEast > 0 ? labelWidthEast
523               : getLabelWidth(fm);
524     }
525
526     return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
527   }
528
529   /**
530    * Returns a pixel width suitable for showing the largest sequence coordinate
531    * (end position) in the alignment. Returns 2 plus the number of decimal
532    * digits to be shown (3 for 1-10, 4 for 11-99 etc).
533    * 
534    * @param fm
535    * @return
536    */
537   protected int getLabelWidth(FontMetrics fm)
538   {
539     /*
540      * find the biggest sequence end position we need to show
541      * (note this is not necessarily the sequence length)
542      */
543     int maxWidth = 0;
544     AlignmentI alignment = av.getAlignment();
545     for (int i = 0; i < alignment.getHeight(); i++)
546     {
547       maxWidth = Math.max(maxWidth, alignment.getSequenceAt(i).getEnd());
548     }
549
550     int length = 2;
551     for (int i = maxWidth; i > 0; i /= 10)
552     {
553       length++;
554     }
555
556     return fm.stringWidth(ZEROS.substring(0, length));
557   }
558
559   /**
560    * DOCUMENT ME!
561    * 
562    * @param g
563    *          DOCUMENT ME!
564    * @param canvasWidth
565    *          DOCUMENT ME!
566    * @param canvasHeight
567    *          DOCUMENT ME!
568    * @param startRes
569    *          DOCUMENT ME!
570    */
571   public void drawWrappedPanel(Graphics g, int canvasWidth,
572           int canvasHeight, int startRes)
573   {
574     updateViewport();
575     AlignmentI al = av.getAlignment();
576
577     int labelWidth = 0;
578     if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
579     {
580       FontMetrics fm = getFontMetrics(av.getFont());
581       labelWidth = getLabelWidth(fm);
582     }
583
584     labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
585     labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
586
587     int hgap = charHeight;
588     if (av.getScaleAboveWrapped())
589     {
590       hgap += charHeight;
591     }
592
593     int cWidth = (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
594     int cHeight = av.getAlignment().getHeight() * charHeight;
595
596     av.setWrappedWidth(cWidth);
597
598     av.getRanges().setViewportStartAndWidth(startRes, cWidth);
599
600     int endx;
601     int ypos = hgap;
602     int maxwidth = av.getAlignment().getWidth();
603
604     if (av.hasHiddenColumns())
605     {
606       maxwidth = av.getAlignment().getHiddenColumns()
607               .findColumnPosition(maxwidth);
608     }
609
610     int annotationHeight = getAnnotationHeight();
611
612     while ((ypos <= canvasHeight) && (startRes < maxwidth))
613     {
614       endx = startRes + cWidth - 1;
615
616       if (endx > maxwidth)
617       {
618         endx = maxwidth;
619       }
620
621       g.setFont(av.getFont());
622       g.setColor(Color.black);
623
624       if (av.getScaleLeftWrapped())
625       {
626         drawWestScale(g, startRes, endx, ypos);
627       }
628
629       if (av.getScaleRightWrapped())
630       {
631         g.translate(canvasWidth - labelWidthEast, 0);
632         drawEastScale(g, startRes, endx, ypos);
633         g.translate(-(canvasWidth - labelWidthEast), 0);
634       }
635
636       g.translate(labelWidthWest, 0);
637
638       if (av.getScaleAboveWrapped())
639       {
640         drawNorthScale(g, startRes, endx, ypos);
641       }
642
643       if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
644       {
645         g.setColor(Color.blue);
646         int res;
647         HiddenColumns hidden = av.getAlignment().getHiddenColumns();
648         List<Integer> positions = hidden.findHiddenRegionPositions();
649         for (int pos : positions)
650         {
651           res = pos - startRes;
652
653           if (res < 0 || res > endx - startRes)
654           {
655             continue;
656           }
657
658           gg.fillPolygon(
659                   new int[]
660                   { res * charWidth - charHeight / 4,
661                       res * charWidth + charHeight / 4, res * charWidth },
662                   new int[]
663                   { ypos - (charHeight / 2), ypos - (charHeight / 2),
664                       ypos - (charHeight / 2) + 8 },
665                   3);
666
667         }
668       }
669
670       // When printing we have an extra clipped region,
671       // the Printable page which we need to account for here
672       Shape clip = g.getClip();
673
674       if (clip == null)
675       {
676         g.setClip(0, 0, cWidth * charWidth, canvasHeight);
677       }
678       else
679       {
680         g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
681                 (int) clip.getBounds().getHeight());
682       }
683
684       drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
685
686       if (av.isShowAnnotation())
687       {
688         g.translate(0, cHeight + ypos + 3);
689         if (annotations == null)
690         {
691           annotations = new AnnotationPanel(av);
692         }
693
694         annotations.renderer.drawComponent(annotations, av, g, -1, startRes,
695                 endx + 1);
696         g.translate(0, -cHeight - ypos - 3);
697       }
698       g.setClip(clip);
699       g.translate(-labelWidthWest, 0);
700
701       ypos += cHeight + annotationHeight + hgap;
702
703       startRes += cWidth;
704     }
705   }
706
707   /*
708    * Draw a selection group over a wrapped alignment
709    */
710   private void drawWrappedSelection(Graphics2D g, SequenceGroup group,
711           int canvasWidth,
712           int canvasHeight, int startRes)
713   {
714     // height gap above each panel
715     int hgap = charHeight;
716     if (av.getScaleAboveWrapped())
717     {
718       hgap += charHeight;
719     }
720
721     int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
722             / charWidth;
723     int cHeight = av.getAlignment().getHeight() * charHeight;
724
725     int startx = startRes;
726     int endx;
727     int ypos = hgap; // vertical offset
728     int maxwidth = av.getAlignment().getWidth();
729
730     if (av.hasHiddenColumns())
731     {
732       maxwidth = av.getAlignment().getHiddenColumns()
733               .findColumnPosition(maxwidth);
734     }
735
736     // chop the wrapped alignment extent up into panel-sized blocks and treat
737     // each block as if it were a block from an unwrapped alignment
738     while ((ypos <= canvasHeight) && (startx < maxwidth))
739     {
740       // set end value to be start + width, or maxwidth, whichever is smaller
741       endx = startx + cWidth - 1;
742
743       if (endx > maxwidth)
744       {
745         endx = maxwidth;
746       }
747
748       g.translate(labelWidthWest, 0);
749
750       drawUnwrappedSelection(g, group, startx, endx, 0,
751               av.getAlignment().getHeight() - 1,
752               ypos);
753
754       g.translate(-labelWidthWest, 0);
755
756       // update vertical offset
757       ypos += cHeight + getAnnotationHeight() + hgap;
758
759       // update horizontal offset
760       startx += cWidth;
761     }
762   }
763
764   int getAnnotationHeight()
765   {
766     if (!av.isShowAnnotation())
767     {
768       return 0;
769     }
770
771     if (annotations == null)
772     {
773       annotations = new AnnotationPanel(av);
774     }
775
776     return annotations.adjustPanelHeight();
777   }
778
779
780   /**
781    * Draws the visible region of the alignment on the graphics context. If there
782    * are hidden column markers in the visible region, then each sub-region
783    * between the markers is drawn separately, followed by the hidden column
784    * marker.
785    * 
786    * @param g1
787    *          Graphics object to draw with
788    * @param startRes
789    *          offset of the first column in the visible region (0..)
790    * @param endRes
791    *          offset of the last column in the visible region (0..)
792    * @param startSeq
793    *          offset of the first sequence in the visible region (0..)
794    * @param endSeq
795    *          offset of the last sequence in the visible region (0..)
796    * @param yOffset
797    *          vertical offset at which to draw (for wrapped alignments)
798    */
799   private void drawPanel(Graphics g1, int startRes, int endRes,
800           int startSeq, int endSeq, int yOffset)
801
802   {
803     updateViewport();
804     if (!av.hasHiddenColumns())
805     {
806       draw(g1, startRes, endRes, startSeq, endSeq, yOffset);
807     }
808     else
809     {
810       int screenY = 0;
811       final int screenYMax = endRes - startRes;
812       int blockStart = startRes;
813       int blockEnd = endRes;
814
815       for (int[] region : av.getAlignment().getHiddenColumns()
816               .getHiddenColumnsCopy())
817       {
818         int hideStart = region[0];
819         int hideEnd = region[1];
820
821         if (hideStart <= blockStart)
822         {
823           blockStart += (hideEnd - hideStart) + 1;
824           continue;
825         }
826
827         /*
828          * draw up to just before the next hidden region, or the end of
829          * the visible region, whichever comes first
830          */
831         blockEnd = Math.min(hideStart - 1, blockStart + screenYMax
832                 - screenY);
833
834         g1.translate(screenY * charWidth, 0);
835
836         draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
837
838         /*
839          * draw the downline of the hidden column marker (ScalePanel draws the
840          * triangle on top) if we reached it
841          */
842         if (av.getShowHiddenMarkers() && blockEnd == hideStart - 1)
843         {
844           g1.setColor(Color.blue);
845
846           g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
847                   0 + yOffset, (blockEnd - blockStart + 1) * charWidth - 1,
848                   (endSeq - startSeq + 1) * charHeight + yOffset);
849         }
850
851         g1.translate(-screenY * charWidth, 0);
852         screenY += blockEnd - blockStart + 1;
853         blockStart = hideEnd + 1;
854
855         if (screenY > screenYMax)
856         {
857           // already rendered last block
858           return;
859         }
860       }
861
862       if (screenY <= screenYMax)
863       {
864         // remaining visible region to render
865         blockEnd = blockStart + screenYMax - screenY;
866         g1.translate(screenY * charWidth, 0);
867         draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
868
869         g1.translate(-screenY * charWidth, 0);
870       }
871     }
872
873   }
874
875
876   /**
877    * Draws a region of the visible alignment
878    * 
879    * @param g1
880    * @param startRes
881    *          offset of the first column in the visible region (0..)
882    * @param endRes
883    *          offset of the last column in the visible region (0..)
884    * @param startSeq
885    *          offset of the first sequence in the visible region (0..)
886    * @param endSeq
887    *          offset of the last sequence in the visible region (0..)
888    * @param yOffset
889    *          vertical offset at which to draw (for wrapped alignments)
890    */
891   private void draw(Graphics g, int startRes, int endRes, int startSeq,
892           int endSeq, int offset)
893   {
894     g.setFont(av.getFont());
895     seqRdr.prepare(g, av.isRenderGaps());
896
897     SequenceI nextSeq;
898
899     // / First draw the sequences
900     // ///////////////////////////
901     for (int i = startSeq; i <= endSeq; i++)
902     {
903       nextSeq = av.getAlignment().getSequenceAt(i);
904       if (nextSeq == null)
905       {
906         // occasionally, a race condition occurs such that the alignment row is
907         // empty
908         continue;
909       }
910       seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
911               startRes, endRes, offset + ((i - startSeq) * charHeight));
912
913       if (av.isShowSequenceFeatures())
914       {
915         fr.drawSequence(g, nextSeq, startRes, endRes,
916                 offset + ((i - startSeq) * charHeight), false);
917       }
918
919       /*
920        * highlight search Results once sequence has been drawn
921        */
922       if (av.hasSearchResults())
923       {
924         SearchResultsI searchResults = av.getSearchResults();
925         int[] visibleResults = searchResults.getResults(nextSeq,
926                 startRes, endRes);
927         if (visibleResults != null)
928         {
929           for (int r = 0; r < visibleResults.length; r += 2)
930           {
931             seqRdr.drawHighlightedText(nextSeq, visibleResults[r],
932                     visibleResults[r + 1], (visibleResults[r] - startRes)
933                             * charWidth, offset
934                             + ((i - startSeq) * charHeight));
935           }
936         }
937       }
938
939       if (av.cursorMode && cursorY == i && cursorX >= startRes
940               && cursorX <= endRes)
941       {
942         seqRdr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
943                 offset + ((i - startSeq) * charHeight));
944       }
945     }
946
947     if (av.getSelectionGroup() != null
948             || av.getAlignment().getGroups().size() > 0)
949     {
950       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
951     }
952
953   }
954
955   void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
956           int startSeq, int endSeq, int offset)
957   {
958     Graphics2D g = (Graphics2D) g1;
959     //
960     // ///////////////////////////////////
961     // Now outline any areas if necessary
962     // ///////////////////////////////////
963
964     SequenceGroup group = null;
965     int groupIndex = -1;
966
967     if (av.getAlignment().getGroups().size() > 0)
968     {
969       group = av.getAlignment().getGroups().get(0);
970       groupIndex = 0;
971     }
972
973     if (group != null)
974     {
975       g.setStroke(new BasicStroke());
976       g.setColor(group.getOutlineColour());
977       
978       do
979       {
980         drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
981                 endSeq, offset);
982         
983         groupIndex++;
984
985         g.setStroke(new BasicStroke());
986
987         if (groupIndex >= av.getAlignment().getGroups().size())
988         {
989           break;
990         }
991
992         group = av.getAlignment().getGroups().get(groupIndex);
993
994       } while (groupIndex < av.getAlignment().getGroups().size());
995
996     }
997
998   }
999
1000
1001   /*
1002    * Draw the selection group as a separate image and overlay
1003    */
1004   private BufferedImage drawSelectionGroup(int startRes, int endRes,
1005           int startSeq, int endSeq)
1006   {
1007     // get a new image of the correct size
1008     BufferedImage selectionImage = setupImage();
1009
1010     if (selectionImage == null)
1011     {
1012       return null;
1013     }
1014
1015     SequenceGroup group = av.getSelectionGroup();
1016     if (group == null)
1017     {
1018       // nothing to draw
1019       return null;
1020     }
1021
1022     // set up drawing colour
1023     Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1024
1025     // set background to transparent
1026     g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1027     g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1028
1029     // set up foreground to draw red dashed line
1030     g.setComposite(AlphaComposite.Src);
1031     g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1032             BasicStroke.JOIN_ROUND, 3f, new float[]
1033     { 5f, 3f }, 0f));
1034     g.setColor(Color.RED);
1035
1036     if (!av.getWrapAlignment())
1037     {
1038       drawUnwrappedSelection(g, group, startRes, endRes, startSeq, endSeq,
1039               0);
1040     }
1041     else
1042     {
1043       drawWrappedSelection(g, group, getWidth(), getHeight(),
1044               av.getRanges().getStartRes());
1045     }
1046
1047     g.dispose();
1048     return selectionImage;
1049   }
1050
1051   /*
1052    * Draw a selection group over an unwrapped alignment
1053    * @param g graphics object to draw with
1054    * @param group selection group
1055    * @param startRes start residue of area to draw
1056    * @param endRes end residue of area to draw
1057    * @param startSeq start sequence of area to draw
1058    * @param endSeq end sequence of area to draw
1059    * @param offset vertical offset (used when called from wrapped alignment code)
1060    */
1061   private void drawUnwrappedSelection(Graphics2D g, SequenceGroup group,
1062           int startRes, int endRes, int startSeq, int endSeq, int offset)
1063   {
1064     if (!av.hasHiddenColumns())
1065     {
1066       drawPartialGroupOutline(g, group, startRes, endRes, startSeq, endSeq,
1067               offset);
1068     }
1069     else
1070     {
1071       // package into blocks of visible columns
1072       int screenY = 0;
1073       int blockStart = startRes;
1074       int blockEnd = endRes;
1075
1076       for (int[] region : av.getAlignment().getHiddenColumns()
1077               .getHiddenColumnsCopy())
1078       {
1079         int hideStart = region[0];
1080         int hideEnd = region[1];
1081
1082         if (hideStart <= blockStart)
1083         {
1084           blockStart += (hideEnd - hideStart) + 1;
1085           continue;
1086         }
1087
1088         blockEnd = hideStart - 1;
1089
1090         g.translate(screenY * charWidth, 0);
1091         drawPartialGroupOutline(g, group,
1092                 blockStart, blockEnd, startSeq, endSeq, offset);
1093
1094         g.translate(-screenY * charWidth, 0);
1095         screenY += blockEnd - blockStart + 1;
1096         blockStart = hideEnd + 1;
1097
1098         if (screenY > (endRes - startRes))
1099         {
1100           // already rendered last block
1101           break;
1102         }
1103       }
1104
1105       if (screenY <= (endRes - startRes))
1106       {
1107         // remaining visible region to render
1108         blockEnd = blockStart + (endRes - startRes) - screenY;
1109         g.translate(screenY * charWidth, 0);
1110         drawPartialGroupOutline(g, group,
1111                 blockStart, blockEnd, startSeq, endSeq, offset);
1112         
1113         g.translate(-screenY * charWidth, 0);
1114       }
1115     }
1116   }
1117
1118   /*
1119    * Draw the selection group as a separate image and overlay
1120    */
1121   private void drawPartialGroupOutline(Graphics2D g, SequenceGroup group,
1122           int startRes, int endRes, int startSeq, int endSeq,
1123           int verticalOffset)
1124   {
1125     int visWidth = (endRes - startRes + 1) * charWidth;
1126
1127     int oldY = -1;
1128     int i = 0;
1129     boolean inGroup = false;
1130     int top = -1;
1131     int bottom = -1;
1132
1133     int sx = -1;
1134     int sy = -1;
1135     int xwidth = -1;
1136
1137     for (i = startSeq; i <= endSeq; i++)
1138     {
1139       // position of start residue of group relative to startRes, in pixels
1140       sx = (group.getStartRes() - startRes) * charWidth;
1141
1142       // width of group in pixels
1143       xwidth = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth)
1144               - 1;
1145
1146       sy = verticalOffset + (i - startSeq) * charHeight;
1147
1148       if (sx + xwidth < 0 || sx > visWidth)
1149       {
1150         continue;
1151       }
1152
1153       if ((sx <= (endRes - startRes) * charWidth)
1154               && group.getSequences(null)
1155                       .contains(av.getAlignment().getSequenceAt(i)))
1156       {
1157         if ((bottom == -1) && !group.getSequences(null)
1158                 .contains(av.getAlignment().getSequenceAt(i + 1)))
1159         {
1160           bottom = sy + charHeight;
1161         }
1162
1163         if (!inGroup)
1164         {
1165           if (((top == -1) && (i == 0)) || !group.getSequences(null)
1166                   .contains(av.getAlignment().getSequenceAt(i - 1)))
1167           {
1168             top = sy;
1169           }
1170
1171           oldY = sy;
1172           inGroup = true;
1173         }
1174       }
1175       else
1176       {
1177         if (inGroup)
1178         {
1179           // if start position is visible, draw vertical line to left of
1180           // group
1181           if (sx >= 0 && sx < visWidth)
1182           {
1183             g.drawLine(sx, oldY, sx, sy);
1184           }
1185
1186           // if end position is visible, draw vertical line to right of
1187           // group
1188           if (sx + xwidth < visWidth)
1189           {
1190             g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1191           }
1192
1193           if (sx < 0)
1194           {
1195             xwidth += sx;
1196             sx = 0;
1197           }
1198
1199           // don't let width extend beyond current block, or group extent
1200           // fixes JAL-2672
1201           if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
1202           {
1203             xwidth = (endRes - startRes + 1) * charWidth - sx;
1204           }
1205           
1206           // draw horizontal line at top of group
1207           if (top != -1)
1208           {
1209             g.drawLine(sx, top, sx + xwidth, top);
1210             top = -1;
1211           }
1212
1213           // draw horizontal line at bottom of group
1214           if (bottom != -1)
1215           {
1216             g.drawLine(sx, bottom, sx + xwidth, bottom);
1217             bottom = -1;
1218           }
1219
1220           inGroup = false;
1221         }
1222       }
1223     }
1224
1225     if (inGroup)
1226     {
1227       sy = verticalOffset + ((i - startSeq) * charHeight);
1228       if (sx >= 0 && sx < visWidth)
1229       {
1230         g.drawLine(sx, oldY, sx, sy);
1231       }
1232
1233       if (sx + xwidth < visWidth)
1234       {
1235         g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1236       }
1237
1238       if (sx < 0)
1239       {
1240         xwidth += sx;
1241         sx = 0;
1242       }
1243
1244       if (sx + xwidth > visWidth)
1245       {
1246         xwidth = visWidth;
1247       }
1248       else if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
1249       {
1250         xwidth = (endRes - startRes + 1) * charWidth;
1251       }
1252
1253       if (top != -1)
1254       {
1255         g.drawLine(sx, top, sx + xwidth, top);
1256         top = -1;
1257       }
1258
1259       if (bottom != -1)
1260       {
1261         g.drawLine(sx, bottom - 1, sx + xwidth, bottom - 1);
1262         bottom = -1;
1263       }
1264
1265       inGroup = false;
1266     }
1267   }
1268   
1269   /**
1270    * Highlights search results in the visible region by rendering as white text
1271    * on a black background. Any previous highlighting is removed. Answers true
1272    * if any highlight was left on the visible alignment (so status bar should be
1273    * set to match), else false.
1274    * <p>
1275    * Currently fastPaint is not implemented for wrapped alignments. If a wrapped
1276    * alignment had to be scrolled to show the highlighted region, then it should
1277    * be fully redrawn, otherwise a fast paint can be performed. This argument
1278    * could be removed if fast paint of scrolled wrapped alignment is coded in
1279    * future (JAL-2609).
1280    * 
1281    * @param results
1282    * @param noFastPaint
1283    * @return
1284    */
1285   public boolean highlightSearchResults(SearchResultsI results,
1286           boolean noFastPaint)
1287   {
1288     if (fastpainting)
1289     {
1290       return false;
1291     }
1292     boolean wrapped = av.getWrapAlignment();
1293
1294     try
1295     {
1296       fastPaint = !noFastPaint;
1297       fastpainting = fastPaint;
1298
1299       updateViewport();
1300
1301       /*
1302        * to avoid redrawing the whole visible region, we instead
1303        * redraw just the minimal regions to remove previous highlights
1304        * and add new ones
1305        */
1306       SearchResultsI previous = av.getSearchResults();
1307       av.setSearchResults(results);
1308       boolean redrawn = false;
1309       boolean drawn = false;
1310       if (wrapped)
1311       {
1312         redrawn = drawMappedPositionsWrapped(previous);
1313         drawn = drawMappedPositionsWrapped(results);
1314         redrawn |= drawn;
1315       }
1316       else
1317       {
1318         redrawn = drawMappedPositions(previous);
1319         drawn = drawMappedPositions(results);
1320         redrawn |= drawn;
1321       }
1322
1323       /*
1324        * if highlights were either removed or added, repaint
1325        */
1326       if (redrawn)
1327       {
1328         repaint();
1329       }
1330
1331       /*
1332        * return true only if highlights were added
1333        */
1334       return drawn;
1335
1336     } finally
1337     {
1338       fastpainting = false;
1339     }
1340   }
1341
1342   /**
1343    * Redraws the minimal rectangle in the visible region (if any) that includes
1344    * mapped positions of the given search results. Whether or not positions are
1345    * highlighted depends on the SearchResults set on the Viewport. This allows
1346    * this method to be called to either clear or set highlighting. Answers true
1347    * if any positions were drawn (in which case a repaint is still required),
1348    * else false.
1349    * 
1350    * @param results
1351    * @return
1352    */
1353   protected boolean drawMappedPositions(SearchResultsI results)
1354   {
1355     if (results == null)
1356     {
1357       return false;
1358     }
1359
1360     /*
1361      * calculate the minimal rectangle to redraw that 
1362      * includes both new and existing search results
1363      */
1364     int firstSeq = Integer.MAX_VALUE;
1365     int lastSeq = -1;
1366     int firstCol = Integer.MAX_VALUE;
1367     int lastCol = -1;
1368     boolean matchFound = false;
1369
1370     ViewportRanges ranges = av.getRanges();
1371     int firstVisibleColumn = ranges.getStartRes();
1372     int lastVisibleColumn = ranges.getEndRes();
1373     AlignmentI alignment = av.getAlignment();
1374     if (av.hasHiddenColumns())
1375     {
1376       firstVisibleColumn = alignment.getHiddenColumns()
1377               .adjustForHiddenColumns(firstVisibleColumn);
1378       lastVisibleColumn = alignment.getHiddenColumns()
1379               .adjustForHiddenColumns(lastVisibleColumn);
1380     }
1381
1382     for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1383             .getEndSeq(); seqNo++)
1384     {
1385       SequenceI seq = alignment.getSequenceAt(seqNo);
1386
1387       int[] visibleResults = results.getResults(seq, firstVisibleColumn,
1388               lastVisibleColumn);
1389       if (visibleResults != null)
1390       {
1391         for (int i = 0; i < visibleResults.length - 1; i += 2)
1392         {
1393           int firstMatchedColumn = visibleResults[i];
1394           int lastMatchedColumn = visibleResults[i + 1];
1395           if (firstMatchedColumn <= lastVisibleColumn
1396                   && lastMatchedColumn >= firstVisibleColumn)
1397           {
1398             /*
1399              * found a search results match in the visible region - 
1400              * remember the first and last sequence matched, and the first
1401              * and last visible columns in the matched positions
1402              */
1403             matchFound = true;
1404             firstSeq = Math.min(firstSeq, seqNo);
1405             lastSeq = Math.max(lastSeq, seqNo);
1406             firstMatchedColumn = Math.max(firstMatchedColumn,
1407                     firstVisibleColumn);
1408             lastMatchedColumn = Math.min(lastMatchedColumn,
1409                     lastVisibleColumn);
1410             firstCol = Math.min(firstCol, firstMatchedColumn);
1411             lastCol = Math.max(lastCol, lastMatchedColumn);
1412           }
1413         }
1414       }
1415     }
1416
1417     if (matchFound)
1418     {
1419       if (av.hasHiddenColumns())
1420       {
1421         firstCol = alignment.getHiddenColumns()
1422                 .findColumnPosition(firstCol);
1423         lastCol = alignment.getHiddenColumns().findColumnPosition(lastCol);
1424       }
1425       int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
1426       int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
1427       gg.translate(transX, transY);
1428       drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
1429       gg.translate(-transX, -transY);
1430     }
1431
1432     return matchFound;
1433   }
1434
1435   @Override
1436   public void propertyChange(PropertyChangeEvent evt)
1437   {
1438     String eventName = evt.getPropertyName();
1439
1440     if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1441     {
1442       fastPaint = true;
1443       repaint();
1444     }
1445     else if (av.getWrapAlignment())
1446     {
1447       if (eventName.equals(ViewportRanges.STARTRES))
1448       {
1449         repaint();
1450       }
1451     }
1452     else
1453     {
1454       int scrollX = 0;
1455       if (eventName.equals(ViewportRanges.STARTRES))
1456       {
1457         // Make sure we're not trying to draw a panel
1458         // larger than the visible window
1459         ViewportRanges vpRanges = av.getRanges();
1460         scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1461         int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1462         if (scrollX > range)
1463         {
1464           scrollX = range;
1465         }
1466         else if (scrollX < -range)
1467         {
1468           scrollX = -range;
1469         }
1470       }
1471
1472       // Both scrolling and resizing change viewport ranges: scrolling changes
1473       // both start and end points, but resize only changes end values.
1474       // Here we only want to fastpaint on a scroll, with resize using a normal
1475       // paint, so scroll events are identified as changes to the horizontal or
1476       // vertical start value.
1477       if (eventName.equals(ViewportRanges.STARTRES))
1478       {
1479         // scroll - startres and endres both change
1480         fastPaint(scrollX, 0);
1481       }
1482       else if (eventName.equals(ViewportRanges.STARTSEQ))
1483       {
1484         // scroll
1485         fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1486       }
1487     }
1488   }
1489
1490   /**
1491    * Redraws any positions in the search results in the visible region of a
1492    * wrapped alignment. Any highlights are drawn depending on the search results
1493    * set on the Viewport, not the <code>results</code> argument. This allows
1494    * this method to be called either to clear highlights (passing the previous
1495    * search results), or to draw new highlights.
1496    * 
1497    * @param results
1498    * @return
1499    */
1500   protected boolean drawMappedPositionsWrapped(SearchResultsI results)
1501   {
1502     if (results == null)
1503     {
1504       return false;
1505     }
1506   
1507     boolean matchFound = false;
1508
1509     int wrappedWidth = av.getWrappedWidth();
1510     int wrappedHeight = getRepeatHeightWrapped();
1511
1512     ViewportRanges ranges = av.getRanges();
1513     int canvasHeight = getHeight();
1514     int repeats = canvasHeight / wrappedHeight;
1515     if (canvasHeight / wrappedHeight > 0)
1516     {
1517       repeats++;
1518     }
1519
1520     int firstVisibleColumn = ranges.getStartRes();
1521     int lastVisibleColumn = ranges.getStartRes() + repeats
1522             * ranges.getViewportWidth() - 1;
1523
1524     AlignmentI alignment = av.getAlignment();
1525     if (av.hasHiddenColumns())
1526     {
1527       firstVisibleColumn = alignment.getHiddenColumns()
1528               .adjustForHiddenColumns(firstVisibleColumn);
1529       lastVisibleColumn = alignment.getHiddenColumns()
1530               .adjustForHiddenColumns(lastVisibleColumn);
1531     }
1532
1533     int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
1534
1535     for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1536             .getEndSeq(); seqNo++)
1537     {
1538       SequenceI seq = alignment.getSequenceAt(seqNo);
1539
1540       int[] visibleResults = results.getResults(seq, firstVisibleColumn,
1541               lastVisibleColumn);
1542       if (visibleResults != null)
1543       {
1544         for (int i = 0; i < visibleResults.length - 1; i += 2)
1545         {
1546           int firstMatchedColumn = visibleResults[i];
1547           int lastMatchedColumn = visibleResults[i + 1];
1548           if (firstMatchedColumn <= lastVisibleColumn
1549                   && lastMatchedColumn >= firstVisibleColumn)
1550           {
1551             /*
1552              * found a search results match in the visible region
1553              */
1554             firstMatchedColumn = Math.max(firstMatchedColumn,
1555                     firstVisibleColumn);
1556             lastMatchedColumn = Math.min(lastMatchedColumn,
1557                     lastVisibleColumn);
1558
1559             /*
1560              * draw each mapped position separately (as contiguous positions may
1561              * wrap across lines)
1562              */
1563             for (int mappedPos = firstMatchedColumn; mappedPos <= lastMatchedColumn; mappedPos++)
1564             {
1565               int displayColumn = mappedPos;
1566               if (av.hasHiddenColumns())
1567               {
1568                 displayColumn = alignment.getHiddenColumns()
1569                         .findColumnPosition(displayColumn);
1570               }
1571
1572               /*
1573                * transX: offset from left edge of canvas to residue position
1574                */
1575               int transX = labelWidthWest
1576                       + ((displayColumn - ranges.getStartRes()) % wrappedWidth)
1577                       * av.getCharWidth();
1578
1579               /*
1580                * transY: offset from top edge of canvas to residue position
1581                */
1582               int transY = gapHeight;
1583               transY += (displayColumn - ranges.getStartRes())
1584                       / wrappedWidth * wrappedHeight;
1585               transY += (seqNo - ranges.getStartSeq()) * av.getCharHeight();
1586
1587               /*
1588                * yOffset is from graphics origin to start of visible region
1589                */
1590               int yOffset = 0;// (displayColumn / wrappedWidth) * wrappedHeight;
1591               if (transY < getHeight())
1592               {
1593                 matchFound = true;
1594                 gg.translate(transX, transY);
1595                 drawPanel(gg, displayColumn, displayColumn, seqNo, seqNo,
1596                         yOffset);
1597                 gg.translate(-transX, -transY);
1598               }
1599             }
1600           }
1601         }
1602       }
1603     }
1604   
1605     return matchFound;
1606   }
1607
1608   /**
1609    * Answers the height in pixels of a repeating section of the wrapped
1610    * alignment, including space above, scale above if shown, sequences, and
1611    * annotation panel if shown
1612    * 
1613    * @return
1614    */
1615   protected int getRepeatHeightWrapped()
1616   {
1617     // gap (and maybe scale) above
1618     int repeatHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
1619
1620     // add sequences
1621     repeatHeight += av.getRanges().getViewportHeight() * charHeight;
1622
1623     // add annotations panel height if shown
1624     repeatHeight += getAnnotationHeight();
1625
1626     return repeatHeight;
1627   }
1628 }