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