-
Notifications
You must be signed in to change notification settings - Fork 45
/
EBooleanIfDescriptionProvider.java
122 lines (104 loc) · 5.58 KB
/
EBooleanIfDescriptionProvider.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*******************************************************************************
* Copyright (c) 2019, 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.compatibility.emf.properties;
import java.util.List;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EcorePackage;
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.forms.description.CheckboxDescription;
import org.eclipse.sirius.components.forms.description.IfDescription;
import org.eclipse.sirius.components.representations.Failure;
import org.eclipse.sirius.components.representations.IStatus;
import org.eclipse.sirius.components.representations.Success;
import org.eclipse.sirius.components.representations.VariableManager;
/**
* Provides the default description of the widget to use to support an EBoolean feature.
*
* @author sbegaudeau
*/
public class EBooleanIfDescriptionProvider {
private static final String IF_DESCRIPTION_ID = "EBoolean";
private static final String CHECKBOX_DESCRIPTION_ID = "Checkbox";
private final ComposedAdapterFactory composedAdapterFactory;
private final IPropertiesValidationProvider propertiesValidationProvider;
private final Function<VariableManager, String> semanticTargetIdProvider;
public EBooleanIfDescriptionProvider(ComposedAdapterFactory composedAdapterFactory, IPropertiesValidationProvider propertiesValidationProvider, Function<VariableManager, String> semanticTargetIdProvider) {
this.composedAdapterFactory = Objects.requireNonNull(composedAdapterFactory);
this.propertiesValidationProvider = Objects.requireNonNull(propertiesValidationProvider);
this.semanticTargetIdProvider = Objects.requireNonNull(semanticTargetIdProvider);
}
public IfDescription getIfDescription() {
// @formatter:off
return IfDescription.newIfDescription(IF_DESCRIPTION_ID)
.targetObjectIdProvider(this.semanticTargetIdProvider)
.predicate(this.getPredicate())
.controlDescriptions(List.of(this.getCheckboxDescription()))
.build();
// @formatter:on
}
private Function<VariableManager, Boolean> getPredicate() {
return variableManager -> {
var optionalEAttribute = variableManager.get(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class);
return optionalEAttribute.filter(eAttribute -> eAttribute.getEType().equals(EcorePackage.Literals.EBOOLEAN)).isPresent();
};
}
private CheckboxDescription getCheckboxDescription() {
// @formatter:off
return CheckboxDescription.newCheckboxDescription(CHECKBOX_DESCRIPTION_ID)
.targetObjectIdProvider(this.semanticTargetIdProvider)
.idProvider(new WidgetIdProvider())
.labelProvider(this.getLabelProvider())
.valueProvider(this.getValueProvider())
.newValueHandler(this.getNewValueHandler())
.diagnosticsProvider(this.propertiesValidationProvider.getDiagnosticsProvider())
.kindProvider(this.propertiesValidationProvider.getKindProvider())
.messageProvider(this.propertiesValidationProvider.getMessageProvider())
.build();
// @formatter:on
}
private Function<VariableManager, String> getLabelProvider() {
return new EStructuralFeatureLabelProvider(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, this.composedAdapterFactory);
}
private Function<VariableManager, Boolean> getValueProvider() {
return variableManager -> {
var optionalEObject = variableManager.get(VariableManager.SELF, EObject.class);
var optionalEAttribute = variableManager.get(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class);
if (optionalEObject.isPresent() && optionalEAttribute.isPresent()) {
EObject eObject = optionalEObject.get();
EAttribute eAttribute = optionalEAttribute.get();
Object value = eObject.eGet(eAttribute);
return Boolean.valueOf(value.toString());
}
return Boolean.FALSE;
};
}
private BiFunction<VariableManager, Boolean, IStatus> getNewValueHandler() {
return (variableManager, newValue) -> {
var optionalEObject = variableManager.get(VariableManager.SELF, EObject.class);
var optionalEAttribute = variableManager.get(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class);
if (optionalEObject.isPresent() && optionalEAttribute.isPresent()) {
EObject eObject = optionalEObject.get();
EAttribute eAttribute = optionalEAttribute.get();
eObject.eSet(eAttribute, newValue);
return new Success();
}
return new Failure("");
};
}
}