Search in sources :

Example 1 with RadioDescription

use of org.eclipse.sirius.components.forms.description.RadioDescription in project sirius-components by eclipse-sirius.

the class WidgetComponent method render.

@Override
public Element render() {
    VariableManager variableManager = this.props.getVariableManager();
    AbstractWidgetDescription widgetDescription = this.props.getWidgetDescription();
    Element element = null;
    if (widgetDescription instanceof TextfieldDescription) {
        TextfieldComponentProps textfieldProps = new TextfieldComponentProps(variableManager, (TextfieldDescription) widgetDescription);
        element = new Element(TextfieldComponent.class, textfieldProps);
    } else if (widgetDescription instanceof TextareaDescription) {
        TextareaComponentProps textareaProps = new TextareaComponentProps(variableManager, (TextareaDescription) widgetDescription);
        element = new Element(TextareaComponent.class, textareaProps);
    } else if (widgetDescription instanceof CheckboxDescription) {
        CheckboxComponentProps checkboxProps = new CheckboxComponentProps(variableManager, (CheckboxDescription) widgetDescription);
        element = new Element(CheckboxComponent.class, checkboxProps);
    } else if (widgetDescription instanceof SelectDescription) {
        SelectComponentProps selectProps = new SelectComponentProps(variableManager, (SelectDescription) widgetDescription);
        element = new Element(SelectComponent.class, selectProps);
    } else if (widgetDescription instanceof MultiSelectDescription) {
        MultiSelectComponentProps multiSelectProps = new MultiSelectComponentProps(variableManager, (MultiSelectDescription) widgetDescription);
        element = new Element(MultiSelectComponent.class, multiSelectProps);
    } else if (widgetDescription instanceof RadioDescription) {
        RadioComponentProps radioProps = new RadioComponentProps(variableManager, (RadioDescription) widgetDescription);
        element = new Element(RadioComponent.class, radioProps);
    } else if (widgetDescription instanceof ListDescription) {
        ListComponentProps listProps = new ListComponentProps(variableManager, (ListDescription) widgetDescription);
        element = new Element(ListComponent.class, listProps);
    } else if (widgetDescription instanceof LinkDescription) {
        LinkComponentProps linkProps = new LinkComponentProps(variableManager, (LinkDescription) widgetDescription);
        element = new Element(LinkComponent.class, linkProps);
    } else {
        // $NON-NLS-1$
        String pattern = "Unsupported widget description: {}";
        this.logger.warn(pattern, widgetDescription.getClass().getSimpleName());
    }
    return element;
}
Also used : VariableManager(org.eclipse.sirius.components.representations.VariableManager) CheckboxDescription(org.eclipse.sirius.components.forms.description.CheckboxDescription) RadioDescription(org.eclipse.sirius.components.forms.description.RadioDescription) LinkDescription(org.eclipse.sirius.components.forms.description.LinkDescription) Element(org.eclipse.sirius.components.representations.Element) MultiSelectDescription(org.eclipse.sirius.components.forms.description.MultiSelectDescription) ListDescription(org.eclipse.sirius.components.forms.description.ListDescription) AbstractWidgetDescription(org.eclipse.sirius.components.forms.description.AbstractWidgetDescription) TextareaDescription(org.eclipse.sirius.components.forms.description.TextareaDescription) TextfieldDescription(org.eclipse.sirius.components.forms.description.TextfieldDescription) SelectDescription(org.eclipse.sirius.components.forms.description.SelectDescription) MultiSelectDescription(org.eclipse.sirius.components.forms.description.MultiSelectDescription)

Example 2 with RadioDescription

use of org.eclipse.sirius.components.forms.description.RadioDescription in project sirius-components by eclipse-sirius.

the class RadioComponent method render.

@Override
public Element render() {
    VariableManager variableManager = this.props.getVariableManager();
    RadioDescription radioDescription = this.props.getRadioDescription();
    String id = radioDescription.getIdProvider().apply(variableManager);
    String label = radioDescription.getLabelProvider().apply(variableManager);
    List<?> optionCandidates = radioDescription.getOptionsProvider().apply(variableManager);
    List<Element> children = List.of(new Element(DiagnosticComponent.class, new DiagnosticComponentProps(radioDescription, variableManager)));
    List<RadioOption> options = new ArrayList<>(optionCandidates.size());
    for (Object candidate : optionCandidates) {
        VariableManager optionVariableManager = variableManager.createChild();
        optionVariableManager.put(CANDIDATE_VARIABLE, candidate);
        String optionId = radioDescription.getOptionIdProvider().apply(optionVariableManager);
        String optionLabel = radioDescription.getOptionLabelProvider().apply(optionVariableManager);
        Boolean optionSelected = radioDescription.getOptionSelectedProvider().apply(optionVariableManager);
        // @formatter:off
        RadioOption option = RadioOption.newRadioOption(optionId).label(optionLabel).selected(optionSelected).build();
        // @formatter:on
        options.add(option);
    }
    BiFunction<VariableManager, String, IStatus> genericHandler = radioDescription.getNewValueHandler();
    Function<String, IStatus> specializedHandler = newValue -> {
        return genericHandler.apply(variableManager, newValue);
    };
    // @formatter:off
    RadioElementProps radioElementProps = RadioElementProps.newRadioElementProps(id).label(label).options(options).newValueHandler(specializedHandler).children(children).build();
    return new Element(RadioElementProps.TYPE, radioElementProps);
// @formatter:on
}
Also used : VariableManager(org.eclipse.sirius.components.representations.VariableManager) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) Element(org.eclipse.sirius.components.representations.Element) IComponent(org.eclipse.sirius.components.representations.IComponent) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) RadioElementProps(org.eclipse.sirius.components.forms.elements.RadioElementProps) IStatus(org.eclipse.sirius.components.representations.IStatus) DiagnosticComponentProps(org.eclipse.sirius.components.forms.validation.DiagnosticComponentProps) RadioDescription(org.eclipse.sirius.components.forms.description.RadioDescription) VariableManager(org.eclipse.sirius.components.representations.VariableManager) RadioOption(org.eclipse.sirius.components.forms.RadioOption) DiagnosticComponent(org.eclipse.sirius.components.forms.validation.DiagnosticComponent) IStatus(org.eclipse.sirius.components.representations.IStatus) RadioDescription(org.eclipse.sirius.components.forms.description.RadioDescription) Element(org.eclipse.sirius.components.representations.Element) DiagnosticComponentProps(org.eclipse.sirius.components.forms.validation.DiagnosticComponentProps) ArrayList(java.util.ArrayList) DiagnosticComponent(org.eclipse.sirius.components.forms.validation.DiagnosticComponent) RadioElementProps(org.eclipse.sirius.components.forms.elements.RadioElementProps) RadioOption(org.eclipse.sirius.components.forms.RadioOption)

Example 3 with RadioDescription

use of org.eclipse.sirius.components.forms.description.RadioDescription 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)

Aggregations

RadioDescription (org.eclipse.sirius.components.forms.description.RadioDescription)3 VariableManager (org.eclipse.sirius.components.representations.VariableManager)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects (java.util.Objects)2 BiFunction (java.util.function.BiFunction)2 Function (java.util.function.Function)2 AbstractWidgetDescription (org.eclipse.sirius.components.forms.description.AbstractWidgetDescription)2 CheckboxDescription (org.eclipse.sirius.components.forms.description.CheckboxDescription)2 SelectDescription (org.eclipse.sirius.components.forms.description.SelectDescription)2 TextareaDescription (org.eclipse.sirius.components.forms.description.TextareaDescription)2 TextfieldDescription (org.eclipse.sirius.components.forms.description.TextfieldDescription)2 Element (org.eclipse.sirius.components.representations.Element)2 IStatus (org.eclipse.sirius.components.representations.IStatus)2 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1 IIdentifierProvider (org.eclipse.sirius.components.compatibility.api.IIdentifierProvider)1 IModelOperationHandler (org.eclipse.sirius.components.compatibility.api.IModelOperationHandler)1 IModelOperationHandlerSwitchProvider (org.eclipse.sirius.components.compatibility.api.IModelOperationHandlerSwitchProvider)1