-
Notifications
You must be signed in to change notification settings - Fork 45
/
NonContainmentReferenceIfDescriptionProvider.java
155 lines (129 loc) · 7.01 KB
/
NonContainmentReferenceIfDescriptionProvider.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
154
155
/*******************************************************************************
* 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.compatibility.emf.properties;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
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;
import org.eclipse.sirius.components.representations.VariableManager;
import org.eclipse.sirius.components.widget.reference.ReferenceWidgetComponent;
import org.eclipse.sirius.components.widget.reference.ReferenceWidgetDescription;
/**
* Provides the default description of the widget to use to support non-containment reference.
*
* @author frouene
*/
public class NonContainmentReferenceIfDescriptionProvider {
private static final String ID_DESCRIPTION_ID = "NonContainment Reference";
private static final String REFERENCE_WIDGET_DESCRIPTION_ID = "ReferenceWidget";
private final ComposedAdapterFactory composedAdapterFactory;
private final IObjectService objectService;
private final IPropertiesValidationProvider propertiesValidationProvider;
private final 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);
}
public IfDescription getIfDescription() {
return IfDescription.newIfDescription(ID_DESCRIPTION_ID)
.targetObjectIdProvider(this.semanticTargetIdProvider)
.predicate(this.getPredicate())
.controlDescriptions(List.of(this.getReferenceWidgetDescription()))
.build();
}
private Function<VariableManager, Boolean> getPredicate() {
return variableManager -> {
var optionalEReference = variableManager.get(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, EReference.class);
return optionalEReference.filter(eReference -> !eReference.isContainment()).isPresent();
};
}
private ReferenceWidgetDescription getReferenceWidgetDescription() {
return ReferenceWidgetDescription.newReferenceWidgetDescription(REFERENCE_WIDGET_DESCRIPTION_ID)
.targetObjectIdProvider(this.semanticTargetIdProvider)
.idProvider(new WidgetIdProvider())
.labelProvider(this.getLabelProvider())
.optionsProvider(this.getOptionsProvider())
.iconURLProvider(variableManager -> "")
.itemsProvider(this::getReferenceValue)
.itemIdProvider(this::getItemId)
.itemKindProvider(this::getItemKind)
.itemLabelProvider(this::getItemLabel)
.itemImageURLProvider(this::getItemIconURL)
.settingProvider(this::resolveSetting)
.styleProvider(variableManager -> null)
.ownerIdProvider(this::getOwnerId)
.diagnosticsProvider(this.propertiesValidationProvider.getDiagnosticsProvider())
.kindProvider(this.propertiesValidationProvider.getKindProvider())
.messageProvider(this.propertiesValidationProvider.getMessageProvider())
.build();
}
private List<?> getReferenceValue(VariableManager variableManager) {
List<?> value = List.of();
EStructuralFeature.Setting setting = this.resolveSetting(variableManager);
if (setting != null) {
var rawValue = setting.get(true);
if (setting.getEStructuralFeature().isMany()) {
value = (List<?>) rawValue;
} else if (rawValue != null) {
value = List.of(rawValue);
} else {
value = List.of();
}
}
return value;
}
private Function<VariableManager, List<?>> getOptionsProvider() {
return new EStructuralFeatureChoiceOfValueProvider(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, this.composedAdapterFactory);
}
private EStructuralFeature.Setting resolveSetting(VariableManager variableManager) {
EObject referenceOwner = variableManager.get(VariableManager.SELF, EObject.class).orElse(null);
var optionalEReference = variableManager.get(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, EReference.class);
if (referenceOwner != null && optionalEReference.isPresent()) {
return ((InternalEObject) referenceOwner).eSetting(optionalEReference.get());
} else {
return null;
}
}
private Optional<Object> getItem(VariableManager variableManager) {
return variableManager.get(ReferenceWidgetComponent.ITEM_VARIABLE, Object.class);
}
private String getItemLabel(VariableManager variableManager) {
return this.getItem(variableManager).map(this.objectService::getLabel).orElse("");
}
private String getItemIconURL(VariableManager variableManager) {
return this.getItem(variableManager).map(this.objectService::getImagePath).orElse("");
}
private String getItemKind(VariableManager variableManager) {
return this.getItem(variableManager).map(this.objectService::getKind).orElse("");
}
private String getItemId(VariableManager variableManager) {
return this.getItem(variableManager).map(this.objectService::getId).orElse("");
}
private String getOwnerId(VariableManager variableManager) {
return variableManager.get(VariableManager.SELF, EObject.class).map(this.objectService::getId).orElse("");
}
private Function<VariableManager, String> getLabelProvider() {
return new EStructuralFeatureLabelProvider(PropertiesDefaultDescriptionProvider.ESTRUCTURAL_FEATURE, this.composedAdapterFactory);
}
}