CoordinateUtilities { public static Point2D convertDOMToGridCoordinate(final GridWidget view, final Point2D point) { Viewport viewport = view.getViewport(); Transform transform = viewport != null ? viewport.getTransform() : null; if (transform == null) { transform = new Transform(); } if (viewport != null) { view.getViewport().setTransform(transform); } transform = transform.copy().getInverse(); final Point2D p = new Point2D(point.getX(), point.getY()); transform.transform(p, p); return p.add(new Point2D(view.getComputedLocation().getX(), view.getComputedLocation().getY()).mul(-1.0)); }  static Point2D convertDOMToGridCoordinate(final GridWidget view,
                                                     final Point2D point); static Integer getUiRowIndex(final GridWidget gridWidget,
                                        final double relativeY); static Integer getUiColumnIndex(final GridWidget gridWidget,
                                           final double relativeX); static Integer getUiHeaderRowIndex(final GridWidget gridWidget,
                                              final Point2D rp); static int getRelativeXOfEvent(final DomEvent event); static int getRelativeYOfEvent(final DomEvent event);  }
@Test public void testConvertDOMToGridCoordinateNoParent() throws Exception { point = new Point2D(15D, 20D); setupGridWidget(); convertedPoint = CoordinateUtilities.convertDOMToGridCoordinate(view, point); Assertions.assertThat(convertedPoint).isNotNull(); Assertions.assertThat(convertedPoint.getX()).isEqualTo(15D); Assertions.assertThat(convertedPoint.getY()).isEqualTo(20D); }
@Test public void testConvertDOMToGridCoordinateWithParentWithoutTransform() throws Exception { point = new Point2D(15D, 20D); setupGridWidget(); when(view.getViewport()).thenReturn(mock(Viewport.class)); convertedPoint = CoordinateUtilities.convertDOMToGridCoordinate(view, point); Assertions.assertThat(convertedPoint).isNotNull(); Assertions.assertThat(convertedPoint.getX()).isEqualTo(15D); Assertions.assertThat(convertedPoint.getY()).isEqualTo(20D); }
@Test public void testConvertDOMToGridCoordinateWithParentWithTransformMove() throws Exception { final Viewport viewport = mock(Viewport.class); final Transform transform = new Transform(); transform.translate(10D, 10D); point = new Point2D(15D, 20D); setupGridWidget(); when(view.getViewport()).thenReturn(viewport); when(viewport.getTransform()).thenReturn(transform); convertedPoint = CoordinateUtilities.convertDOMToGridCoordinate(view, point); Assertions.assertThat(convertedPoint).isNotNull(); Assertions.assertThat(convertedPoint.getX()).isEqualTo(5D); Assertions.assertThat(convertedPoint.getY()).isEqualTo(10D); }
@Test public void testConvertDOMToGridCoordinateWithParentWithTransformMoveAndRotate() throws Exception { final Viewport viewport = mock(Viewport.class); final Transform transform = new Transform(); transform.translate(10D, 10D); transform.rotate(Math.PI); point = new Point2D(15D, 20D); setupGridWidget(); when(view.getViewport()).thenReturn(viewport); when(viewport.getTransform()).thenReturn(transform); convertedPoint = CoordinateUtilities.convertDOMToGridCoordinate(view, point); Assertions.assertThat(convertedPoint).isNotNull(); Assertions.assertThat(Double.valueOf(convertedPoint.getX()).intValue()).isEqualTo(-5); Assertions.assertThat(Double.valueOf(convertedPoint.getY()).intValue()).isEqualTo(-10); }