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