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