Search in sources :

Example 1 with ListItem

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

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

the class DeleteListItemEventHandlerTests method testListItemDeletion.

@Test
public void testListItemDeletion() {
    // $NON-NLS-1$
    String listId = "List id";
    // $NON-NLS-1$
    String listItemId = "element id to delete";
    // $NON-NLS-1$
    String changeKind = "delete something";
    // $NON-NLS-1$
    String changeDescriptionParameterKey = "change_description_parameter_key";
    Map<String, Object> parameters = new HashMap<>();
    parameters.put(changeDescriptionParameterKey, listItemId);
    var input = new DeleteListItemInput(UUID.randomUUID(), FORM_ID.toString(), UUID.randomUUID().toString(), listId, listItemId);
    AtomicBoolean hasBeenExecuted = new AtomicBoolean();
    Supplier<IStatus> deleteHandler = () -> {
        hasBeenExecuted.set(true);
        return new Success(changeKind, parameters);
    };
    // @formatter:off
    ListItem listItem = ListItem.newListItem(listItemId).deletable(true).deleteHandler(deleteHandler).imageURL(// $NON-NLS-1$
    "").kind(// $NON-NLS-1$
    "Diagram").label(// $NON-NLS-1$
    "empty representation").build();
    List list = List.newList(listId).diagnostics(Collections.emptyList()).items(Collections.singletonList(listItem)).label(// $NON-NLS-1$
    "").build();
    Group group = // $NON-NLS-1$
    Group.newGroup("groupId").label(// $NON-NLS-1$
    "group label").widgets(Collections.singletonList(list)).build();
    Page page = // $NON-NLS-1$
    Page.newPage("pageId").label(// $NON-NLS-1$
    "page label").groups(Collections.singletonList(group)).build();
    Form form = Form.newForm(FORM_ID.toString()).targetObjectId(// $NON-NLS-1$
    "targetObjectId").descriptionId(UUID.randomUUID()).label(// $NON-NLS-1$
    "form label").pages(Collections.singletonList(page)).build();
    // @formatter:on
    IFormQueryService formQueryService = new IFormQueryService.NoOp() {

        @Override
        public Optional<AbstractWidget> findWidget(Form form, String widgetId) {
            return Optional.of(list);
        }
    };
    DeleteListItemEventHandler handler = new DeleteListItemEventHandler(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);
    Map<String, Object> changeDescriptionParameters = changeDescription.getParameters();
    assertThat(changeDescriptionParameters.get(changeDescriptionParameterKey)).isEqualTo(listItemId);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(DeleteListItemSuccessPayload.class);
    assertThat(hasBeenExecuted.get()).isTrue();
}
Also used : Group(org.eclipse.sirius.components.forms.Group) IStatus(org.eclipse.sirius.components.representations.IStatus) ICollaborativeFormMessageService(org.eclipse.sirius.components.collaborative.forms.messages.ICollaborativeFormMessageService) HashMap(java.util.HashMap) Form(org.eclipse.sirius.components.forms.Form) IFormQueryService(org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService) Page(org.eclipse.sirius.components.forms.Page) Success(org.eclipse.sirius.components.representations.Success) IPayload(org.eclipse.sirius.components.core.api.IPayload) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) List(org.eclipse.sirius.components.forms.List) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractWidget(org.eclipse.sirius.components.forms.AbstractWidget) ListItem(org.eclipse.sirius.components.forms.ListItem) DeleteListItemInput(org.eclipse.sirius.components.collaborative.forms.dto.DeleteListItemInput) Test(org.junit.jupiter.api.Test)

Example 3 with ListItem

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

the class DeleteListItemEventHandler 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(), DeleteListItemInput.class.getSimpleName());
    IPayload payload = new ErrorPayload(formInput.getId(), message);
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, formInput.getRepresentationId(), formInput);
    if (formInput instanceof DeleteListItemInput) {
        DeleteListItemInput input = (DeleteListItemInput) formInput;
        // @formatter:off
        var optionalListItem = this.formQueryService.findWidget(form, input.getListId()).filter(List.class::isInstance).map(List.class::cast).stream().map(List::getItems).flatMap(Collection::stream).filter(item -> item.getId().toString().equals(input.getListItemId())).findFirst();
        var status = optionalListItem.map(ListItem::getDeleteHandler).map(handler -> handler.get()).orElse(// $NON-NLS-1$
        new Failure(""));
        if (status instanceof Success) {
            Success success = (Success) status;
            changeDescription = new ChangeDescription(success.getChangeKind(), formInput.getRepresentationId(), formInput, success.getParameters());
            payload = new DeleteListItemSuccessPayload(formInput.getId());
        } else if (status instanceof Failure) {
            payload = new ErrorPayload(formInput.getId(), ((Failure) status).getMessage());
        }
    }
    changeDescriptionSink.tryEmitNext(changeDescription);
    payloadSink.tryEmitValue(payload);
}
Also used : DeleteListItemInput(org.eclipse.sirius.components.collaborative.forms.dto.DeleteListItemInput) 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) ListItem(org.eclipse.sirius.components.forms.ListItem) Collection(java.util.Collection) Success(org.eclipse.sirius.components.representations.Success) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) One(reactor.core.publisher.Sinks.One) List(org.eclipse.sirius.components.forms.List) Objects(java.util.Objects) IFormEventHandler(org.eclipse.sirius.components.collaborative.forms.api.IFormEventHandler) DeleteListItemSuccessPayload(org.eclipse.sirius.components.collaborative.forms.dto.DeleteListItemSuccessPayload) 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) Monitoring(org.eclipse.sirius.components.collaborative.api.Monitoring) Service(org.springframework.stereotype.Service) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Many(reactor.core.publisher.Sinks.Many) IFormInput(org.eclipse.sirius.components.collaborative.forms.api.IFormInput) Success(org.eclipse.sirius.components.representations.Success) DeleteListItemSuccessPayload(org.eclipse.sirius.components.collaborative.forms.dto.DeleteListItemSuccessPayload) IPayload(org.eclipse.sirius.components.core.api.IPayload) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) Collection(java.util.Collection) List(org.eclipse.sirius.components.forms.List) ListItem(org.eclipse.sirius.components.forms.ListItem) DeleteListItemInput(org.eclipse.sirius.components.collaborative.forms.dto.DeleteListItemInput) Failure(org.eclipse.sirius.components.representations.Failure)

Example 4 with ListItem

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

the class ListComponent method render.

@Override
public Element render() {
    VariableManager variableManager = this.props.getVariableManager();
    ListDescription listDescription = this.props.getListDescription();
    String id = listDescription.getIdProvider().apply(variableManager);
    String label = listDescription.getLabelProvider().apply(variableManager);
    List<?> itemCandidates = listDescription.getItemsProvider().apply(variableManager);
    List<Element> children = List.of(new Element(DiagnosticComponent.class, new DiagnosticComponentProps(listDescription, variableManager)));
    List<ListItem> items = new ArrayList<>(itemCandidates.size());
    for (Object itemCandidate : itemCandidates) {
        VariableManager itemVariableManager = variableManager.createChild();
        itemVariableManager.put(CANDIDATE_VARIABLE, itemCandidate);
        String itemId = listDescription.getItemIdProvider().apply(itemVariableManager);
        String itemLabel = listDescription.getItemLabelProvider().apply(itemVariableManager);
        String itemKind = listDescription.getItemKindProvider().apply(itemVariableManager);
        String itemImageURL = listDescription.getItemImageURLProvider().apply(itemVariableManager);
        boolean isItemDeletable = listDescription.getItemDeletableProvider().apply(itemVariableManager);
        Function<VariableManager, IStatus> genericHandler = listDescription.getItemDeleteHandlerProvider();
        Supplier<IStatus> specializedHandler = () -> {
            return genericHandler.apply(itemVariableManager);
        };
        // @formatter:off
        ListItem item = ListItem.newListItem(itemId).label(itemLabel).kind(itemKind).imageURL(itemImageURL).deletable(isItemDeletable).deleteHandler(specializedHandler).build();
        // @formatter:on
        items.add(item);
    }
    // @formatter:off
    ListElementProps listElementProps = ListElementProps.newListElementProps(id).label(label).items(items).children(children).build();
    return new Element(ListElementProps.TYPE, listElementProps);
}
Also used : VariableManager(org.eclipse.sirius.components.representations.VariableManager) IStatus(org.eclipse.sirius.components.representations.IStatus) Element(org.eclipse.sirius.components.representations.Element) DiagnosticComponentProps(org.eclipse.sirius.components.forms.validation.DiagnosticComponentProps) ArrayList(java.util.ArrayList) ListElementProps(org.eclipse.sirius.components.forms.elements.ListElementProps) ListDescription(org.eclipse.sirius.components.forms.description.ListDescription) DiagnosticComponent(org.eclipse.sirius.components.forms.validation.DiagnosticComponent) ListItem(org.eclipse.sirius.components.forms.ListItem)

Aggregations

ListItem (org.eclipse.sirius.components.forms.ListItem)4 List (org.eclipse.sirius.components.forms.List)3 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)2 IFormQueryService (org.eclipse.sirius.components.collaborative.forms.api.IFormQueryService)2 DeleteListItemInput (org.eclipse.sirius.components.collaborative.forms.dto.DeleteListItemInput)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 IStatus (org.eclipse.sirius.components.representations.IStatus)2 Success (org.eclipse.sirius.components.representations.Success)2 Counter (io.micrometer.core.instrument.Counter)1 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Objects (java.util.Objects)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ChangeKind (org.eclipse.sirius.components.collaborative.api.ChangeKind)1 Monitoring (org.eclipse.sirius.components.collaborative.api.Monitoring)1