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.basic;
35
36 import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
37 import info.magnolia.ui.form.field.transformer.Transformer;
38 import info.magnolia.ui.form.field.transformer.UndefinedPropertyType;
39 import info.magnolia.ui.vaadin.integration.jcr.DefaultProperty;
40
41 import java.util.Locale;
42
43 import javax.inject.Inject;
44
45 import com.vaadin.data.Item;
46 import com.vaadin.data.Property;
47
48
49
50
51
52
53
54
55
56
57 public class BasicTransformer<T> implements Transformer<T> {
58
59 protected Item relatedFormItem;
60 protected final ConfiguredFieldDefinition definition;
61
62 protected String basePropertyName;
63 protected String i18NPropertyName;
64 private Locale locale;
65 protected Class<T> type;
66
67 @Inject
68 public BasicTransformer(Item relatedFormItem, ConfiguredFieldDefinition definition, Class<T> type) {
69 this.definition = definition;
70 this.relatedFormItem = relatedFormItem;
71 this.basePropertyName = definition.getName();
72 if (hasI18NSupport()) {
73 this.i18NPropertyName = this.basePropertyName;
74 }
75 setType(type);
76 }
77
78 @Override
79 public void writeToItem(T newValue) {
80 Property<T> p = getOrCreateProperty(type);
81 p.setValue(newValue);
82 }
83
84 @Override
85 public T readFromItem() {
86 String defaultValue = definition.getDefaultValue();
87 Property<T> p = getOrCreateProperty(type);
88 if (definition.isReadOnly()) {
89 p.setReadOnly(true);
90 }
91 return p.getValue();
92 }
93
94
95
96
97
98
99 protected void setType(Class<T> typeFromDefinition) {
100 if (typeFromDefinition.isAssignableFrom(UndefinedPropertyType.class)) {
101 String propertyName = definePropertyName();
102 Property<T> property = relatedFormItem.getItemProperty(propertyName);
103 if (property != null) {
104 this.type = (Class<T>) property.getType();
105 } else {
106 this.type = (Class<T>) String.class;
107 }
108 } else {
109 this.type = typeFromDefinition;
110 }
111 }
112
113
114
115
116
117
118
119
120 @SuppressWarnings("unchecked")
121 protected <T> Property<T> getOrCreateProperty(Class<T> type) {
122 String propertyName = definePropertyName();
123 Property<T> property = relatedFormItem.getItemProperty(propertyName);
124
125 if (property == null) {
126 property = new DefaultProperty<T>(type, null);
127 relatedFormItem.addItemProperty(propertyName, property);
128 }
129 return property;
130 }
131
132
133
134
135 protected String definePropertyName() {
136 String propertyName = this.basePropertyName;
137
138 if (hasI18NSupport()) {
139 propertyName = this.i18NPropertyName;
140 }
141 return propertyName;
142 }
143
144
145
146
147
148 @Override
149 public void setLocale(Locale locale) {
150 this.locale = locale;
151 }
152
153 @Override
154 public void setI18NPropertyName(String i18nPropertyName) {
155 this.i18NPropertyName = i18nPropertyName;
156 }
157
158 @Override
159 public Locale getLocale() {
160 return this.locale;
161 }
162
163 @Override
164 public String getBasePropertyName() {
165 return basePropertyName;
166 }
167
168 @Override
169 public boolean hasI18NSupport() {
170 return definition.isI18n();
171 }
172
173 @Override
174 public Class<T> getType() {
175 return type;
176 }
177
178 }