Search in sources :

Example 1 with InitialOperation

use of org.eclipse.sirius.viewpoint.description.tool.InitialOperation in project sirius-components by eclipse-sirius.

the class WidgetDescriptionConverter method convertSelect.

private SelectDescription convertSelect(org.eclipse.sirius.properties.SelectDescription selectDescription) {
    // @formatter:off
    StringValueProvider labelProvider = new StringValueProvider(this.interpreter, selectDescription.getLabelExpression());
    Function<VariableManager, String> valueProvider = variableManager -> {
        String valueExpression = selectDescription.getValueExpression();
        return this.interpreter.evaluateExpression(variableManager.getVariables(), valueExpression).asObject().map(this.objectService::getId).orElse(null);
    };
    // @formatter:on
    Function<VariableManager, List<?>> optionsProvider = (variableManager) -> {
        String candidateExpression = selectDescription.getCandidatesExpression();
        return this.interpreter.evaluateExpression(variableManager.getVariables(), candidateExpression).asObjects().orElse(new ArrayList<>());
    };
    // $NON-NLS-1$
    String candidateDisplayExpression = Optional.ofNullable(selectDescription.getCandidateDisplayExpression()).orElse("");
    StringValueProvider optionLabelProvider = new StringValueProvider(this.interpreter, candidateDisplayExpression);
    Function<VariableManager, String> optionIdProvider = variableManager -> {
        Object candidate = variableManager.getVariables().get(SelectComponent.CANDIDATE_VARIABLE);
        return this.objectService.getId(candidate);
    };
    BiFunction<VariableManager, String, IStatus> newValueHandler = (variableManager, newValue) -> {
        Map<String, Object> variables = variableManager.getVariables();
        variables.put(NEW_VALUE, newValue);
        InitialOperation initialOperation = selectDescription.getInitialOperation();
        ModelOperation modelOperation = initialOperation.getFirstModelOperations();
        var modelOperationHandlerSwitch = this.modelOperationHandlerSwitchProvider.getModelOperationHandlerSwitch(this.interpreter);
        Optional<IModelOperationHandler> optionalModelOperationHandler = modelOperationHandlerSwitch.apply(modelOperation);
        return optionalModelOperationHandler.map(handler -> {
            return handler.handle(variables);
        }).orElse(// $NON-NLS-1$
        new Failure(""));
    };
    // @formatter:off
    return SelectDescription.newSelectDescription(this.identifierProvider.getIdentifier(selectDescription)).idProvider(new WidgetIdProvider()).labelProvider(labelProvider).valueProvider(valueProvider).optionsProvider(optionsProvider).optionIdProvider(optionIdProvider).optionLabelProvider(optionLabelProvider).newValueHandler(newValueHandler).diagnosticsProvider((variableManager) -> List.of()).kindProvider(// $NON-NLS-1$
    (object) -> "").messageProvider(// $NON-NLS-1$
    (object) -> "").build();
// @formatter:on
}
Also used : VariableManager(org.eclipse.sirius.components.representations.VariableManager) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IObjectService(org.eclipse.sirius.components.core.api.IObjectService) BiFunction(java.util.function.BiFunction) InitialOperation(org.eclipse.sirius.viewpoint.description.tool.InitialOperation) LoggerFactory(org.slf4j.LoggerFactory) SelectComponent(org.eclipse.sirius.components.forms.components.SelectComponent) Function(java.util.function.Function) ArrayList(java.util.ArrayList) TextareaDescription(org.eclipse.sirius.components.forms.description.TextareaDescription) IStatus(org.eclipse.sirius.components.representations.IStatus) StringValueProvider(org.eclipse.sirius.components.compatibility.utils.StringValueProvider) TextfieldDescription(org.eclipse.sirius.components.forms.description.TextfieldDescription) Map(java.util.Map) WidgetDescription(org.eclipse.sirius.properties.WidgetDescription) CheckboxDescription(org.eclipse.sirius.components.forms.description.CheckboxDescription) Logger(org.slf4j.Logger) AbstractWidgetDescription(org.eclipse.sirius.components.forms.description.AbstractWidgetDescription) Failure(org.eclipse.sirius.components.representations.Failure) IModelOperationHandlerSwitchProvider(org.eclipse.sirius.components.compatibility.api.IModelOperationHandlerSwitchProvider) SelectDescription(org.eclipse.sirius.components.forms.description.SelectDescription) BooleanValueProvider(org.eclipse.sirius.components.compatibility.utils.BooleanValueProvider) IModelOperationHandler(org.eclipse.sirius.components.compatibility.api.IModelOperationHandler) AQLInterpreter(org.eclipse.sirius.components.interpreter.AQLInterpreter) Objects(java.util.Objects) RadioComponent(org.eclipse.sirius.components.forms.components.RadioComponent) List(java.util.List) ModelOperation(org.eclipse.sirius.viewpoint.description.tool.ModelOperation) Optional(java.util.Optional) RadioDescription(org.eclipse.sirius.components.forms.description.RadioDescription) VariableManager(org.eclipse.sirius.components.representations.VariableManager) Collections(java.util.Collections) IIdentifierProvider(org.eclipse.sirius.components.compatibility.api.IIdentifierProvider) InitialOperation(org.eclipse.sirius.viewpoint.description.tool.InitialOperation) IStatus(org.eclipse.sirius.components.representations.IStatus) StringValueProvider(org.eclipse.sirius.components.compatibility.utils.StringValueProvider) Optional(java.util.Optional) ArrayList(java.util.ArrayList) ModelOperation(org.eclipse.sirius.viewpoint.description.tool.ModelOperation) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Failure(org.eclipse.sirius.components.representations.Failure)

Example 2 with InitialOperation

use of org.eclipse.sirius.viewpoint.description.tool.InitialOperation in project sirius-components by eclipse-sirius.

the class WidgetDescriptionConverter method convertCheckbox.

private CheckboxDescription convertCheckbox(org.eclipse.sirius.properties.CheckboxDescription checkboxDescription) {
    StringValueProvider labelProvider = new StringValueProvider(this.interpreter, checkboxDescription.getLabelExpression());
    BiFunction<VariableManager, Boolean, IStatus> newValueHandler = (variableManager, newValue) -> {
        Map<String, Object> variables = variableManager.getVariables();
        variables.put(NEW_VALUE, newValue);
        InitialOperation initialOperation = checkboxDescription.getInitialOperation();
        ModelOperation modelOperation = initialOperation.getFirstModelOperations();
        var modelOperationHandlerSwitch = this.modelOperationHandlerSwitchProvider.getModelOperationHandlerSwitch(this.interpreter);
        Optional<IModelOperationHandler> optionalModelOperationHandler = modelOperationHandlerSwitch.apply(modelOperation);
        return optionalModelOperationHandler.map(handler -> {
            return handler.handle(variables);
        }).orElse(// $NON-NLS-1$
        new Failure(""));
    };
    // $NON-NLS-1$
    String valueExpression = Optional.ofNullable(checkboxDescription.getValueExpression()).orElse("");
    Function<VariableManager, Boolean> valueProvider = new BooleanValueProvider(this.interpreter, valueExpression);
    // @formatter:off
    return CheckboxDescription.newCheckboxDescription(this.identifierProvider.getIdentifier(checkboxDescription)).idProvider(new WidgetIdProvider()).labelProvider(labelProvider).valueProvider(valueProvider).newValueHandler(newValueHandler).diagnosticsProvider((variableManager) -> List.of()).kindProvider(// $NON-NLS-1$
    (object) -> "").messageProvider(// $NON-NLS-1$
    (object) -> "").build();
// @formatter:on
}
Also used : VariableManager(org.eclipse.sirius.components.representations.VariableManager) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IObjectService(org.eclipse.sirius.components.core.api.IObjectService) BiFunction(java.util.function.BiFunction) InitialOperation(org.eclipse.sirius.viewpoint.description.tool.InitialOperation) LoggerFactory(org.slf4j.LoggerFactory) SelectComponent(org.eclipse.sirius.components.forms.components.SelectComponent) Function(java.util.function.Function) ArrayList(java.util.ArrayList) TextareaDescription(org.eclipse.sirius.components.forms.description.TextareaDescription) IStatus(org.eclipse.sirius.components.representations.IStatus) StringValueProvider(org.eclipse.sirius.components.compatibility.utils.StringValueProvider) TextfieldDescription(org.eclipse.sirius.components.forms.description.TextfieldDescription) Map(java.util.Map) WidgetDescription(org.eclipse.sirius.properties.WidgetDescription) CheckboxDescription(org.eclipse.sirius.components.forms.description.CheckboxDescription) Logger(org.slf4j.Logger) AbstractWidgetDescription(org.eclipse.sirius.components.forms.description.AbstractWidgetDescription) Failure(org.eclipse.sirius.components.representations.Failure) IModelOperationHandlerSwitchProvider(org.eclipse.sirius.components.compatibility.api.IModelOperationHandlerSwitchProvider) SelectDescription(org.eclipse.sirius.components.forms.description.SelectDescription) BooleanValueProvider(org.eclipse.sirius.components.compatibility.utils.BooleanValueProvider) IModelOperationHandler(org.eclipse.sirius.components.compatibility.api.IModelOperationHandler) AQLInterpreter(org.eclipse.sirius.components.interpreter.AQLInterpreter) Objects(java.util.Objects) RadioComponent(org.eclipse.sirius.components.forms.components.RadioComponent) List(java.util.List) ModelOperation(org.eclipse.sirius.viewpoint.description.tool.ModelOperation) Optional(java.util.Optional) RadioDescription(org.eclipse.sirius.components.forms.description.RadioDescription) VariableManager(org.eclipse.sirius.components.representations.VariableManager) Collections(java.util.Collections) IIdentifierProvider(org.eclipse.sirius.components.compatibility.api.IIdentifierProvider) InitialOperation(org.eclipse.sirius.viewpoint.description.tool.InitialOperation) IStatus(org.eclipse.sirius.components.representations.IStatus) StringValueProvider(org.eclipse.sirius.components.compatibility.utils.StringValueProvider) Optional(java.util.Optional) BooleanValueProvider(org.eclipse.sirius.components.compatibility.utils.BooleanValueProvider) ModelOperation(org.eclipse.sirius.viewpoint.description.tool.ModelOperation) Map(java.util.Map) Failure(org.eclipse.sirius.components.representations.Failure)

Example 3 with InitialOperation

use of org.eclipse.sirius.viewpoint.description.tool.InitialOperation in project sirius-components by eclipse-sirius.

the class WidgetDescriptionConverter method convertRadio.

private RadioDescription convertRadio(org.eclipse.sirius.properties.RadioDescription radioDescription) {
    // $NON-NLS-1$
    String labelExpression = Optional.ofNullable(radioDescription.getLabelExpression()).orElse("");
    StringValueProvider labelProvider = new StringValueProvider(this.interpreter, labelExpression);
    Function<VariableManager, String> optionIdProvider = variableManager -> {
        Object candidate = variableManager.getVariables().get(RadioComponent.CANDIDATE_VARIABLE);
        return this.objectService.getId(candidate);
    };
    Function<VariableManager, Boolean> optionSelectedProvider = variableManager -> {
        Optional<Object> optionalResult = this.interpreter.evaluateExpression(variableManager.getVariables(), radioDescription.getValueExpression()).asObject();
        Object candidate = variableManager.getVariables().get(RadioComponent.CANDIDATE_VARIABLE);
        return optionalResult.map(candidate::equals).orElse(Boolean.FALSE);
    };
    Function<VariableManager, List<?>> optionsProvider = variableManager -> {
        Optional<List<Object>> optional = this.interpreter.evaluateExpression(variableManager.getVariables(), radioDescription.getCandidatesExpression()).asObjects();
        return optional.orElse(Collections.emptyList());
    };
    // $NON-NLS-1$
    String candidateDisplayExpression = Optional.ofNullable(radioDescription.getCandidateDisplayExpression()).orElse("");
    StringValueProvider optionLabelProvider = new StringValueProvider(this.interpreter, candidateDisplayExpression);
    BiFunction<VariableManager, String, IStatus> newValueHandler = (variableManager, newValue) -> {
        VariableManager childVariableManager = variableManager.createChild();
        var optionalEditingContext = variableManager.get(IEditingContext.EDITING_CONTEXT, IEditingContext.class);
        Optional<Object> optionalObject = optionalEditingContext.flatMap(context -> this.objectService.getObject(context, newValue));
        if (optionalObject.isPresent()) {
            childVariableManager.put(NEW_VALUE, optionalObject.get());
        } else {
            childVariableManager.put(NEW_VALUE, newValue);
        }
        InitialOperation initialOperation = radioDescription.getInitialOperation();
        ModelOperation modelOperation = initialOperation.getFirstModelOperations();
        var modelOperationHandlerSwitch = this.modelOperationHandlerSwitchProvider.getModelOperationHandlerSwitch(this.interpreter);
        Optional<IModelOperationHandler> optionalModelOperationHandler = modelOperationHandlerSwitch.apply(modelOperation);
        return optionalModelOperationHandler.map(handler -> {
            return handler.handle(childVariableManager.getVariables());
        }).orElse(// $NON-NLS-1$
        new Failure(""));
    };
    // @formatter:off
    return RadioDescription.newRadioDescription(this.identifierProvider.getIdentifier(radioDescription)).idProvider(new WidgetIdProvider()).labelProvider(labelProvider).optionIdProvider(optionIdProvider).optionLabelProvider(optionLabelProvider).optionSelectedProvider(optionSelectedProvider).optionsProvider(optionsProvider).newValueHandler(newValueHandler).diagnosticsProvider((variableManager) -> List.of()).kindProvider(// $NON-NLS-1$
    (object) -> "").messageProvider(// $NON-NLS-1$
    (object) -> "").build();
// @formatter:on
}
Also used : VariableManager(org.eclipse.sirius.components.representations.VariableManager) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IObjectService(org.eclipse.sirius.components.core.api.IObjectService) BiFunction(java.util.function.BiFunction) InitialOperation(org.eclipse.sirius.viewpoint.description.tool.InitialOperation) LoggerFactory(org.slf4j.LoggerFactory) SelectComponent(org.eclipse.sirius.components.forms.components.SelectComponent) Function(java.util.function.Function) ArrayList(java.util.ArrayList) TextareaDescription(org.eclipse.sirius.components.forms.description.TextareaDescription) IStatus(org.eclipse.sirius.components.representations.IStatus) StringValueProvider(org.eclipse.sirius.components.compatibility.utils.StringValueProvider) TextfieldDescription(org.eclipse.sirius.components.forms.description.TextfieldDescription) Map(java.util.Map) WidgetDescription(org.eclipse.sirius.properties.WidgetDescription) CheckboxDescription(org.eclipse.sirius.components.forms.description.CheckboxDescription) Logger(org.slf4j.Logger) AbstractWidgetDescription(org.eclipse.sirius.components.forms.description.AbstractWidgetDescription) Failure(org.eclipse.sirius.components.representations.Failure) IModelOperationHandlerSwitchProvider(org.eclipse.sirius.components.compatibility.api.IModelOperationHandlerSwitchProvider) SelectDescription(org.eclipse.sirius.components.forms.description.SelectDescription) BooleanValueProvider(org.eclipse.sirius.components.compatibility.utils.BooleanValueProvider) IModelOperationHandler(org.eclipse.sirius.components.compatibility.api.IModelOperationHandler) AQLInterpreter(org.eclipse.sirius.components.interpreter.AQLInterpreter) Objects(java.util.Objects) RadioComponent(org.eclipse.sirius.components.forms.components.RadioComponent) List(java.util.List) ModelOperation(org.eclipse.sirius.viewpoint.description.tool.ModelOperation) Optional(java.util.Optional) RadioDescription(org.eclipse.sirius.components.forms.description.RadioDescription) VariableManager(org.eclipse.sirius.components.representations.VariableManager) Collections(java.util.Collections) IIdentifierProvider(org.eclipse.sirius.components.compatibility.api.IIdentifierProvider) InitialOperation(org.eclipse.sirius.viewpoint.description.tool.InitialOperation) IStatus(org.eclipse.sirius.components.representations.IStatus) StringValueProvider(org.eclipse.sirius.components.compatibility.utils.StringValueProvider) Optional(java.util.Optional) ModelOperation(org.eclipse.sirius.viewpoint.description.tool.ModelOperation) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) ArrayList(java.util.ArrayList) List(java.util.List) Failure(org.eclipse.sirius.components.representations.Failure)

Example 4 with InitialOperation

use of org.eclipse.sirius.viewpoint.description.tool.InitialOperation 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

List (java.util.List)4 Map (java.util.Map)4 Objects (java.util.Objects)4 Optional (java.util.Optional)4 BiFunction (java.util.function.BiFunction)4 Function (java.util.function.Function)4 IModelOperationHandlerSwitchProvider (org.eclipse.sirius.components.compatibility.api.IModelOperationHandlerSwitchProvider)4 AQLInterpreter (org.eclipse.sirius.components.interpreter.AQLInterpreter)4 Failure (org.eclipse.sirius.components.representations.Failure)4 IStatus (org.eclipse.sirius.components.representations.IStatus)4 VariableManager (org.eclipse.sirius.components.representations.VariableManager)4 InitialOperation (org.eclipse.sirius.viewpoint.description.tool.InitialOperation)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 IIdentifierProvider (org.eclipse.sirius.components.compatibility.api.IIdentifierProvider)3 IModelOperationHandler (org.eclipse.sirius.components.compatibility.api.IModelOperationHandler)3 BooleanValueProvider (org.eclipse.sirius.components.compatibility.utils.BooleanValueProvider)3 StringValueProvider (org.eclipse.sirius.components.compatibility.utils.StringValueProvider)3 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)3 IObjectService (org.eclipse.sirius.components.core.api.IObjectService)3