Skip to content

Commit

Permalink
[781] Use Text area for default form representation
Browse files Browse the repository at this point in the history
It will allow multiple line edition.
Shift + return in Text area only adds a line return but does not send
the mutation.

Bug: #781
Signed-off-by: Laurent Fasani <[email protected]>
  • Loading branch information
lfasani committed Mar 2, 2022
1 parent c54871c commit 600adf7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -78,11 +80,16 @@ public void handle(One<IPayload> payloadSink, Many<ChangeDescription> 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<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(new Failure("")); //$NON-NLS-1$
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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
}
Expand All @@ -69,9 +69,9 @@ private Function<VariableManager, Boolean> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,6 +32,8 @@ public final class Textarea extends AbstractWidget {

private String value;

private Function<String, IStatus> newValueHandler;

private Textarea() {
// Prevent instantiation
}
Expand All @@ -42,6 +46,10 @@ public String getValue() {
return this.value;
}

public Function<String, IStatus> getNewValueHandler() {
return this.newValueHandler;
}

public static Builder newTextarea(String id) {
return new Builder(id);
}
Expand All @@ -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<String, IStatus> newValueHandler;

private List<Diagnostic> diagnostics;

private Builder(String id) {
Expand All @@ -82,6 +91,11 @@ public Builder value(String value) {
return this;
}

public Builder newValueHandler(Function<String, IStatus> newValueHandler) {
this.newValueHandler = Objects.requireNonNull(newValueHandler);
return this;
}

public Builder diagnostics(List<Diagnostic> diagnostics) {
this.diagnostics = Objects.requireNonNull(diagnostics);
return this;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -193,6 +193,7 @@ private Textarea instantiateTextarea(TextareaElementProps props, List<Object> ch
return Textarea.newTextarea(props.getId())
.label(props.getLabel())
.value(props.getValue())
.newValueHandler(props.getNewValueHandler())
.diagnostics(diagnostics)
.build();
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -185,7 +185,7 @@ export const TextfieldPropertySection = ({
};

const onKeyPress = (event) => {
if ('Enter' === event.key) {
if ('Enter' === event.key && !event.shiftKey) {
sendEditedValue();
}
};
Expand Down

0 comments on commit 600adf7

Please sign in to comment.