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.field.factory;
35
36 import info.magnolia.objectfactory.ComponentProvider;
37 import info.magnolia.ui.field.FieldDefinition;
38
39 import javax.inject.Inject;
40
41 import org.apache.commons.lang3.StringUtils;
42
43 import com.vaadin.data.HasValue;
44 import com.vaadin.server.Sizeable;
45 import com.vaadin.ui.Component;
46
47
48
49
50
51
52
53
54
55
56 public abstract class AbstractFieldFactory<T, D extends FieldDefinition<T>> implements FieldFactory<T> {
57
58 protected final D definition;
59 protected final ComponentProvider componentProvider;
60
61 @Inject
62 public AbstractFieldFactory(D definition, ComponentProvider componentProvider) {
63 this.definition = definition;
64 this.componentProvider = componentProvider;
65 }
66
67 @Override
68 public HasValue<T> createField() {
69
70 Component field = createFieldComponent();
71
72 if (StringUtils.isNotBlank(getDefinition().getStyleName())) {
73 field.addStyleName(getDefinition().getStyleName());
74 }
75
76 field.setWidth(100, Sizeable.Unit.PERCENTAGE);
77 field.setCaption(getDefinition().getLabel());
78
79 if (!(field instanceof HasValue)) {
80 throw new IllegalStateException("[" + getClass().getSimpleName() + "] produced something other than a HasValue instance");
81 }
82
83 HasValue<T> hasValue = (HasValue<T>) field;
84 hasValue.setRequiredIndicatorVisible(getDefinition().isRequired());
85 hasValue.setReadOnly(getDefinition().isReadOnly());
86
87 return hasValue;
88 }
89
90 protected abstract Component createFieldComponent();
91
92 protected D getDefinition() {
93 return definition;
94 }
95
96
97
98
99
100
101
102 protected boolean isMessageKey(String key) {
103 return !StringUtils.endsWith(key, ".") && StringUtils.contains(key, ".") && !StringUtils.contains(key, " ");
104 }
105 }