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.workbench.tree;
35
36 import java.util.Locale;
37
38 import com.vaadin.data.Container;
39 import com.vaadin.data.Property;
40 import com.vaadin.data.Property.ValueChangeListener;
41 import com.vaadin.data.util.converter.AbstractStringToNumberConverter;
42 import com.vaadin.event.FieldEvents.BlurListener;
43 import com.vaadin.ui.AbstractTextField;
44 import com.vaadin.ui.Component;
45 import com.vaadin.ui.DefaultFieldFactory;
46 import com.vaadin.ui.Field;
47 import com.vaadin.ui.TableFieldFactory;
48 import com.vaadin.ui.TextField;
49
50
51
52
53
54
55
56
57 public class InplaceEditingFieldFactory implements TableFieldFactory {
58
59 private Object editingItemId;
60 private Object editingPropertyId;
61
62 private Field<?> field;
63 private BlurListener fieldBlurListener;
64
65
66
67
68 public Object getEditingItemId() {
69 return editingItemId;
70 }
71
72
73
74
75 public Object getEditingPropertyId() {
76 return editingPropertyId;
77 }
78
79 public Field<?> getField() {
80 return field;
81 }
82
83
84
85
86 public void setEditing(Object editingItemId, Object editingPropertyId) {
87 this.editingItemId = editingItemId;
88 this.editingPropertyId = editingPropertyId;
89 if (editingItemId == null || editingPropertyId == null) {
90 field = null;
91 }
92 }
93
94
95
96
97 public void setFieldBlurListener(BlurListener fieldBlurListener) {
98 this.fieldBlurListener = fieldBlurListener;
99 }
100
101 @Override
102 public Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) {
103
104 if (itemId.equals(editingItemId) && propertyId.equals(editingPropertyId)) {
105 Field<?> field = createFieldAndRegister(container, itemId, propertyId);
106
107
108 this.field = field;
109 return field;
110 }
111 return null;
112 }
113
114
115
116
117
118 private Field<?> createFieldAndRegister(Container container, Object itemId, Object propertyId) {
119
120 Property<?> containerProperty = container.getContainerProperty(itemId, propertyId);
121
122 if (containerProperty == null) {
123 return null;
124 }
125
126 Class<?> type = containerProperty.getType();
127 Field<?> field = createFieldByPropertyType(type);
128 if (field != null) {
129 field.setCaption(DefaultFieldFactory.createCaptionByPropertyId(propertyId));
130 field.setSizeFull();
131 }
132
133
134 if (field instanceof AbstractTextField) {
135 final AbstractTextField tf = (AbstractTextField) field;
136 tf.addBlurListener(fieldBlurListener);
137 tf.focus();
138
139 tf.addValueChangeListener(new ValueChangeListener() {
140
141 @Override
142 public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
143 final Object text = event.getProperty().getValue();
144 if (text instanceof String) {
145 tf.selectAll();
146 }
147 tf.removeValueChangeListener(this);
148 }
149 });
150 }
151 return field;
152 }
153
154 private Field<?> createFieldByPropertyType(Class<?> type) {
155 if (type == null) {
156 return null;
157 }
158 Field<?> field = new TextField();
159
160 if (Long.class.equals(type)) {
161 ((AbstractTextField) field).setConverter(new StringToLongConverter());
162 }
163 return field;
164 }
165
166
167
168
169
170
171
172
173 @Deprecated
174 static class StringToLongConverter extends AbstractStringToNumberConverter<Long> {
175
176 @Override
177 public Long convertToModel(String value, Class<? extends Long> targetType, Locale locale) throws ConversionException {
178 Number n = convertToNumber(value, targetType, locale);
179 return n == null ? null : n.longValue();
180 }
181
182 @Override
183 public Class<Long> getModelType() {
184 return Long.class;
185 }
186 }
187
188 }