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