Search in sources :

Example 1 with Textfield

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

the class WidgetAssert method matches.

public WidgetAssert matches(AbstractWidget widget, IdPolicy idPolicy) {
    this.isNotNull();
    if (idPolicy == IdPolicy.WITH_ID) {
        assertThat(this.actual.getId()).isEqualTo(widget.getId());
    }
    if (this.actual instanceof List && widget instanceof List) {
        List actualList = (List) this.actual;
        List list = (List) widget;
        assertThat(actualList.getLabel()).isEqualTo(list.getLabel());
        assertThat(actualList.getItems().size()).isEqualTo(list.getItems().size());
        int size = actualList.getItems().size();
        for (int i = 0; i < size; i++) {
            ListItem actualListItem = actualList.getItems().get(i);
            ListItem listItem = list.getItems().get(i);
            assertThat(actualListItem).matches(listItem, idPolicy);
        }
    } else if (this.actual instanceof Checkbox && widget instanceof Checkbox) {
        Checkbox actualCheckbox = (Checkbox) this.actual;
        Checkbox checkbox = (Checkbox) widget;
        assertThat(actualCheckbox.getLabel()).isEqualTo(checkbox.getLabel());
        assertThat(actualCheckbox.isValue()).isEqualTo(checkbox.isValue());
    } else if (this.actual instanceof Radio && widget instanceof Radio) {
        this.assertRadio((Radio) this.actual, (Radio) widget, idPolicy);
    } else if (this.actual instanceof Select && widget instanceof Select) {
        this.assertSelect((Select) this.actual, (Select) widget, idPolicy);
    } else if (this.actual instanceof Textarea && widget instanceof Textarea) {
        Textarea actualTextarea = (Textarea) this.actual;
        Textarea textarea = (Textarea) widget;
        assertThat(actualTextarea.getLabel()).isEqualTo(textarea.getLabel());
        assertThat(actualTextarea.getValue()).isEqualTo(textarea.getValue());
    } else if (this.actual instanceof Textfield && widget instanceof Textfield) {
        Textfield actualTextfield = (Textfield) this.actual;
        Textfield textfield = (Textfield) widget;
        assertThat(actualTextfield.getLabel()).isEqualTo(textfield.getLabel());
        assertThat(actualTextfield.getValue()).isEqualTo(textfield.getValue());
    }
    return this;
}
Also used : Textarea(org.eclipse.sirius.components.forms.Textarea) Checkbox(org.eclipse.sirius.components.forms.Checkbox) Select(org.eclipse.sirius.components.forms.Select) List(org.eclipse.sirius.components.forms.List) Radio(org.eclipse.sirius.components.forms.Radio) Textfield(org.eclipse.sirius.components.forms.Textfield) ListItem(org.eclipse.sirius.components.forms.ListItem)

Example 2 with Textfield

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

the class EditTextfieldEventHandlerTests method testTextfieldEdition.

@Test
public void testTextfieldEdition() {
    // $NON-NLS-1$
    String id = "Textfield id";
    // $NON-NLS-1$
    var input = new EditTextfieldInput(UUID.randomUUID(), UUID.randomUUID().toString(), FORM_ID, id, "New value");
    AtomicBoolean hasBeenExecuted = new AtomicBoolean();
    Function<String, IStatus> newValueHandler = newValue -> {
        hasBeenExecuted.set(true);
        return new Success();
    };
    // @formatter:off
    Textfield textfield = Textfield.newTextfield(id).label(// $NON-NLS-1$
    "label").value(// $NON-NLS-1$
    "Previous value").newValueHandler(newValueHandler).diagnostics(List.of()).build();
    Group group = // $NON-NLS-1$
    Group.newGroup("groupId").label(// $NON-NLS-1$
    "group label").widgets(List.of(textfield)).build();
    Page page = // $NON-NLS-1$
    Page.newPage("pageId").label(// $NON-NLS-1$
    "page label").groups(List.of(group)).build();
    Form form = Form.newForm(FORM_ID).targetObjectId(// $NON-NLS-1$
    "targetObjectId").descriptionId(UUID.randomUUID()).label(// $NON-NLS-1$
    "form label").pages(List.of(page)).build();
    // @formatter:on
    IFormQueryService formQueryService = new IFormQueryService.NoOp() {

        @Override
        public Optional<AbstractWidget> findWidget(Form form, String widgetId) {
            return Optional.of(textfield);
        }
    };
    EditTextfieldEventHandler handler = new EditTextfieldEventHandler(formQueryService, new ICollaborativeFormMessageService.NoOp(), new SimpleMeterRegistry());
    assertThat(handler.canHandle(input)).isTrue();
    Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
    One<IPayload> payloadSink = Sinks.one();
    handler.handle(payloadSink, changeDescriptionSink, form, input);
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(EditTextfieldSuccessPayload.class);
    assertThat(hasBeenExecuted.get()).isTrue();
}
Also used : Sinks(reactor.core.publisher.Sinks) Form(org.eclipse.sirius.components.forms.Form) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) One(reactor.core.publisher.Sinks.One) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(java.util.function.Function) AbstractWidget(org.eclipse.sirius.components.forms.AbstractWidget) IFormQueryService(org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService) Textfield(org.eclipse.sirius.components.forms.Textfield) IStatus(org.eclipse.sirius.components.representations.IStatus) EditTextfieldInput(org.eclipse.sirius.components.collaborative.forms.dto.EditTextfieldInput) Page(org.eclipse.sirius.components.forms.Page) ChangeKind(org.eclipse.sirius.components.collaborative.api.ChangeKind) Success(org.eclipse.sirius.components.representations.Success) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) UUID(java.util.UUID) EditTextfieldSuccessPayload(org.eclipse.sirius.components.collaborative.forms.dto.EditTextfieldSuccessPayload) Test(org.junit.jupiter.api.Test) List(java.util.List) ICollaborativeFormMessageService(org.eclipse.sirius.components.collaborative.forms.messages.ICollaborativeFormMessageService) IPayload(org.eclipse.sirius.components.core.api.IPayload) Group(org.eclipse.sirius.components.forms.Group) Many(reactor.core.publisher.Sinks.Many) Optional(java.util.Optional) Group(org.eclipse.sirius.components.forms.Group) IStatus(org.eclipse.sirius.components.representations.IStatus) ICollaborativeFormMessageService(org.eclipse.sirius.components.collaborative.forms.messages.ICollaborativeFormMessageService) Form(org.eclipse.sirius.components.forms.Form) IFormQueryService(org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Page(org.eclipse.sirius.components.forms.Page) Success(org.eclipse.sirius.components.representations.Success) IPayload(org.eclipse.sirius.components.core.api.IPayload) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EditTextfieldInput(org.eclipse.sirius.components.collaborative.forms.dto.EditTextfieldInput) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) Textfield(org.eclipse.sirius.components.forms.Textfield) AbstractWidget(org.eclipse.sirius.components.forms.AbstractWidget) Test(org.junit.jupiter.api.Test)

Example 3 with Textfield

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

the class EditTextfieldEventHandler method handle.

@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, Form form, IFormInput formInput) {
    this.counter.increment();
    String message = this.messageService.invalidInput(formInput.getClass().getSimpleName(), EditTextfieldInput.class.getSimpleName());
    IPayload payload = new ErrorPayload(formInput.getId(), message);
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, formInput.getRepresentationId(), formInput);
    if (formInput instanceof EditTextfieldInput) {
        EditTextfieldInput input = (EditTextfieldInput) formInput;
        // @formatter:off
        IStatus status = this.formQueryService.findWidget(form, input.getTextfieldId()).map(widget -> {
            Function<String, IStatus> handlerFunction = null;
            if (widget instanceof Textfield) {
                handlerFunction = ((Textfield) widget).getNewValueHandler();
            } else if (widget instanceof Textarea) {
                handlerFunction = ((Textarea) widget).getNewValueHandler();
            }
            return handlerFunction;
        }).map(handler -> handler.apply(input.getNewValue())).orElse(// $NON-NLS-1$
        new Failure(""));
        if (status instanceof Success) {
            payload = new EditTextfieldSuccessPayload(formInput.getId());
            changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, formInput.getRepresentationId(), formInput);
        } else if (status instanceof Failure) {
            payload = new ErrorPayload(formInput.getId(), ((Failure) status).getMessage());
        }
    }
    changeDescriptionSink.tryEmitNext(changeDescription);
    payloadSink.tryEmitValue(payload);
}
Also used : ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) Counter(io.micrometer.core.instrument.Counter) Form(org.eclipse.sirius.components.forms.Form) ChangeKind(org.eclipse.sirius.components.collaborative.api.ChangeKind) Failure(org.eclipse.sirius.components.representations.Failure) Success(org.eclipse.sirius.components.representations.Success) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) One(reactor.core.publisher.Sinks.One) Textarea(org.eclipse.sirius.components.forms.Textarea) Function(java.util.function.Function) EditTextfieldSuccessPayload(org.eclipse.sirius.components.collaborative.forms.dto.EditTextfieldSuccessPayload) Objects(java.util.Objects) IFormEventHandler(org.eclipse.sirius.components.collaborative.forms.api.IFormEventHandler) IFormQueryService(org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService) ICollaborativeFormMessageService(org.eclipse.sirius.components.collaborative.forms.messages.ICollaborativeFormMessageService) IPayload(org.eclipse.sirius.components.core.api.IPayload) Textfield(org.eclipse.sirius.components.forms.Textfield) IStatus(org.eclipse.sirius.components.representations.IStatus) Monitoring(org.eclipse.sirius.components.collaborative.api.Monitoring) Service(org.springframework.stereotype.Service) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) EditTextfieldInput(org.eclipse.sirius.components.collaborative.forms.dto.EditTextfieldInput) Many(reactor.core.publisher.Sinks.Many) IFormInput(org.eclipse.sirius.components.collaborative.forms.api.IFormInput) IStatus(org.eclipse.sirius.components.representations.IStatus) Success(org.eclipse.sirius.components.representations.Success) IPayload(org.eclipse.sirius.components.core.api.IPayload) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) EditTextfieldInput(org.eclipse.sirius.components.collaborative.forms.dto.EditTextfieldInput) Textarea(org.eclipse.sirius.components.forms.Textarea) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) Textfield(org.eclipse.sirius.components.forms.Textfield) Failure(org.eclipse.sirius.components.representations.Failure) EditTextfieldSuccessPayload(org.eclipse.sirius.components.collaborative.forms.dto.EditTextfieldSuccessPayload)

Aggregations

Textfield (org.eclipse.sirius.components.forms.Textfield)3 Function (java.util.function.Function)2 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)2 ChangeKind (org.eclipse.sirius.components.collaborative.api.ChangeKind)2 IFormQueryService (org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService)2 EditTextfieldInput (org.eclipse.sirius.components.collaborative.forms.dto.EditTextfieldInput)2 EditTextfieldSuccessPayload (org.eclipse.sirius.components.collaborative.forms.dto.EditTextfieldSuccessPayload)2 ICollaborativeFormMessageService (org.eclipse.sirius.components.collaborative.forms.messages.ICollaborativeFormMessageService)2 IPayload (org.eclipse.sirius.components.core.api.IPayload)2 Form (org.eclipse.sirius.components.forms.Form)2 Textarea (org.eclipse.sirius.components.forms.Textarea)2 IStatus (org.eclipse.sirius.components.representations.IStatus)2 Success (org.eclipse.sirius.components.representations.Success)2 Many (reactor.core.publisher.Sinks.Many)2 One (reactor.core.publisher.Sinks.One)2 Counter (io.micrometer.core.instrument.Counter)1 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 List (java.util.List)1 Objects (java.util.Objects)1