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