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.composite;
35
36 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
37 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
38
39 import java.util.Collection;
40 import java.util.List;
41
42 import org.apache.commons.lang3.StringUtils;
43
44 import com.vaadin.v7.data.Item;
45 import com.vaadin.v7.data.util.PropertysetItem;
46
47
48
49
50
51
52
53
54
55 public class SwitchableTransformer extends CompositeTransformer {
56
57
58
59
60 @Deprecated
61 public SwitchableTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName) {
62 super(relatedFormItem, definition, type, fieldsName);
63 }
64
65 public SwitchableTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<PropertysetItem> type, List<String> fieldsName, I18NAuthoringSupport i18NAuthoringSupport) {
66 super(relatedFormItem, definition, type, fieldsName, i18NAuthoringSupport);
67 }
68
69 @Override
70 public void writeToItem(PropertysetItem newValues) {
71
72 String currentSelection = (String) newValues.getItemProperty(propertyPrefix).getValue();
73
74
75 Collection<String> propertyNames = (Collection<String>) newValues.getItemPropertyIds();
76 for (String propertyName : propertyNames) {
77
78 String compositePropertyName = getCompositePropertyName(propertyName);
79 if (!propertyName.equals(currentSelection) && !propertyName.equals(propertyPrefix)) {
80 relatedFormItem.removeItemProperty(compositePropertyName);
81 } else {
82 if (newValues.getItemProperty(propertyName) != null) {
83 relatedFormItem.addItemProperty(compositePropertyName, newValues.getItemProperty(propertyName));
84 }
85 }
86 }
87 }
88
89 @Override
90 public PropertysetItem readFromItem() {
91 return super.readFromItem();
92 }
93
94 @Override
95 protected String getCompositePropertyName(String propertyName) {
96 if (StringUtils.equals(propertyName, propertyPrefix)) {
97 propertyName = StringUtils.EMPTY;
98 }
99 return super.getCompositePropertyName(propertyName);
100 }
101 }