-
Notifications
You must be signed in to change notification settings - Fork 45
/
DiagramEventProcessor.java
258 lines (213 loc) · 12.6 KB
/
DiagramEventProcessor.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*******************************************************************************
* 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.collaborative.diagrams;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.eclipse.sirius.components.collaborative.api.ChangeDescription;
import org.eclipse.sirius.components.collaborative.api.ChangeKind;
import org.eclipse.sirius.components.collaborative.api.IRepresentationPersistenceService;
import org.eclipse.sirius.components.collaborative.api.IRepresentationRefreshPolicy;
import org.eclipse.sirius.components.collaborative.api.IRepresentationRefreshPolicyRegistry;
import org.eclipse.sirius.components.collaborative.api.ISubscriptionManager;
import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramContext;
import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramCreationService;
import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramEventHandler;
import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramEventProcessor;
import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramInput;
import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramInputReferencePositionProvider;
import org.eclipse.sirius.components.collaborative.diagrams.dto.DiagramRefreshedEventPayload;
import org.eclipse.sirius.components.collaborative.diagrams.dto.LayoutDiagramInput;
import org.eclipse.sirius.components.collaborative.diagrams.dto.NodeLayoutDataInput;
import org.eclipse.sirius.components.collaborative.diagrams.dto.ReferencePosition;
import org.eclipse.sirius.components.collaborative.diagrams.dto.RenameDiagramInput;
import org.eclipse.sirius.components.collaborative.dto.RenameRepresentationInput;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IInput;
import org.eclipse.sirius.components.core.api.IPayload;
import org.eclipse.sirius.components.core.api.IRepresentationDescriptionSearchService;
import org.eclipse.sirius.components.core.api.IRepresentationInput;
import org.eclipse.sirius.components.core.api.SuccessPayload;
import org.eclipse.sirius.components.diagrams.Diagram;
import org.eclipse.sirius.components.diagrams.description.DiagramDescription;
import org.eclipse.sirius.components.diagrams.layoutdata.DiagramLayoutData;
import org.eclipse.sirius.components.diagrams.layoutdata.NodeLayoutData;
import org.eclipse.sirius.components.representations.IRepresentation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks.Many;
import reactor.core.publisher.Sinks.One;
/**
* Reacts to input that target a specific diagram, and {@link #getOutputEvents(IInput)} publishes} updated versions of the
* diagram to interested subscribers.
*
* @author sbegaudeau
* @author pcdavid
*/
public class DiagramEventProcessor implements IDiagramEventProcessor {
private final Logger logger = LoggerFactory.getLogger(DiagramEventProcessor.class);
private final IEditingContext editingContext;
private final IDiagramContext diagramContext;
private final List<IDiagramEventHandler> diagramEventHandlers;
private final ISubscriptionManager subscriptionManager;
private final IDiagramCreationService diagramCreationService;
private final IRepresentationDescriptionSearchService representationDescriptionSearchService;
private final IRepresentationRefreshPolicyRegistry representationRefreshPolicyRegistry;
private final IRepresentationPersistenceService representationPersistenceService;
private final DiagramEventFlux diagramEventFlux;
private final List<IDiagramInputReferencePositionProvider> diagramInputReferencePositionProviders;
private UUID currentRevisionId = UUID.randomUUID();
private String currentRevisionCause = DiagramRefreshedEventPayload.CAUSE_REFRESH;
public DiagramEventProcessor(DiagramEventProcessorParameters parameters) {
this.logger.trace("Creating the diagram event processor {}", parameters.diagramContext().getDiagram().getId());
this.editingContext = parameters.editingContext();
this.diagramContext = parameters.diagramContext();
this.diagramEventHandlers = parameters.diagramEventHandlers();
this.subscriptionManager = parameters.subscriptionManager();
this.representationDescriptionSearchService = parameters.representationDescriptionSearchService();
this.representationRefreshPolicyRegistry = parameters.representationRefreshPolicyRegistry();
this.representationPersistenceService = parameters.representationPersistenceService();
this.diagramCreationService = parameters.diagramCreationService();
this.diagramInputReferencePositionProviders = parameters.diagramInputReferencePositionProviders();
// We automatically refresh the representation before using it since things may have changed since the moment it
// has been saved in the database. This is quite similar to the auto-refresh on loading in Sirius.
Diagram diagram = this.diagramCreationService.refresh(this.editingContext, this.diagramContext).orElse(null);
this.representationPersistenceService.save(parameters.editingContext(), diagram);
this.diagramContext.update(diagram);
this.diagramEventFlux = new DiagramEventFlux(diagram);
if (diagram != null) {
this.logger.trace("Diagram refreshed: {})", diagram.getId());
}
}
@Override
public IRepresentation getRepresentation() {
return this.diagramContext.getDiagram();
}
@Override
public ISubscriptionManager getSubscriptionManager() {
return this.subscriptionManager;
}
@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IRepresentationInput representationInput) {
if (representationInput instanceof LayoutDiagramInput layoutDiagramInput) {
if (layoutDiagramInput.id().equals(this.currentRevisionId)) {
var diagram = this.diagramContext.getDiagram();
var nodeLayoutData = layoutDiagramInput.diagramLayoutData().nodeLayoutData().stream()
.collect(Collectors.toMap(
NodeLayoutDataInput::id,
nodeLayoutDataInput -> new NodeLayoutData(nodeLayoutDataInput.id(), nodeLayoutDataInput.position(), nodeLayoutDataInput.size())
));
var layoutData = new DiagramLayoutData(nodeLayoutData, Map.of(), Map.of());
var laidOutDiagram = Diagram.newDiagram(diagram)
.layoutData(layoutData)
.build();
this.representationPersistenceService.save(this.editingContext, laidOutDiagram);
this.diagramContext.reset();
this.diagramContext.update(laidOutDiagram);
this.diagramEventFlux.diagramRefreshed(layoutDiagramInput.id(), laidOutDiagram, DiagramRefreshedEventPayload.CAUSE_LAYOUT, null);
this.currentRevisionCause = DiagramRefreshedEventPayload.CAUSE_LAYOUT;
payloadSink.tryEmitValue(new SuccessPayload(layoutDiagramInput.id()));
} else {
payloadSink.tryEmitValue(new SuccessPayload(layoutDiagramInput.id()));
}
return;
}
IRepresentationInput effectiveInput = representationInput;
if (representationInput instanceof RenameRepresentationInput renameRepresentationInput) {
effectiveInput = new RenameDiagramInput(renameRepresentationInput.id(), renameRepresentationInput.editingContextId(), renameRepresentationInput.representationId(),
renameRepresentationInput.newLabel());
}
if (effectiveInput instanceof IDiagramInput diagramInput) {
Optional<IDiagramEventHandler> optionalDiagramEventHandler = this.diagramEventHandlers.stream().filter(handler -> handler.canHandle(diagramInput)).findFirst();
if (optionalDiagramEventHandler.isPresent()) {
IDiagramEventHandler diagramEventHandler = optionalDiagramEventHandler.get();
diagramEventHandler.handle(payloadSink, changeDescriptionSink, this.editingContext, this.diagramContext, diagramInput);
} else {
this.logger.warn("No handler found for event: {}", diagramInput);
}
}
}
@Override
public void refresh(ChangeDescription changeDescription) {
if (this.shouldRefresh(changeDescription)) {
Diagram refreshedDiagram = this.diagramCreationService.refresh(this.editingContext, this.diagramContext).orElse(null);
this.representationPersistenceService.save(this.editingContext, refreshedDiagram);
if (refreshedDiagram != null) {
this.logger.trace("Diagram refreshed: {}", refreshedDiagram.getId());
}
this.diagramContext.reset();
this.diagramContext.update(refreshedDiagram);
this.currentRevisionId = changeDescription.getInput().id();
this.currentRevisionCause = DiagramRefreshedEventPayload.CAUSE_REFRESH;
ReferencePosition referencePosition = this.getReferencePosition(changeDescription.getInput());
this.diagramEventFlux.diagramRefreshed(changeDescription.getInput().id(), refreshedDiagram, DiagramRefreshedEventPayload.CAUSE_REFRESH, referencePosition);
}
}
private ReferencePosition getReferencePosition(IInput diagramInput) {
return this.diagramInputReferencePositionProviders.stream()
.filter(handler -> handler.canHandle(diagramInput))
.findFirst()
.map(provider -> provider.getReferencePosition(diagramInput, this.diagramContext))
.orElse(null);
}
/**
* A diagram is refresh if there is a semantic change or if there is a diagram layout change coming from this very
* diagram (not other diagrams).
*
* @param changeDescription
* The change description
* @return <code>true</code> if the diagram should be refreshed, <code>false</code> otherwise
*/
public boolean shouldRefresh(ChangeDescription changeDescription) {
Diagram diagram = this.diagramContext.getDiagram();
// @formatter:off
var optionalDiagramDescription = this.representationDescriptionSearchService.findById(this.editingContext, diagram.getDescriptionId())
.filter(DiagramDescription.class::isInstance)
.map(DiagramDescription.class::cast);
// @formatter:on
// @formatter:off
return optionalDiagramDescription.flatMap(this.representationRefreshPolicyRegistry::getRepresentationRefreshPolicy)
.orElseGet(this::getDefaultRefreshPolicy)
.shouldRefresh(changeDescription);
// @formatter:on
}
private IRepresentationRefreshPolicy getDefaultRefreshPolicy() {
return changeDescription -> {
boolean shouldRefresh = false;
shouldRefresh = shouldRefresh || ChangeKind.SEMANTIC_CHANGE.equals(changeDescription.getKind());
if (!shouldRefresh && changeDescription.getSourceId().equals(this.diagramContext.getDiagram().getId())) {
shouldRefresh = shouldRefresh || DiagramChangeKind.DIAGRAM_LAYOUT_CHANGE.equals(changeDescription.getKind());
shouldRefresh = shouldRefresh || DiagramChangeKind.DIAGRAM_ELEMENT_VISIBILITY_CHANGE.equals(changeDescription.getKind());
shouldRefresh = shouldRefresh || DiagramChangeKind.DIAGRAM_ELEMENT_COLLAPSING_STATE_CHANGE.equals(changeDescription.getKind());
}
return shouldRefresh;
};
}
@Override
public Flux<IPayload> getOutputEvents(IInput input) {
// @formatter:off
return Flux.merge(
this.diagramEventFlux.getFlux(this.currentRevisionId, this.currentRevisionCause),
this.subscriptionManager.getFlux(input)
);
}
@Override
public void dispose() {
this.logger.trace("Disposing the diagram event processor {}", this.diagramContext.getDiagram().getId());
this.subscriptionManager.dispose();
this.diagramEventFlux.dispose();
}
}