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 package info.magnolia.ui.editor;
35
36 import static java.util.stream.Collectors.toList;
37
38 import info.magnolia.i18nsystem.SimpleTranslator;
39 import info.magnolia.icons.MagnoliaIcons;
40 import info.magnolia.ui.contentapp.Datasource;
41 import info.magnolia.ui.framework.WithImplementation;
42 import info.magnolia.ui.theme.ResurfaceTheme;
43
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.LinkedHashMap;
47 import java.util.List;
48 import java.util.Locale;
49 import java.util.Map;
50 import java.util.concurrent.CompletableFuture;
51 import java.util.stream.Stream;
52
53 import javax.inject.Inject;
54
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 import com.vaadin.data.BinderValidationStatus;
59 import com.vaadin.ui.AbstractOrderedLayout;
60 import com.vaadin.ui.Alignment;
61 import com.vaadin.ui.Button;
62 import com.vaadin.ui.Component;
63 import com.vaadin.ui.HorizontalLayout;
64 import com.vaadin.ui.Notification;
65 import com.vaadin.ui.VerticalLayout;
66
67 import lombok.EqualsAndHashCode;
68 import lombok.Getter;
69 import lombok.Setter;
70
71
72
73
74
75
76
77 public class MultiFormView<T> implements EditorView<T> {
78
79 private static final Logger log = LoggerFactory.getLogger(MultiFormView.class);
80
81 private final MultiFormDefinition<T> definition;
82 private final Locale locale;
83 private final Datasource<T> datasource;
84 private final MultiFormState state = new MultiFormState();
85
86 private VerticalLayout rootLayout = new VerticalLayout();
87
88 @Inject
89 public MultiFormView(MultiFormDefinition<T> definition, LocaleContext localeContext, Datasource<T> datasource) {
90 this.definition = definition;
91 this.locale = localeContext.getDefault();
92 this.datasource = datasource;
93 this.state.entryResolution = create(definition.getEntryResolution(), this.locale);
94 this.rootLayout.setSpacing(true);
95 this.rootLayout.addStyleName("multi-form-view");
96
97 layout();
98 }
99
100
101
102
103 @Deprecated
104 public MultiFormView(MultiFormDefinition<T> definition, SimpleTranslator i18n, LocaleContext localeContext, Datasource<T> datasource) {
105 this(definition, localeContext, datasource);
106 }
107
108 @Override
109 public List<BinderValidationStatus<?>> validate() {
110 return getAllChildren()
111 .flatMap(view -> view.validate().stream())
112 .collect(toList());
113 }
114
115 @Override
116 public void write(T item) {
117 Map<EditorView<T>, ItemProviderStrategy<T, T>> children = state.children;
118 state.removedItemAccessors.stream()
119 .map(accessor -> accessor.read(item, locale))
120 .forEach(itemToMaybeDelete ->
121 itemToMaybeDelete
122 .ifPresent(this.datasource::remove));
123
124 List<T> itemOrder = new ArrayList<>();
125 state.order.forEach(subForm ->
126 children.get(subForm).read(item, locale).ifPresent(localisedItem -> {
127 itemOrder.add(localisedItem);
128 subForm.write(localisedItem);
129 }));
130
131 create(this.definition.getOrderHandler(), locale).applyOrder(itemOrder);
132 }
133
134 @Override
135 public Component asVaadinComponent() {
136 return rootLayout;
137 }
138
139 @Override
140 public void populate(T item) {
141 getAllChildren().forEach(subForm -> {
142 rootLayout.removeComponent(subForm.asVaadinComponent());
143 subForm.destroy();
144 });
145
146 state.clear();
147 state.entryResolution.resolveForRoot(item).forEach(propertyDefinition -> {
148 if (propertyDefinition != null) {
149 EditorView<T> subForm = createSubForm(propertyDefinition);
150 addSubFormToState(subForm, propertyDefinition);
151 populate(item, subForm);
152 }
153 });
154
155 layout();
156 }
157
158 private EditorView<T> createSubForm(ComplexPropertyDefinition<T> propertyDefinition) {
159 final EditorView<T> subForm = create("child", propertyDefinition.getEditorDefinition());
160 if (subForm.asVaadinComponent().getCaption() == null) {
161 subForm.asVaadinComponent().setCaption(propertyDefinition.getLabel());
162 }
163 return subForm;
164 }
165
166 private void addSubFormToState(EditorView<T> subForm, ComplexPropertyDefinition<T> propertyDefinition) {
167 ItemProviderStrategy<T, T> itemProviderStrategy = create(propertyDefinition.getItemProvider());
168 state.children.put(subForm, itemProviderStrategy);
169 state.order.add(subForm);
170 }
171
172 private void populate(T item, EditorView<T> subForm) {
173 state.children.get(subForm).read(item, locale).ifPresent(subForm::populate);
174 }
175
176 public void layout() {
177 rootLayout.removeAllComponents();
178 rootLayout.setMargin(false);
179 state.children.keySet().stream()
180 .map(this::wrapChildForm)
181 .forEach(this.rootLayout::addComponent);
182 attachAddButton();
183 }
184
185 protected Component wrapChildForm(EditorView<T> subForm) {
186 HorizontalLayout wrapLayout = new HorizontalLayout();
187 wrapLayout.setWidth("100%");
188 subForm.asVaadinComponent().addStyleName("multi-form-entry-content");
189
190 HorizontalLayout buttonLayout = createButtonsLayout(subForm, wrapLayout);
191 wrapLayout.addComponents(subForm.asVaadinComponent(), buttonLayout);
192 wrapLayout.setExpandRatio(subForm.asVaadinComponent(), 1f);
193 wrapLayout.setComponentAlignment(buttonLayout, Alignment.TOP_CENTER);
194 wrapLayout.addStyleName("multi-form-entry");
195
196 return wrapLayout;
197 }
198
199 private HorizontalLayout createButtonsLayout(EditorView<T> subForm, HorizontalLayout wrapLayout) {
200 HorizontalLayout buttonLayout = new HorizontalLayout();
201 buttonLayout.setSpacing(false);
202
203 if (!(definition.getOrderHandler() instanceof MultiFormDefinition.OrderHandlerDefinition.Noop)) {
204 buttonLayout.addComponents(
205 new Button(MagnoliaIcons.ARROW2_N, e -> onMove(wrapLayout, true)),
206 new Button(MagnoliaIcons.ARROW2_S, e -> onMove(wrapLayout, false))
207 );
208 }
209 if (definition.isCanRemoveItems()) {
210 Button removeButton = new Button(MagnoliaIcons.TRASH, e -> onDelete(subForm));
211 removeButton.setDescription(definition.getButtonSelectRemoveLabel());
212 buttonLayout.addComponent(removeButton);
213 }
214
215 buttonLayout.forEach(button -> button.addStyleName(ResurfaceTheme.BUTTON_ICON));
216 return buttonLayout;
217 }
218
219 protected void attachAddButton() {
220 Button addButton = new Button(definition.getButtonSelectAddLabel());
221 addButton.addStyleName("add-multi-form-entry-button");
222 addButton.addClickListener(e -> {
223 state.entryResolution.pick().thenAccept(propertyDefinition -> {
224 if (propertyDefinition != null) {
225 EditorView<T> subForm = createSubForm(propertyDefinition);
226 addSubFormToState(subForm, propertyDefinition);
227 subForm.applyDefaults();
228 rootLayout.addComponent(wrapChildForm(subForm), rootLayout.getComponentCount() - 1);
229 }
230 }).exceptionally(ex -> {
231 log.warn("Failed to create a multi field entry", ex);
232 Notification.show("Failed to create multi field entry");
233 return null;
234 });
235 });
236
237 rootLayout.addComponent(addButton);
238 }
239
240 private Stream<EditorView<T>> getAllChildren() {
241 return state.children.keySet().stream();
242 }
243
244 private void onMove(AbstractOrderedLayout movedLayout, boolean moveUp) {
245 int currentPosition = rootLayout.getComponentIndex(movedLayout);
246 int newPosition = moveUp ? currentPosition - 1 : currentPosition + 1;
247
248 if (currentPosition == 0 && moveUp) {
249 return;
250 }
251
252 if (newPosition >= rootLayout.getComponentCount() - 1) {
253 return;
254 }
255
256 Collections.swap(state.order, currentPosition, newPosition);
257 rootLayout.replaceComponent(rootLayout.getComponent(currentPosition), rootLayout.getComponent(newPosition));
258 }
259
260 private void onDelete(EditorView<T> subForm) {
261 rootLayout.removeComponent(subForm.asVaadinComponent().getParent());
262
263 state.removedItemAccessors.add(state.children.get(subForm));
264 state.children.remove(subForm);
265 state.order.remove(subForm);
266 }
267
268 class MultiFormState {
269 Map<EditorView<T>, ItemProviderStrategy<T, T>> children = new LinkedHashMap<>();
270 List<ItemProviderStrategy<T, T>> removedItemAccessors = new ArrayList<>();
271 List<EditorView<T>> order = new ArrayList<>();
272 EntryResolution<T> entryResolution;
273
274 void clear() {
275 children.clear();
276 removedItemAccessors.clear();
277 order.clear();
278 }
279 }
280
281
282
283
284
285
286 public interface EntryResolution<T> {
287
288 Stream<ComplexPropertyDefinition<T>> resolveForRoot(T rootDatasource);
289
290 CompletableFuture<ComplexPropertyDefinition<T>> pick();
291
292 @Getter
293 @Setter
294 @EqualsAndHashCode
295 class Definition<T> implements WithImplementation<EntryResolution<T>> {
296 private Class<? extends EntryResolution<T>> implementationClass;
297 }
298 }
299 }