Search in sources :

Example 1 with ResizeEvent

use of org.eclipse.sirius.components.diagrams.events.ResizeEvent in project sirius-components by eclipse-sirius.

the class UpdateNodeBoundsEventHandler method handleUpdateNodeBounds.

private void handleUpdateNodeBounds(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IDiagramContext diagramContext, UpdateNodeBoundsInput diagramInput) {
    // @formatter:off
    Position newPosition = Position.newPosition().x(diagramInput.getNewPositionX()).y(diagramInput.getNewPositionY()).build();
    Size newSize = Size.newSize().width(diagramInput.getNewWidth()).height(diagramInput.getNewHeight()).build();
    // @formatter:on
    Optional<Node> optionalNode = this.diagramQueryService.findNodeById(diagramContext.getDiagram(), diagramInput.getDiagramElementId());
    if (optionalNode.isPresent()) {
        Position oldPosition = optionalNode.get().getPosition();
        // @formatter:off
        Position delta = Position.newPosition().x(oldPosition.getX() - newPosition.getX()).y(oldPosition.getY() - newPosition.getY()).build();
        // @formatter:on
        diagramContext.setDiagramEvent(new ResizeEvent(diagramInput.getDiagramElementId(), delta, newSize));
        payloadSink.tryEmitValue(new UpdateNodeBoundsSuccessPayload(diagramInput.getId(), diagramContext.getDiagram()));
        changeDescriptionSink.tryEmitNext(new ChangeDescription(DiagramChangeKind.DIAGRAM_LAYOUT_CHANGE, diagramInput.getRepresentationId(), diagramInput));
    } else {
        String message = this.messageService.nodeNotFound(String.valueOf(diagramInput.getDiagramElementId()));
        payloadSink.tryEmitValue(new ErrorPayload(diagramInput.getId(), message));
        changeDescriptionSink.tryEmitNext(new ChangeDescription(ChangeKind.NOTHING, diagramInput.getRepresentationId(), diagramInput));
    }
}
Also used : ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) Position(org.eclipse.sirius.components.diagrams.Position) Size(org.eclipse.sirius.components.diagrams.Size) Node(org.eclipse.sirius.components.diagrams.Node) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) UpdateNodeBoundsSuccessPayload(org.eclipse.sirius.components.collaborative.diagrams.dto.UpdateNodeBoundsSuccessPayload) ResizeEvent(org.eclipse.sirius.components.diagrams.events.ResizeEvent)

Example 2 with ResizeEvent

use of org.eclipse.sirius.components.diagrams.events.ResizeEvent in project sirius-components by eclipse-sirius.

the class IncrementalLayoutEngine method updateBorderNodeAccordingParentResize.

/**
 * Move the border node along the side according to the parent Size changes.
 */
private void updateBorderNodeAccordingParentResize(Optional<IDiagramEvent> optionalDiagramElementEvent, Bounds initialNodeBounds, Bounds newNodeBounds, List<BorderNodesOnSide> borderNodesPerSideList, String parentId) {
    // @formatter:off
    boolean isParentRectangleResized = optionalDiagramElementEvent.filter(ResizeEvent.class::isInstance).map(ResizeEvent.class::cast).map(ResizeEvent::getNodeId).filter(parentId::equals).isPresent();
    if (isParentRectangleResized && !initialNodeBounds.equals(newNodeBounds)) {
        EnumMap<RectangleSide, Double> sideHomotheticRatio = this.getHomotheticRatio(initialNodeBounds.getSize(), newNodeBounds.getSize());
        Size initialSize = initialNodeBounds.getSize();
        Size newSize = newNodeBounds.getSize();
        for (BorderNodesOnSide borderNodesOnSide : borderNodesPerSideList) {
            RectangleSide side = borderNodesOnSide.getSide();
            List<NodeLayoutData> borderNodes = borderNodesOnSide.getBorderNodes();
            double homotheticRatio = sideHomotheticRatio.get(side);
            for (NodeLayoutData borderNodeLayoutData : borderNodes) {
                // The border node position is done in the parent node coordinate system
                Position position = borderNodeLayoutData.getPosition();
                Size size = borderNodeLayoutData.getSize();
                if (RectangleSide.NORTH.equals(side)) {
                    borderNodeLayoutData.setPosition(Position.at((position.getX() + size.getWidth() / 2) * homotheticRatio - size.getWidth() / 2, position.getY()));
                } else if (RectangleSide.SOUTH.equals(side)) {
                    double dySouthShift = newSize.getHeight() - initialSize.getHeight();
                    borderNodeLayoutData.setPosition(Position.at((position.getX() + size.getWidth() / 2) * homotheticRatio - size.getWidth() / 2, position.getY() + dySouthShift));
                } else if (RectangleSide.WEST.equals(side)) {
                    borderNodeLayoutData.setPosition(Position.at(position.getX(), (position.getY() + size.getHeight() / 2) * homotheticRatio - size.getHeight() / 2));
                } else if (RectangleSide.EAST.equals(side)) {
                    double dxEastShift = newSize.getWidth() - initialSize.getWidth();
                    borderNodeLayoutData.setPosition(Position.at(position.getX() + dxEastShift, (position.getY() + size.getHeight() / 2) * homotheticRatio - size.getHeight() / 2));
                }
            }
        }
    }
}
Also used : Position(org.eclipse.sirius.components.diagrams.Position) Size(org.eclipse.sirius.components.diagrams.Size) ResizeEvent(org.eclipse.sirius.components.diagrams.events.ResizeEvent) RectangleSide(org.eclipse.sirius.components.diagrams.layout.incremental.utils.RectangleSide) NodeLayoutData(org.eclipse.sirius.components.diagrams.layout.incremental.data.NodeLayoutData)

Example 3 with ResizeEvent

use of org.eclipse.sirius.components.diagrams.events.ResizeEvent in project sirius-components by eclipse-sirius.

the class BorderNodeLabelPositionProvider method updateLabelPosition.

public void updateLabelPosition(Optional<IDiagramEvent> optionalDiagramElementEvent, RectangleSide side, NodeLayoutData borderNodeLayoutData) {
    LabelLayoutData label = borderNodeLayoutData.getLabel();
    if (label != null) {
        // @formatter:off
        boolean isBorderNodeMoved = optionalDiagramElementEvent.filter(MoveEvent.class::isInstance).map(MoveEvent.class::cast).map(MoveEvent::getNodeId).filter(borderNodeLayoutData.getId()::equals).isPresent();
        boolean isBorderNodeResized = optionalDiagramElementEvent.filter(ResizeEvent.class::isInstance).map(ResizeEvent.class::cast).map(ResizeEvent::getNodeId).filter(borderNodeLayoutData.getId()::equals).isPresent();
        if (borderNodeLayoutData.getLabel().getPosition().getX() == -1 || isBorderNodeMoved || isBorderNodeResized) {
            if (RectangleSide.NORTH.equals(side)) {
                label.setPosition(Position.at(-label.getTextBounds().getSize().getWidth(), -label.getTextBounds().getSize().getHeight()));
            } else if (RectangleSide.EAST.equals(side)) {
                label.setPosition(Position.at(borderNodeLayoutData.getSize().getWidth(), borderNodeLayoutData.getSize().getHeight()));
            } else {
                label.setPosition(Position.at(-label.getTextBounds().getSize().getWidth(), borderNodeLayoutData.getSize().getHeight()));
            }
        }
    }
}
Also used : LabelLayoutData(org.eclipse.sirius.components.diagrams.layout.incremental.data.LabelLayoutData) MoveEvent(org.eclipse.sirius.components.diagrams.events.MoveEvent) ResizeEvent(org.eclipse.sirius.components.diagrams.events.ResizeEvent)

Example 4 with ResizeEvent

use of org.eclipse.sirius.components.diagrams.events.ResizeEvent in project sirius-components by eclipse-sirius.

the class BorderNodePositionTests method testResizeParentNodeNorthWestEvent.

@Test
public void testResizeParentNodeNorthWestEvent() {
    DiagramLayoutData initializeDiagram = this.initializeDiagram();
    String nodeId = initializeDiagram.getChildrenNodes().get(0).getId();
    List<NodeLayoutData> borderNodes = initializeDiagram.getChildrenNodes().get(0).getBorderNodes();
    NodeSizeProvider nodeSizeProvider = new NodeSizeProvider(new ImageSizeProvider());
    IncrementalLayoutEngine incrementalLayoutEngine = new IncrementalLayoutEngine(nodeSizeProvider);
    // Decrease the parent node size
    Optional<IDiagramEvent> resizeEvent = Optional.of(new ResizeEvent(nodeId, Position.at(100, 50), Size.of(100, 50)));
    incrementalLayoutEngine.layout(resizeEvent, initializeDiagram, new LayoutConfiguratorRegistry(List.of()).getDefaultLayoutConfigurator());
    this.checkBorderNodesAfterResize(borderNodes);
    // Increase the parent node size to get the initial size
    resizeEvent = Optional.of(new ResizeEvent(nodeId, ZERO_POSITION, DEFAULT_NODE_SIZE));
    incrementalLayoutEngine.layout(resizeEvent, initializeDiagram, new LayoutConfiguratorRegistry(List.of()).getDefaultLayoutConfigurator());
    this.checkBorderNodesAtInitialPosition(borderNodes);
    this.checkBorderNodeLabel(borderNodes.get(0).getLabel(), BORDER_NODE_LABEL_TEXT_POSITION, BORDER_NODE_LABEL_TEXT_BOUNDS);
}
Also used : DiagramLayoutData(org.eclipse.sirius.components.diagrams.layout.incremental.data.DiagramLayoutData) NodeSizeProvider(org.eclipse.sirius.components.diagrams.layout.incremental.provider.NodeSizeProvider) IDiagramEvent(org.eclipse.sirius.components.diagrams.events.IDiagramEvent) LayoutConfiguratorRegistry(org.eclipse.sirius.components.diagrams.layout.LayoutConfiguratorRegistry) ImageSizeProvider(org.eclipse.sirius.components.diagrams.layout.incremental.provider.ImageSizeProvider) NodeLayoutData(org.eclipse.sirius.components.diagrams.layout.incremental.data.NodeLayoutData) ResizeEvent(org.eclipse.sirius.components.diagrams.events.ResizeEvent) Test(org.junit.jupiter.api.Test)

Example 5 with ResizeEvent

use of org.eclipse.sirius.components.diagrams.events.ResizeEvent in project sirius-components by eclipse-sirius.

the class NodeSizeProvider method getSize.

/**
 * Provides the new {@link Size} of the given node. If the node size is no need to be updated, the current size is
 * returned. If the provided {@link NodeLayoutData} is resized by the {@link ResizeEvent}, we mark it as
 * resizedByUser.
 *
 * @param optionalDiagramElementEvent
 *            The event which is currently taken into account
 * @param node
 *            the {@link NodeLayoutData} for which we want to retrieve the new size.
 * @return the new {@link Size} if updated or the current one.
 */
public Size getSize(Optional<IDiagramEvent> optionalDiagramElementEvent, NodeLayoutData node, ISiriusWebLayoutConfigurator layoutConfigurator) {
    Size size;
    if (this.isRelevantResizeEvent(optionalDiagramElementEvent, node)) {
        node.setResizedByUser(true);
        // @formatter:off
        size = optionalDiagramElementEvent.filter(ResizeEvent.class::isInstance).map(ResizeEvent.class::cast).map(ResizeEvent::getNewSize).orElse(Size.UNDEFINED);
    // @formatter:on
    } else if (this.isAlreadySized(node)) {
        size = node.getSize();
    } else {
        size = this.getInitialSize(node, layoutConfigurator);
    }
    return size;
}
Also used : Size(org.eclipse.sirius.components.diagrams.Size) ResizeEvent(org.eclipse.sirius.components.diagrams.events.ResizeEvent)

Aggregations

ResizeEvent (org.eclipse.sirius.components.diagrams.events.ResizeEvent)11 IDiagramEvent (org.eclipse.sirius.components.diagrams.events.IDiagramEvent)7 NodeLayoutData (org.eclipse.sirius.components.diagrams.layout.incremental.data.NodeLayoutData)7 Size (org.eclipse.sirius.components.diagrams.Size)6 Test (org.junit.jupiter.api.Test)6 DiagramLayoutData (org.eclipse.sirius.components.diagrams.layout.incremental.data.DiagramLayoutData)5 Position (org.eclipse.sirius.components.diagrams.Position)4 LayoutConfiguratorRegistry (org.eclipse.sirius.components.diagrams.layout.LayoutConfiguratorRegistry)4 ImageSizeProvider (org.eclipse.sirius.components.diagrams.layout.incremental.provider.ImageSizeProvider)4 NodeSizeProvider (org.eclipse.sirius.components.diagrams.layout.incremental.provider.NodeSizeProvider)4 ArrayList (java.util.ArrayList)2 Node (org.eclipse.sirius.components.diagrams.Node)2 NodePositionProvider (org.eclipse.sirius.components.diagrams.layout.incremental.provider.NodePositionProvider)2 Path (java.nio.file.Path)1 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)1 UpdateNodeBoundsSuccessPayload (org.eclipse.sirius.components.collaborative.diagrams.dto.UpdateNodeBoundsSuccessPayload)1 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)1 Diagram (org.eclipse.sirius.components.diagrams.Diagram)1 MoveEvent (org.eclipse.sirius.components.diagrams.events.MoveEvent)1 ISiriusWebLayoutConfigurator (org.eclipse.sirius.components.diagrams.layout.ISiriusWebLayoutConfigurator)1