Search in sources :

Example 21 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class LocationControlImplTest method testLocationAcceptor.

@Test
@SuppressWarnings("unchecked")
public void testLocationAcceptor() {
    tested.enable(canvasHandler);
    tested.register(element);
    ArgumentCaptor<ILocationAcceptor> locationAcceptorArgumentCaptor = ArgumentCaptor.forClass(ILocationAcceptor.class);
    verify(wiresManager, times(1)).setLocationAcceptor(locationAcceptorArgumentCaptor.capture());
    final ILocationAcceptor locationAcceptor = locationAcceptorArgumentCaptor.getValue();
    final WiresShapeView wiresContainer = mock(WiresShapeView.class);
    when(wiresContainer.getUUID()).thenReturn(ELEMENT_UUID);
    final com.ait.lienzo.client.core.types.Point2D point = new com.ait.lienzo.client.core.types.Point2D(40d, 50d);
    locationAcceptor.accept(new WiresContainer[] { wiresContainer }, new com.ait.lienzo.client.core.types.Point2D[] { point });
    ArgumentCaptor<CanvasCommand> commandArgumentCaptor = ArgumentCaptor.forClass(CanvasCommand.class);
    verify(commandManager, times(1)).execute(eq(canvasHandler), commandArgumentCaptor.capture());
    final UpdateElementPositionCommand command = (UpdateElementPositionCommand) commandArgumentCaptor.getValue();
    assertEquals(element, command.getElement());
    assertEquals(new Point2D(40d, 50d), command.getLocation());
}
Also used : ILocationAcceptor(com.ait.lienzo.client.core.shape.wires.ILocationAcceptor) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) WiresShapeView(org.kie.workbench.common.stunner.client.lienzo.shape.view.wires.WiresShapeView) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) CanvasCommand(org.kie.workbench.common.stunner.core.client.command.CanvasCommand) Test(org.junit.Test)

Example 22 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class ResizeControlImplTest method testResize.

@Test
@SuppressWarnings("unchecked")
public void testResize() {
    when(commandManager.execute(eq(canvasHandler), any(Command.class))).thenReturn(CanvasCommandResultBuilder.SUCCESS);
    tested.enable(canvasHandler);
    assertFalse(tested.isRegistered(element));
    tested.register(element);
    verify(shapeEventHandler, times(1)).supports(eq(ViewEventType.RESIZE));
    final ArgumentCaptor<ResizeHandler> resizeHandlerArgumentCaptor = ArgumentCaptor.forClass(ResizeHandler.class);
    verify(shapeEventHandler, times(1)).addHandler(eq(ViewEventType.RESIZE), resizeHandlerArgumentCaptor.capture());
    final ResizeHandler resizeHandler = resizeHandlerArgumentCaptor.getValue();
    final double x = 121.45d;
    final double y = 23.456d;
    final double width = 100d;
    final double height = 200d;
    final ResizeEvent event = new ResizeEvent(x, y, x, y, width, height);
    resizeHandler.end(event);
    final ArgumentCaptor<Command> commandArgumentCaptor = ArgumentCaptor.forClass(Command.class);
    verify(commandManager, times(1)).execute(eq(canvasHandler), commandArgumentCaptor.capture());
    final Command command = commandArgumentCaptor.getValue();
    assertNotNull(command);
    assertTrue(command instanceof AbstractCompositeCommand);
    final List commands = ((AbstractCompositeCommand) command).getCommands();
    assertNotNull(commands);
    assertEquals(4, commands.size());
    assertTrue(commands.get(0) instanceof UpdateElementPositionCommand);
    final UpdateElementPositionCommand positionCommand = (UpdateElementPositionCommand) commands.get(0);
    assertEquals(element, positionCommand.getElement());
    assertEquals(new Point2D(x, y), positionCommand.getLocation());
    assertTrue(commands.get(1) instanceof UpdateElementPropertyCommand);
    final UpdateElementPropertyCommand wPropertyCommand = (UpdateElementPropertyCommand) commands.get(1);
    assertEquals(element, wPropertyCommand.getElement());
    assertEquals(W_PROPERTY_ID, wPropertyCommand.getPropertyId());
    assertEquals(width, wPropertyCommand.getValue());
    assertTrue(commands.get(2) instanceof UpdateElementPropertyCommand);
    final UpdateElementPropertyCommand hPropertyCommand = (UpdateElementPropertyCommand) commands.get(2);
    assertEquals(element, hPropertyCommand.getElement());
    assertEquals(H_PROPERTY_ID, hPropertyCommand.getPropertyId());
    assertEquals(height, hPropertyCommand.getValue());
    assertTrue(commands.get(3) instanceof UpdateElementPropertyCommand);
    final UpdateElementPropertyCommand rPropertyCommand = (UpdateElementPropertyCommand) commands.get(3);
    assertEquals(element, rPropertyCommand.getElement());
    assertEquals(R_PROPERTY_ID, rPropertyCommand.getPropertyId());
    assertEquals(50d, rPropertyCommand.getValue());
}
Also used : UpdateElementPropertyCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPropertyCommand) AbstractCompositeCommand(org.kie.workbench.common.stunner.core.command.impl.AbstractCompositeCommand) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) Command(org.kie.workbench.common.stunner.core.command.Command) AbstractCompositeCommand(org.kie.workbench.common.stunner.core.command.impl.AbstractCompositeCommand) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) ResizeHandler(org.kie.workbench.common.stunner.core.client.shape.view.event.ResizeHandler) List(java.util.List) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) UpdateElementPropertyCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPropertyCommand) ResizeEvent(org.kie.workbench.common.stunner.core.client.shape.view.event.ResizeEvent) Test(org.junit.Test)

Example 23 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class ObserverBuilderControlTest method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ServiceCallback<Node<View<Object>, Edge>> callback = (ServiceCallback<Node<View<Object>, Edge>>) (invocationOnMock.getArguments()[2]);
            view = new ViewImpl<>(new Object(), new BoundsImpl(new BoundImpl(0.0, 0.0), new BoundImpl(10.0, 20.0)));
            Node<View<Object>, Edge> item = new NodeImpl<>("UUID");
            item.setContent(view);
            callback.onSuccess(item);
            return null;
        }
    }).when(clientFactoryServices).newElement(anyString(), anyString(), any(ServiceCallback.class));
    when(graphBoundsIndexer.setRootUUID(anyString())).thenReturn(graphBoundsIndexer);
    AdapterManager adapters = mock(AdapterManager.class);
    DefinitionAdapter definitionAdapter = mock(DefinitionAdapter.class);
    DefinitionSetRuleAdapter rulesAdapter = mock(DefinitionSetRuleAdapter.class);
    when(definitionAdapter.getId(any())).thenReturn("Object");
    when(rulesAdapter.getRuleSet(any())).thenReturn(mock(RuleSet.class));
    when(adapters.forDefinition()).thenReturn(definitionAdapter);
    when(adapters.forRules()).thenReturn(rulesAdapter);
    when(clientDefinitionManager.adapters()).thenReturn(adapters);
    when(clientDefinitionManager.definitionSets()).thenReturn(mock(TypeDefinitionSetRegistry.class));
    when(canvasCommandFactory.addNode(any(Node.class), anyString())).thenAnswer(new Answer<Command>() {

        @Override
        public Command answer(InvocationOnMock invocationOnMock) {
            Node node = (Node) invocationOnMock.getArguments()[0];
            String uid = (String) invocationOnMock.getArguments()[1];
            return new AddCanvasNodeCommand(node, uid);
        }
    });
    when(canvasCommandFactory.updatePosition(any(Node.class), any(Point2D.class))).thenAnswer(new Answer<Command>() {

        @Override
        public Command answer(InvocationOnMock invocationOnMock) {
            Node node = (Node) invocationOnMock.getArguments()[0];
            Point2D location = (Point2D) invocationOnMock.getArguments()[1];
            return new UpdateElementPositionCommand(node, location);
        }
    });
    when(canvasCommandFactory.draw()).thenReturn(mock(CanvasCommand.class));
    ShapeSet shapeSet = mock(ShapeSet.class);
    ShapeFactory shapeFactory = mock(ShapeFactory.class);
    when(shapeFactory.newShape(any())).thenReturn(mock(ElementShape.class));
    when(shapeSet.getShapeFactory()).thenReturn(shapeFactory);
    when(shapeManager.getShapeSet(anyString())).thenReturn(shapeSet);
    when(shapeManager.getDefaultShapeSet(anyString())).thenReturn(shapeSet);
    tested = new ObserverBuilderControl(clientDefinitionManager, clientFactoryServices, graphUtils, ruleManager, canvasCommandFactory, graphBoundsIndexer, canvasLayoutUtils, mock(Event.class));
    Diagram diagram = mock(Diagram.class);
    Metadata metadata = mock(Metadata.class);
    when(metadata.getCanvasRootUUID()).thenReturn("ID");
    when(diagram.getMetadata()).thenReturn(metadata);
    MutableIndex index = mock(MutableIndex.class);
    Graph graph = mock(Graph.class);
    DefinitionSet graphContent = mock(DefinitionSet.class);
    when(graphContent.getBounds()).thenReturn(new BoundsImpl(new BoundImpl(10d, 10d), new BoundImpl(100d, 100d)));
    when(graph.getContent()).thenReturn(graphContent);
    when(index.getGraph()).thenReturn(graph);
    when(graphIndexBuilder.build(any(Graph.class))).thenReturn(index);
    canvasHandler = new CanvasHandlerImpl(clientDefinitionManager, canvasCommandFactory, clientFactoryServices, ruleManager, graphUtils, graphIndexBuilder, shapeManager, mock(TextPropertyProviderFactory.class), mock(Event.class), null, null, null);
    canvasHandler.handle(mock(AbstractCanvas.class));
    canvasHandler.draw(diagram, mock(ParameterizedCommand.class));
    when(diagram.getGraph()).thenReturn(graph);
    when(graph.nodes()).thenReturn(Collections.emptyList());
    CanvasCommandManager commandManager = mock(CanvasCommandManager.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Command command = (Command) invocationOnMock.getArguments()[1];
            command.execute(invocationOnMock.getArguments()[0]);
            return null;
        }
    }).when(commandManager).execute(any(), any(Command.class));
    when(commandManagerProvider.getCommandManager()).thenReturn(commandManager);
    tested.enable(canvasHandler);
    tested.setCommandManagerProvider(commandManagerProvider);
}
Also used : TypeDefinitionSetRegistry(org.kie.workbench.common.stunner.core.registry.definition.TypeDefinitionSetRegistry) Node(org.kie.workbench.common.stunner.core.graph.Node) BoundImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl) ShapeSet(org.kie.workbench.common.stunner.core.client.ShapeSet) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) ShapeFactory(org.kie.workbench.common.stunner.core.client.shape.factory.ShapeFactory) ObserverBuilderControl(org.kie.workbench.common.stunner.core.client.canvas.controls.builder.impl.ObserverBuilderControl) Matchers.anyString(org.mockito.Matchers.anyString) DefinitionAdapter(org.kie.workbench.common.stunner.core.definition.adapter.DefinitionAdapter) ServiceCallback(org.kie.workbench.common.stunner.core.client.service.ServiceCallback) AdapterManager(org.kie.workbench.common.stunner.core.definition.adapter.AdapterManager) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) ElementShape(org.kie.workbench.common.stunner.core.client.shape.ElementShape) ViewImpl(org.kie.workbench.common.stunner.core.graph.content.view.ViewImpl) BoundsImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) CanvasCommand(org.kie.workbench.common.stunner.core.client.command.CanvasCommand) RuleSet(org.kie.workbench.common.stunner.core.rule.RuleSet) MutableIndex(org.kie.workbench.common.stunner.core.graph.processing.index.MutableIndex) CanvasHandlerImpl(org.kie.workbench.common.stunner.core.client.canvas.CanvasHandlerImpl) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Diagram(org.kie.workbench.common.stunner.core.diagram.Diagram) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Graph(org.kie.workbench.common.stunner.core.graph.Graph) CanvasCommand(org.kie.workbench.common.stunner.core.client.command.CanvasCommand) AddCanvasNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddCanvasNodeCommand) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) Command(org.kie.workbench.common.stunner.core.command.Command) AbstractCanvas(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CanvasCommandManager(org.kie.workbench.common.stunner.core.client.command.CanvasCommandManager) AddCanvasNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddCanvasNodeCommand) DefinitionSetRuleAdapter(org.kie.workbench.common.stunner.core.definition.adapter.DefinitionSetRuleAdapter) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) Edge(org.kie.workbench.common.stunner.core.graph.Edge) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) Before(org.junit.Before)

Example 24 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class AbstractRegistrationCanvasNodeCommand method register.

@SuppressWarnings("unchecked")
protected void register(final AbstractCanvasHandler context) {
    context.register(shapeSetId, candidate);
    // Update view bounds for the element, if not set, by using the values from the shape view.
    final double[] size = GraphUtils.getNodeSize(candidate.getContent());
    if (size[0] <= 0 || size[1] <= 0) {
        final Shape shape = context.getCanvas().getShape(candidate.getUUID());
        final ShapeView shapeView = shape.getShapeView();
        final Point2D location = GraphUtils.getPosition(candidate.getContent());
        final BoundingBox boundingBox = shapeView.getBoundingBox();
        candidate.getContent().setBounds(new BoundsImpl(new BoundImpl(location.getX(), location.getY()), new BoundImpl(location.getX() + boundingBox.getWidth(), location.getY() + boundingBox.getHeight())));
    }
}
Also used : Shape(org.kie.workbench.common.stunner.core.client.shape.Shape) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) BoundingBox(org.kie.workbench.common.stunner.core.client.shape.view.BoundingBox) BoundImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl) ShapeView(org.kie.workbench.common.stunner.core.client.shape.view.ShapeView) BoundsImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl)

Example 25 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class AbstractElementBuilderControl method build.

@Override
@SuppressWarnings("unchecked")
public void build(final ElementBuildRequest<AbstractCanvasHandler> request, final BuildCallback buildCallback) {
    if (null == canvasHandler) {
        buildCallback.onSuccess(null);
        return;
    }
    double x = request.getX();
    ;
    double y = request.getY();
    ;
    if (checkOutOfBoundsCanvas(x, y)) {
        buildCallback.onError(new ClientRuntimeError(new ElementOutOfBoundsException("Element is placed outside canvas bounds")));
        return;
    }
    final Object definition = request.getDefinition();
    // Notify processing starts.
    fireProcessingStarted();
    final Node<View<?>, Edge> parent = getParent(x, y);
    final Point2D childCoordinates = getChildCoordinates(parent, x, y);
    getCommands(definition, parent, childCoordinates.getX(), childCoordinates.getY(), new CommandsCallback() {

        @Override
        public void onComplete(final String uuid, final List<Command<AbstractCanvasHandler, CanvasViolation>> commands) {
            getCommandManager().execute(canvasHandler, new CompositeCommand.Builder().addCommands(commands).build());
            buildCallback.onSuccess(uuid);
            // Notify processing ends.
            fireProcessingCompleted();
        }

        @Override
        public void onError(final ClientRuntimeError error) {
            buildCallback.onError(error);
            // Notify processing ends.
            fireProcessingCompleted();
        }
    });
}
Also used : ElementOutOfBoundsException(org.kie.workbench.common.stunner.core.client.canvas.controls.exceptions.ElementOutOfBoundsException) RuleContextBuilder(org.kie.workbench.common.stunner.core.rule.context.impl.RuleContextBuilder) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) Command(org.kie.workbench.common.stunner.core.command.Command) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) ClientRuntimeError(org.kie.workbench.common.stunner.core.client.service.ClientRuntimeError) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Aggregations

Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)55 Test (org.junit.Test)25 Edge (org.kie.workbench.common.stunner.core.graph.Edge)18 View (org.kie.workbench.common.stunner.core.graph.content.view.View)17 Node (org.kie.workbench.common.stunner.core.graph.Node)15 Command (org.kie.workbench.common.stunner.core.command.Command)10 BoundImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl)10 BoundsImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl)10 Before (org.junit.Before)9 Bounds (org.kie.workbench.common.stunner.core.graph.content.Bounds)7 AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)6 CompositeCommand (org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)6 TestingGraphMockHandler (org.kie.workbench.common.stunner.core.TestingGraphMockHandler)5 UpdateElementPositionCommand (org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand)5 Ignore (org.junit.Ignore)4 Element (org.kie.workbench.common.stunner.core.graph.Element)4 AbstractCanvas (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas)3 CanvasCommand (org.kie.workbench.common.stunner.core.client.command.CanvasCommand)3 ShapeView (org.kie.workbench.common.stunner.core.client.shape.view.ShapeView)3 CommandResult (org.kie.workbench.common.stunner.core.command.CommandResult)3