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.form.field.transformer.multi;
35
36 import info.magnolia.objectfactory.Components;
37 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
38 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
39 import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
40
41 import java.util.Iterator;
42 import java.util.LinkedList;
43 import java.util.List;
44
45 import javax.inject.Inject;
46
47 import com.vaadin.v7.data.Item;
48 import com.vaadin.v7.data.Property;
49 import com.vaadin.v7.data.util.ObjectProperty;
50 import com.vaadin.v7.data.util.PropertysetItem;
51
52
53
54
55
56
57
58 public class MultiValueTransformer extends BasicTransformer<PropertysetItem> {
59
60
61
62
63 @Deprecated
64 public MultiValueTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type) {
65 this(relatedFormItem, definition, type, Components.getComponent(I18NAuthoringSupport.class));
66 }
67
68 @Inject
69 public MultiValueTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, I18NAuthoringSupport i18NAuthoringSupport) {
70 super(relatedFormItem, definition, type, i18NAuthoringSupport);
71 }
72
73 @SuppressWarnings("rawtypes")
74 @Override
75 public void writeToItem(PropertysetItem newValue) {
76 Property<List> property = getOrCreateProperty(List.class);
77
78 List<Object> propertyValue = null;
79 if (newValue != null && !newValue.getItemPropertyIds().isEmpty()) {
80 propertyValue = new LinkedList<>();
81 Iterator<?> it = newValue.getItemPropertyIds().iterator();
82 while (it.hasNext()) {
83 propertyValue.add(newValue.getItemProperty(it.next()).getValue());
84 }
85 }
86 property.setValue(propertyValue);
87 }
88
89 @SuppressWarnings("rawtypes")
90 @Override
91 public PropertysetItem readFromItem() {
92 PropertysetItem newValues = new PropertysetItem();
93 Property<List> property = getOrCreateProperty(List.class);
94 if (property.getValue() != null) {
95 List<?> values = property.getValue();
96 int position = 0;
97 for (Object o : values) {
98 newValues.addItemProperty(position, new ObjectProperty<>(o));
99 position += 1;
100 }
101 }
102
103 return newValues;
104 }
105
106 }