| package org.eclipse.nebula.widgets.nattable.core.layer.impl.viewport |
| |
| import java.io.Serializable |
| import java.math.BigDecimal |
| import org.eclipse.nebula.widgets.nattable.core.layer.axis.Axis |
| import org.eclipse.nebula.widgets.nattable.core.layer.axis.impl.AbstractAxis |
| |
| import static extension org.eclipse.nebula.widgets.nattable.core.layer.axis.AxisInvariants.* |
| import static extension org.eclipse.nebula.widgets.nattable.core.math.BigDecimalExtensions.* |
| |
| class ViewportAxis extends AbstractAxis { |
| |
| val Axis underlyingAxis |
| val ViewportAxisListener viewportAxisListener |
| |
| /** |
| * Indicates the visible size of the axis. Note that the pixel locations returned by this ClientDimensionProvider |
| * must be in terms of underlying axis pixel coordinates. |
| */ |
| BigDecimal visiblePixelRange = new BigDecimal(-1.0) |
| |
| BigDecimal pixelOrigin = new BigDecimal(0) |
| |
| new(Axis underlyingAxis, ViewportAxisListener viewportAxisListener) { |
| this.underlyingAxis = underlyingAxis |
| this.viewportAxisListener = viewportAxisListener |
| } |
| |
| def getUnderlyingAxis() { underlyingAxis } |
| |
| def getVisiblePixelRange() { |
| if (visiblePixelRange >= new BigDecimal(0)) visiblePixelRange |
| else underlyingAxis.pixelSize |
| } |
| |
| def setVisiblePixelRange(double visiblePixelRange) { |
| setVisiblePixelRange(new BigDecimal(visiblePixelRange)) |
| } |
| |
| def setVisiblePixelRange(BigDecimal visiblePixelRange) { |
| this.visiblePixelRange = visiblePixelRange |
| } |
| |
| def getPixelOrigin() { pixelOrigin } |
| |
| def void setPixelOrigin(double pixelOrigin) { |
| setPixelOrigin(new BigDecimal(pixelOrigin)) |
| } |
| |
| def void setPixelOrigin(BigDecimal pixelOrigin) { |
| this.pixelOrigin = pixelOrigin |
| viewportAxisListener?.viewportAxisChanged |
| } |
| |
| def long getOriginSegmentPosition() { |
| underlyingAxis.getSegmentPositionOfPixelLocation(pixelOrigin) |
| } |
| |
| def void handleResize() { |
| // If the visible pixel size is greater than the content size, move origin to fill as much content as possible. |
| val additionalSize = pixelOrigin + getVisiblePixelRange - underlyingAxis.pixelSize |
| if (additionalSize > new BigDecimal(0)) |
| pixelOrigin = new BigDecimal(0).max(pixelOrigin - additionalSize) |
| } |
| |
| // Layer interface |
| |
| override getSegmentCount() { |
| val endPixel = pixelOrigin + getVisiblePixelRange |
| val endSegmentPosition = underlyingAxis.getSegmentPositionOfPixelLocation(endPixel) |
| val isEndPixelInsideEndSegment = endPixel > underlyingAxis.getStartPixelOfSegmentPosition(endSegmentPosition) && endPixel < underlyingAxis.getStartPixelOfSegmentPosition(underlyingAxis.segmentCount) |
| (endSegmentPosition + if (isEndPixelInsideEndSegment) 1 else 0) - originSegmentPosition |
| } |
| |
| override getStartPixelOfSegmentPosition(long segmentPosition) { |
| underlyingAxis.getStartPixelOfSegmentPosition(originSegmentPosition + segmentPosition) - pixelOrigin |
| } |
| |
| override getSegmentPositionOfPixelLocation(BigDecimal pixelLocation) { |
| val underlyingPixelLocation = pixelOrigin + pixelLocation |
| if (underlyingPixelLocation < new BigDecimal(0)) return -1 |
| if (underlyingPixelLocation >= underlyingAxis.pixelSize) return segmentCount |
| underlyingAxis.getSegmentPositionOfPixelLocation(underlyingPixelLocation) - originSegmentPosition |
| } |
| |
| override getIdOfSegmentPosition(long segmentPosition) { |
| underlyingAxis.getIdOfSegmentPosition(originSegmentPosition + segmentPosition) |
| } |
| |
| override getSegmentPositionOfId(Serializable segmentId) { |
| underlyingAxis.getSegmentPositionOfId(segmentId) - originSegmentPosition |
| } |
| |
| } |