-
Notifications
You must be signed in to change notification settings - Fork 45
/
FormEventProcessor.java
220 lines (178 loc) · 10.4 KB
/
FormEventProcessor.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
/*******************************************************************************
* 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.forms;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.sirius.components.collaborative.api.ChangeDescription;
import org.eclipse.sirius.components.collaborative.api.ChangeKind;
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.dto.RenameRepresentationInput;
import org.eclipse.sirius.components.collaborative.forms.api.FormCreationParameters;
import org.eclipse.sirius.components.collaborative.forms.api.IFormEventHandler;
import org.eclipse.sirius.components.collaborative.forms.api.IFormEventProcessor;
import org.eclipse.sirius.components.collaborative.forms.api.IFormInput;
import org.eclipse.sirius.components.collaborative.forms.api.IFormProcessor;
import org.eclipse.sirius.components.collaborative.forms.api.IWidgetSubscriptionManager;
import org.eclipse.sirius.components.collaborative.forms.configuration.FormEventProcessorConfiguration;
import org.eclipse.sirius.components.collaborative.forms.dto.FormRefreshedEventPayload;
import org.eclipse.sirius.components.collaborative.forms.dto.RenameFormInput;
import org.eclipse.sirius.components.collaborative.forms.dto.UpdateWidgetFocusInput;
import org.eclipse.sirius.components.collaborative.forms.dto.UpdateWidgetFocusSuccessPayload;
import org.eclipse.sirius.components.collaborative.forms.variables.FormVariableProvider;
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.IRepresentationInput;
import org.eclipse.sirius.components.forms.Form;
import org.eclipse.sirius.components.forms.components.FormComponent;
import org.eclipse.sirius.components.forms.components.FormComponentProps;
import org.eclipse.sirius.components.forms.renderer.FormRenderer;
import org.eclipse.sirius.components.forms.renderer.IWidgetDescriptor;
import org.eclipse.sirius.components.representations.Element;
import org.eclipse.sirius.components.representations.GetOrCreateRandomIdProvider;
import org.eclipse.sirius.components.representations.IRepresentation;
import org.eclipse.sirius.components.representations.VariableManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;
import reactor.core.publisher.Sinks.EmitResult;
import reactor.core.publisher.Sinks.Many;
import reactor.core.publisher.Sinks.One;
/**
* Reacts to the input that target the property sheet of a specific object and publishes updated versions of the
* {@link Form} to interested subscribers.
*
* @author pcdavid
*/
public class FormEventProcessor implements IFormEventProcessor {
private final Logger logger = LoggerFactory.getLogger(FormEventProcessor.class);
private final IEditingContext editingContext;
private final FormCreationParameters formCreationParameters;
private final List<IWidgetDescriptor> widgetDescriptors;
private final List<IFormEventHandler> formEventHandlers;
private final ISubscriptionManager subscriptionManager;
private final IWidgetSubscriptionManager widgetSubscriptionManager;
private final IRepresentationRefreshPolicyRegistry representationRefreshPolicyRegistry;
private final Many<IPayload> sink = Sinks.many().multicast().directBestEffort();
private final AtomicReference<Form> currentForm = new AtomicReference<>();
private final IFormProcessor formProcessor;
public FormEventProcessor(FormEventProcessorConfiguration configuration,
ISubscriptionManager subscriptionManager, IWidgetSubscriptionManager widgetSubscriptionManager, IRepresentationRefreshPolicyRegistry representationRefreshPolicyRegistry, IFormProcessor formProcessor) {
this.logger.trace("Creating the form event processor {}", configuration.formCreationParameters().getId());
this.editingContext = Objects.requireNonNull(configuration.editingContext());
this.formCreationParameters = Objects.requireNonNull(configuration.formCreationParameters());
this.widgetDescriptors = Objects.requireNonNull(configuration.widgetDescriptors());
this.formEventHandlers = Objects.requireNonNull(configuration.formEventHandlers());
this.subscriptionManager = Objects.requireNonNull(subscriptionManager);
this.widgetSubscriptionManager = Objects.requireNonNull(widgetSubscriptionManager);
this.representationRefreshPolicyRegistry = Objects.requireNonNull(representationRefreshPolicyRegistry);
this.formProcessor = formProcessor;
Form form = this.refreshForm();
this.currentForm.set(form);
}
@Override
public IRepresentation getRepresentation() {
return this.currentForm.get();
}
@Override
public ISubscriptionManager getSubscriptionManager() {
return this.subscriptionManager;
}
@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IRepresentationInput representationInput) {
IRepresentationInput effectiveInput = representationInput;
if (representationInput instanceof RenameRepresentationInput renameRepresentationInput) {
effectiveInput = new RenameFormInput(renameRepresentationInput.id(), renameRepresentationInput.editingContextId(), renameRepresentationInput.representationId(),
renameRepresentationInput.newLabel());
}
if (effectiveInput instanceof IFormInput formInput) {
if (formInput instanceof UpdateWidgetFocusInput input) {
this.widgetSubscriptionManager.handle(input);
payloadSink.tryEmitValue(new UpdateWidgetFocusSuccessPayload(input.id(), input.widgetId()));
changeDescriptionSink.tryEmitNext(new ChangeDescription(ChangeKind.FOCUS_CHANGE, input.representationId(), input));
} else {
Optional<IFormEventHandler> optionalFormEventHandler = this.formEventHandlers.stream().filter(handler -> handler.canHandle(formInput)).findFirst();
if (optionalFormEventHandler.isPresent()) {
IFormEventHandler formEventHandler = optionalFormEventHandler.get();
formEventHandler.handle(payloadSink, changeDescriptionSink, this.editingContext, this.currentForm.get(), formInput);
} else {
this.logger.warn("No handler found for event: {}", formInput);
}
}
}
}
@Override
public void refresh(ChangeDescription changeDescription) {
if (this.shouldRefresh(changeDescription)) {
Form form = this.refreshForm();
this.currentForm.set(form);
if (this.sink.currentSubscriberCount() > 0) {
EmitResult emitResult = this.sink.tryEmitNext(new FormRefreshedEventPayload(changeDescription.getInput().id(), form));
if (emitResult.isFailure()) {
String pattern = "An error has occurred while emitting a FormRefreshedEventPayload: {}";
this.logger.warn(pattern, emitResult);
}
}
}
}
private boolean shouldRefresh(ChangeDescription changeDescription) {
return this.representationRefreshPolicyRegistry.getRepresentationRefreshPolicy(this.formCreationParameters.getFormDescription())
.orElseGet(this::getDefaultRefreshPolicy)
.shouldRefresh(changeDescription);
}
private IRepresentationRefreshPolicy getDefaultRefreshPolicy() {
return (changeDescription) -> ChangeKind.SEMANTIC_CHANGE.equals(changeDescription.getKind());
}
private Form refreshForm() {
VariableManager variableManager = new VariableManager();
variableManager.put(VariableManager.SELF, this.formCreationParameters.getObject());
variableManager.put(FormVariableProvider.SELECTION.name(), this.formCreationParameters.getSelection());
variableManager.put(GetOrCreateRandomIdProvider.PREVIOUS_REPRESENTATION_ID, this.formCreationParameters.getId());
variableManager.put(IEditingContext.EDITING_CONTEXT, this.formCreationParameters.getEditingContext());
FormComponentProps formComponentProps = new FormComponentProps(variableManager, this.formCreationParameters.getFormDescription(), this.widgetDescriptors);
Element element = new Element(FormComponent.class, formComponentProps);
Form form = new FormRenderer(this.widgetDescriptors).render(element);
if (this.formProcessor != null) {
form = this.formProcessor.process(form, variableManager);
}
this.logger.trace("Form refreshed: {}", form.getId());
return form;
}
@Override
public Flux<IPayload> getOutputEvents(IInput input) {
var initialRefresh = Mono.fromCallable(() -> new FormRefreshedEventPayload(input.id(), this.currentForm.get()));
var refreshEventFlux = Flux.concat(initialRefresh, this.sink.asFlux());
return Flux.merge(
refreshEventFlux,
this.widgetSubscriptionManager.getFlux(input),
this.subscriptionManager.getFlux(input)
);
}
@Override
public void dispose() {
this.logger.trace("Disposing the form event processor {}", this.formCreationParameters.getId());
this.subscriptionManager.dispose();
this.widgetSubscriptionManager.dispose();
EmitResult emitResult = this.sink.tryEmitComplete();
if (emitResult.isFailure()) {
String pattern = "An error has occurred while marking the publisher as complete: {}";
this.logger.warn(pattern, emitResult);
}
}
}