Skip to content

Commit

Permalink
[2358] Adds diagnostic messages to the reference widget
Browse files Browse the repository at this point in the history
Bug: #2358
Signed-off-by: Florian ROUËNÉ <florian.rouene@obeosoft.com>
  • Loading branch information
frouene authored and sbegaudeau committed Sep 12, 2023
1 parent 069480a commit 5b23d6c
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Expand Up @@ -117,6 +117,7 @@ To illustrate this new feature, we contribute a new tool on the _Papaya Diagram_

- https://github.com/eclipse-sirius/sirius-web/issues/2343[#2343] [emf] Add icon for type selection in new object modal.
- https://github.com/eclipse-sirius/sirius-web/issues/2365[#2365] [diagram] Support key down and F2 to enable direct edit on edges on react-flow diagrams.
- https://github.com/eclipse-sirius/sirius-web/issues/2358[#2358] [form] Adds diagnostic messages to the reference widget.

== v2023.8.0

Expand Down
15 changes: 15 additions & 0 deletions integration-tests/cypress/e2e/project/edit/widget-reference.cy.js
Expand Up @@ -322,4 +322,19 @@ describe('/projects/:projectId/edit - FormDescriptionEditor', () => {
cy.getByTestId('reference-value-unused').should('exist');
cy.getByTestId('unused').should('exist');
});

it('check diagnostic messages are display on widget reference', () => {
cy.createProjectFromTemplate('flow-template').then((res) => {
const projectId = res.body.data.createProjectFromTemplate.project.id;
cy.visit(`/projects/${projectId}/edit`);
});

cy.getByTestId('explorer://').findByTestId('FlowNewModel').dblclick();
cy.getByTestId('explorer://').findByTestId('NewSystem').dblclick();
cy.getByTestId('explorer://').findByTestId('DataSource1').dblclick();
cy.getByTestId('explorer://').findByTestId('standard').click();
cy.getByTestId('Target').find('p[class*="Mui-error"]').should('not.exist');
cy.getByTestId('Target-clear').click();
cy.getByTestId('Target').find('p[class*="Mui-error"]').should('exist');
});
});
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.sirius.components.compatibility.emf.properties.api.IPropertiesValidationProvider;
import org.eclipse.sirius.components.compatibility.forms.WidgetIdProvider;
import org.eclipse.sirius.components.core.api.IObjectService;
import org.eclipse.sirius.components.forms.description.IfDescription;
Expand All @@ -44,11 +45,14 @@ public class NonContainmentReferenceIfDescriptionProvider {

private final IObjectService objectService;

private final IPropertiesValidationProvider propertiesValidationProvider;

private final Function<VariableManager, String> semanticTargetIdProvider;

public NonContainmentReferenceIfDescriptionProvider(ComposedAdapterFactory composedAdapterFactory, IObjectService objectService, Function<VariableManager, String> semanticTargetIdProvider) {
public NonContainmentReferenceIfDescriptionProvider(ComposedAdapterFactory composedAdapterFactory, IObjectService objectService, IPropertiesValidationProvider propertiesValidationProvider, Function<VariableManager, String> semanticTargetIdProvider) {
this.composedAdapterFactory = Objects.requireNonNull(composedAdapterFactory);
this.objectService = Objects.requireNonNull(objectService);
this.propertiesValidationProvider = Objects.requireNonNull(propertiesValidationProvider);
this.semanticTargetIdProvider = Objects.requireNonNull(semanticTargetIdProvider);
}

Expand Down Expand Up @@ -82,9 +86,9 @@ private ReferenceWidgetDescription getReferenceWidgetDescription() {
.settingProvider(this::resolveSetting)
.styleProvider(variableManager -> null)
.ownerIdProvider(this::getOwnerId)
.diagnosticsProvider(variableManager -> List.of())
.kindProvider(object -> "")
.messageProvider(object -> "")
.diagnosticsProvider(this.propertiesValidationProvider.getDiagnosticsProvider())
.kindProvider(this.propertiesValidationProvider.getKindProvider())
.messageProvider(this.propertiesValidationProvider.getMessageProvider())
.build();
}

Expand Down
Expand Up @@ -161,7 +161,7 @@ private GroupDescription getGroupDescription() {
ifDescriptions.add(new EBooleanIfDescriptionProvider(this.composedAdapterFactory, this.propertiesValidationProvider, this.semanticTargetIdProvider).getIfDescription());
ifDescriptions.add(new EEnumIfDescriptionProvider(this.composedAdapterFactory, this.propertiesValidationProvider, this.semanticTargetIdProvider).getIfDescription());

ifDescriptions.add(new NonContainmentReferenceIfDescriptionProvider(this.composedAdapterFactory, this.objectService, this.semanticTargetIdProvider).getIfDescription());
ifDescriptions.add(new NonContainmentReferenceIfDescriptionProvider(this.composedAdapterFactory, this.objectService, this.propertiesValidationProvider, this.semanticTargetIdProvider).getIfDescription());

// @formatter:off
var numericDataTypes = List.of(
Expand Down
Expand Up @@ -17,8 +17,8 @@
import java.util.function.Function;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.sirius.components.collaborative.validation.api.IValidationService;
import org.eclipse.sirius.components.compatibility.emf.properties.api.IPropertiesValidationProvider;
import org.eclipse.sirius.components.representations.VariableManager;
Expand All @@ -42,7 +42,7 @@ public PropertiesValidationProvider(IValidationService validationService) {
public Function<VariableManager, List<?>> getDiagnosticsProvider() {
return variableManager -> {
var optionalEObject = variableManager.get(VariableManager.SELF, EObject.class);
var optionalEAttribute = variableManager.get(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class);
var optionalEAttribute = variableManager.get(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, EStructuralFeature.class);
if (optionalEObject.isPresent() && optionalEAttribute.isPresent()) {
return this.validationService.validate(optionalEObject.get(), optionalEAttribute.get());
}
Expand All @@ -55,8 +55,7 @@ public Function<VariableManager, List<?>> getDiagnosticsProvider() {
public Function<Object, String> getKindProvider() {
return object -> {
String kind = "Unknown";
if (object instanceof Diagnostic) {
Diagnostic diagnostic = (Diagnostic) object;
if (object instanceof Diagnostic diagnostic) {
switch (diagnostic.getSeverity()) {
case org.eclipse.emf.common.util.Diagnostic.ERROR:
kind = "Error";
Expand All @@ -79,8 +78,7 @@ public Function<Object, String> getKindProvider() {
@Override
public Function<Object, String> getMessageProvider() {
return object -> {
if (object instanceof Diagnostic) {
Diagnostic diagnostic = (Diagnostic) object;
if (object instanceof Diagnostic diagnostic) {
return diagnostic.getMessage();
}
return "";
Expand Down
Expand Up @@ -19,7 +19,7 @@

import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.sirius.components.annotations.Immutable;
import org.eclipse.sirius.components.forms.validation.Diagnostic;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IProps;

/**
Expand All @@ -38,8 +38,6 @@ public final class ReferenceElementProps implements IProps {

private String iconURL;

private List<Diagnostic> diagnostics;

private Supplier<String> helpTextProvider;

private boolean readOnly;
Expand All @@ -54,6 +52,8 @@ public final class ReferenceElementProps implements IProps {

private String ownerId;

private List<Element> children;

private ReferenceElementProps() {
// Prevent instantiation
}
Expand All @@ -74,10 +74,6 @@ public String getIconURL() {
return this.iconURL;
}

public List<Diagnostic> getDiagnostics() {
return this.diagnostics;
}

public Supplier<String> getHelpTextProvider() {
return this.helpTextProvider;
}
Expand Down Expand Up @@ -106,6 +102,11 @@ public String getOwnerId() {
return this.ownerId;
}

@Override
public List<Element> getChildren() {
return this.children;
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, label: {2}'}'";
Expand All @@ -128,8 +129,6 @@ public static final class Builder {
private boolean readOnly;
private Supplier<String> helpTextProvider;

private List<Diagnostic> diagnostics;

private List<ReferenceValue> values;

private List<ReferenceValue> options;
Expand All @@ -140,6 +139,8 @@ public static final class Builder {

private String ownerId;

private List<Element> children;

private Builder(String id) {
this.id = Objects.requireNonNull(id);
}
Expand All @@ -164,11 +165,6 @@ public Builder readOnly(boolean readOnly) {
return this;
}

public Builder diagnostics(List<Diagnostic> diagnostics) {
this.diagnostics = Objects.requireNonNull(diagnostics);
return this;
}

public Builder values(List<ReferenceValue> values) {
this.values = Objects.requireNonNull(values);
return this;
Expand All @@ -194,19 +190,24 @@ public Builder ownerId(String ownerId) {
return this;
}

public Builder children(List<Element> children) {
this.children = Objects.requireNonNull(children);
return this;
}

public ReferenceElementProps build() {
ReferenceElementProps referenceElementProps = new ReferenceElementProps();
referenceElementProps.id = Objects.requireNonNull(this.id);
referenceElementProps.label = Objects.requireNonNull(this.label);
referenceElementProps.iconURL = this.iconURL;
referenceElementProps.diagnostics = this.diagnostics;
referenceElementProps.readOnly = this.readOnly;
referenceElementProps.values = Objects.requireNonNull(this.values);
referenceElementProps.options = Objects.requireNonNull(this.options);
referenceElementProps.setting = Objects.requireNonNull(this.setting);
referenceElementProps.helpTextProvider = this.helpTextProvider; // Optional on purpose
referenceElementProps.style = this.style; // Optional on purpose
referenceElementProps.ownerId = Objects.requireNonNull(this.ownerId);
referenceElementProps.children = Objects.requireNonNull(this.children);
return referenceElementProps;
}
}
Expand Down
Expand Up @@ -19,6 +19,8 @@
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.sirius.components.forms.ClickEventKind;
import org.eclipse.sirius.components.forms.components.FormComponent;
import org.eclipse.sirius.components.forms.validation.DiagnosticComponent;
import org.eclipse.sirius.components.forms.validation.DiagnosticComponentProps;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IComponent;
import org.eclipse.sirius.components.representations.IStatus;
Expand Down Expand Up @@ -106,14 +108,16 @@ public Element render() {
})
.toList();

List<Element> children = List.of(new Element(DiagnosticComponent.class, new DiagnosticComponentProps(referenceDescription, variableManager)));

var builder = ReferenceElementProps.newReferenceElementProps(id)
.label(label)
.iconURL(iconURL)
.diagnostics(List.of())
.values(items)
.options(options)
.setting(setting)
.ownerId(ownerId);
.ownerId(ownerId)
.children(children);
if (referenceDescription.getHelpTextProvider() != null) {
builder.helpTextProvider(() -> referenceDescription.getHelpTextProvider().apply(variableManager));
}
Expand Down
Expand Up @@ -18,6 +18,7 @@

import org.eclipse.sirius.components.forms.description.AbstractWidgetDescription;
import org.eclipse.sirius.components.forms.renderer.IWidgetDescriptor;
import org.eclipse.sirius.components.forms.validation.Diagnostic;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IProps;
import org.eclipse.sirius.components.representations.VariableManager;
Expand Down Expand Up @@ -59,11 +60,17 @@ public Optional<Boolean> validateInstanceProps(String type, IProps props) {
@Override
public Optional<Object> instanciate(String type, IProps elementProps, List<Object> children) {
Optional<Object> result = Optional.empty();

List<Diagnostic> diagnostics = children.stream()
.filter(Diagnostic.class::isInstance)
.map(Diagnostic.class::cast)
.toList();

if (Objects.equals(type, ReferenceElementProps.TYPE) && elementProps instanceof ReferenceElementProps props) {
var builder = ReferenceWidget.newReferenceWidget(props.getId())
.label(props.getLabel())
.iconURL(props.getIconURL())
.diagnostics(props.getDiagnostics())
.diagnostics(diagnostics)
.readOnly(props.isReadOnly())
.setting(props.getSetting())
.referenceValues(props.getValues())
Expand Down
Expand Up @@ -138,6 +138,8 @@ export const ValuedReferenceAutocomplete = ({
{...params}
variant="standard"
placeholder={placeholder}
error={widget.diagnostics.length > 0}
helperText={widget.diagnostics[0]?.message}
InputProps={{
...params.InputProps,
endAdornment: (
Expand Down
Expand Up @@ -193,7 +193,7 @@ public boolean handles(EAttribute eAttribute) {
ifDescriptions.add(new EBooleanIfDescriptionProvider(this.composedAdapterFactory, this.propertiesValidationProvider, this.semanticTargetIdProvider).getIfDescription());
ifDescriptions.add(new EEnumIfDescriptionProvider(this.composedAdapterFactory, this.propertiesValidationProvider, this.semanticTargetIdProvider).getIfDescription());

ifDescriptions.add(new NonContainmentReferenceIfDescriptionProvider(this.composedAdapterFactory, this.objectService, this.semanticTargetIdProvider).getIfDescription());
ifDescriptions.add(new NonContainmentReferenceIfDescriptionProvider(this.composedAdapterFactory, this.objectService, this.propertiesValidationProvider, this.semanticTargetIdProvider).getIfDescription());

var numericDataTypes = List.of(
EcorePackage.Literals.EINT,
Expand Down

0 comments on commit 5b23d6c

Please sign in to comment.