1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.vaadin.component.common;
|
10
|
|
11
|
import java.util.EnumSet;
|
12
|
import java.util.List;
|
13
|
|
14
|
import org.vaadin.teemu.switchui.Switch;
|
15
|
import org.vaadin.viritin.fields.LazyComboBox;
|
16
|
|
17
|
import com.vaadin.data.Validator.InvalidValueException;
|
18
|
import com.vaadin.data.fieldgroup.BeanFieldGroup;
|
19
|
import com.vaadin.data.fieldgroup.FieldGroup;
|
20
|
import com.vaadin.ui.Button;
|
21
|
import com.vaadin.ui.Component;
|
22
|
import com.vaadin.ui.CssLayout;
|
23
|
import com.vaadin.ui.Field;
|
24
|
import com.vaadin.ui.TextField;
|
25
|
import com.vaadin.ui.themes.ValoTheme;
|
26
|
|
27
|
import eu.etaxonomy.cdm.model.agent.Person;
|
28
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
29
|
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
|
30
|
import eu.etaxonomy.vaadin.component.CompositeCustomField;
|
31
|
import eu.etaxonomy.vaadin.component.SwitchButton;
|
32
|
|
33
|
/**
|
34
|
* @author a.kohlbecker
|
35
|
* @since May 11, 2017
|
36
|
*
|
37
|
*/
|
38
|
public class PersonField extends CompositeCustomField<Person> {
|
39
|
|
40
|
private static final long serialVersionUID = 8346575511284469356L;
|
41
|
|
42
|
private static final String PRIMARY_STYLE = "v-person-field";
|
43
|
|
44
|
/**
|
45
|
* do not allow entities which are having only <code>null</code> values in all fields
|
46
|
* {@link #getValue()} would return <code>null</code> in this case.
|
47
|
*/
|
48
|
boolean allowNewEmptyEntity = true;
|
49
|
|
50
|
private LazyComboBox<Person> personSelect = new LazyComboBox<Person>(Person.class);
|
51
|
|
52
|
private Button personSelectConfirmButton = new Button("OK");
|
53
|
private Button newPersonButton = new Button("New");
|
54
|
|
55
|
private BeanFieldGroup<Person> fieldGroup = new BeanFieldGroup<>(Person.class);
|
56
|
|
57
|
enum Mode {
|
58
|
CACHE_MODE, DETAILS_MODE;
|
59
|
|
60
|
public String toCssClass() {
|
61
|
return name().toLowerCase().replace("_", "-");
|
62
|
}
|
63
|
}
|
64
|
|
65
|
private Mode currentMode = null;
|
66
|
|
67
|
private float baseWidth = 100 / 8;
|
68
|
|
69
|
private CssLayout root = new CssLayout();
|
70
|
private CssLayout selectOrNewContainer = new CssLayout();
|
71
|
|
72
|
private TextField cacheField = new TextField();
|
73
|
private CssLayout detailsContainer = new CssLayout();
|
74
|
private TextField firstNameField = new TextField();
|
75
|
private TextField lastNameField = new TextField();
|
76
|
private TextField prefixField = new TextField();
|
77
|
private TextField suffixField = new TextField();
|
78
|
private SwitchButton unlockSwitch = new SwitchButton();
|
79
|
|
80
|
private boolean onCommit = false;
|
81
|
|
82
|
|
83
|
/**
|
84
|
* @param caption
|
85
|
*/
|
86
|
public PersonField(String caption) {
|
87
|
|
88
|
this();
|
89
|
setCaption(caption);
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* @param caption
|
94
|
*/
|
95
|
public PersonField() {
|
96
|
|
97
|
root.setPrimaryStyleName(PRIMARY_STYLE);
|
98
|
|
99
|
// select existing or create new person
|
100
|
addStyledComponents(personSelect, personSelectConfirmButton, newPersonButton);
|
101
|
personSelect.addValueChangeListener(e -> {
|
102
|
if(personSelect.getValue() != null){
|
103
|
personSelectConfirmButton.setEnabled(true);
|
104
|
}
|
105
|
});
|
106
|
personSelectConfirmButton.setEnabled(false);
|
107
|
personSelectConfirmButton.addClickListener(e -> {
|
108
|
setValue(personSelect.getValue());
|
109
|
personSelect.clear();
|
110
|
});
|
111
|
selectOrNewContainer.addComponents(personSelect, personSelectConfirmButton, newPersonButton);
|
112
|
newPersonButton.addClickListener(e -> {
|
113
|
setValue(Person.NewInstance());
|
114
|
});
|
115
|
|
116
|
// edit person
|
117
|
addStyledComponent(cacheField);
|
118
|
addStyledComponent(firstNameField);
|
119
|
addStyledComponent(lastNameField);
|
120
|
addStyledComponent(prefixField);
|
121
|
addStyledComponent(suffixField);
|
122
|
addStyledComponent(unlockSwitch);
|
123
|
|
124
|
addSizedComponent(root);
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
*
|
129
|
*/
|
130
|
private void checkUserPermissions(Person newValue) {
|
131
|
boolean userCanEdit = UserHelper.fromSession().userHasPermission(newValue, "DELETE", "UPDATE");
|
132
|
boolean isUnsavedEnitity = newValue.getId() == 0;
|
133
|
setEnabled(isUnsavedEnitity || userCanEdit);
|
134
|
}
|
135
|
|
136
|
private void setMode(Mode mode){
|
137
|
if(mode.equals(currentMode)){
|
138
|
return;
|
139
|
}
|
140
|
if(currentMode != null){
|
141
|
String removeMode = currentMode.toCssClass();
|
142
|
root.removeStyleName(removeMode);
|
143
|
}
|
144
|
String newMode = mode.toCssClass();
|
145
|
root.addStyleName(newMode);
|
146
|
currentMode = mode;
|
147
|
}
|
148
|
|
149
|
|
150
|
/**
|
151
|
* {@inheritDoc}
|
152
|
*/
|
153
|
@Override
|
154
|
protected Component initContent() {
|
155
|
|
156
|
selectOrNewContainer.setWidth(100, Unit.PERCENTAGE);
|
157
|
personSelect.setWidthUndefined();
|
158
|
|
159
|
root.addComponent(cacheField);
|
160
|
root.addComponent(unlockSwitch);
|
161
|
root.addComponent(selectOrNewContainer);
|
162
|
|
163
|
cacheField.setWidth(100, Unit.PERCENTAGE);
|
164
|
|
165
|
prefixField.setWidth(baseWidth, Unit.PERCENTAGE);
|
166
|
prefixField.setInputPrompt("Prefix");
|
167
|
|
168
|
firstNameField.setWidth(baseWidth * 3, Unit.PERCENTAGE);
|
169
|
firstNameField.setInputPrompt("First Name");
|
170
|
|
171
|
lastNameField.setWidth(baseWidth * 3, Unit.PERCENTAGE);
|
172
|
lastNameField.setInputPrompt("Last Name");
|
173
|
|
174
|
suffixField.setWidth(baseWidth, Unit.PERCENTAGE);
|
175
|
suffixField.setInputPrompt("Suffix");
|
176
|
|
177
|
detailsContainer.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
|
178
|
detailsContainer.addComponent(prefixField);
|
179
|
detailsContainer.addComponent(firstNameField);
|
180
|
detailsContainer.addComponent(lastNameField);
|
181
|
detailsContainer.addComponent(suffixField);
|
182
|
root.addComponent(detailsContainer);
|
183
|
|
184
|
unlockSwitch.addValueChangeListener(e -> {
|
185
|
if(refreshMode()){
|
186
|
switch (currentMode) {
|
187
|
case CACHE_MODE:
|
188
|
cacheField.focus();
|
189
|
break;
|
190
|
case DETAILS_MODE:
|
191
|
firstNameField.focus();
|
192
|
break;
|
193
|
default:
|
194
|
break;
|
195
|
|
196
|
}
|
197
|
}
|
198
|
});
|
199
|
unlockSwitch.setValueSetLister(e -> {
|
200
|
refreshMode();
|
201
|
});
|
202
|
|
203
|
addDefaultStyles();
|
204
|
setMode(Mode.DETAILS_MODE);
|
205
|
|
206
|
fieldGroup.bind(cacheField, "titleCache");
|
207
|
fieldGroup.bind(prefixField, "prefix");
|
208
|
fieldGroup.bind(firstNameField, "firstname");
|
209
|
fieldGroup.bind(lastNameField, "lastname");
|
210
|
fieldGroup.bind(suffixField, "suffix");
|
211
|
fieldGroup.bind(unlockSwitch, "protectedTitleCache");
|
212
|
fieldGroup.setBuffered(false);
|
213
|
|
214
|
updateVisibilities(getValue());
|
215
|
|
216
|
return root;
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
*
|
221
|
* @return true if the mode has changed
|
222
|
*/
|
223
|
protected boolean refreshMode() {
|
224
|
Mode lastMode = currentMode;
|
225
|
setMode(unlockSwitch.getValue() ? Mode.CACHE_MODE: Mode.DETAILS_MODE);
|
226
|
return !lastMode.equals(currentMode);
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* {@inheritDoc}
|
231
|
*/
|
232
|
@Override
|
233
|
public Class<? extends Person> getType() {
|
234
|
return Person.class;
|
235
|
}
|
236
|
|
237
|
@Override
|
238
|
public void setValue(Person person){
|
239
|
// if(person == null){
|
240
|
// person = Person.NewInstance();
|
241
|
// }
|
242
|
super.setValue(person);
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* {@inheritDoc}
|
247
|
*/
|
248
|
@Override
|
249
|
protected void setInternalValue(Person newValue) {
|
250
|
|
251
|
super.setInternalValue(newValue);
|
252
|
fieldGroup.setItemDataSource(newValue);
|
253
|
checkUserPermissions(newValue);
|
254
|
updateVisibilities(newValue);
|
255
|
}
|
256
|
|
257
|
/**
|
258
|
*
|
259
|
*/
|
260
|
private void updateVisibilities(Person person) {
|
261
|
|
262
|
selectOrNewContainer.setVisible(person == null);
|
263
|
|
264
|
detailsContainer.setVisible(person != null);
|
265
|
unlockSwitch.setVisible(person != null);
|
266
|
cacheField.setVisible(person != null);
|
267
|
|
268
|
}
|
269
|
|
270
|
@Override
|
271
|
protected void addDefaultStyles(){
|
272
|
cacheField.addStyleName("cache-field");
|
273
|
detailsContainer.addStyleName("details-fields");
|
274
|
unlockSwitch.addStyleName(Switch.DOM_STYLE);
|
275
|
}
|
276
|
|
277
|
/**
|
278
|
* {@inheritDoc}
|
279
|
*/
|
280
|
@Override
|
281
|
public FieldGroup getFieldGroup() {
|
282
|
return fieldGroup;
|
283
|
}
|
284
|
|
285
|
|
286
|
/**
|
287
|
* {@inheritDoc}
|
288
|
*/
|
289
|
@Override
|
290
|
protected List<Field> nullValueCheckIgnoreFields() {
|
291
|
|
292
|
List<Field>ignoreFields = super.nullValueCheckIgnoreFields();
|
293
|
ignoreFields.add(unlockSwitch);
|
294
|
|
295
|
if(unlockSwitch.getValue().booleanValue() == false){
|
296
|
if(getValue().getId() == 0){
|
297
|
// only it the entity is unsaved!
|
298
|
ignoreFields.add(cacheField);
|
299
|
cacheField.setValue(null);
|
300
|
}
|
301
|
}
|
302
|
return ignoreFields;
|
303
|
}
|
304
|
|
305
|
/**
|
306
|
* {@inheritDoc}
|
307
|
*/
|
308
|
@Override
|
309
|
public void commit() throws SourceException, InvalidValueException {
|
310
|
|
311
|
super.commit();
|
312
|
|
313
|
Person bean = getValue();
|
314
|
if(bean != null){
|
315
|
boolean isUnsaved = bean.getId() == 0;
|
316
|
if(isUnsaved && !(hasNullContent() && !allowNewEmptyEntity)){
|
317
|
UserHelper.fromSession().createAuthorityForCurrentUser(bean, EnumSet.of(CRUD.UPDATE, CRUD.DELETE), null);
|
318
|
}
|
319
|
}
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
* {@inheritDoc}
|
324
|
*
|
325
|
* @return returns <code>null</code> in case the edited entity is unsaved and if
|
326
|
* it only has null values.
|
327
|
*/
|
328
|
@Override
|
329
|
public Person getValue() {
|
330
|
Person bean = super.getValue();
|
331
|
if(bean == null){
|
332
|
return null;
|
333
|
}
|
334
|
// boolean isUnsaved = bean.getId() == 0;
|
335
|
// if(isUnsaved && hasNullContent() && !allowNewEmptyEntity) {
|
336
|
// return null;
|
337
|
// }
|
338
|
return bean;
|
339
|
}
|
340
|
|
341
|
/**
|
342
|
* @return the personSelect
|
343
|
*/
|
344
|
public LazyComboBox<Person> getPersonSelect() {
|
345
|
return personSelect;
|
346
|
}
|
347
|
|
348
|
/**
|
349
|
* @param personSelect the personSelect to set
|
350
|
*/
|
351
|
public void setPersonSelect(LazyComboBox<Person> personSelect) {
|
352
|
this.personSelect = personSelect;
|
353
|
}
|
354
|
|
355
|
/**
|
356
|
* @return the allowNewEmptyEntity
|
357
|
*/
|
358
|
public boolean isAllowNewEmptyEntity() {
|
359
|
return allowNewEmptyEntity;
|
360
|
}
|
361
|
|
362
|
/**
|
363
|
* @param allowNewEmptyEntity the allowNewEmptyEntity to set
|
364
|
*/
|
365
|
public void setAllowNewEmptyEntity(boolean allowNewEmptyEntity) {
|
366
|
this.allowNewEmptyEntity = allowNewEmptyEntity;
|
367
|
}
|
368
|
|
369
|
|
370
|
|
371
|
}
|