+ if (fastpainting)
+ {
+ return false;
+ }
+ boolean wrapped = av.getWrapAlignment();
+
+ try
+ {
+ fastPaint = !noFastPaint;
+ fastpainting = fastPaint;
+
+ updateViewport();
+
+ /*
+ * to avoid redrawing the whole visible region, we instead
+ * redraw just the minimal regions to remove previous highlights
+ * and add new ones
+ */
+ SearchResultsI previous = av.getSearchResults();
+ av.setSearchResults(results);
+ boolean redrawn = false;
+ boolean drawn = false;
+ if (wrapped)
+ {
+ redrawn = drawMappedPositionsWrapped(previous);
+ drawn = drawMappedPositionsWrapped(results);
+ redrawn |= drawn;
+ }
+ else
+ {
+ redrawn = drawMappedPositions(previous);
+ drawn = drawMappedPositions(results);
+ redrawn |= drawn;
+ }
+
+ /*
+ * if highlights were either removed or added, repaint
+ */
+ if (redrawn)
+ {
+ repaint();
+ }
+
+ /*
+ * return true only if highlights were added
+ */
+ return drawn;
+
+ } finally
+ {
+ fastpainting = false;
+ }
+ }
+
+ /**
+ * Redraws the minimal rectangle in the visible region (if any) that includes
+ * mapped positions of the given search results. Whether or not positions are
+ * highlighted depends on the SearchResults set on the Viewport. This allows
+ * this method to be called to either clear or set highlighting. Answers true
+ * if any positions were drawn (in which case a repaint is still required),
+ * else false.
+ *
+ * @param results
+ * @return
+ */
+ protected boolean drawMappedPositions(SearchResultsI results)
+ {
+ if (results == null)
+ {
+ return false;
+ }
+
+ /*
+ * calculate the minimal rectangle to redraw that
+ * includes both new and existing search results
+ */
+ int firstSeq = Integer.MAX_VALUE;
+ int lastSeq = -1;
+ int firstCol = Integer.MAX_VALUE;
+ int lastCol = -1;
+ boolean matchFound = false;
+
+ ViewportRanges ranges = av.getRanges();
+ int firstVisibleColumn = ranges.getStartRes();
+ int lastVisibleColumn = ranges.getEndRes();
+ AlignmentI alignment = av.getAlignment();
+ if (av.hasHiddenColumns())
+ {
+ firstVisibleColumn = alignment.getHiddenColumns()
+ .adjustForHiddenColumns(firstVisibleColumn);
+ lastVisibleColumn = alignment.getHiddenColumns()
+ .adjustForHiddenColumns(lastVisibleColumn);
+ }
+
+ for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
+ .getEndSeq(); seqNo++)
+ {
+ SequenceI seq = alignment.getSequenceAt(seqNo);
+
+ int[] visibleResults = results.getResults(seq, firstVisibleColumn,
+ lastVisibleColumn);
+ if (visibleResults != null)
+ {
+ for (int i = 0; i < visibleResults.length - 1; i += 2)
+ {
+ int firstMatchedColumn = visibleResults[i];
+ int lastMatchedColumn = visibleResults[i + 1];
+ if (firstMatchedColumn <= lastVisibleColumn
+ && lastMatchedColumn >= firstVisibleColumn)
+ {
+ /*
+ * found a search results match in the visible region -
+ * remember the first and last sequence matched, and the first
+ * and last visible columns in the matched positions
+ */
+ matchFound = true;
+ firstSeq = Math.min(firstSeq, seqNo);
+ lastSeq = Math.max(lastSeq, seqNo);
+ firstMatchedColumn = Math.max(firstMatchedColumn,
+ firstVisibleColumn);
+ lastMatchedColumn = Math.min(lastMatchedColumn,
+ lastVisibleColumn);
+ firstCol = Math.min(firstCol, firstMatchedColumn);
+ lastCol = Math.max(lastCol, lastMatchedColumn);
+ }
+ }
+ }
+ }
+
+ if (matchFound)
+ {
+ if (av.hasHiddenColumns())
+ {
+ firstCol = alignment.getHiddenColumns()
+ .findColumnPosition(firstCol);
+ lastCol = alignment.getHiddenColumns().findColumnPosition(lastCol);
+ }
+ int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
+ int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
+ gg.translate(transX, transY);
+ drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
+ gg.translate(-transX, -transY);
+ }
+
+ return matchFound;
+ }
+
+ @Override
+ public void propertyChange(PropertyChangeEvent evt)
+ {
+ String eventName = evt.getPropertyName();
+
+ int scrollX = 0;
+ if (eventName.equals(ViewportRanges.STARTRES))
+ {
+ // Make sure we're not trying to draw a panel
+ // larger than the visible window
+ ViewportRanges vpRanges = av.getRanges();
+ scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
+ int range = vpRanges.getEndRes() - vpRanges.getStartRes();
+ if (scrollX > range)
+ {
+ scrollX = range;
+ }
+ else if (scrollX < -range)
+ {
+ scrollX = -range;
+ }
+ }
+
+ // Both scrolling and resizing change viewport ranges: scrolling changes
+ // both start and end points, but resize only changes end values.
+ // Here we only want to fastpaint on a scroll, with resize using a normal
+ // paint, so scroll events are identified as changes to the horizontal or
+ // vertical start value.
+ if (eventName.equals(ViewportRanges.STARTRES))
+ {
+ // scroll - startres and endres both change
+ if (av.getWrapAlignment())
+ {
+ fastPaintWrapped(scrollX);
+ }
+ else
+ {
+ fastPaint(scrollX, 0);
+ }
+ }
+ else if (eventName.equals(ViewportRanges.STARTSEQ))
+ {
+ fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
+ }
+ }
+
+ /**
+ * Does a minimal update of the image for a scroll movement. This method
+ * handles scroll movements of up to one width of the wrapped alignment (one
+ * click in the vertical scrollbar). Larger movements (for example after a
+ * scroll to highlight a mapped position) trigger a full redraw instead.
+ *
+ * @param scrollX
+ * number of positions scrolled (right if positive, left if negative)
+ */
+ protected void fastPaintWrapped(int scrollX)
+ {
+ if (Math.abs(scrollX) > av.getRanges().getViewportWidth())
+ {
+ /*
+ * shift of more than one view width is
+ * overcomplicated to handle in this method
+ */
+ fastPaint = false;
+ repaint();
+ return;
+ }
+
+ if (fastpainting || gg == null)
+ {
+ return;
+ }
+
+ fastPaint = true;
+ fastpainting = true;
+
+ try
+ {
+ /*
+ * relocate the regions of the alignment that are still visible
+ */
+ shiftWrappedAlignment(-scrollX);
+
+ /*
+ * add new columns (scale above, sequence, annotation)
+ * - at top left if scrollX < 0
+ * - at right of last two widths if scrollX > 0
+ * also West scale top left or East scale bottom right if shown
+ */
+ if (scrollX < 0)
+ {
+ fastPaintWrappedAddLeft(-scrollX);
+ }
+ else
+ {
+ fastPaintWrappedAddRight(scrollX);
+ }
+
+ repaint();
+ } finally
+ {
+ fastpainting = false;
+ }
+ }
+
+ /**
+ * Draws the specified number of columns at the 'end' (bottom right) of a
+ * wrapped alignment view, including scale above and right and annotations if
+ * shown. Also draws the same number of columns at the right hand end of the
+ * second last width shown, if the last width is not full height (so cannot
+ * simply be copied from the graphics image).
+ *
+ * @param columns
+ */
+ protected void fastPaintWrappedAddRight(int columns)
+ {
+ if (columns == 0)
+ {
+ return;
+ }
+
+ /*
+ * how many widths are visible? we will be adding
+ * columns to the last visible width, right hand end
+ */
+ int repeatHeight = getRepeatHeightWrapped();
+ int canvasHeight = getHeight();
+ int visibleWidths = canvasHeight / repeatHeight;
+ int remainder = canvasHeight % repeatHeight;
+ int hgap = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
+ boolean lastWidthPartHeight = false;
+ if (remainder >= (hgap + charHeight))
+ {
+ visibleWidths++;
+ lastWidthPartHeight = true;
+ }
+
+ /*
+ * limit visible widths to max widths of alignment, from the
+ * current start residue (we may be scrolled down)
+ */
+ ViewportRanges ranges = av.getRanges();
+ int availableAlignmentWidth = ranges.getVisibleAlignmentWidth()
+ - ranges.getStartRes();
+ int viewportWidth = ranges.getViewportWidth();
+ int maxWidths = availableAlignmentWidth / viewportWidth;
+ if (availableAlignmentWidth % viewportWidth > 0)
+ {
+ maxWidths++;
+ }
+ visibleWidths = Math.min(visibleWidths, maxWidths);
+ int canvasWidth = getWidth();
+ int widthInColumns = (canvasWidth - labelWidthEast - labelWidthWest)
+ / charWidth;
+
+ /**
+ * draw full height alignment in the second last row, last columns, if the
+ * last row was not full height
+ */
+ if (lastWidthPartHeight)
+ {
+ int widthsAbove = visibleWidths - 2;
+ int ypos = repeatHeight * widthsAbove + hgap;
+ int endRes = ranges.getEndRes();
+ endRes += widthsAbove * viewportWidth;
+ int startRes = endRes - columns;
+ int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
+ * charWidth;
+ gg.translate(xOffset, 0);
+
+ /*
+ * white fill first to erase annotations
+ */
+ gg.setColor(Color.white);
+ gg.fillRect(labelWidthWest, ypos,
+ (endRes - startRes + 1) * charWidth, repeatHeight);
+
+ drawWrappedRegion(gg, startRes, endRes, canvasHeight, widthInColumns,
+ ypos);
+ gg.translate(-xOffset, 0);
+ }
+
+ /*
+ * y-offset for drawing is height of widths above,
+ * plus one gap row
+ */
+ int widthsAbove = visibleWidths - 1;
+ int ypos = repeatHeight * widthsAbove + hgap;
+ int endRes = ranges.getEndRes();
+ endRes += widthsAbove * viewportWidth;
+ endRes = Math.min(endRes, ranges.getVisibleAlignmentWidth());
+
+ /*
+ * draw one extra column than strictly needed - this is a (harmless)
+ * fudge to ensure scale marks get drawn (JAL-2636)
+ */
+ int startRes = endRes - columns;
+
+ /*
+ * x-offset is x-start modulo viewport start residue;
+ * doesn't include label West (offset is applied in drawWrappedRegion)
+ */
+
+ int leftEndColumn = ranges.getStartRes() + widthsAbove
+ * ranges.getViewportWidth();
+ // startRes = Math.max(startRes - 0, leftEndColumn);
+ int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
+ * charWidth;
+ gg.translate(xOffset, 0);
+
+ /*
+ * white fill the region to be drawn including scale left or above;
+ * extend to right hand margin so as to erase scale above when
+ * scrolling right beyond end of alignment
+ */
+ gg.setColor(Color.white);
+ int width = canvasWidth - labelWidthWest - xOffset;
+ gg.fillRect(labelWidthWest, ypos - hgap, width, repeatHeight);
+
+ gg.setFont(av.getFont());
+ gg.setColor(Color.black);
+
+ drawWrappedRegion(gg, startRes, endRes, canvasHeight, widthInColumns,
+ ypos);
+ gg.translate(-xOffset, 0);
+
+ /*
+ * draw scale right if shown, passing in the start/end columns
+ * for the whole line, not just the last few columns
+ */
+ if (av.getScaleRightWrapped())
+ {
+ drawVerticalScale(gg, leftEndColumn, endRes, ypos, false);
+ }
+
+ /*
+ * and finally, white fill any space below the visible alignment
+ * (in case it has wrapped to just the top part of the panel)
+ */
+ int heightBelow = canvasHeight - visibleWidths * repeatHeight;
+ if (heightBelow > 0)
+ {
+ gg.setColor(Color.white);
+ gg.fillRect(0, canvasHeight - heightBelow, canvasWidth, heightBelow);
+ }
+ }
+
+ /**
+ * Draws the specified number of columns at the 'start' (top left) of a
+ * wrapped alignment view, including scale above and left and annotations if
+ * shown
+ *
+ * @param columns
+ */
+ protected void fastPaintWrappedAddLeft(int columns)
+ {
+ int startRes = av.getRanges().getStartRes();
+
+ /*
+ * draw one extra column than strictly needed - this is a (harmless)
+ * fudge to ensure scale marks get drawn (JAL-2636)
+ */
+ int endx = startRes + columns;
+ int ypos = 0;
+
+ /*
+ * white fill the region to be drawn including scale left or above
+ */
+ gg.setColor(Color.white);
+ int height = getRepeatHeightWrapped();
+ gg.fillRect(0, ypos, labelWidthWest + columns * charWidth, height);
+ ypos += charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
+
+ gg.setFont(av.getFont());
+ gg.setColor(Color.black);
+
+ if (av.getScaleLeftWrapped())
+ {
+ drawVerticalScale(gg, startRes, endx, ypos, true);
+ }
+
+ int cWidth = (getWidth() - labelWidthEast - labelWidthWest) / charWidth;
+
+ drawWrappedRegion(gg, startRes, endx, getHeight(), cWidth, ypos);
+ }
+
+ /**
+ * Shifts the visible alignment by the specified number of columns - left if
+ * negative, right if positive. Includes scale above, left or right and
+ * annotations (if shown). Does not draw newly visible columns.
+ *
+ * @param positions
+ */
+ protected void shiftWrappedAlignment(int positions)
+ {
+ if (positions == 0)
+ {
+ return;
+ }
+
+ int repeatHeight = getRepeatHeightWrapped();
+ ViewportRanges ranges = av.getRanges();
+ int xMax = ranges.getVisibleAlignmentWidth();
+ int widthToCopy = (ranges.getViewportWidth() - Math.abs(positions))
+ * charWidth;
+ int canvasHeight = getHeight();
+ int visibleWidths = canvasHeight / repeatHeight;
+ if (canvasHeight % repeatHeight > 0)
+ {
+ visibleWidths++;
+ }
+ int viewportWidth = ranges.getViewportWidth();
+ int hgap = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
+
+ int remainder = canvasHeight % repeatHeight;
+ if (remainder >= (hgap + charHeight))
+ {
+ visibleWidths++;
+ }
+ // todo limit visibleWidths to not exceed width of alignment
+ // (don't process white space below)
+
+ if (positions > 0)
+ {
+ /*
+ * shift right (after scroll left)
+ * for each wrapped width (starting with the last), copy (width-positions)
+ * columns from the left margin to the right margin, and copy positions
+ * columns from the right margin of the row above (if any) to the
+ * left margin of the current row
+ */
+ int xpos = ranges.getStartRes() + (visibleWidths - 1) * viewportWidth;
+
+ /*
+ * get y-offset of last wrapped width
+ */
+ int y = canvasHeight / repeatHeight * repeatHeight;
+ int copyFromLeftStart = labelWidthWest;
+ int copyFromRightStart = copyFromLeftStart + widthToCopy;
+
+ while (y >= 0)
+ {
+ gg.copyArea(copyFromLeftStart, y, widthToCopy, repeatHeight,
+ positions * charWidth, 0);
+ if (y > 0)
+ {
+ gg.copyArea(copyFromRightStart, y - repeatHeight, positions
+ * charWidth, repeatHeight, -widthToCopy, repeatHeight);
+ }
+
+ if (av.getScaleLeftWrapped())
+ {
+ drawVerticalScale(gg, xpos, xpos + viewportWidth - 1, y + hgap,
+ true);
+ }
+ if (av.getScaleRightWrapped())
+ {
+ drawVerticalScale(gg, xpos, xpos + viewportWidth - 1, y + hgap,
+ false);
+ }
+
+ y -= repeatHeight;
+ xpos -= viewportWidth;
+ }
+ }
+ else
+ {
+ /*
+ * shift left (after scroll right)
+ * for each wrapped width (starting with the first), copy (width-positions)
+ * columns from the right margin to the left margin, and copy positions
+ * columns from the left margin of the row below (if any) to the
+ * right margin of the current row
+ */
+ int xpos = ranges.getStartRes();
+ int y = 0;
+ int copyFromRightStart = labelWidthWest - positions * charWidth;
+
+ while (y < canvasHeight)
+ {
+ gg.copyArea(copyFromRightStart, y, widthToCopy, repeatHeight,
+ positions * charWidth, 0);
+ if (y + repeatHeight < canvasHeight - repeatHeight
+ && (xpos + viewportWidth <= xMax))
+ {
+ gg.copyArea(labelWidthWest, y + repeatHeight, -positions
+ * charWidth, repeatHeight, widthToCopy, -repeatHeight);
+ }
+
+ if (av.getScaleLeftWrapped())
+ {
+ drawVerticalScale(gg, xpos, xpos + viewportWidth, y + hgap, true);
+ }
+ if (av.getScaleRightWrapped())
+ {
+ drawVerticalScale(gg, xpos, xpos + viewportWidth, y + hgap, false);
+ }
+
+ y += repeatHeight;
+ xpos += viewportWidth;
+ }
+ }
+ }
+
+ /**
+ * Redraws any positions in the search results in the visible region of a
+ * wrapped alignment. Any highlights are drawn depending on the search results
+ * set on the Viewport, not the <code>results</code> argument. This allows
+ * this method to be called either to clear highlights (passing the previous
+ * search results), or to draw new highlights.
+ *
+ * @param results
+ * @return
+ */
+ protected boolean drawMappedPositionsWrapped(SearchResultsI results)
+ {
+ if (results == null)
+ {
+ return false;
+ }
+
+ boolean matchFound = false;
+
+ int wrappedWidth = av.getWrappedWidth();
+ int wrappedHeight = getRepeatHeightWrapped();
+
+ ViewportRanges ranges = av.getRanges();
+ int canvasHeight = getHeight();
+ int repeats = canvasHeight / wrappedHeight;
+ if (canvasHeight / wrappedHeight > 0)
+ {
+ repeats++;
+ }
+
+ int firstVisibleColumn = ranges.getStartRes();
+ int lastVisibleColumn = ranges.getStartRes() + repeats
+ * ranges.getViewportWidth() - 1;
+
+ AlignmentI alignment = av.getAlignment();
+ if (av.hasHiddenColumns())
+ {
+ firstVisibleColumn = alignment.getHiddenColumns()
+ .adjustForHiddenColumns(firstVisibleColumn);
+ lastVisibleColumn = alignment.getHiddenColumns()
+ .adjustForHiddenColumns(lastVisibleColumn);
+ }
+
+ int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
+
+ for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
+ .getEndSeq(); seqNo++)
+ {
+ SequenceI seq = alignment.getSequenceAt(seqNo);
+
+ int[] visibleResults = results.getResults(seq, firstVisibleColumn,
+ lastVisibleColumn);
+ if (visibleResults != null)
+ {
+ for (int i = 0; i < visibleResults.length - 1; i += 2)
+ {
+ int firstMatchedColumn = visibleResults[i];
+ int lastMatchedColumn = visibleResults[i + 1];
+ if (firstMatchedColumn <= lastVisibleColumn
+ && lastMatchedColumn >= firstVisibleColumn)
+ {
+ /*
+ * found a search results match in the visible region
+ */
+ firstMatchedColumn = Math.max(firstMatchedColumn,
+ firstVisibleColumn);
+ lastMatchedColumn = Math.min(lastMatchedColumn,
+ lastVisibleColumn);
+
+ /*
+ * draw each mapped position separately (as contiguous positions may
+ * wrap across lines)
+ */
+ for (int mappedPos = firstMatchedColumn; mappedPos <= lastMatchedColumn; mappedPos++)
+ {
+ int displayColumn = mappedPos;
+ if (av.hasHiddenColumns())
+ {
+ displayColumn = alignment.getHiddenColumns()
+ .findColumnPosition(displayColumn);
+ }
+
+ /*
+ * transX: offset from left edge of canvas to residue position
+ */
+ int transX = labelWidthWest
+ + ((displayColumn - ranges.getStartRes()) % wrappedWidth)
+ * av.getCharWidth();
+
+ /*
+ * transY: offset from top edge of canvas to residue position
+ */
+ int transY = gapHeight;
+ transY += (displayColumn - ranges.getStartRes())
+ / wrappedWidth * wrappedHeight;
+ transY += (seqNo - ranges.getStartSeq()) * av.getCharHeight();
+
+ /*
+ * yOffset is from graphics origin to start of visible region
+ */
+ int yOffset = 0;// (displayColumn / wrappedWidth) * wrappedHeight;
+ if (transY < getHeight())
+ {
+ matchFound = true;
+ gg.translate(transX, transY);
+ drawPanel(gg, displayColumn, displayColumn, seqNo, seqNo,
+ yOffset);
+ gg.translate(-transX, -transY);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return matchFound;
+ }
+
+ /**
+ * Answers the height in pixels of a repeating section of the wrapped
+ * alignment, including space above, scale above if shown, sequences, and
+ * annotation panel if shown
+ *
+ * @return
+ */
+ protected int getRepeatHeightWrapped()
+ {
+ // gap (and maybe scale) above
+ int repeatHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
+
+ // add sequences
+ repeatHeight += av.getRanges().getViewportHeight() * charHeight;