-
Notifications
You must be signed in to change notification settings - Fork 45
/
NumberIfDescriptionProvider.java
153 lines (130 loc) · 6.83 KB
/
NumberIfDescriptionProvider.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
* 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.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.sirius.components.compatibility.emf.properties.api.IPropertiesValidationProvider;
import org.eclipse.sirius.components.emf.services.messages.IEMFMessageService;
import org.eclipse.sirius.components.forms.description.IfDescription;
import org.eclipse.sirius.components.forms.description.TextfieldDescription;
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 numbers.
*
* @author sbegaudeau
*/
public class NumberIfDescriptionProvider {
private static final String TEXTFIELD_DESCRIPTION_ID = "Textfield";
private final EDataType eDataType;
private final ComposedAdapterFactory composedAdapterFactory;
private final IPropertiesValidationProvider propertiesValidationProvider;
private final IEMFMessageService emfMessageService;
private final Function<VariableManager, String> semanticTargetIdProvider;
public NumberIfDescriptionProvider(EDataType eDataType, ComposedAdapterFactory composedAdapterFactory, IPropertiesValidationProvider propertiesValidationProvider,
IEMFMessageService emfMessageService, Function<VariableManager, String> semanticTargetIdProvider) {
this.eDataType = Objects.requireNonNull(eDataType);
this.composedAdapterFactory = Objects.requireNonNull(composedAdapterFactory);
this.propertiesValidationProvider = Objects.requireNonNull(propertiesValidationProvider);
this.emfMessageService = Objects.requireNonNull(emfMessageService);
this.semanticTargetIdProvider = Objects.requireNonNull(semanticTargetIdProvider);
}
public IfDescription getIfDescription() {
// @formatter:off
return IfDescription.newIfDescription(this.eDataType.getName())
.targetObjectIdProvider(this.semanticTargetIdProvider)
.predicate(this.getPredicate())
.childrenDescription(List.of(this.getTextfieldDescription()))
.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(this.eDataType)).isPresent();
};
}
private TextfieldDescription getTextfieldDescription() {
// @formatter:off
return TextfieldDescription.newTextfieldDescription(TEXTFIELD_DESCRIPTION_ID)
.targetObjectIdProvider(this.semanticTargetIdProvider)
.idProvider(this.getIdProvider())
.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> getIdProvider() {
return variableManager -> {
var eObjectPart = variableManager.get(VariableManager.SELF, EObject.class);
var featurePart = variableManager.get(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, EObject.class);
return this.getEObjectId(eObjectPart) + this.getEObjectId(featurePart);
};
}
private String getEObjectId(Optional<EObject> potentialEObject) {
// @formatter:off
return potentialEObject.map(EcoreUtil::getURI)
.map(Object::toString)
.orElse("");
// @formatter:on
}
private Function<VariableManager, String> getLabelProvider() {
return new EStructuralFeatureLabelProvider(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, this.composedAdapterFactory);
}
private Function<VariableManager, String> 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 EcoreUtil.convertToString(this.eDataType, value);
}
return "";
};
}
private BiFunction<VariableManager, String, IStatus> getNewValueHandler() {
return (variableManager, newValue) -> {
var optionalEObject = variableManager.get(VariableManager.SELF, EObject.class);
var optionalEAttribute = variableManager.get(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, EAttribute.class);
IStatus result = new Failure("");
if (optionalEObject.isPresent() && optionalEAttribute.isPresent()) {
EObject eObject = optionalEObject.get();
EAttribute eAttribute = optionalEAttribute.get();
try {
Object value = EcoreUtil.createFromString(this.eDataType, newValue);
eObject.eSet(eAttribute, value);
result = new Success();
} catch (NumberFormatException nfe) {
result = new Failure(this.emfMessageService.invalidNumber(newValue));
}
}
return result;
};
}
}