-
Notifications
You must be signed in to change notification settings - Fork 45
/
AbstractNodeMappingConverter.java
224 lines (188 loc) · 12.9 KB
/
AbstractNodeMappingConverter.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*******************************************************************************
* 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.diagrams;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import org.eclipse.sirius.components.compatibility.api.IIdentifierProvider;
import org.eclipse.sirius.components.compatibility.api.IModelOperationHandlerSwitchProvider;
import org.eclipse.sirius.components.compatibility.api.ISemanticCandidatesProviderFactory;
import org.eclipse.sirius.components.compatibility.utils.StringValueProvider;
import org.eclipse.sirius.components.core.api.IEditService;
import org.eclipse.sirius.components.core.api.IObjectService;
import org.eclipse.sirius.components.diagrams.ILayoutStrategy;
import org.eclipse.sirius.components.diagrams.INodeStyle;
import org.eclipse.sirius.components.diagrams.IconLabelNodeStyle;
import org.eclipse.sirius.components.diagrams.ImageNodeStyle;
import org.eclipse.sirius.components.diagrams.InsideLabelLocation;
import org.eclipse.sirius.components.diagrams.NodeType;
import org.eclipse.sirius.components.diagrams.description.InsideLabelDescription;
import org.eclipse.sirius.components.diagrams.description.LabelStyleDescription;
import org.eclipse.sirius.components.diagrams.description.NodeDescription;
import org.eclipse.sirius.components.diagrams.description.SynchronizationPolicy;
import org.eclipse.sirius.components.diagrams.layout.incremental.provider.ImageSizeProvider;
import org.eclipse.sirius.components.interpreter.AQLInterpreter;
import org.eclipse.sirius.components.representations.VariableManager;
import org.eclipse.sirius.diagram.business.api.query.ContainerMappingQuery;
import org.eclipse.sirius.diagram.description.AbstractNodeMapping;
import org.eclipse.sirius.diagram.description.ContainerMapping;
import org.eclipse.sirius.viewpoint.description.style.BasicLabelStyleDescription;
import org.eclipse.sirius.viewpoint.description.style.StyleFactory;
import org.springframework.stereotype.Service;
/**
* This class is used to convert a Sirius AbstractNodeMapping to an Sirius Web NodeDescription.
*
* @author sbegaudeau
*/
@Service
public class AbstractNodeMappingConverter {
private final IObjectService objectService;
private final IEditService editService;
private final IIdentifierProvider identifierProvider;
private final ISemanticCandidatesProviderFactory semanticCandidatesProviderFactory;
private final IModelOperationHandlerSwitchProvider modelOperationHandlerSwitchProvider;
private final ImageSizeProvider imageSizeProvider;
public AbstractNodeMappingConverter(IObjectService objectService, IEditService editService, IIdentifierProvider identifierProvider,
ISemanticCandidatesProviderFactory semanticCandidatesProviderFactory, IModelOperationHandlerSwitchProvider modelOperationHandlerSwitchProvider, ImageSizeProvider imageSizeService) {
this.objectService = Objects.requireNonNull(objectService);
this.editService = Objects.requireNonNull(editService);
this.identifierProvider = Objects.requireNonNull(identifierProvider);
this.semanticCandidatesProviderFactory = Objects.requireNonNull(semanticCandidatesProviderFactory);
this.modelOperationHandlerSwitchProvider = Objects.requireNonNull(modelOperationHandlerSwitchProvider);
this.imageSizeProvider = Objects.requireNonNull(imageSizeService);
}
public NodeDescription convert(AbstractNodeMapping abstractNodeMapping, AQLInterpreter interpreter, Map<String, NodeDescription> id2NodeDescriptions) {
LabelStyleDescriptionConverter labelStyleDescriptionConverter = new LabelStyleDescriptionConverter(interpreter, this.objectService);
Function<VariableManager, org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription> abstractNodeMappingDescriptionProvider = new LabelStyleDescriptionProvider(interpreter,
abstractNodeMapping);
Function<VariableManager, String> labelIdProvider = this.getLabelIdProvider();
Function<VariableManager, String> labelExpressionProvider = this.getLabelExpressionProvider(interpreter, abstractNodeMappingDescriptionProvider);
Function<VariableManager, LabelStyleDescription> labelStyleDescriptionProvider = this.getLabelStyleDescriptionProvider(labelStyleDescriptionConverter, abstractNodeMappingDescriptionProvider);
Function<VariableManager, Boolean> isHeaderProvider = new AbstractNodeMappingIsHeaderProvider(interpreter, abstractNodeMapping);
InsideLabelDescription insideLabelDescription = InsideLabelDescription.newInsideLabelDescription(this.identifierProvider.getIdentifier(abstractNodeMapping) + InsideLabelDescription.INSIDE_LABEL_SUFFIX)
.idProvider(labelIdProvider)
.textProvider(labelExpressionProvider)
.styleDescriptionProvider(labelStyleDescriptionProvider)
.isHeaderProvider(isHeaderProvider)
.insideLabelLocation(InsideLabelLocation.TOP_CENTER)
.build();
Function<VariableManager, String> semanticTargetIdProvider = variableManager -> {
return variableManager.get(VariableManager.SELF, Object.class).map(this.objectService::getId).orElse(null);
};
Function<VariableManager, String> semanticTargetKindProvider = variableManager -> {
return variableManager.get(VariableManager.SELF, Object.class).map(this.objectService::getKind).orElse(null);
};
Function<VariableManager, String> semanticTargetLabelProvider = variableManager -> {
return variableManager.get(VariableManager.SELF, Object.class).map(this.objectService::getLabel).orElse(null);
};
Function<VariableManager, INodeStyle> styleProvider = new AbstractNodeMappingStyleProvider(interpreter, abstractNodeMapping);
Function<VariableManager, ILayoutStrategy> childrenLayoutStrategyProvider = new AbstractNodeMappingChildrenLayoutStrategyProvider(abstractNodeMapping);
Function<VariableManager, String> typeProvider = variableManager -> {
var result = NodeType.NODE_RECTANGLE;
INodeStyle style = styleProvider.apply(variableManager);
if (style instanceof ImageNodeStyle) {
result = NodeType.NODE_IMAGE;
} else if (style instanceof IconLabelNodeStyle) {
result = NodeType.NODE_ICON_LABEL;
}
return result;
};
AbstractNodeMappingSizeProvider sizeProvider = new AbstractNodeMappingSizeProvider(interpreter, abstractNodeMapping, this.imageSizeProvider);
String domainClass = abstractNodeMapping.getDomainClass();
String semanticCandidatesExpression = abstractNodeMapping.getSemanticCandidatesExpression();
String preconditionExpression = abstractNodeMapping.getPreconditionExpression();
Function<VariableManager, List<?>> semanticElementsProvider = this.semanticCandidatesProviderFactory.getSemanticCandidatesProvider(interpreter, domainClass, semanticCandidatesExpression,
preconditionExpression);
List<NodeDescription> childNodeDescriptions = this.getChildNodeDescriptions(abstractNodeMapping, interpreter, id2NodeDescriptions);
List<NodeDescription> borderNodeDescriptions = abstractNodeMapping.getBorderedNodeMappings().stream()
.map(borderNodeMapping -> this.convert(borderNodeMapping, interpreter, id2NodeDescriptions))
.toList();
ToolConverter toolConverter = new ToolConverter(interpreter, this.editService, this.modelOperationHandlerSwitchProvider);
var deleteHandler = toolConverter.createDeleteToolHandler(abstractNodeMapping.getDeletionDescription());
var labelEditHandler = toolConverter.createNodeDirectEditToolHandler(abstractNodeMapping.getLabelDirectEdit());
SynchronizationPolicy synchronizationPolicy = SynchronizationPolicy.SYNCHRONIZED;
if (!abstractNodeMapping.isCreateElements()) {
synchronizationPolicy = SynchronizationPolicy.UNSYNCHRONIZED;
}
boolean collapsible = false;
if (abstractNodeMapping instanceof ContainerMapping containerMapping) {
collapsible = new ContainerMappingQuery(containerMapping).isRegion();
}
NodeDescription description = NodeDescription.newNodeDescription(this.identifierProvider.getIdentifier(abstractNodeMapping))
.typeProvider(typeProvider)
.targetObjectIdProvider(semanticTargetIdProvider)
.targetObjectKindProvider(semanticTargetKindProvider)
.targetObjectLabelProvider(semanticTargetLabelProvider)
.semanticElementsProvider(semanticElementsProvider)
.synchronizationPolicy(synchronizationPolicy)
.insideLabelDescription(insideLabelDescription)
.styleProvider(styleProvider)
.childrenLayoutStrategyProvider(childrenLayoutStrategyProvider)
.sizeProvider(sizeProvider)
.borderNodeDescriptions(borderNodeDescriptions)
.childNodeDescriptions(childNodeDescriptions)
.collapsible(collapsible)
.labelEditHandler(labelEditHandler)
.deleteHandler(deleteHandler)
.build();
id2NodeDescriptions.put(description.getId(), description);
return description;
}
private Function<VariableManager, LabelStyleDescription> getLabelStyleDescriptionProvider(LabelStyleDescriptionConverter labelStyleDescriptionConverter,
Function<VariableManager, org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription> abstractNodeMappingDescriptionProvider) {
return variableManager -> {
org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription styleDescription = abstractNodeMappingDescriptionProvider.apply(variableManager);
BasicLabelStyleDescription basicLabelStyleDescription = Optional.ofNullable(styleDescription).map(BasicLabelStyleDescription.class::cast).orElse(this.getDefaultLabelStyle());
return labelStyleDescriptionConverter.convert(basicLabelStyleDescription);
};
}
private Function<VariableManager, String> getLabelExpressionProvider(AQLInterpreter interpreter,
Function<VariableManager, org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription> abstractNodeMappingDescriptionProvider) {
return variableManager -> {
org.eclipse.sirius.viewpoint.description.style.LabelStyleDescription styleDescription = abstractNodeMappingDescriptionProvider.apply(variableManager);
String labelExpression = Optional.ofNullable(styleDescription).map(BasicLabelStyleDescription::getLabelExpression).orElse("");
return new StringValueProvider(interpreter, labelExpression).apply(variableManager);
};
}
private Function<VariableManager, String> getLabelIdProvider() {
return variableManager -> {
Object parentId = variableManager.getVariables().get(InsideLabelDescription.OWNER_ID);
return String.valueOf(parentId) + InsideLabelDescription.INSIDE_LABEL_SUFFIX;
};
}
private List<NodeDescription> getChildNodeDescriptions(AbstractNodeMapping abstractNodeMapping, AQLInterpreter interpreter, Map<String, NodeDescription> id2NodeDescriptions) {
List<NodeDescription> childNodeDescriptions = new ArrayList<>();
if (abstractNodeMapping instanceof ContainerMapping) {
ContainerMapping containerMapping = (ContainerMapping) abstractNodeMapping;
List<NodeDescription> childNodeMappingDescriptions = containerMapping.getSubNodeMappings().stream()
.map(childNodeMapping -> this.convert(childNodeMapping, interpreter, id2NodeDescriptions))
.toList();
List<NodeDescription> childContainerMappingDescriptions = containerMapping.getSubContainerMappings().stream()
.map(childContainerMapping -> this.convert(childContainerMapping, interpreter, id2NodeDescriptions))
.toList();
childNodeDescriptions.addAll(childNodeMappingDescriptions);
childNodeDescriptions.addAll(childContainerMappingDescriptions);
}
return childNodeDescriptions;
}
private BasicLabelStyleDescription getDefaultLabelStyle() {
var labelStyle = StyleFactory.eINSTANCE.createBasicLabelStyleDescription();
labelStyle.setLabelExpression("");
labelStyle.setShowIcon(true);
labelStyle.setLabelSize(16);
return labelStyle;
}
}