GridTransformMediator implements TransformMediator { @Override public Transform adjust(final Transform transform, final Bounds visibleBounds) { Transform newTransform = transform.copy(); final double minX = gridWidget.getX(); final double minY = gridWidget.getY(); final double gridWidth = gridWidget.getWidth(); final double gridHeight = gridWidget.getHeight(); final double maxX = gridWidget.getX() + gridWidth; final double maxY = gridWidget.getY() + gridHeight; final double scaleX = transform.getScaleX(); final double scaleY = transform.getScaleY(); final double translateX = newTransform.getTranslateX(); final double translateY = newTransform.getTranslateY(); final double scaledTranslateX = translateX / scaleX; final double scaledTranslateY = translateY / scaleY; final double visibleBoundsWidth = visibleBounds.getWidth(); final double visibleBoundsHeight = visibleBounds.getHeight(); if (gridWidth <= visibleBoundsWidth) { newTransform = newTransform.translate(-scaledTranslateX - minX, 0); } else { if (-scaledTranslateX < minX) { newTransform = newTransform.translate(-scaledTranslateX - minX, 0); } if (-scaledTranslateX + visibleBoundsWidth > maxX) { newTransform = newTransform.translate(-scaledTranslateX + visibleBoundsWidth - maxX, 0); } } if (gridHeight <= visibleBoundsHeight) { newTransform = newTransform.translate(0, -scaledTranslateY - minY); } else { if (-scaledTranslateY < minY) { newTransform = newTransform.translate(0, -scaledTranslateY - minY); } if (-scaledTranslateY + visibleBoundsHeight > maxY) { newTransform = newTransform.translate(0, -scaledTranslateY + visibleBoundsHeight - maxY); } } return newTransform; } GridTransformMediator(final GridWidget gridWidget); @Override Transform adjust(final Transform transform,
                            final Bounds visibleBounds);  }
@Test public void testGridNarrowerThanVisibleBounds() { when(grid.getX()).thenReturn(10.0); when(grid.getY()).thenReturn(0.0); when(grid.getWidth()).thenReturn(100.0); when(grid.getHeight()).thenReturn(100.0); final Bounds visibleBounds = new BaseBounds(-500, -500, 1000, 1000); final Transform test = new Transform().translate(1200.0, 0.0); final Transform result = restriction.adjust(test, visibleBounds); assertNotNull(result); assertEquals(-10.0, result.getTranslateX(), 0.0); assertEquals(0.0, result.getTranslateY(), 0.0); }
@Test public void testGridShorterThanVisibleBounds() { when(grid.getX()).thenReturn(0.0); when(grid.getY()).thenReturn(10.0); when(grid.getWidth()).thenReturn(100.0); when(grid.getHeight()).thenReturn(100.0); final Bounds visibleBounds = new BaseBounds(-500, -500, 1000, 1000); final Transform test = new Transform().translate(1200.0, 0.0); final Transform result = restriction.adjust(test, visibleBounds); assertNotNull(result); assertEquals(0.0, result.getTranslateX(), 0.0); assertEquals(-10.0, result.getTranslateY(), 0.0); }
@Test public void testGridWiderThanVisibleBoundsLeftEdge() { when(grid.getX()).thenReturn(10.0); when(grid.getY()).thenReturn(0.0); when(grid.getWidth()).thenReturn(1100.0); when(grid.getHeight()).thenReturn(100.0); final Bounds visibleBounds = new BaseBounds(-500, -500, 1000, 1000); final Transform test = new Transform().translate(1200.0, 0.0); final Transform result = restriction.adjust(test, visibleBounds); assertNotNull(result); assertEquals(-10.0, result.getTranslateX(), 0.0); assertEquals(0.0, result.getTranslateY(), 0.0); }
@Test public void testGridWiderThanVisibleBoundsRightEdge() { when(grid.getX()).thenReturn(10.0); when(grid.getY()).thenReturn(0.0); when(grid.getWidth()).thenReturn(1100.0); when(grid.getHeight()).thenReturn(100.0); final Bounds visibleBounds = new BaseBounds(-500, -500, 1000, 1000); final Transform test = new Transform().translate(-200.0, 0.0); final Transform result = restriction.adjust(test, visibleBounds); assertNotNull(result); assertEquals(-110.0, result.getTranslateX(), 0.0); assertEquals(0.0, result.getTranslateY(), 0.0); }
@Test public void testGridTallerThanVisibleBoundsTopEdge() { when(grid.getX()).thenReturn(0.0); when(grid.getY()).thenReturn(10.0); when(grid.getWidth()).thenReturn(100.0); when(grid.getHeight()).thenReturn(1100.0); final Bounds visibleBounds = new BaseBounds(-500, -500, 1000, 1000); final Transform test = new Transform().translate(0.0, 1200.0); final Transform result = restriction.adjust(test, visibleBounds); assertNotNull(result); assertEquals(0.0, result.getTranslateX(), 0.0); assertEquals(-10.0, result.getTranslateY(), 0.0); }
@Test public void testGridTallerThanVisibleBoundsBottomEdge() { when(grid.getX()).thenReturn(0.0); when(grid.getY()).thenReturn(10.0); when(grid.getWidth()).thenReturn(100.0); when(grid.getHeight()).thenReturn(1100.0); final Bounds visibleBounds = new BaseBounds(-500, -500, 1000, 1000); final Transform test = new Transform().translate(0.0, -200.0); final Transform result = restriction.adjust(test, visibleBounds); assertNotNull(result); assertEquals(0.0, result.getTranslateX(), 0.0); assertEquals(-110.0, result.getTranslateY(), 0.0); }