JAL-2778 try another way to draw sequences with reduced synchronisation
[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.util.Comparison;
31 import jalview.viewmodel.ViewportListenerI;
32 import jalview.viewmodel.ViewportRanges;
33
34 import java.awt.AlphaComposite;
35 import java.awt.BasicStroke;
36 import java.awt.BorderLayout;
37 import java.awt.Color;
38 import java.awt.FontMetrics;
39 import java.awt.Graphics;
40 import java.awt.Graphics2D;
41 import java.awt.RenderingHints;
42 import java.awt.Shape;
43 import java.awt.image.BufferedImage;
44 import java.beans.PropertyChangeEvent;
45 import java.util.List;
46 import java.util.function.Consumer;
47
48 import javax.swing.JComponent;
49
50 /**
51  * The Swing component on which the alignment sequences, and annotations (if
52  * shown), are drawn. This includes scales above, left and right (if shown) in
53  * Wrapped mode, but not the scale above in Unwrapped mode.
54  * 
55  */
56 public class SeqCanvas extends JComponent implements ViewportListenerI
57 {
58   private static final String ZEROS = "0000000000";
59
60   final FeatureRenderer fr;
61
62   BufferedImage img;
63
64   AlignViewport av;
65
66   int cursorX = 0;
67
68   int cursorY = 0;
69
70   private final SequenceRenderer seqRdr;
71
72   private boolean fastPaint = false;
73
74   private boolean fastpainting = false;
75
76   private AnnotationPanel annotations;
77
78   /*
79    * measurements for drawing a wrapped alignment
80    */
81   private int labelWidthEast; // label right width in pixels if shown
82
83   private int labelWidthWest; // label left width in pixels if shown
84
85   private int wrappedSpaceAboveAlignment; // gap between widths
86
87   private int wrappedRepeatHeightPx; // height in pixels of wrapped width
88
89   private int wrappedVisibleWidths; // number of wrapped widths displayed
90
91   private Graphics2D gg;
92
93   /**
94    * Creates a new SeqCanvas object.
95    * 
96    * @param ap
97    */
98   public SeqCanvas(AlignmentPanel ap)
99   {
100     this.av = ap.av;
101     fr = new FeatureRenderer(ap);
102     seqRdr = new SequenceRenderer(av);
103     setLayout(new BorderLayout());
104     PaintRefresher.Register(this, av.getSequenceSetId());
105     setBackground(Color.white);
106
107     av.getRanges().addPropertyChangeListener(this);
108   }
109
110   public SequenceRenderer getSequenceRenderer()
111   {
112     return seqRdr;
113   }
114
115   public FeatureRenderer getFeatureRenderer()
116   {
117     return fr;
118   }
119
120   /**
121    * Draws the scale above a region of a wrapped alignment, consisting of a
122    * column number every major interval (10 columns).
123    * 
124    * @param g
125    *          the graphics context to draw on, positioned at the start (bottom
126    *          left) of the line on which to draw any scale marks
127    * @param startx
128    *          start alignment column (0..)
129    * @param endx
130    *          end alignment column (0..)
131    * @param ypos
132    *          y offset to draw at
133    */
134   private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
135   {
136     int charHeight = av.getCharHeight();
137     int charWidth = av.getCharWidth();
138
139     /*
140      * white fill the scale space (for the fastPaint case)
141      */
142     g.setColor(Color.white);
143     g.fillRect(0, ypos - charHeight - charHeight / 2, getWidth(),
144             charHeight * 3 / 2 + 2);
145     g.setColor(Color.black);
146
147     List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
148             endx);
149     for (ScaleMark mark : marks)
150     {
151       int mpos = mark.column; // (i - startx - 1)
152       if (mpos < 0)
153       {
154         continue;
155       }
156       String mstring = mark.text;
157
158       if (mark.major)
159       {
160         if (mstring != null)
161         {
162           g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
163         }
164
165         /*
166          * draw a tick mark below the column number, centred on the column;
167          * height of tick mark is 4 pixels less than half a character
168          */
169         int xpos = (mpos * charWidth) + (charWidth / 2);
170         g.drawLine(xpos, (ypos + 2) - (charHeight / 2), xpos, ypos - 2);
171       }
172     }
173   }
174
175   /**
176    * Draw the scale to the left or right of a wrapped alignment
177    * 
178    * @param g
179    *          graphics context, positioned at the start of the scale to be drawn
180    * @param startx
181    *          first column of wrapped width (0.. excluding any hidden columns)
182    * @param endx
183    *          last column of wrapped width (0.. excluding any hidden columns)
184    * @param ypos
185    *          vertical offset at which to begin the scale
186    * @param left
187    *          if true, scale is left of residues, if false, scale is right
188    */
189   void drawVerticalScale(Graphics g, final int startx, final int endx,
190           final int ypos, final boolean left)
191   {
192     int charHeight = av.getCharHeight();
193     int charWidth = av.getCharWidth();
194
195     int yPos = ypos + charHeight;
196     int startX = startx;
197     int endX = endx;
198
199     if (av.hasHiddenColumns())
200     {
201       HiddenColumns hiddenColumns = av.getAlignment().getHiddenColumns();
202       startX = hiddenColumns.adjustForHiddenColumns(startx);
203       endX = hiddenColumns.adjustForHiddenColumns(endx);
204     }
205     FontMetrics fm = getFontMetrics(av.getFont());
206
207     for (int i = 0; i < av.getAlignment().getHeight(); i++)
208     {
209       SequenceI seq = av.getAlignment().getSequenceAt(i);
210
211       /*
212        * find sequence position of first non-gapped position -
213        * to the right if scale left, to the left if scale right
214        */
215       int index = left ? startX : endX;
216       int value = -1;
217       while (index >= startX && index <= endX)
218       {
219         if (!Comparison.isGap(seq.getCharAt(index)))
220         {
221           value = seq.findPosition(index);
222           break;
223         }
224         if (left)
225         {
226           index++;
227         }
228         else
229         {
230           index--;
231         }
232       }
233
234       /*
235        * white fill the space for the scale
236        */
237       g.setColor(Color.white);
238       int y = (yPos + (i * charHeight)) - (charHeight / 5);
239       // fillRect origin is top left of rectangle
240       g.fillRect(0, y - charHeight, left ? labelWidthWest : labelWidthEast,
241               charHeight + 1);
242
243       if (value != -1)
244       {
245         /*
246          * draw scale value, right justified within its width less half a
247          * character width padding on the right
248          */
249         int labelSpace = left ? labelWidthWest : labelWidthEast;
250         labelSpace -= charWidth / 2; // leave space to the right
251         String valueAsString = String.valueOf(value);
252         int labelLength = fm.stringWidth(valueAsString);
253         int xOffset = labelSpace - labelLength;
254         g.setColor(Color.black);
255         g.drawString(valueAsString, xOffset, y);
256       }
257     }
258   }
259
260   /**
261    * Does a fast paint of an alignment in response to a scroll. Most of the
262    * visible region is simply copied and shifted, and then any newly visible
263    * columns or rows are drawn. The scroll may be horizontal or vertical, but
264    * not both at once. Scrolling may be the result of
265    * <ul>
266    * <li>dragging a scroll bar</li>
267    * <li>clicking in the scroll bar</li>
268    * <li>scrolling by trackpad, middle mouse button, or other device</li>
269    * <li>by moving the box in the Overview window</li>
270    * <li>programmatically to make a highlighted position visible</li>
271    * </ul>
272    * 
273    * @param horizontal
274    *          columns to shift right (positive) or left (negative)
275    * @param vertical
276    *          rows to shift down (positive) or up (negative)
277    */
278   public void fastPaint(int horizontal, int vertical)
279   {
280     if (fastpainting || gg == null || img == null)
281     {
282       return;
283     }
284     fastpainting = true;
285     fastPaint = true;
286
287     try
288     {
289       int charHeight = av.getCharHeight();
290       int charWidth = av.getCharWidth();
291     
292       ViewportRanges ranges = av.getRanges();
293       int startRes = ranges.getStartRes();
294       int endRes = ranges.getEndRes();
295       int startSeq = ranges.getStartSeq();
296       int endSeq = ranges.getEndSeq();
297       int transX = 0;
298       int transY = 0;
299       
300       gg.copyArea(horizontal * charWidth, vertical * charHeight,
301               img.getWidth(), img.getHeight(), -horizontal * charWidth,
302               -vertical * charHeight);
303
304       if (horizontal > 0) // scrollbar pulled right, image to the left
305       {
306         transX = (endRes - startRes - horizontal) * charWidth;
307         startRes = endRes - horizontal;
308       }
309       else if (horizontal < 0)
310       {
311         endRes = startRes - horizontal;
312       }
313
314       if (vertical > 0) // scroll down
315       {
316         startSeq = endSeq - vertical;
317
318         if (startSeq < ranges.getStartSeq())
319         { // ie scrolling too fast, more than a page at a time
320           startSeq = ranges.getStartSeq();
321         }
322         else
323         {
324           transY = img.getHeight() - ((vertical + 1) * charHeight);
325         }
326       }
327       else if (vertical < 0)
328       {
329         endSeq = startSeq - vertical;
330
331         if (endSeq > ranges.getEndSeq())
332         {
333           endSeq = ranges.getEndSeq();
334         }
335       }
336
337       gg.translate(transX, transY);
338       drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
339       gg.translate(-transX, -transY);
340
341       // Call repaint on alignment panel so that repaints from other alignment
342       // panel components can be aggregated. Otherwise performance of the
343       // overview window and others may be adversely affected.
344       av.getAlignPanel().repaint();
345     } finally
346     {
347       fastpainting = false;
348     }
349   }
350
351   @Override
352   public void paintComponent(Graphics g)
353   {
354     super.paintComponent(g);    
355     
356     int charHeight = av.getCharHeight();
357     int charWidth = av.getCharWidth();
358     
359     ViewportRanges ranges = av.getRanges();
360     
361     int width = getWidth();
362     int height = getHeight();
363     
364     width -= (width % charWidth);
365     height -= (height % charHeight);
366     
367     // selectImage is the selection group outline image
368     BufferedImage selectImage = drawSelectionGroup(
369             ranges.getStartRes(), ranges.getEndRes(),
370             ranges.getStartSeq(), ranges.getEndSeq());
371     
372     if ((img != null) && (fastPaint
373             || (getVisibleRect().width != g.getClipBounds().width)
374             || (getVisibleRect().height != g.getClipBounds().height)))
375     {
376       BufferedImage lcimg = buildLocalImage(selectImage);
377       g.drawImage(lcimg, 0, 0, this);
378       fastPaint = false;
379     }
380     else if ((width > 0) && (height > 0))
381     {
382       // img is a cached version of the last view we drew, if any
383       // if we have no img or the size has changed, make a new one
384       if (img == null || width != img.getWidth()
385               || height != img.getHeight())
386       {
387         img = setupImage();
388         if (img == null)
389         {
390           return;
391         }
392         gg = (Graphics2D) img.getGraphics();
393         gg.setFont(av.getFont());
394       }
395     
396       if (av.antiAlias)
397       {
398         gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
399                 RenderingHints.VALUE_ANTIALIAS_ON);
400       }
401     
402       gg.setColor(Color.white);
403       gg.fillRect(0, 0, img.getWidth(), img.getHeight());
404     
405       if (av.getWrapAlignment())
406       {
407         drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
408       }
409       else
410       {
411         drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
412                 ranges.getStartSeq(), ranges.getEndSeq(), 0);
413       }
414     
415       // lcimg is a local *copy* of img which we'll draw selectImage on top of
416       BufferedImage lcimg = buildLocalImage(selectImage);
417       g.drawImage(lcimg, 0, 0, this);
418     }
419   }
420   
421   /**
422    * Draw an alignment panel for printing
423    * 
424    * @param g1
425    *          Graphics object to draw with
426    * @param startRes
427    *          start residue of print area
428    * @param endRes
429    *          end residue of print area
430    * @param startSeq
431    *          start sequence of print area
432    * @param endSeq
433    *          end sequence of print area
434    */
435   public void drawPanelForPrinting(Graphics g1, int startRes, int endRes,
436           int startSeq, int endSeq)
437   {
438     drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
439
440     BufferedImage selectImage = drawSelectionGroup(startRes, endRes,
441             startSeq, endSeq);
442     if (selectImage != null)
443     {
444       ((Graphics2D) g1).setComposite(AlphaComposite
445               .getInstance(AlphaComposite.SRC_OVER));
446       g1.drawImage(selectImage, 0, 0, this);
447     }
448   }
449
450   /**
451    * Draw a wrapped alignment panel for printing
452    * 
453    * @param g
454    *          Graphics object to draw with
455    * @param canvasWidth
456    *          width of drawing area
457    * @param canvasHeight
458    *          height of drawing area
459    * @param startRes
460    *          start residue of print area
461    */
462   public void drawWrappedPanelForPrinting(Graphics g, int canvasWidth,
463           int canvasHeight, int startRes)
464   {
465     SequenceGroup group = av.getSelectionGroup();
466
467     drawWrappedPanel(g, canvasWidth, canvasHeight, startRes);
468
469     if (group != null)
470     {
471       BufferedImage selectImage = null;
472       try
473       {
474         selectImage = new BufferedImage(canvasWidth, canvasHeight,
475                 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
476       } catch (OutOfMemoryError er)
477       {
478         System.gc();
479         System.err.println("Print image OutOfMemory Error.\n" + er);
480         new OOMWarning("Creating wrapped alignment image for printing", er);
481       }
482       if (selectImage != null)
483       {
484         Graphics2D g2 = selectImage.createGraphics();
485         setupSelectionGroup(g2, selectImage);
486         drawWrappedSelection(g2, group, canvasWidth, canvasHeight,
487                 startRes);
488
489         g2.setComposite(
490                 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
491         g.drawImage(selectImage, 0, 0, this);
492         g2.dispose();
493       }
494     }
495   }
496
497   /*
498    * Make a local image by combining the cached image img
499    * with any selection
500    */
501   private BufferedImage buildLocalImage(BufferedImage selectImage)
502   {
503     // clone the cached image
504           BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
505                     img.getType());
506
507     // BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
508     // img.getType());
509     Graphics2D g2d = lcimg.createGraphics();
510     g2d.drawImage(img, 0, 0, null);
511
512     // overlay selection group on lcimg
513     if (selectImage != null)
514     {
515       g2d.setComposite(
516               AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
517       g2d.drawImage(selectImage, 0, 0, this);
518     }
519     g2d.dispose();
520
521     return lcimg;
522   }
523
524   /*
525    * Set up a buffered image of the correct height and size for the sequence canvas
526    */
527   private BufferedImage setupImage()
528   {
529     BufferedImage lcimg = null;
530
531     int charWidth = av.getCharWidth();
532     int charHeight = av.getCharHeight();
533     
534     int width = getWidth();
535     int height = getHeight();
536
537     width -= (width % charWidth);
538     height -= (height % charHeight);
539
540     if ((width < 1) || (height < 1))
541     {
542       return null;
543     }
544
545     try
546     {
547         lcimg = new BufferedImage(width, height,
548                 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
549     } catch (OutOfMemoryError er)
550     {
551       System.gc();
552       System.err.println(
553               "Group image OutOfMemory Redraw Error.\n" + er);
554       new OOMWarning("Creating alignment image for display", er);
555
556       return null;
557     }
558
559     return lcimg;
560   }
561
562   /**
563    * Returns the visible width of the canvas in residues, after allowing for
564    * East or West scales (if shown)
565    * 
566    * @param canvasWidth
567    *          the width in pixels (possibly including scales)
568    * 
569    * @return
570    */
571   public int getWrappedCanvasWidth(int canvasWidth)
572   {
573     int charWidth = av.getCharWidth();
574
575     FontMetrics fm = getFontMetrics(av.getFont());
576
577     int labelWidth = 0;
578     
579     if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
580     {
581       labelWidth = getLabelWidth(fm);
582     }
583
584     labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
585
586     labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
587
588     return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
589   }
590
591   /**
592    * Returns a pixel width sufficient to show the largest sequence coordinate
593    * (end position) in the alignment, calculated as the FontMetrics width of
594    * zeroes "0000000" limited to the number of decimal digits to be shown (3 for
595    * 1-10, 4 for 11-99 etc). One character width is added to this, to allow for
596    * half a character width space on either side.
597    * 
598    * @param fm
599    * @return
600    */
601   protected int getLabelWidth(FontMetrics fm)
602   {
603     /*
604      * find the biggest sequence end position we need to show
605      * (note this is not necessarily the sequence length)
606      */
607     int maxWidth = 0;
608     AlignmentI alignment = av.getAlignment();
609     for (int i = 0; i < alignment.getHeight(); i++)
610     {
611       maxWidth = Math.max(maxWidth, alignment.getSequenceAt(i).getEnd());
612     }
613
614     int length = 0;
615     for (int i = maxWidth; i > 0; i /= 10)
616     {
617       length++;
618     }
619
620     return fm.stringWidth(ZEROS.substring(0, length)) + av.getCharWidth();
621   }
622
623   /**
624    * Draws as many widths of a wrapped alignment as can fit in the visible
625    * window
626    * 
627    * @param g
628    * @param canvasWidth
629    *          available width in pixels
630    * @param canvasHeight
631    *          available height in pixels
632    * @param startColumn
633    *          the first column (0...) of the alignment to draw
634    */
635   public void drawWrappedPanel(Graphics g, int canvasWidth,
636           int canvasHeight, final int startColumn)
637   {
638     int wrappedWidthInResidues = calculateWrappedGeometry(canvasWidth,
639             canvasHeight);
640
641     av.setWrappedWidth(wrappedWidthInResidues);
642
643     ViewportRanges ranges = av.getRanges();
644     ranges.setViewportStartAndWidth(startColumn, wrappedWidthInResidues);
645
646     /*
647      * draw one width at a time (including any scales or annotation shown),
648      * until we have run out of either alignment or vertical space available
649      */
650     int ypos = wrappedSpaceAboveAlignment;
651     int maxWidth = ranges.getVisibleAlignmentWidth();
652
653     int start = startColumn;
654     int currentWidth = 0;
655     while ((currentWidth < wrappedVisibleWidths) && (start < maxWidth))
656     {
657       int endColumn = Math
658               .min(maxWidth, start + wrappedWidthInResidues - 1);
659       drawWrappedWidth(g, ypos, start, endColumn, canvasHeight);
660       ypos += wrappedRepeatHeightPx;
661       start += wrappedWidthInResidues;
662       currentWidth++;
663     }
664
665     drawWrappedDecorators(g, startColumn);
666   }
667
668   /**
669    * Calculates and saves values needed when rendering a wrapped alignment.
670    * These depend on many factors, including
671    * <ul>
672    * <li>canvas width and height</li>
673    * <li>number of visible sequences, and height of annotations if shown</li>
674    * <li>font and character width</li>
675    * <li>whether scales are shown left, right or above the alignment</li>
676    * </ul>
677    * 
678    * @param canvasWidth
679    * @param canvasHeight
680    * @return the number of residue columns in each width
681    */
682   protected int calculateWrappedGeometry(int canvasWidth, int canvasHeight)
683   {
684     int charHeight = av.getCharHeight();
685
686     /*
687      * vertical space in pixels between wrapped widths of alignment
688      * - one character height, or two if scale above is drawn
689      */
690     wrappedSpaceAboveAlignment = charHeight
691             * (av.getScaleAboveWrapped() ? 2 : 1);
692
693     /*
694      * height in pixels of the wrapped widths
695      */
696     wrappedRepeatHeightPx = wrappedSpaceAboveAlignment;
697     // add sequences
698     wrappedRepeatHeightPx += av.getRanges().getViewportHeight()
699             * charHeight;
700     // add annotations panel height if shown
701     wrappedRepeatHeightPx += getAnnotationHeight();
702
703     /*
704      * number of visible widths (the last one may be part height),
705      * ensuring a part height includes at least one sequence
706      */
707     ViewportRanges ranges = av.getRanges();
708     wrappedVisibleWidths = canvasHeight / wrappedRepeatHeightPx;
709     int remainder = canvasHeight % wrappedRepeatHeightPx;
710     if (remainder >= (wrappedSpaceAboveAlignment + charHeight))
711     {
712       wrappedVisibleWidths++;
713     }
714
715     /*
716      * compute width in residues; this also sets East and West label widths
717      */
718     int wrappedWidthInResidues = getWrappedCanvasWidth(canvasWidth);
719
720     /*
721      *  limit visibleWidths to not exceed width of alignment
722      */
723     int xMax = ranges.getVisibleAlignmentWidth();
724     int startToEnd = xMax - ranges.getStartRes();
725     int maxWidths = startToEnd / wrappedWidthInResidues;
726     if (startToEnd % wrappedWidthInResidues > 0)
727     {
728       maxWidths++;
729     }
730     wrappedVisibleWidths = Math.min(wrappedVisibleWidths, maxWidths);
731
732     return wrappedWidthInResidues;
733   }
734
735   /**
736    * Draws one width of a wrapped alignment, including sequences and
737    * annnotations, if shown, but not scales or hidden column markers
738    * 
739    * @param g
740    * @param ypos
741    * @param startColumn
742    * @param endColumn
743    * @param canvasHeight
744    */
745   protected void drawWrappedWidth(Graphics g, int ypos, int startColumn,
746           int endColumn, int canvasHeight)
747   {
748     ViewportRanges ranges = av.getRanges();
749     int viewportWidth = ranges.getViewportWidth();
750
751     int endx = Math.min(startColumn + viewportWidth - 1, endColumn);
752
753     /*
754      * move right before drawing by the width of the scale left (if any)
755      * plus column offset from left margin (usually zero, but may be non-zero
756      * when fast painting is drawing just a few columns)
757      */
758     int charWidth = av.getCharWidth();
759     int xOffset = labelWidthWest
760             + ((startColumn - ranges.getStartRes()) % viewportWidth)
761             * charWidth;
762     g.translate(xOffset, 0);
763
764     // When printing we have an extra clipped region,
765     // the Printable page which we need to account for here
766     Shape clip = g.getClip();
767
768     if (clip == null)
769     {
770       g.setClip(0, 0, viewportWidth * charWidth, canvasHeight);
771     }
772     else
773     {
774       g.setClip(0, (int) clip.getBounds().getY(),
775               viewportWidth * charWidth, (int) clip.getBounds().getHeight());
776     }
777
778     /*
779      * white fill the region to be drawn (so incremental fast paint doesn't
780      * scribble over an existing image)
781      */
782     gg.setColor(Color.white);
783     gg.fillRect(0, ypos, (endx - startColumn + 1) * charWidth,
784             wrappedRepeatHeightPx);
785
786     drawPanel(g, startColumn, endx, 0, av.getAlignment().getHeight() - 1,
787             ypos);
788
789     int cHeight = av.getAlignment().getHeight() * av.getCharHeight();
790
791     if (av.isShowAnnotation())
792     {
793       g.translate(0, cHeight + ypos + 3);
794       if (annotations == null)
795       {
796         annotations = new AnnotationPanel(av);
797       }
798
799       annotations.renderer.drawComponent(annotations, av, g, -1,
800               startColumn, endx + 1);
801       g.translate(0, -cHeight - ypos - 3);
802     }
803     g.setClip(clip);
804     g.translate(-xOffset, 0);
805   }
806
807   /**
808    * Draws scales left, right and above (if shown), and any hidden column
809    * markers, on all widths of the wrapped alignment
810    * 
811    * @param g
812    * @param startColumn
813    */
814   protected void drawWrappedDecorators(Graphics g, final int startColumn)
815   {
816     int charWidth = av.getCharWidth();
817
818     g.setFont(av.getFont());
819     g.setColor(Color.black);
820
821     int ypos = wrappedSpaceAboveAlignment;
822     ViewportRanges ranges = av.getRanges();
823     int viewportWidth = ranges.getViewportWidth();
824     int maxWidth = ranges.getVisibleAlignmentWidth();
825     int widthsDrawn = 0;
826     int startCol = startColumn;
827
828     while (widthsDrawn < wrappedVisibleWidths)
829     {
830       int endColumn = Math.min(maxWidth, startCol + viewportWidth - 1);
831
832       if (av.getScaleLeftWrapped())
833       {
834         drawVerticalScale(g, startCol, endColumn - 1, ypos, true);
835       }
836
837       if (av.getScaleRightWrapped())
838       {
839         int x = labelWidthWest + viewportWidth * charWidth;
840         g.translate(x, 0);
841         drawVerticalScale(g, startCol, endColumn, ypos, false);
842         g.translate(-x, 0);
843       }
844
845       /*
846        * white fill region of scale above and hidden column markers
847        * (to support incremental fast paint of image)
848        */
849       g.translate(labelWidthWest, 0);
850       g.setColor(Color.white);
851       g.fillRect(0, ypos - wrappedSpaceAboveAlignment, viewportWidth
852               * charWidth + labelWidthWest, wrappedSpaceAboveAlignment);
853       g.setColor(Color.black);
854       g.translate(-labelWidthWest, 0);
855
856       g.translate(labelWidthWest, 0);
857
858       if (av.getScaleAboveWrapped())
859       {
860         drawNorthScale(g, startCol, endColumn, ypos);
861       }
862
863       if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
864       {
865         drawHiddenColumnMarkers(g, ypos, startCol, endColumn);
866       }
867
868       g.translate(-labelWidthWest, 0);
869
870       ypos += wrappedRepeatHeightPx;
871       startCol += viewportWidth;
872       widthsDrawn++;
873     }
874   }
875
876   /**
877    * Draws markers (triangles) above hidden column positions between startColumn
878    * and endColumn.
879    * 
880    * @param g
881    * @param ypos
882    * @param startColumn
883    * @param endColumn
884    */
885   protected void drawHiddenColumnMarkers(Graphics g, int ypos,
886           int startColumn, int endColumn)
887   {
888     int charHeight = av.getCharHeight();
889     int charWidth = av.getCharWidth();
890
891     g.setColor(Color.blue);
892     HiddenColumns hidden = av.getAlignment().getHiddenColumns();
893     List<Integer> positions = hidden.findHiddenRegionPositions();
894     for (int pos : positions)
895     {
896       int res = pos - startColumn;
897
898       if (res < 0 || res > endColumn - startColumn + 1)
899       {
900         continue;
901       }
902
903       /*
904        * draw a downward-pointing triangle at the hidden columns location
905        * (before the following visible column)
906        */
907       int xMiddle = res * charWidth;
908       int[] xPoints = new int[] { xMiddle - charHeight / 4,
909           xMiddle + charHeight / 4, xMiddle };
910       int yTop = ypos - (charHeight / 2);
911       int[] yPoints = new int[] { yTop, yTop, yTop + 8 };
912       g.fillPolygon(xPoints, yPoints, 3);
913     }
914   }
915
916   /*
917    * Draw a selection group over a wrapped alignment
918    */
919   private void drawWrappedSelection(Graphics2D g, SequenceGroup group,
920           int canvasWidth,
921           int canvasHeight, int startRes)
922   {
923     int charHeight = av.getCharHeight();
924     int charWidth = av.getCharWidth();
925       
926     // height gap above each panel
927     int hgap = charHeight;
928     if (av.getScaleAboveWrapped())
929     {
930       hgap += charHeight;
931     }
932
933     int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
934             / charWidth;
935     int cHeight = av.getAlignment().getHeight() * charHeight;
936
937     int startx = startRes;
938     int endx;
939     int ypos = hgap; // vertical offset
940     int maxwidth = av.getAlignment().getWidth();
941
942     if (av.hasHiddenColumns())
943     {
944       maxwidth = av.getAlignment().getHiddenColumns()
945               .findColumnPosition(maxwidth);
946     }
947
948     // chop the wrapped alignment extent up into panel-sized blocks and treat
949     // each block as if it were a block from an unwrapped alignment
950     while ((ypos <= canvasHeight) && (startx < maxwidth))
951     {
952       // set end value to be start + width, or maxwidth, whichever is smaller
953       endx = startx + cWidth - 1;
954
955       if (endx > maxwidth)
956       {
957         endx = maxwidth;
958       }
959
960       g.translate(labelWidthWest, 0);
961
962       drawUnwrappedSelection(g, group, startx, endx, 0,
963               av.getAlignment().getHeight() - 1,
964               ypos);
965
966       g.translate(-labelWidthWest, 0);
967
968       // update vertical offset
969       ypos += cHeight + getAnnotationHeight() + hgap;
970
971       // update horizontal offset
972       startx += cWidth;
973     }
974   }
975
976   int getAnnotationHeight()
977   {
978     if (!av.isShowAnnotation())
979     {
980       return 0;
981     }
982
983     if (annotations == null)
984     {
985       annotations = new AnnotationPanel(av);
986     }
987
988     return annotations.adjustPanelHeight();
989   }
990
991   /**
992    * Draws the visible region of the alignment on the graphics context. If there
993    * are hidden column markers in the visible region, then each sub-region
994    * between the markers is drawn separately, followed by the hidden column
995    * marker.
996    * 
997    * @param g1
998    *          the graphics context, positioned at the first residue to be drawn
999    * @param startRes
1000    *          offset of the first column to draw (0..)
1001    * @param endRes
1002    *          offset of the last column to draw (0..)
1003    * @param startSeq
1004    *          offset of the first sequence to draw (0..)
1005    * @param endSeq
1006    *          offset of the last sequence to draw (0..)
1007    * @param yOffset
1008    *          vertical offset at which to draw (for wrapped alignments)
1009    */
1010   public void drawPanel(Graphics g1, final int startRes, final int endRes,
1011           final int startSeq, final int endSeq, final int yOffset)
1012   {
1013     int charHeight = av.getCharHeight();
1014     int charWidth = av.getCharWidth();
1015
1016     if (!av.hasHiddenColumns())
1017     {
1018       draw(g1, startRes, endRes, startSeq, endSeq, yOffset);
1019     }
1020     else
1021     {
1022       int screenY = 0;
1023       final int screenYMax = endRes - startRes;
1024       int blockStart = startRes;
1025       int blockEnd = endRes;
1026
1027       for (int[] region : av.getAlignment().getHiddenColumns()
1028               .getHiddenColumnsCopy())
1029       {
1030         int hideStart = region[0];
1031         int hideEnd = region[1];
1032
1033         if (hideStart <= blockStart)
1034         {
1035           blockStart += (hideEnd - hideStart) + 1;
1036           continue;
1037         }
1038
1039         /*
1040          * draw up to just before the next hidden region, or the end of
1041          * the visible region, whichever comes first
1042          */
1043         blockEnd = Math.min(hideStart - 1, blockStart + screenYMax
1044                 - screenY);
1045
1046         g1.translate(screenY * charWidth, 0);
1047
1048         draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
1049
1050         /*
1051          * draw the downline of the hidden column marker (ScalePanel draws the
1052          * triangle on top) if we reached it
1053          */
1054         if (av.getShowHiddenMarkers() && blockEnd == hideStart - 1)
1055         {
1056           g1.setColor(Color.blue);
1057
1058           g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
1059                   0 + yOffset, (blockEnd - blockStart + 1) * charWidth - 1,
1060                   (endSeq - startSeq + 1) * charHeight + yOffset);
1061         }
1062
1063         g1.translate(-screenY * charWidth, 0);
1064         screenY += blockEnd - blockStart + 1;
1065         blockStart = hideEnd + 1;
1066
1067         if (screenY > screenYMax)
1068         {
1069           // already rendered last block
1070           return;
1071         }
1072       }
1073
1074       if (screenY <= screenYMax)
1075       {
1076         // remaining visible region to render
1077         blockEnd = blockStart + screenYMax - screenY;
1078         g1.translate(screenY * charWidth, 0);
1079         draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
1080
1081         g1.translate(-screenY * charWidth, 0);
1082       }
1083     }
1084
1085   }
1086
1087   /**
1088    * Draws a region of the visible alignment
1089    * 
1090    * @param g1
1091    * @param startRes
1092    *          offset of the first column in the visible region (0..)
1093    * @param endRes
1094    *          offset of the last column in the visible region (0..)
1095    * @param startSeq
1096    *          offset of the first sequence in the visible region (0..)
1097    * @param endSeq
1098    *          offset of the last sequence in the visible region (0..)
1099    * @param yOffset
1100    *          vertical offset at which to draw (for wrapped alignments)
1101    */
1102   private void draw(final Graphics g, final int startRes, final int endRes,
1103           final int startSeq, final int endSeq, final int offset)
1104   {
1105     g.setFont(av.getFont());
1106     seqRdr.prepare(g, av.isRenderGaps());
1107
1108     // First draw the sequences
1109     av.getAlignment().forEachSequence(new Consumer<SequenceI>()
1110     {
1111       int i = startSeq;
1112
1113       @Override
1114       public void accept(SequenceI s)
1115       {
1116         int heightPosition = offset + ((i - startSeq) * av.getCharHeight());
1117         drawSequence(s, g, startRes, endRes, heightPosition, i);
1118         i++;
1119       }
1120     }, startSeq, endSeq + 1);
1121
1122     // now selection groups
1123     if (av.getSelectionGroup() != null
1124             || av.getAlignment().getGroups().size() > 0)
1125     {
1126       drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
1127     }
1128
1129   }
1130
1131   /**
1132    * Draw a single sequence
1133    * 
1134    * @param nextSeq
1135    *          the next sequence to draw
1136    * @param g
1137    *          graphics context
1138    * @param startRes
1139    *          offset of the first column in the visible region (0..)
1140    * @param endRes
1141    *          offset of the last column in the visible region (0..)
1142    * @param heightPosition
1143    *          vertical location of the sequence in the alignment
1144    * @param i
1145    *          index of sequence
1146    */
1147   private void drawSequence(SequenceI nextSeq, Graphics g, int startRes,
1148           int endRes, int heightPosition, int i)
1149   {
1150     int charWidth = av.getCharWidth();
1151
1152     if (nextSeq == null)
1153     {
1154       // occasionally, a race condition occurs such that the alignment row is
1155       // empty
1156       // TODO Don't think this will happen any more?
1157       return;
1158     }
1159     seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
1160             startRes, endRes, heightPosition);
1161
1162     if (av.isShowSequenceFeatures())
1163     {
1164       fr.drawSequence(g, nextSeq, startRes, endRes, heightPosition, false);
1165     }
1166
1167     /*
1168      * highlight search Results once sequence has been drawn
1169      */
1170     if (av.hasSearchResults())
1171     {
1172       SearchResultsI searchResults = av.getSearchResults();
1173       int[] visibleResults = searchResults.getResults(nextSeq, startRes,
1174               endRes);
1175       if (visibleResults != null)
1176       {
1177         for (int r = 0; r < visibleResults.length; r += 2)
1178         {
1179           seqRdr.drawHighlightedText(nextSeq, visibleResults[r],
1180                   visibleResults[r + 1],
1181                   (visibleResults[r] - startRes) * charWidth,
1182                   heightPosition);
1183         }
1184       }
1185     }
1186
1187     if (av.cursorMode && cursorY == i && cursorX >= startRes
1188             && cursorX <= endRes)
1189     {
1190       seqRdr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
1191               heightPosition);
1192     }
1193   }
1194
1195   void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
1196           int startSeq, int endSeq, int offset)
1197   {
1198     Graphics2D g = (Graphics2D) g1;
1199     //
1200     // ///////////////////////////////////
1201     // Now outline any areas if necessary
1202     // ///////////////////////////////////
1203
1204     SequenceGroup group = null;
1205     int groupIndex = -1;
1206
1207     if (av.getAlignment().getGroups().size() > 0)
1208     {
1209       group = av.getAlignment().getGroups().get(0);
1210       groupIndex = 0;
1211     }
1212
1213     if (group != null)
1214     {
1215       g.setStroke(new BasicStroke());
1216       g.setColor(group.getOutlineColour());
1217       
1218       do
1219       {
1220         drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
1221                 endSeq, offset);
1222
1223         groupIndex++;
1224
1225         g.setStroke(new BasicStroke());
1226
1227         if (groupIndex >= av.getAlignment().getGroups().size())
1228         {
1229           break;
1230         }
1231
1232         group = av.getAlignment().getGroups().get(groupIndex);
1233
1234       } while (groupIndex < av.getAlignment().getGroups().size());
1235
1236     }
1237
1238   }
1239
1240
1241   /*
1242    * Draw the selection group as a separate image and overlay
1243    */
1244   private BufferedImage drawSelectionGroup(int startRes, int endRes,
1245           int startSeq, int endSeq)
1246   {
1247     // get a new image of the correct size
1248     BufferedImage selectionImage = setupImage();
1249
1250     if (selectionImage == null)
1251     {
1252       return null;
1253     }
1254
1255     SequenceGroup group = av.getSelectionGroup();
1256     if (group == null)
1257     {
1258       // nothing to draw
1259       return null;
1260     }
1261
1262     // set up drawing colour
1263     Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1264
1265     setupSelectionGroup(g, selectionImage);
1266
1267     if (!av.getWrapAlignment())
1268     {
1269       drawUnwrappedSelection(g, group, startRes, endRes, startSeq, endSeq,
1270               0);
1271     }
1272     else
1273     {
1274       drawWrappedSelection(g, group, getWidth(), getHeight(),
1275               av.getRanges().getStartRes());
1276     }
1277
1278     g.dispose();
1279     return selectionImage;
1280   }
1281
1282   /*
1283    * Set up graphics for selection group
1284    */
1285   private void setupSelectionGroup(Graphics2D g,
1286           BufferedImage selectionImage)
1287   {
1288     // set background to transparent
1289     g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1290     g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1291
1292     // set up foreground to draw red dashed line
1293     g.setComposite(AlphaComposite.Src);
1294     g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1295             BasicStroke.JOIN_ROUND, 3f, new float[]
1296     { 5f, 3f }, 0f));
1297     g.setColor(Color.RED);
1298   }
1299
1300   /*
1301    * Draw a selection group over an unwrapped alignment
1302    * @param g graphics object to draw with
1303    * @param group selection group
1304    * @param startRes start residue of area to draw
1305    * @param endRes end residue of area to draw
1306    * @param startSeq start sequence of area to draw
1307    * @param endSeq end sequence of area to draw
1308    * @param offset vertical offset (used when called from wrapped alignment code)
1309    */
1310   private void drawUnwrappedSelection(Graphics2D g, SequenceGroup group,
1311           int startRes, int endRes, int startSeq, int endSeq, int offset)
1312   {
1313     int charWidth = av.getCharWidth();
1314           
1315     if (!av.hasHiddenColumns())
1316     {
1317       drawPartialGroupOutline(g, group, startRes, endRes, startSeq, endSeq,
1318               offset);
1319     }
1320     else
1321     {
1322       // package into blocks of visible columns
1323       int screenY = 0;
1324       int blockStart = startRes;
1325       int blockEnd = endRes;
1326
1327       for (int[] region : av.getAlignment().getHiddenColumns()
1328               .getHiddenColumnsCopy())
1329       {
1330         int hideStart = region[0];
1331         int hideEnd = region[1];
1332
1333         if (hideStart <= blockStart)
1334         {
1335           blockStart += (hideEnd - hideStart) + 1;
1336           continue;
1337         }
1338
1339         blockEnd = hideStart - 1;
1340
1341         g.translate(screenY * charWidth, 0);
1342         drawPartialGroupOutline(g, group,
1343                 blockStart, blockEnd, startSeq, endSeq, offset);
1344
1345         g.translate(-screenY * charWidth, 0);
1346         screenY += blockEnd - blockStart + 1;
1347         blockStart = hideEnd + 1;
1348
1349         if (screenY > (endRes - startRes))
1350         {
1351           // already rendered last block
1352           break;
1353         }
1354       }
1355
1356       if (screenY <= (endRes - startRes))
1357       {
1358         // remaining visible region to render
1359         blockEnd = blockStart + (endRes - startRes) - screenY;
1360         g.translate(screenY * charWidth, 0);
1361         drawPartialGroupOutline(g, group,
1362                 blockStart, blockEnd, startSeq, endSeq, offset);
1363         
1364         g.translate(-screenY * charWidth, 0);
1365       }
1366     }
1367   }
1368
1369   /*
1370    * Draw the selection group as a separate image and overlay
1371    */
1372   private void drawPartialGroupOutline(Graphics2D g, SequenceGroup group,
1373           int startRes, int endRes, int startSeq, int endSeq,
1374           int verticalOffset)
1375   {
1376         int charHeight = av.getCharHeight();
1377         int charWidth = av.getCharWidth();
1378           
1379     int visWidth = (endRes - startRes + 1) * charWidth;
1380
1381     int oldY = -1;
1382     int i = 0;
1383     boolean inGroup = false;
1384     int top = -1;
1385     int bottom = -1;
1386
1387     int sx = -1;
1388     int sy = -1;
1389     int xwidth = -1;
1390
1391     for (i = startSeq; i <= endSeq; i++)
1392     {
1393       // position of start residue of group relative to startRes, in pixels
1394       sx = (group.getStartRes() - startRes) * charWidth;
1395
1396       // width of group in pixels
1397       xwidth = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth)
1398               - 1;
1399
1400       sy = verticalOffset + (i - startSeq) * charHeight;
1401
1402       if (sx + xwidth < 0 || sx > visWidth)
1403       {
1404         continue;
1405       }
1406
1407       if ((sx <= (endRes - startRes) * charWidth)
1408               && group.getSequences(null)
1409                       .contains(av.getAlignment().getSequenceAt(i)))
1410       {
1411         if ((bottom == -1) && !group.getSequences(null)
1412                 .contains(av.getAlignment().getSequenceAt(i + 1)))
1413         {
1414           bottom = sy + charHeight;
1415         }
1416
1417         if (!inGroup)
1418         {
1419           if (((top == -1) && (i == 0)) || !group.getSequences(null)
1420                   .contains(av.getAlignment().getSequenceAt(i - 1)))
1421           {
1422             top = sy;
1423           }
1424
1425           oldY = sy;
1426           inGroup = true;
1427         }
1428       }
1429       else
1430       {
1431         if (inGroup)
1432         {
1433           // if start position is visible, draw vertical line to left of
1434           // group
1435           if (sx >= 0 && sx < visWidth)
1436           {
1437             g.drawLine(sx, oldY, sx, sy);
1438           }
1439
1440           // if end position is visible, draw vertical line to right of
1441           // group
1442           if (sx + xwidth < visWidth)
1443           {
1444             g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1445           }
1446
1447           if (sx < 0)
1448           {
1449             xwidth += sx;
1450             sx = 0;
1451           }
1452
1453           // don't let width extend beyond current block, or group extent
1454           // fixes JAL-2672
1455           if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
1456           {
1457             xwidth = (endRes - startRes + 1) * charWidth - sx;
1458           }
1459           
1460           // draw horizontal line at top of group
1461           if (top != -1)
1462           {
1463             g.drawLine(sx, top, sx + xwidth, top);
1464             top = -1;
1465           }
1466
1467           // draw horizontal line at bottom of group
1468           if (bottom != -1)
1469           {
1470             g.drawLine(sx, bottom, sx + xwidth, bottom);
1471             bottom = -1;
1472           }
1473
1474           inGroup = false;
1475         }
1476       }
1477     }
1478
1479     if (inGroup)
1480     {
1481       sy = verticalOffset + ((i - startSeq) * charHeight);
1482       if (sx >= 0 && sx < visWidth)
1483       {
1484         g.drawLine(sx, oldY, sx, sy);
1485       }
1486
1487       if (sx + xwidth < visWidth)
1488       {
1489         g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1490       }
1491
1492       if (sx < 0)
1493       {
1494         xwidth += sx;
1495         sx = 0;
1496       }
1497
1498       if (sx + xwidth > visWidth)
1499       {
1500         xwidth = visWidth;
1501       }
1502       else if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
1503       {
1504         xwidth = (endRes - startRes + 1) * charWidth;
1505       }
1506
1507       if (top != -1)
1508       {
1509         g.drawLine(sx, top, sx + xwidth, top);
1510         top = -1;
1511       }
1512
1513       if (bottom != -1)
1514       {
1515         g.drawLine(sx, bottom - 1, sx + xwidth, bottom - 1);
1516         bottom = -1;
1517       }
1518
1519       inGroup = false;
1520     }
1521   }
1522   
1523   /**
1524    * Highlights search results in the visible region by rendering as white text
1525    * on a black background. Any previous highlighting is removed. Answers true
1526    * if any highlight was left on the visible alignment (so status bar should be
1527    * set to match), else false.
1528    * <p>
1529    * Currently fastPaint is not implemented for wrapped alignments. If a wrapped
1530    * alignment had to be scrolled to show the highlighted region, then it should
1531    * be fully redrawn, otherwise a fast paint can be performed. This argument
1532    * could be removed if fast paint of scrolled wrapped alignment is coded in
1533    * future (JAL-2609).
1534    * 
1535    * @param results
1536    * @param noFastPaint
1537    * @return
1538    */
1539   public boolean highlightSearchResults(SearchResultsI results,
1540           boolean noFastPaint)
1541   {
1542     if (fastpainting)
1543     {
1544       return false;
1545     }
1546     boolean wrapped = av.getWrapAlignment();
1547     try
1548     {
1549       fastPaint = !noFastPaint;
1550       fastpainting = fastPaint;
1551
1552       /*
1553        * to avoid redrawing the whole visible region, we instead
1554        * redraw just the minimal regions to remove previous highlights
1555        * and add new ones
1556        */
1557       SearchResultsI previous = av.getSearchResults();
1558       av.setSearchResults(results);
1559       boolean redrawn = false;
1560       boolean drawn = false;
1561       if (wrapped)
1562       {
1563         redrawn = drawMappedPositionsWrapped(previous);
1564         drawn = drawMappedPositionsWrapped(results);
1565         redrawn |= drawn;
1566       }
1567       else
1568       {
1569         redrawn = drawMappedPositions(previous);
1570         drawn = drawMappedPositions(results);
1571         redrawn |= drawn;
1572       }
1573
1574       /*
1575        * if highlights were either removed or added, repaint
1576        */
1577       if (redrawn)
1578       {
1579         repaint();
1580       }
1581
1582       /*
1583        * return true only if highlights were added
1584        */
1585       return drawn;
1586
1587     } finally
1588     {
1589       fastpainting = false;
1590     }
1591   }
1592
1593   /**
1594    * Redraws the minimal rectangle in the visible region (if any) that includes
1595    * mapped positions of the given search results. Whether or not positions are
1596    * highlighted depends on the SearchResults set on the Viewport. This allows
1597    * this method to be called to either clear or set highlighting. Answers true
1598    * if any positions were drawn (in which case a repaint is still required),
1599    * else false.
1600    * 
1601    * @param results
1602    * @return
1603    */
1604   protected boolean drawMappedPositions(SearchResultsI results)
1605   {
1606     if (results == null)
1607     {
1608       return false;
1609     }
1610
1611     /*
1612      * calculate the minimal rectangle to redraw that 
1613      * includes both new and existing search results
1614      */
1615     int firstSeq = Integer.MAX_VALUE;
1616     int lastSeq = -1;
1617     int firstCol = Integer.MAX_VALUE;
1618     int lastCol = -1;
1619     boolean matchFound = false;
1620
1621     ViewportRanges ranges = av.getRanges();
1622     int firstVisibleColumn = ranges.getStartRes();
1623     int lastVisibleColumn = ranges.getEndRes();
1624     AlignmentI alignment = av.getAlignment();
1625     if (av.hasHiddenColumns())
1626     {
1627       firstVisibleColumn = alignment.getHiddenColumns()
1628               .adjustForHiddenColumns(firstVisibleColumn);
1629       lastVisibleColumn = alignment.getHiddenColumns()
1630               .adjustForHiddenColumns(lastVisibleColumn);
1631     }
1632
1633     for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1634             .getEndSeq(); seqNo++)
1635     {
1636       SequenceI seq = alignment.getSequenceAt(seqNo);
1637
1638       int[] visibleResults = results.getResults(seq, firstVisibleColumn,
1639               lastVisibleColumn);
1640       if (visibleResults != null)
1641       {
1642         for (int i = 0; i < visibleResults.length - 1; i += 2)
1643         {
1644           int firstMatchedColumn = visibleResults[i];
1645           int lastMatchedColumn = visibleResults[i + 1];
1646           if (firstMatchedColumn <= lastVisibleColumn
1647                   && lastMatchedColumn >= firstVisibleColumn)
1648           {
1649             /*
1650              * found a search results match in the visible region - 
1651              * remember the first and last sequence matched, and the first
1652              * and last visible columns in the matched positions
1653              */
1654             matchFound = true;
1655             firstSeq = Math.min(firstSeq, seqNo);
1656             lastSeq = Math.max(lastSeq, seqNo);
1657             firstMatchedColumn = Math.max(firstMatchedColumn,
1658                     firstVisibleColumn);
1659             lastMatchedColumn = Math.min(lastMatchedColumn,
1660                     lastVisibleColumn);
1661             firstCol = Math.min(firstCol, firstMatchedColumn);
1662             lastCol = Math.max(lastCol, lastMatchedColumn);
1663           }
1664         }
1665       }
1666     }
1667
1668     if (matchFound)
1669     {
1670       if (av.hasHiddenColumns())
1671       {
1672         firstCol = alignment.getHiddenColumns()
1673                 .findColumnPosition(firstCol);
1674         lastCol = alignment.getHiddenColumns().findColumnPosition(lastCol);
1675       }
1676       int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
1677       int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
1678       gg.translate(transX, transY);
1679       drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
1680       gg.translate(-transX, -transY);
1681     }
1682
1683     return matchFound;
1684   }
1685
1686   @Override
1687   public void propertyChange(PropertyChangeEvent evt)
1688   {
1689     String eventName = evt.getPropertyName();
1690
1691     if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1692     {
1693       fastPaint = true;
1694       repaint();
1695       return;
1696     }
1697
1698     int scrollX = 0;
1699     if (eventName.equals(ViewportRanges.STARTRES)
1700             || eventName.equals(ViewportRanges.STARTRESANDSEQ))
1701     {
1702       // Make sure we're not trying to draw a panel
1703       // larger than the visible window
1704       if (eventName.equals(ViewportRanges.STARTRES))
1705       {
1706         scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1707       }
1708       else
1709       {
1710         scrollX = ((int[]) evt.getNewValue())[0]
1711                 - ((int[]) evt.getOldValue())[0];
1712       }
1713       ViewportRanges vpRanges = av.getRanges();
1714
1715       int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1716       if (scrollX > range)
1717       {
1718         scrollX = range;
1719       }
1720       else if (scrollX < -range)
1721       {
1722         scrollX = -range;
1723       }
1724     }
1725
1726     // Both scrolling and resizing change viewport ranges: scrolling changes
1727     // both start and end points, but resize only changes end values.
1728     // Here we only want to fastpaint on a scroll, with resize using a normal
1729     // paint, so scroll events are identified as changes to the horizontal or
1730     // vertical start value.
1731     if (eventName.equals(ViewportRanges.STARTRES))
1732     {
1733       if (av.getWrapAlignment())
1734       {
1735         fastPaintWrapped(scrollX);
1736       }
1737       else
1738       {
1739         fastPaint(scrollX, 0);
1740       }
1741     }
1742     else if (eventName.equals(ViewportRanges.STARTSEQ))
1743     {
1744       // scroll
1745       fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1746     }
1747     else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1748     {
1749       fastPaint(scrollX, 0);
1750       // bizarrely, we only need to scroll on the x value here as fastpaint
1751       // copies the full height of the image anyway. Passing in the y value
1752       // causes nasty repaint artefacts, which only disappear on a full
1753       // repaint.
1754     }
1755   }
1756
1757   /**
1758    * Does a minimal update of the image for a scroll movement. This method
1759    * handles scroll movements of up to one width of the wrapped alignment (one
1760    * click in the vertical scrollbar). Larger movements (for example after a
1761    * scroll to highlight a mapped position) trigger a full redraw instead.
1762    * 
1763    * @param scrollX
1764    *          number of positions scrolled (right if positive, left if negative)
1765    */
1766   protected void fastPaintWrapped(int scrollX)
1767   {
1768     ViewportRanges ranges = av.getRanges();
1769
1770     if (Math.abs(scrollX) > ranges.getViewportWidth())
1771     {
1772       /*
1773        * shift of more than one view width is 
1774        * overcomplicated to handle in this method
1775        */
1776       fastPaint = false;
1777       repaint();
1778       return;
1779     }
1780
1781     if (fastpainting || gg == null)
1782     {
1783       return;
1784     }
1785
1786     fastPaint = true;
1787     fastpainting = true;
1788
1789     try
1790     {
1791       calculateWrappedGeometry(getWidth(), getHeight());
1792
1793       /*
1794        * relocate the regions of the alignment that are still visible
1795        */
1796       shiftWrappedAlignment(-scrollX);
1797
1798       /*
1799        * add new columns (sequence, annotation)
1800        * - at top left if scrollX < 0 
1801        * - at right of last two widths if scrollX > 0
1802        */
1803       if (scrollX < 0)
1804       {
1805         int startRes = ranges.getStartRes();
1806         drawWrappedWidth(gg, wrappedSpaceAboveAlignment, startRes, startRes
1807                 - scrollX - 1, getHeight());
1808       }
1809       else
1810       {
1811         fastPaintWrappedAddRight(scrollX);
1812       }
1813
1814       /*
1815        * draw all scales (if  shown) and hidden column markers
1816        */
1817       drawWrappedDecorators(gg, ranges.getStartRes());
1818
1819       repaint();
1820     } finally
1821     {
1822       fastpainting = false;
1823     }
1824   }
1825
1826   /**
1827    * Draws the specified number of columns at the 'end' (bottom right) of a
1828    * wrapped alignment view, including sequences and annotations if shown, but
1829    * not scales. Also draws the same number of columns at the right hand end of
1830    * the second last width shown, if the last width is not full height (so
1831    * cannot simply be copied from the graphics image).
1832    * 
1833    * @param columns
1834    */
1835   protected void fastPaintWrappedAddRight(int columns)
1836   {
1837     if (columns == 0)
1838     {
1839       return;
1840     }
1841
1842     ViewportRanges ranges = av.getRanges();
1843     int viewportWidth = ranges.getViewportWidth();
1844     int charWidth = av.getCharWidth();
1845
1846     /**
1847      * draw full height alignment in the second last row, last columns, if the
1848      * last row was not full height
1849      */
1850     int visibleWidths = wrappedVisibleWidths;
1851     int canvasHeight = getHeight();
1852     boolean lastWidthPartHeight = (wrappedVisibleWidths * wrappedRepeatHeightPx) > canvasHeight;
1853
1854     if (lastWidthPartHeight)
1855     {
1856       int widthsAbove = Math.max(0, visibleWidths - 2);
1857       int ypos = wrappedRepeatHeightPx * widthsAbove
1858               + wrappedSpaceAboveAlignment;
1859       int endRes = ranges.getEndRes();
1860       endRes += widthsAbove * viewportWidth;
1861       int startRes = endRes - columns;
1862       int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1863               * charWidth;
1864
1865       /*
1866        * white fill first to erase annotations
1867        */
1868       gg.translate(xOffset, 0);
1869       gg.setColor(Color.white);
1870       gg.fillRect(labelWidthWest, ypos,
1871               (endRes - startRes + 1) * charWidth, wrappedRepeatHeightPx);
1872       gg.translate(-xOffset, 0);
1873
1874       drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1875     }
1876
1877     /*
1878      * draw newly visible columns in last wrapped width (none if we
1879      * have reached the end of the alignment)
1880      * y-offset for drawing last width is height of widths above,
1881      * plus one gap row
1882      */
1883     int widthsAbove = visibleWidths - 1;
1884     int ypos = wrappedRepeatHeightPx * widthsAbove
1885             + wrappedSpaceAboveAlignment;
1886     int endRes = ranges.getEndRes();
1887     endRes += widthsAbove * viewportWidth;
1888     int startRes = endRes - columns + 1;
1889
1890     /*
1891      * white fill first to erase annotations
1892      */
1893     int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1894             * charWidth;
1895     gg.translate(xOffset, 0);
1896     gg.setColor(Color.white);
1897     int width = viewportWidth * charWidth - xOffset;
1898     gg.fillRect(labelWidthWest, ypos, width, wrappedRepeatHeightPx);
1899     gg.translate(-xOffset, 0);
1900
1901     gg.setFont(av.getFont());
1902     gg.setColor(Color.black);
1903
1904     if (startRes < ranges.getVisibleAlignmentWidth())
1905     {
1906       drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1907     }
1908
1909     /*
1910      * and finally, white fill any space below the visible alignment
1911      */
1912     int heightBelow = canvasHeight - visibleWidths * wrappedRepeatHeightPx;
1913     if (heightBelow > 0)
1914     {
1915       gg.setColor(Color.white);
1916       gg.fillRect(0, canvasHeight - heightBelow, getWidth(), heightBelow);
1917     }
1918   }
1919
1920   /**
1921    * Shifts the visible alignment by the specified number of columns - left if
1922    * negative, right if positive. Copies and moves sequences and annotations (if
1923    * shown). Scales, hidden column markers and any newly visible columns must be
1924    * drawn separately.
1925    * 
1926    * @param positions
1927    */
1928   protected void shiftWrappedAlignment(int positions)
1929   {
1930     if (positions == 0)
1931     {
1932       return;
1933     }
1934     int charWidth = av.getCharWidth();
1935
1936     int canvasHeight = getHeight();
1937     ViewportRanges ranges = av.getRanges();
1938     int viewportWidth = ranges.getViewportWidth();
1939     int widthToCopy = (ranges.getViewportWidth() - Math.abs(positions))
1940             * charWidth;
1941     int heightToCopy = wrappedRepeatHeightPx - wrappedSpaceAboveAlignment;
1942     int xMax = ranges.getVisibleAlignmentWidth();
1943
1944     if (positions > 0)
1945     {
1946       /*
1947        * shift right (after scroll left)
1948        * for each wrapped width (starting with the last), copy (width-positions) 
1949        * columns from the left margin to the right margin, and copy positions 
1950        * columns from the right margin of the row above (if any) to the 
1951        * left margin of the current row
1952        */
1953
1954       /*
1955        * get y-offset of last wrapped width, first row of sequences
1956        */
1957       int y = canvasHeight / wrappedRepeatHeightPx * wrappedRepeatHeightPx;
1958       y += wrappedSpaceAboveAlignment;
1959       int copyFromLeftStart = labelWidthWest;
1960       int copyFromRightStart = copyFromLeftStart + widthToCopy;
1961
1962       while (y >= 0)
1963       {
1964         gg.copyArea(copyFromLeftStart, y, widthToCopy, heightToCopy,
1965                 positions * charWidth, 0);
1966         if (y > 0)
1967         {
1968           gg.copyArea(copyFromRightStart, y - wrappedRepeatHeightPx,
1969                   positions * charWidth, heightToCopy, -widthToCopy,
1970                   wrappedRepeatHeightPx);
1971         }
1972
1973         y -= wrappedRepeatHeightPx;
1974       }
1975     }
1976     else
1977     {
1978       /*
1979        * shift left (after scroll right)
1980        * for each wrapped width (starting with the first), copy (width-positions) 
1981        * columns from the right margin to the left margin, and copy positions 
1982        * columns from the left margin of the row below (if any) to the 
1983        * right margin of the current row
1984        */
1985       int xpos = av.getRanges().getStartRes();
1986       int y = wrappedSpaceAboveAlignment;
1987       int copyFromRightStart = labelWidthWest - positions * charWidth;
1988
1989       while (y < canvasHeight)
1990       {
1991         gg.copyArea(copyFromRightStart, y, widthToCopy, heightToCopy,
1992                 positions * charWidth, 0);
1993         if (y + wrappedRepeatHeightPx < canvasHeight - wrappedRepeatHeightPx
1994                 && (xpos + viewportWidth <= xMax))
1995         {
1996           gg.copyArea(labelWidthWest, y + wrappedRepeatHeightPx, -positions
1997                   * charWidth, heightToCopy, widthToCopy,
1998                   -wrappedRepeatHeightPx);
1999         }
2000
2001         y += wrappedRepeatHeightPx;
2002         xpos += viewportWidth;
2003       }
2004     }
2005   }
2006
2007   
2008   /**
2009    * Redraws any positions in the search results in the visible region of a
2010    * wrapped alignment. Any highlights are drawn depending on the search results
2011    * set on the Viewport, not the <code>results</code> argument. This allows
2012    * this method to be called either to clear highlights (passing the previous
2013    * search results), or to draw new highlights.
2014    * 
2015    * @param results
2016    * @return
2017    */
2018   protected boolean drawMappedPositionsWrapped(SearchResultsI results)
2019   {
2020     if (results == null)
2021     {
2022       return false;
2023     }
2024     int charHeight = av.getCharHeight();
2025
2026     boolean matchFound = false;
2027
2028     calculateWrappedGeometry(getWidth(), getHeight());
2029     int wrappedWidth = av.getWrappedWidth();
2030     int wrappedHeight = wrappedRepeatHeightPx;
2031
2032     ViewportRanges ranges = av.getRanges();
2033     int canvasHeight = getHeight();
2034     int repeats = canvasHeight / wrappedHeight;
2035     if (canvasHeight / wrappedHeight > 0)
2036     {
2037       repeats++;
2038     }
2039
2040     int firstVisibleColumn = ranges.getStartRes();
2041     int lastVisibleColumn = ranges.getStartRes() + repeats
2042             * ranges.getViewportWidth() - 1;
2043
2044     AlignmentI alignment = av.getAlignment();
2045     if (av.hasHiddenColumns())
2046     {
2047       firstVisibleColumn = alignment.getHiddenColumns()
2048               .adjustForHiddenColumns(firstVisibleColumn);
2049       lastVisibleColumn = alignment.getHiddenColumns()
2050               .adjustForHiddenColumns(lastVisibleColumn);
2051     }
2052
2053     int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
2054
2055     for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
2056             .getEndSeq(); seqNo++)
2057     {
2058       SequenceI seq = alignment.getSequenceAt(seqNo);
2059
2060       int[] visibleResults = results.getResults(seq, firstVisibleColumn,
2061               lastVisibleColumn);
2062       if (visibleResults != null)
2063       {
2064         for (int i = 0; i < visibleResults.length - 1; i += 2)
2065         {
2066           int firstMatchedColumn = visibleResults[i];
2067           int lastMatchedColumn = visibleResults[i + 1];
2068           if (firstMatchedColumn <= lastVisibleColumn
2069                   && lastMatchedColumn >= firstVisibleColumn)
2070           {
2071             /*
2072              * found a search results match in the visible region
2073              */
2074             firstMatchedColumn = Math.max(firstMatchedColumn,
2075                     firstVisibleColumn);
2076             lastMatchedColumn = Math.min(lastMatchedColumn,
2077                     lastVisibleColumn);
2078
2079             /*
2080              * draw each mapped position separately (as contiguous positions may
2081              * wrap across lines)
2082              */
2083             for (int mappedPos = firstMatchedColumn; mappedPos <= lastMatchedColumn; mappedPos++)
2084             {
2085               int displayColumn = mappedPos;
2086               if (av.hasHiddenColumns())
2087               {
2088                 displayColumn = alignment.getHiddenColumns()
2089                         .findColumnPosition(displayColumn);
2090               }
2091
2092               /*
2093                * transX: offset from left edge of canvas to residue position
2094                */
2095               int transX = labelWidthWest
2096                       + ((displayColumn - ranges.getStartRes()) % wrappedWidth)
2097                       * av.getCharWidth();
2098
2099               /*
2100                * transY: offset from top edge of canvas to residue position
2101                */
2102               int transY = gapHeight;
2103               transY += (displayColumn - ranges.getStartRes())
2104                       / wrappedWidth * wrappedHeight;
2105               transY += (seqNo - ranges.getStartSeq()) * av.getCharHeight();
2106
2107               /*
2108                * yOffset is from graphics origin to start of visible region
2109                */
2110               int yOffset = 0;// (displayColumn / wrappedWidth) * wrappedHeight;
2111               if (transY < getHeight())
2112               {
2113                 matchFound = true;
2114                 gg.translate(transX, transY);
2115                 drawPanel(gg, displayColumn, displayColumn, seqNo, seqNo,
2116                         yOffset);
2117                 gg.translate(-transX, -transY);
2118               }
2119             }
2120           }
2121         }
2122       }
2123     }
2124   
2125     return matchFound;
2126   }
2127
2128   /**
2129    * Answers the width in pixels of the left scale labels (0 if not shown)
2130    * 
2131    * @return
2132    */
2133   int getLabelWidthWest()
2134   {
2135     return labelWidthWest;
2136   }
2137 }