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 com.vaadin.data.Container;
37 import com.vaadin.data.Property;
38 import com.vaadin.data.Property.ValueChangeListener;
39 import com.vaadin.event.FieldEvents.BlurListener;
40 import com.vaadin.ui.AbstractTextField;
41 import com.vaadin.ui.Component;
42 import com.vaadin.ui.DefaultFieldFactory;
43 import com.vaadin.ui.Field;
44 import com.vaadin.ui.TableFieldFactory;
45 import com.vaadin.ui.TextField;
46
47
48
49
50
51
52
53
54 public class InplaceEditingFieldFactory implements TableFieldFactory {
55
56 private Object editingItemId;
57 private Object editingPropertyId;
58
59 private Field<?> field;
60 private BlurListener fieldBlurListener;
61
62
63
64
65 public Object getEditingItemId() {
66 return editingItemId;
67 }
68
69
70
71
72 public Object getEditingPropertyId() {
73 return editingPropertyId;
74 }
75
76 public Field<?> getField() {
77 return field;
78 }
79
80
81
82
83 public void setEditing(Object editingItemId, Object editingPropertyId) {
84 this.editingItemId = editingItemId;
85 this.editingPropertyId = editingPropertyId;
86 if (editingItemId == null || editingPropertyId == null) {
87 field = null;
88 }
89 }
90
91
92
93
94 public void setFieldBlurListener(BlurListener fieldBlurListener) {
95 this.fieldBlurListener = fieldBlurListener;
96 }
97
98 @Override
99 public Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) {
100
101 if (itemId.equals(editingItemId) && propertyId.equals(editingPropertyId)) {
102 Field<?> field = createFieldAndRegister(container, itemId, propertyId);
103
104
105 this.field = field;
106 return field;
107 }
108 return null;
109 }
110
111
112
113
114
115 private Field<?> createFieldAndRegister(Container container, Object itemId, Object propertyId) {
116
117 Property<?> containerProperty = container.getContainerProperty(itemId, propertyId);
118
119 if (containerProperty == null) {
120 return null;
121 }
122
123 Class<?> type = containerProperty.getType();
124 Field<?> field = createFieldByPropertyType(type);
125 if (field != null) {
126 field.setCaption(DefaultFieldFactory.createCaptionByPropertyId(propertyId));
127 field.setSizeFull();
128 }
129
130
131 if (field instanceof AbstractTextField) {
132 final AbstractTextField tf = (AbstractTextField) field;
133 tf.addBlurListener(fieldBlurListener);
134 tf.focus();
135
136 tf.addValueChangeListener(new ValueChangeListener() {
137
138 @Override
139 public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
140 final Object text = event.getProperty().getValue();
141 if (text instanceof String) {
142 tf.selectAll();
143 }
144 tf.removeValueChangeListener(this);
145 }
146 });
147 }
148 return field;
149 }
150
151 private Field<?> createFieldByPropertyType(Class<?> type) {
152 if (type == null) {
153 return null;
154 }
155 Field<?> field = new TextField();
156 return field;
157 }
158
159 }