-
Notifications
You must be signed in to change notification settings - Fork 45
/
ReferenceWidgetDescriptor.java
99 lines (88 loc) · 3.85 KB
/
ReferenceWidgetDescriptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
* Copyright (c) 2023 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
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.widget.reference;
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.forms.validation.Diagnostic;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.IProps;
import org.eclipse.sirius.components.representations.VariableManager;
import org.springframework.stereotype.Component;
/**
* Widget descriptor for the reference widget(s).
*
* @author pcdavid
*/
@Component
public class ReferenceWidgetDescriptor implements IWidgetDescriptor {
public static final String TYPE = "ReferenceWidget";
@Override
public List<String> getWidgetTypes() {
return List.of(ReferenceElementProps.TYPE);
}
@Override
public Optional<Boolean> validateComponentProps(Class<?> componentType, IProps props) {
if (ReferenceWidgetComponent.class.equals(componentType)) {
return Optional.of(props instanceof ReferenceWidgetComponentProps);
} else {
return Optional.empty();
}
}
@Override
public Optional<Boolean> validateInstanceProps(String type, IProps props) {
Optional<Boolean> result = Optional.empty();
if (Objects.equals(type, ReferenceElementProps.TYPE)) {
result = Optional.of(props instanceof ReferenceElementProps);
}
return result;
}
@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(diagnostics)
.readOnly(props.isReadOnly())
.setting(props.getSetting())
.referenceValues(props.getValues())
.referenceOptions(props.getOptions())
.ownerId(props.getOwnerId());
if (props.getHelpTextProvider() != null) {
builder.helpTextProvider(props.getHelpTextProvider());
}
if (props.getStyle() != null) {
builder.style(props.getStyle());
}
result = Optional.of(builder.build());
}
return result;
}
@Override
public Optional<Element> createElement(VariableManager variableManager, AbstractWidgetDescription widgetDescription) {
if (widgetDescription instanceof ReferenceWidgetDescription referenceWidgetDescription) {
ReferenceWidgetComponentProps referenceComponentProps = new ReferenceWidgetComponentProps(variableManager, referenceWidgetDescription);
return Optional.of(new Element(ReferenceWidgetComponent.class, referenceComponentProps));
} else {
return Optional.empty();
}
}
}