Search in sources :

Example 1 with DeletionPolicy

use of org.eclipse.sirius.components.collaborative.diagrams.dto.DeletionPolicy in project sirius-components by eclipse-sirius.

the class CanonicalBehaviors method deleteElement.

public IStatus deleteElement(VariableManager variableManager) {
    // @formatter:off
    DeletionPolicy deletionPolicy = variableManager.get(DeleteFromDiagramEventHandler.DELETION_POLICY, DeletionPolicy.class).orElse(DeletionPolicy.SEMANTIC);
    // @formatter:on
    switch(deletionPolicy) {
        case SEMANTIC:
            this.self(variableManager).ifPresent(this.editService::delete);
            break;
        case GRAPHICAL:
            var optionalDiagramContext = variableManager.get(IDiagramContext.DIAGRAM_CONTEXT, DiagramContext.class);
            if (optionalDiagramContext.isPresent()) {
                String elementId = null;
                if (variableManager.get(Node.SELECTED_NODE, Node.class).isPresent()) {
                    elementId = variableManager.get(Node.SELECTED_NODE, Node.class).get().getId();
                } else if (variableManager.get(Edge.SELECTED_EDGE, Edge.class).isPresent()) {
                    elementId = variableManager.get(Edge.SELECTED_EDGE, Edge.class).get().getId();
                }
                if (elementId != null) {
                    ViewDeletionRequest viewDeletionRequest = ViewDeletionRequest.newViewDeletionRequest().elementId(elementId).build();
                    optionalDiagramContext.get().getViewDeletionRequests().add(viewDeletionRequest);
                }
            }
            break;
        default:
            break;
    }
    return new Success();
}
Also used : DeletionPolicy(org.eclipse.sirius.components.collaborative.diagrams.dto.DeletionPolicy) Node(org.eclipse.sirius.components.diagrams.Node) ViewDeletionRequest(org.eclipse.sirius.components.diagrams.ViewDeletionRequest) Success(org.eclipse.sirius.components.representations.Success)

Example 2 with DeletionPolicy

use of org.eclipse.sirius.components.collaborative.diagrams.dto.DeletionPolicy in project sirius-components by eclipse-sirius.

the class DeleteFromDiagramEventHandler method invokeDeleteEdgeTool.

private IStatus invokeDeleteEdgeTool(Edge edge, IEditingContext editingContext, IDiagramContext diagramContext, DeletionPolicy deletionPolicy) {
    // $NON-NLS-1$
    IStatus result = new Failure("");
    var optionalEdgeDescription = this.findEdgeDescription(edge, diagramContext.getDiagram(), editingContext);
    if (optionalEdgeDescription.isPresent()) {
        var optionalSelf = this.objectService.getObject(editingContext, edge.getTargetObjectId());
        if (optionalSelf.isPresent()) {
            VariableManager variableManager = new VariableManager();
            variableManager.put(VariableManager.SELF, optionalSelf.get());
            variableManager.put(IDiagramContext.DIAGRAM_CONTEXT, diagramContext);
            variableManager.put(Edge.SELECTED_EDGE, edge);
            variableManager.put(DELETION_POLICY, deletionPolicy);
            variableManager.put(Environment.ENVIRONMENT, new Environment(Environment.SIRIUS_COMPONENTS));
            // @formatter:off
            this.diagramQueryService.findNodeById(diagramContext.getDiagram(), edge.getSourceId()).flatMap(node -> this.objectService.getObject(editingContext, node.getTargetObjectId())).ifPresent(semanticElement -> variableManager.put(EdgeDescription.SEMANTIC_EDGE_SOURCE, semanticElement));
            this.diagramQueryService.findNodeById(diagramContext.getDiagram(), edge.getTargetId()).flatMap(node -> this.objectService.getObject(editingContext, node.getTargetObjectId())).ifPresent(semanticElement -> variableManager.put(EdgeDescription.SEMANTIC_EDGE_TARGET, semanticElement));
            // @formatter:on
            EdgeDescription edgeDescription = optionalEdgeDescription.get();
            // $NON-NLS-1$
            this.logger.debug("Deleted diagram edge {}", edge.getId());
            result = edgeDescription.getDeleteHandler().apply(variableManager);
        } else {
            String message = this.messageService.semanticObjectNotFound(edge.getTargetObjectId());
            this.logger.debug(message);
            result = new Failure(message);
        }
    } else {
        String message = this.messageService.edgeDescriptionNotFound(edge.getId().toString());
        this.logger.debug(message);
        result = new Failure(message);
    }
    return result;
}
Also used : VariableManager(org.eclipse.sirius.components.representations.VariableManager) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) Edge(org.eclipse.sirius.components.diagrams.Edge) Node(org.eclipse.sirius.components.diagrams.Node) Environment(org.eclipse.sirius.components.core.api.Environment) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IDiagramInput(org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramInput) IObjectService(org.eclipse.sirius.components.core.api.IObjectService) LoggerFactory(org.slf4j.LoggerFactory) One(reactor.core.publisher.Sinks.One) DiagramDescription(org.eclipse.sirius.components.diagrams.description.DiagramDescription) NodeDescription(org.eclipse.sirius.components.diagrams.description.NodeDescription) ArrayList(java.util.ArrayList) IDiagramDescriptionService(org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramDescriptionService) Diagram(org.eclipse.sirius.components.diagrams.Diagram) IStatus(org.eclipse.sirius.components.representations.IStatus) Monitoring(org.eclipse.sirius.components.collaborative.api.Monitoring) Service(org.springframework.stereotype.Service) ICollaborativeDiagramMessageService(org.eclipse.sirius.components.collaborative.diagrams.messages.ICollaborativeDiagramMessageService) DeleteFromDiagramInput(org.eclipse.sirius.components.collaborative.diagrams.dto.DeleteFromDiagramInput) IDiagramQueryService(org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramQueryService) Counter(io.micrometer.core.instrument.Counter) ChangeKind(org.eclipse.sirius.components.collaborative.api.ChangeKind) Logger(org.slf4j.Logger) IDiagramContext(org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramContext) Failure(org.eclipse.sirius.components.representations.Failure) Success(org.eclipse.sirius.components.representations.Success) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) DeleteFromDiagramSuccessPayload(org.eclipse.sirius.components.collaborative.diagrams.dto.DeleteFromDiagramSuccessPayload) IRepresentationDescriptionSearchService(org.eclipse.sirius.components.core.api.IRepresentationDescriptionSearchService) DeletionPolicy(org.eclipse.sirius.components.collaborative.diagrams.dto.DeletionPolicy) Objects(java.util.Objects) List(java.util.List) IPayload(org.eclipse.sirius.components.core.api.IPayload) EdgeDescription(org.eclipse.sirius.components.diagrams.description.EdgeDescription) IDiagramEventHandler(org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramEventHandler) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Many(reactor.core.publisher.Sinks.Many) Optional(java.util.Optional) VariableManager(org.eclipse.sirius.components.representations.VariableManager) IStatus(org.eclipse.sirius.components.representations.IStatus) Environment(org.eclipse.sirius.components.core.api.Environment) EdgeDescription(org.eclipse.sirius.components.diagrams.description.EdgeDescription) Failure(org.eclipse.sirius.components.representations.Failure)

Example 3 with DeletionPolicy

use of org.eclipse.sirius.components.collaborative.diagrams.dto.DeletionPolicy in project sirius-components by eclipse-sirius.

the class ToolConverter method createDeleteToolHandler.

public Function<VariableManager, IStatus> createDeleteToolHandler(DeleteElementDescription deleteDescription) {
    var optionalInitialOperation = Optional.ofNullable(deleteDescription).map(DeleteElementDescription::getInitialOperation);
    if (optionalInitialOperation.isPresent()) {
        InitialOperation initialOperation = optionalInitialOperation.get();
        return variableManager -> {
            Map<String, Object> variables = variableManager.getVariables();
            // Sirius Desktop Delete Tools expect an "element" variable to be available with the value
            // of the initial invocation context (self).
            // $NON-NLS-1$
            variables.put("element", variables.get(VariableManager.SELF));
            var selectedNode = variableManager.get(Node.SELECTED_NODE, Node.class);
            if (selectedNode.isPresent()) {
                variables.put(ELEMENT_VIEW, selectedNode.get());
                var diagramContext = variableManager.get(IDiagramContext.DIAGRAM_CONTEXT, IDiagramContext.class);
                if (diagramContext.isPresent()) {
                    variableManager.put(CONTAINER_VIEW, this.getParentNode(selectedNode.get(), diagramContext.get().getDiagram()));
                }
            }
            var modelOperationHandlerSwitch = this.modelOperationHandlerSwitchProvider.getModelOperationHandlerSwitch(this.interpreter);
            return modelOperationHandlerSwitch.apply(initialOperation.getFirstModelOperations()).map(handler -> {
                return handler.handle(variables);
            }).orElse(// $NON-NLS-1$
            new Failure(""));
        };
    } else {
        // If no delete tool is defined, execute the default behavior: delete the underlying semantic element.
        return variableManager -> {
            var optionalObject = variableManager.get(VariableManager.SELF, Object.class);
            var optionalSelectedNode = variableManager.get(Node.SELECTED_NODE, Node.class);
            var optionalDiagramContext = variableManager.get(IDiagramContext.DIAGRAM_CONTEXT, IDiagramContext.class);
            if (optionalObject.isPresent()) {
                Object object = optionalObject.get();
                DeletionPolicy deletionPolicy = variableManager.get(DeleteFromDiagramEventHandler.DELETION_POLICY, DeletionPolicy.class).orElse(DeletionPolicy.SEMANTIC);
                if (DeletionPolicy.SEMANTIC == deletionPolicy) {
                    this.editService.delete(object);
                } else if (optionalDiagramContext.isPresent() && optionalSelectedNode.isPresent()) {
                    IDiagramContext diagramContext = optionalDiagramContext.get();
                    Node selectedNode = optionalSelectedNode.get();
                    // @formatter:off
                    ViewDeletionRequest viewDeletionRequest = ViewDeletionRequest.newViewDeletionRequest().elementId(selectedNode.getId()).build();
                    // @formatter:on
                    diagramContext.getViewDeletionRequests().add(viewDeletionRequest);
                }
                return new Success();
            }
            // $NON-NLS-1$
            return new Failure("");
        };
    }
}
Also used : InitialOperation(org.eclipse.sirius.viewpoint.description.tool.InitialOperation) Node(org.eclipse.sirius.components.diagrams.Node) IDiagramContext(org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramContext) Failure(org.eclipse.sirius.components.representations.Failure) IModelOperationHandlerSwitchProvider(org.eclipse.sirius.components.compatibility.api.IModelOperationHandlerSwitchProvider) ContainerDropDescription(org.eclipse.sirius.diagram.description.tool.ContainerDropDescription) BiFunction(java.util.function.BiFunction) ViewDeletionRequest(org.eclipse.sirius.components.diagrams.ViewDeletionRequest) Success(org.eclipse.sirius.components.representations.Success) InitialOperation(org.eclipse.sirius.viewpoint.description.tool.InitialOperation) InitialContainerDropOperation(org.eclipse.sirius.viewpoint.description.tool.InitialContainerDropOperation) DeletionPolicy(org.eclipse.sirius.components.collaborative.diagrams.dto.DeletionPolicy) Function(java.util.function.Function) AQLInterpreter(org.eclipse.sirius.components.interpreter.AQLInterpreter) DirectEditLabel(org.eclipse.sirius.diagram.description.tool.DirectEditLabel) Objects(java.util.Objects) DeleteElementDescription(org.eclipse.sirius.diagram.description.tool.DeleteElementDescription) List(java.util.List) DeleteFromDiagramEventHandler(org.eclipse.sirius.components.collaborative.diagrams.handlers.DeleteFromDiagramEventHandler) Diagram(org.eclipse.sirius.components.diagrams.Diagram) IStatus(org.eclipse.sirius.components.representations.IStatus) Map(java.util.Map) Optional(java.util.Optional) VariableManager(org.eclipse.sirius.components.representations.VariableManager) IEditService(org.eclipse.sirius.components.core.api.IEditService) DeletionPolicy(org.eclipse.sirius.components.collaborative.diagrams.dto.DeletionPolicy) DeleteElementDescription(org.eclipse.sirius.diagram.description.tool.DeleteElementDescription) Node(org.eclipse.sirius.components.diagrams.Node) IDiagramContext(org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramContext) ViewDeletionRequest(org.eclipse.sirius.components.diagrams.ViewDeletionRequest) Map(java.util.Map) Failure(org.eclipse.sirius.components.representations.Failure) Success(org.eclipse.sirius.components.representations.Success)

Aggregations

DeletionPolicy (org.eclipse.sirius.components.collaborative.diagrams.dto.DeletionPolicy)3 Node (org.eclipse.sirius.components.diagrams.Node)3 Success (org.eclipse.sirius.components.representations.Success)3 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 IDiagramContext (org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramContext)2 Diagram (org.eclipse.sirius.components.diagrams.Diagram)2 ViewDeletionRequest (org.eclipse.sirius.components.diagrams.ViewDeletionRequest)2 Failure (org.eclipse.sirius.components.representations.Failure)2 IStatus (org.eclipse.sirius.components.representations.IStatus)2 VariableManager (org.eclipse.sirius.components.representations.VariableManager)2 Counter (io.micrometer.core.instrument.Counter)1 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)1 ChangeKind (org.eclipse.sirius.components.collaborative.api.ChangeKind)1