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