From 600adf7807c526798b3fddb12cfff9e5234487b3 Mon Sep 17 00:00:00 2001 From: Laurent Fasani Date: Fri, 25 Feb 2022 15:32:12 +0100 Subject: [PATCH] [781] Use Text area for default form representation It will allow multiple line edition. Shift + return in Text area only adds a line return but does not send the mutation. Bug: https://github.com/eclipse-sirius/sirius-components/issues/781 Signed-off-by: Laurent Fasani --- .../handlers/EditTextfieldEventHandler.java | 17 ++++++++++++----- .../EStringIfDescriptionProvider.java | 12 ++++++------ .../sirius/components/forms/Textarea.java | 17 ++++++++++++++++- .../forms/renderer/FormElementFactory.java | 3 ++- .../TextfieldPropertySection.tsx | 4 ++-- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/backend/sirius-components-collaborative-forms/src/main/java/org/eclipse/sirius/components/collaborative/forms/handlers/EditTextfieldEventHandler.java b/backend/sirius-components-collaborative-forms/src/main/java/org/eclipse/sirius/components/collaborative/forms/handlers/EditTextfieldEventHandler.java index 64461cef728..3985ceea509 100644 --- a/backend/sirius-components-collaborative-forms/src/main/java/org/eclipse/sirius/components/collaborative/forms/handlers/EditTextfieldEventHandler.java +++ b/backend/sirius-components-collaborative-forms/src/main/java/org/eclipse/sirius/components/collaborative/forms/handlers/EditTextfieldEventHandler.java @@ -13,6 +13,7 @@ package org.eclipse.sirius.components.collaborative.forms.handlers; import java.util.Objects; +import java.util.function.Function; import org.eclipse.sirius.components.collaborative.api.ChangeDescription; import org.eclipse.sirius.components.collaborative.api.ChangeKind; @@ -26,6 +27,7 @@ import org.eclipse.sirius.components.core.api.ErrorPayload; import org.eclipse.sirius.components.core.api.IPayload; import org.eclipse.sirius.components.forms.Form; +import org.eclipse.sirius.components.forms.Textarea; import org.eclipse.sirius.components.forms.Textfield; import org.eclipse.sirius.components.representations.Failure; import org.eclipse.sirius.components.representations.IStatus; @@ -78,11 +80,16 @@ public void handle(One payloadSink, Many changeDesc EditTextfieldInput input = (EditTextfieldInput) formInput; // @formatter:off - var optionalTextfield = this.formQueryService.findWidget(form, input.getTextfieldId()) - .filter(Textfield.class::isInstance) - .map(Textfield.class::cast); - - IStatus status = optionalTextfield.map(Textfield::getNewValueHandler) + IStatus status = this.formQueryService.findWidget(form, input.getTextfieldId()) + .map(widget -> { + Function 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(new Failure("")); //$NON-NLS-1$ // @formatter:on diff --git a/backend/sirius-components-emf/src/main/java/org/eclipse/sirius/components/emf/compatibility/properties/EStringIfDescriptionProvider.java b/backend/sirius-components-emf/src/main/java/org/eclipse/sirius/components/emf/compatibility/properties/EStringIfDescriptionProvider.java index 5036f29119e..619723a3a70 100644 --- a/backend/sirius-components-emf/src/main/java/org/eclipse/sirius/components/emf/compatibility/properties/EStringIfDescriptionProvider.java +++ b/backend/sirius-components-emf/src/main/java/org/eclipse/sirius/components/emf/compatibility/properties/EStringIfDescriptionProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2021 Obeo. + * Copyright (c) 2019, 2022 Obeo. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -25,7 +25,7 @@ import org.eclipse.sirius.components.compatibility.forms.WidgetIdProvider; import org.eclipse.sirius.components.emf.compatibility.properties.api.IPropertiesValidationProvider; import org.eclipse.sirius.components.forms.description.IfDescription; -import org.eclipse.sirius.components.forms.description.TextfieldDescription; +import org.eclipse.sirius.components.forms.description.TextareaDescription; import org.eclipse.sirius.components.representations.Failure; import org.eclipse.sirius.components.representations.IStatus; import org.eclipse.sirius.components.representations.Success; @@ -39,7 +39,7 @@ public class EStringIfDescriptionProvider { private static final String IF_DESCRIPTION_ID = "EString"; //$NON-NLS-1$ - private static final String TEXTFIELD_DESCRIPTION_ID = "Textfield"; //$NON-NLS-1$ + private static final String TEXTAREA_DESCRIPTION_ID = "Textarea"; //$NON-NLS-1$ private final ComposedAdapterFactory composedAdapterFactory; @@ -54,7 +54,7 @@ public IfDescription getIfDescription() { // @formatter:off return IfDescription.newIfDescription(IF_DESCRIPTION_ID) .predicate(this.getPredicate()) - .widgetDescription(this.getTextfieldDescription()) + .widgetDescription(this.getTextareaDescription()) .build(); // @formatter:on } @@ -69,9 +69,9 @@ private Function getPredicate() { }; } - private TextfieldDescription getTextfieldDescription() { + private TextareaDescription getTextareaDescription() { // @formatter:off - return TextfieldDescription.newTextfieldDescription(TEXTFIELD_DESCRIPTION_ID) + return TextareaDescription.newTextareaDescription(TEXTAREA_DESCRIPTION_ID) .idProvider(new WidgetIdProvider()) .labelProvider(this.getLabelProvider()) .valueProvider(this.getValueProvider()) diff --git a/backend/sirius-components-forms/src/main/java/org/eclipse/sirius/components/forms/Textarea.java b/backend/sirius-components-forms/src/main/java/org/eclipse/sirius/components/forms/Textarea.java index 480efc75bdf..3c62479f78c 100644 --- a/backend/sirius-components-forms/src/main/java/org/eclipse/sirius/components/forms/Textarea.java +++ b/backend/sirius-components-forms/src/main/java/org/eclipse/sirius/components/forms/Textarea.java @@ -15,9 +15,11 @@ import java.text.MessageFormat; import java.util.List; import java.util.Objects; +import java.util.function.Function; import org.eclipse.sirius.components.annotations.Immutable; import org.eclipse.sirius.components.forms.validation.Diagnostic; +import org.eclipse.sirius.components.representations.IStatus; /** * The text area widget. @@ -30,6 +32,8 @@ public final class Textarea extends AbstractWidget { private String value; + private Function newValueHandler; + private Textarea() { // Prevent instantiation } @@ -42,6 +46,10 @@ public String getValue() { return this.value; } + public Function getNewValueHandler() { + return this.newValueHandler; + } + public static Builder newTextarea(String id) { return new Builder(id); } @@ -59,13 +67,14 @@ public String toString() { */ @SuppressWarnings("checkstyle:HiddenField") public static final class Builder { - private String id; private String label; private String value; + private Function newValueHandler; + private List diagnostics; private Builder(String id) { @@ -82,6 +91,11 @@ public Builder value(String value) { return this; } + public Builder newValueHandler(Function newValueHandler) { + this.newValueHandler = Objects.requireNonNull(newValueHandler); + return this; + } + public Builder diagnostics(List diagnostics) { this.diagnostics = Objects.requireNonNull(diagnostics); return this; @@ -92,6 +106,7 @@ public Textarea build() { textarea.id = Objects.requireNonNull(this.id); textarea.label = Objects.requireNonNull(this.label); textarea.value = Objects.requireNonNull(this.value); + textarea.newValueHandler = Objects.requireNonNull(this.newValueHandler); textarea.diagnostics = Objects.requireNonNull(this.diagnostics); return textarea; } diff --git a/backend/sirius-components-forms/src/main/java/org/eclipse/sirius/components/forms/renderer/FormElementFactory.java b/backend/sirius-components-forms/src/main/java/org/eclipse/sirius/components/forms/renderer/FormElementFactory.java index 712ac8010a5..c0c1fcde799 100644 --- a/backend/sirius-components-forms/src/main/java/org/eclipse/sirius/components/forms/renderer/FormElementFactory.java +++ b/backend/sirius-components-forms/src/main/java/org/eclipse/sirius/components/forms/renderer/FormElementFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2021 Obeo. + * Copyright (c) 2019, 2022 Obeo. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -193,6 +193,7 @@ private Textarea instantiateTextarea(TextareaElementProps props, List ch return Textarea.newTextarea(props.getId()) .label(props.getLabel()) .value(props.getValue()) + .newValueHandler(props.getNewValueHandler()) .diagnostics(diagnostics) .build(); // @formatter:on diff --git a/frontend/src/properties/propertysections/TextfieldPropertySection.tsx b/frontend/src/properties/propertysections/TextfieldPropertySection.tsx index 91c7e3ab343..c03b39e55db 100644 --- a/frontend/src/properties/propertysections/TextfieldPropertySection.tsx +++ b/frontend/src/properties/propertysections/TextfieldPropertySection.tsx @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2021 Obeo. + * Copyright (c) 2019, 2022 Obeo. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -185,7 +185,7 @@ export const TextfieldPropertySection = ({ }; const onKeyPress = (event) => { - if ('Enter' === event.key) { + if ('Enter' === event.key && !event.shiftKey) { sendEditedValue(); } };