private void clearState() { state.getGhost().ifPresent(WiresShapeViewExt::destroy); state.setGhost(Optional.empty()); state.setOriginalIndex(OptionalInt.empty()); state.setOriginalParent(Optional.empty()); }
@Override public boolean acceptContainment(final WiresContainer wiresContainer, final WiresShape[] wiresShapes) { if (state.getGhost().isPresent() && containmentAllowed(wiresContainer, wiresShapes)) { final CaseManagementShapeView container = (CaseManagementShapeView) wiresContainer; final CaseManagementShapeView ghost = state.getGhost().get(); final int index = container.getIndex(ghost); if (index >= 0) { final OptionalInt newIndex = OptionalInt.of(index); final Optional<WiresContainer> originalContainer = state.getOriginalParent(); final OptionalInt originalIndex = state.getOriginalIndex(); final CommandResult<CanvasViolation> result = getCommandManager().execute(getCanvasHandler(), makeAddMutationCommand(wiresShapes[0], wiresContainer, newIndex, originalContainer, originalIndex)); return !CommandUtils.isError(result); } } return false; }
private void restore(final CaseManagementShapeView ghost) { state.getOriginalParent().ifPresent(originalParent -> state.getOriginalIndex().ifPresent(originalIndex -> restore(ghost, originalParent, originalIndex) ) ); }
@Override public void execute() { state.getGhost().ifPresent(ghost -> state.getOriginalParent().ifPresent(originalParent -> reparentDraggedShape(ghost, originalParent) ) ); clearState(); batch(); }
@Test public void testClear() { control.clear(); verify(ghost, times(1)).destroy(); verify(state, times(1)).setGhost(Optional.empty()); verify(state, times(1)).setOriginalParent(Optional.empty()); verify(state, times(1)).setOriginalIndex(OptionalInt.empty()); }
@Override public void reset() { state.getGhost().ifPresent(this::restore); clearState(); }
@Test public void testReparentDraggedShape() { when(state.getOriginalParent()).thenReturn(Optional.of(mock(WiresContainer.class))); final CaseManagementShapeView ghostParent = mock(CaseManagementShapeView.class); when(ghost.getParent()).thenReturn(ghostParent); when(ghostParent.getIndex(eq(ghost))).thenReturn(1); control.execute(); verify(containmentControl, times(1)).execute(); verify(ghost, atLeast(1)).removeFromParent(); verify(ghostParent, times(1)).addShape(eq(shape), eq(1)); }
@Test public void testAcceptContainment() { control.init(canvasHandler); final WiresShape parentShape = makeWiresShape(PARENT_UUID); final WiresShape childShape = makeWiresShape(CANDIDATE_UUID); final CaseManagementShapeView ghost = makeWiresShape(CANDIDATE_UUID); state.setGhost(Optional.of(ghost)); parentShape.add(ghost); final boolean isAccept = control.containmentAcceptor.acceptContainment(parentShape, new WiresShape[]{childShape}); assertTrue(isAccept); verify(canvasCommandFactory, times(1)).setChildNode(any(Node.class), any(Node.class), eq(OptionalInt.of(0)), eq(Optional.empty()), eq(OptionalInt.empty())); }
@Before public void setup() { this.state = new CaseManagementContainmentStateHolder(); this.factory = new CaseManagementControlFactory(state); }
@Test public void testReset() { control.reset(); verify(ghost, times(1)).removeFromParent(); verify(parent, times(1)).addShape(eq(shape), eq(0)); verify(state, times(1)).setGhost(Optional.empty()); verify(state, times(1)).setOriginalParent(Optional.empty()); verify(state, times(1)).setOriginalIndex(OptionalInt.empty()); }
@Override public boolean accept() { if (null == getParent() || getParent() instanceof WiresLayer) { return false; } if (!(state.getOriginalParent().isPresent() || state.getOriginalIndex().isPresent())) { return false; } // Execute. return getWiresManager() .getContainmentAcceptor() .acceptContainment(getParent(), new WiresShape[]{getShape()}); }
@Override public boolean onMove(double dx, double dy) { containmentControl.onMove(dx, dy); //Handle moving ghost from one container to another final Optional<CaseManagementShapeView> ghost = state.getGhost(); if (ghost.isPresent() && null != getParent() && null != getParent().getGroup()) { if (getWiresManager().getContainmentAcceptor().containmentAllowed(getParent(), new WiresShape[]{getShape()})) { final double mouseX = containmentControl.getParentPickerControl().getShapeLocationControl().getMouseStartX() + dx; final double mouseY = containmentControl.getParentPickerControl().getShapeLocationControl().getMouseStartY() + dy; final Point2D parentAbsLoc = getParent().getGroup().getComputedLocation(); final Point2D mouseRelativeLoc = new Point2D(mouseX - parentAbsLoc.getX(), mouseY - parentAbsLoc.getY()); //Children contains m_ghost and others excluding m_shape. This therefore moves m_ghost within children. getParent().getLayoutHandler().add(ghost.get(), getParent(), mouseRelativeLoc); containmentControl.getParentPickerControl().rebuildPicker(); } } return false; }
@Before public void setup() { this.state = new CaseManagementContainmentStateHolder(); this.control = new CaseManagementContainmentAcceptorControlImpl(canvasCommandFactory, state);
@Test public void testOnMoveStart() { final double x = 15.5d; final double y = 21.63d; control.onMoveStart(x, y); verify(containmentControl, times(1)).onMoveStart(eq(x), eq(y)); verify(state, times(1)).setGhost(eq(Optional.of(ghost))); verify(state, times(1)).setOriginalIndex(eq(OptionalInt.of(0))); verify(state, times(1)).setOriginalParent(eq(Optional.of(parent))); verify(parent, times(1)).logicallyReplace(eq(shape), eq(ghost)); }
@Override public void onMoveStart(double x, double y) { containmentControl.onMoveStart(x, y); if (!(getShape() instanceof CaseManagementShapeView)) { state.setGhost(Optional.empty()); state.setOriginalIndex(OptionalInt.empty()); state.setOriginalParent(Optional.empty()); return; } state.setOriginalParent(Optional.ofNullable(getParent())); state.setOriginalIndex(getShapeIndex()); state.setGhost(Optional.ofNullable(((CaseManagementShapeView) getShape()).getGhost())); final WiresParentPickerControl.Index index = containmentControl.getParentPickerControl().getIndex(); index.clear(); if (state.getGhost().isPresent()) { index.exclude(state.getGhost().get()); } if ((getParent() instanceof CaseManagementShapeView)) { ((CaseManagementShapeView) getParent()).logicallyReplace((CaseManagementShapeView) getShape(), state.getGhost().get()); } }
@Before public void setup() { layer = spy(new Layer()); final Group shapeGroup = mock(Group.class); when(shapeGroup.getLayer()).thenReturn(layer); when(shape.getGroup()).thenReturn(shapeGroup); when(shape.getWiresManager()).thenReturn(wiresManager); doReturn(ghost).when(shape).getGhost(); when(wiresManager.getContainmentAcceptor()).thenReturn(containmentAcceptor); when(parentPickerControl.getPickerOptions()).thenReturn(PICKER_OPTIONS); when(parentPickerControl.getShapeLocationControl()).thenReturn(shapeLocationControl); when(parentPickerControl.getIndex()).thenReturn(index); when(containmentControl.getParentPickerControl()).thenReturn(parentPickerControl); when(containmentControl.getShape()).thenReturn(shape); when(containmentControl.getParent()).thenReturn(parent); final NFastArrayList<WiresShape> children = new NFastArrayList<>(); children.add(shape); children.add(ghost); when(parent.getChildShapes()).thenReturn(children); when(parent.getLayoutHandler()).thenReturn(parentLayoutHandler); when(parent.getIndex(eq(shape))).thenReturn(0); when(parent.getGroup()).thenReturn(new Group()); when(state.getGhost()).thenReturn(Optional.of(ghost)); when(state.getOriginalParent()).thenReturn(Optional.of(parent)); when(state.getOriginalIndex()).thenReturn(OptionalInt.of(0)); control = new CaseManagementContainmentControl(containmentControl, state); }
@Test public void testExecuteAndRestoreGhost() { control.execute(); verify(parent, never()).logicallyReplace(eq(ghost), eq(shape)); verify(ghost, times(1)).destroy(); verify(state, times(1)).setGhost(Optional.empty()); verify(state, times(1)).setOriginalParent(Optional.empty()); verify(state, times(1)).setOriginalIndex(OptionalInt.empty()); verify(layer, times(1)).batch(); }
@Test public void testOnMoveStartButNoCMShape() { final WiresShape aShape = mock(WiresShape.class); when(containmentControl.getShape()).thenReturn(aShape); final double x = 15.5d; final double y = 21.63d; control.onMoveStart(x, y); verify(containmentControl, times(1)).onMoveStart(eq(x), eq(y)); verify(state, times(1)).setGhost(eq(Optional.empty())); verify(state, times(1)).setOriginalIndex(eq(OptionalInt.empty())); verify(state, times(1)).setOriginalParent(eq(Optional.empty())); verify(parent, never()).logicallyReplace(any(CaseManagementShapeView.class), any(CaseManagementShapeView.class)); }