use of org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl in project kie-wb-common by kiegroup.
the class ResizeControlImpl method doResize.
@SuppressWarnings("unchecked")
private CommandResult<CanvasViolation> doResize(final Element<? extends View<?>> element, final Double x, final Double y, final double w, final double h) {
// Calculate the new graph element's bounds.
final Point2D current = (null != x && null != y) ? new Point2D(x, y) : GraphUtils.getPosition(element.getContent());
final BoundsImpl newBounds = new BoundsImpl(new BoundImpl(current.getX(), current.getY()), new BoundImpl(current.getX() + w, current.getY() + h));
// Check the new bound values that come from the user's action do not exceed graph ones.
if (!GraphUtils.checkBoundsExceeded(canvasHandler.getDiagram().getGraph(), newBounds)) {
final CanvasViolation cv = CanvasViolationImpl.Builder.build(new BoundsExceededViolation(newBounds).setUUID(canvasHandler.getUuid()));
return new CommandResultImpl<>(CommandResult.Type.ERROR, Collections.singleton(cv));
}
// Execute the update position and update property/ies command/s on the bean instance to achieve the new bounds.
final List<Command<AbstractCanvasHandler, CanvasViolation>> commands = getResizeCommands(element, w, h);
final CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation> commandBuilder = new CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation>();
if (null != commands) {
if (null != x && null != y) {
commandBuilder.addCommand(canvasCommandFactory.updatePosition((Node<View<?>, Edge>) element, new Point2D(x, y)));
}
commands.forEach(commandBuilder::addCommand);
}
final CommandResult<CanvasViolation> resizeResults = getCommandManager().execute(canvasHandler, commandBuilder.build());
// Update the view bounds on the node content after successful resize.
if (!CommandUtils.isError(resizeResults)) {
element.getContent().setBounds(newBounds);
}
return resizeResults;
}
use of org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl in project kie-wb-common by kiegroup.
the class ConnectionAcceptorControlImplTest method testCreateConnections.
@Test
public void testCreateConnections() {
// New default connection for a graph element.
Element element = mock(Element.class);
View<?> content = mock(View.class);
BoundsImpl bounds = new BoundsImpl(new BoundImpl(0d, 0d), new BoundImpl(10d, 20d));
when(element.getContent()).thenReturn(content);
when(content.getBounds()).thenReturn(bounds);
MagnetConnection c1 = ConnectionAcceptorControlImpl.createConnection(element);
assertEquals(5, c1.getLocation().getX(), 0);
assertEquals(10, c1.getLocation().getY(), 0);
assertEquals(MagnetConnection.MAGNET_CENTER, c1.getMagnetIndex().getAsInt());
assertFalse(c1.isAuto());
// New default connection for wires.
WiresConnection wiresConnection = mock(WiresConnection.class);
when(wiresConnection.isAutoConnection()).thenReturn(true);
WiresMagnet wiresMagnet = mock(WiresMagnet.class);
when(wiresMagnet.getX()).thenReturn(122d);
when(wiresMagnet.getY()).thenReturn(543d);
when(wiresMagnet.getIndex()).thenReturn(7);
MagnetConnection c2 = ConnectionAcceptorControlImpl.createConnection(wiresConnection, wiresMagnet);
assertEquals(122, c2.getLocation().getX(), 0);
assertEquals(543, c2.getLocation().getY(), 0);
assertEquals(7, c2.getMagnetIndex().getAsInt());
assertTrue(c2.isAuto());
// Connections (view magnets) can be nullified.
assertNull(ConnectionAcceptorControlImpl.createConnection(null));
assertNull(ConnectionAcceptorControlImpl.createConnection(wiresConnection, null));
assertNull(ConnectionAcceptorControlImpl.createConnection(null, null));
}
use of org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl 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);
}
use of org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl 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())));
}
}
use of org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl in project kie-wb-common by kiegroup.
the class DMNCommonActionsToolboxFactoryTest method testBuildToolboxForBusinessKnowledgeModelType.
@Test
@SuppressWarnings("unchecked")
public void testBuildToolboxForBusinessKnowledgeModelType() {
final Node<View<BusinessKnowledgeModel>, Edge> bkmNode = new NodeImpl<>("bkmNode1");
final BusinessKnowledgeModel bkm = new BusinessKnowledgeModel();
final Bounds bounds = new BoundsImpl(new BoundImpl(0d, 0d), new BoundImpl(100d, 150d));
final View<BusinessKnowledgeModel> nodeContent = new ViewImpl<>(bkm, bounds);
bkmNode.setContent(nodeContent);
final Optional<Toolbox<?>> _toolbox = tested.build(canvasHandler, bkmNode);
assertTrue(_toolbox.isPresent());
Toolbox<?> toolbox = _toolbox.get();
assertTrue(toolbox instanceof ActionsToolbox);
final ActionsToolbox actionsToolbox = (ActionsToolbox) toolbox;
assertEquals("bkmNode1", actionsToolbox.getElementUUID());
assertEquals(2, actionsToolbox.size());
final Iterator<ToolboxAction> actionsIt = actionsToolbox.iterator();
assertEquals(deleteNodeAction, actionsIt.next());
assertEquals(editBusinessKnowledgeModelToolboxAction, actionsIt.next());
assertFalse(actionsIt.hasNext());
verify(view, times(1)).init(eq(actionsToolbox));
verify(view, times(2)).addButton(any(Glyph.class), anyString(), any(Consumer.class));
}
Aggregations