Skip to content

Commit

Permalink
[2064] Improve IWidgetDescriptor API
Browse files Browse the repository at this point in the history
Bug: #2064
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
  • Loading branch information
pcdavid authored and AxelRICHARD committed Jun 23, 2023
1 parent d563d18 commit 53916cd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ For View-based widgets, this materializes as an AQL `helpExpression`.
The help text can include multiple lines (separated by `\n`), but no text formatting.
:image:doc/images/Widget_Help_Tooltip.png[Example of a help tooltip on a widget]
- https://github.com/eclipse-sirius/sirius-components/issues/2048[#2048] [diagram] Add a basic support for the resize. It is possible to reduce the size of a node less than the space needed to display all children.

- https://github.com/eclipse-sirius/sirius-components/issues/2064[#2064] [forms] Make the `IWidgetDescriptor` API more flexible.

== v2023.6.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.sirius.components.forms.components;

import java.util.Objects;
import java.util.Optional;

import org.eclipse.sirius.components.forms.description.AbstractWidgetDescription;
import org.eclipse.sirius.components.forms.description.ButtonDescription;
Expand All @@ -30,7 +31,6 @@
import org.eclipse.sirius.components.forms.description.TextareaDescription;
import org.eclipse.sirius.components.forms.description.TextfieldDescription;
import org.eclipse.sirius.components.forms.description.TreeDescription;
import org.eclipse.sirius.components.forms.renderer.IWidgetDescriptor;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IComponent;
import org.eclipse.sirius.components.representations.VariableManager;
Expand Down Expand Up @@ -105,13 +105,12 @@ public Element render() {
RichTextComponentProps richTextComponentProps = new RichTextComponentProps(variableManager, (RichTextDescription) widgetDescription);
element = new Element(RichTextComponent.class, richTextComponentProps);
} else {
for (IWidgetDescriptor widgetDescriptor : this.props.getWidgetDescriptors()) {
var optionalElement = widgetDescriptor.createElement(variableManager, widgetDescription);
if (optionalElement.isPresent()) {
element = optionalElement.get();
break;
}
}
element = this.props.getWidgetDescriptors().stream()
.map(widgetDescriptor -> widgetDescriptor.createElement(variableManager, widgetDescription))
.filter(Optional::isPresent)
.findFirst()
.map(Optional::get)
.orElse(null);
if (element == null) {
String pattern = "Unsupported widget description: {}";
this.logger.warn(pattern, widgetDescription.getClass().getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.sirius.components.charts.barchart.components.BarChartComponent;
import org.eclipse.sirius.components.charts.barchart.components.BarChartComponentProps;
Expand Down Expand Up @@ -137,9 +138,10 @@ public boolean validateComponentProps(Class<?> componentType, IProps props) {
checkValidProps = props instanceof ToolbarActionComponentProps;
} else {
checkValidProps = this.widgetDescriptors.stream()
.filter(widgetDescriptor -> Objects.equals(componentType, widgetDescriptor.getComponentClass()))
.map(widgetDescriptor -> widgetDescriptor.validateComponentProps(componentType, props))
.filter(Optional::isPresent)
.findFirst()
.map(descriptor -> descriptor.getComponentPropsClass().isInstance(props))
.map(Optional::get)
.orElse(false);
}
return checkValidProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,12 @@ public Object instantiateElement(String type, IProps props, List<Object> childre
} else if (ToolbarActionElementProps.TYPE.equals(type) && props instanceof ToolbarActionElementProps) {
object = this.instantiateToolbarAction((ToolbarActionElementProps) props, children);
} else {
for (IWidgetDescriptor widgetDescriptor : this.widgetDescriptors) {
if (widgetDescriptor.getWidgetType().equals(type) && widgetDescriptor.getInstancePropsClass().isInstance(props)) {
Optional<Object> optionalInstance = widgetDescriptor.instanciate(props, children);
if (optionalInstance.isPresent()) {
object = optionalInstance.get();
break;
}
}
}
object = this.widgetDescriptors.stream()
.map(widgetDescriptor -> widgetDescriptor.instanciate(type, props, children))
.filter(Optional::isPresent)
.findFirst()
.map(Optional::get)
.orElse(null);
}

return object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.sirius.components.charts.barchart.elements.BarChartElementProps;
import org.eclipse.sirius.components.charts.piechart.elements.PieChartElementProps;
Expand Down Expand Up @@ -104,9 +105,10 @@ public boolean validateInstanceProps(String type, IProps props) {
checkValidProps = props instanceof ToolbarActionElementProps;
} else {
checkValidProps = this.widgetDescriptors.stream()
.filter(widgetDescriptor -> Objects.equals(type, widgetDescriptor.getWidgetType()))
.map(widgetDescriptor -> widgetDescriptor.validateInstanceProps(type, props))
.filter(Optional::isPresent)
.findFirst()
.map(descriptor -> descriptor.getInstancePropsClass().isInstance(props))
.map(Optional::get)
.orElse(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.eclipse.sirius.components.forms.description.AbstractWidgetDescription;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IComponent;
import org.eclipse.sirius.components.representations.IProps;
import org.eclipse.sirius.components.representations.VariableManager;

Expand All @@ -29,13 +28,11 @@
public interface IWidgetDescriptor {
String getWidgetType();

Class<? extends IComponent> getComponentClass();
Optional<Boolean> validateComponentProps(Class<?> componentType, IProps props);

Class<? extends IProps> getInstancePropsClass();
Optional<Boolean> validateInstanceProps(String type, IProps props);

Class<? extends IProps> getComponentPropsClass();

Optional<Object> instanciate(IProps elementProps, List<Object> children);
Optional<Object> instanciate(String type, IProps elementProps, List<Object> children);

Optional<Element> createElement(VariableManager variableManager, AbstractWidgetDescription widgetDescription);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
package org.eclipse.sirius.web.sample.slider;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.sirius.components.forms.description.AbstractWidgetDescription;
import org.eclipse.sirius.components.forms.renderer.IWidgetDescriptor;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IComponent;
import org.eclipse.sirius.components.representations.IProps;
import org.eclipse.sirius.components.representations.VariableManager;
import org.springframework.stereotype.Component;
Expand All @@ -38,23 +38,26 @@ public String getWidgetType() {
}

@Override
public Class<? extends IComponent> getComponentClass() {
return SliderComponent.class;
}

@Override
public Class<? extends IProps> getInstancePropsClass() {
return SliderElementProps.class;
public Optional<Boolean> validateComponentProps(Class<?> componentType, IProps props) {
if (SliderComponent.class.equals(componentType)) {
return Optional.of(props instanceof SliderComponentProps);
} else {
return Optional.empty();
}
}

@Override
public Class<? extends IProps> getComponentPropsClass() {
return SliderComponentProps.class;
public Optional<Boolean> validateInstanceProps(String type, IProps props) {
if (Objects.equals(type, TYPE)) {
return Optional.of(props instanceof SliderElementProps);
} else {
return Optional.empty();
}
}

@Override
public Optional<Object> instanciate(IProps elementProps, List<Object> children) {
if (elementProps instanceof SliderElementProps props) {
public Optional<Object> instanciate(String type, IProps elementProps, List<Object> children) {
if (Objects.equals(type, TYPE) && elementProps instanceof SliderElementProps props) {
var sliderBuilder = Slider.newSlider(props.getId())
.label(props.getLabel())
.minValue(props.getMinValue())
Expand Down

0 comments on commit 53916cd

Please sign in to comment.