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 com.vaadin.data.fieldgroup.BeanFieldGroup;
|
12
|
import com.vaadin.data.fieldgroup.FieldGroup;
|
13
|
import com.vaadin.ui.Component;
|
14
|
import com.vaadin.ui.CssLayout;
|
15
|
import com.vaadin.ui.TextField;
|
16
|
import com.vaadin.ui.themes.ValoTheme;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.agent.Person;
|
19
|
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
|
20
|
import eu.etaxonomy.vaadin.component.CompositeCustomField;
|
21
|
import eu.etaxonomy.vaadin.component.SwitchButton;
|
22
|
|
23
|
/**
|
24
|
* @author a.kohlbecker
|
25
|
* @since May 11, 2017
|
26
|
*
|
27
|
*/
|
28
|
public class PersonField extends CompositeCustomField<Person> {
|
29
|
|
30
|
private static final long serialVersionUID = 8346575511284469356L;
|
31
|
|
32
|
private static final String PRIMARY_STYLE = "v-person-field";
|
33
|
|
34
|
private BeanFieldGroup<Person> fieldGroup = new BeanFieldGroup<>(Person.class);
|
35
|
|
36
|
enum Mode {
|
37
|
CACHE_MODE, DETAILS_MODE;
|
38
|
|
39
|
public String toCssClass() {
|
40
|
return name().toLowerCase().replace("_", "-");
|
41
|
}
|
42
|
}
|
43
|
|
44
|
private Mode currentMode = null;
|
45
|
|
46
|
private float baseWidth = 100 / 8;
|
47
|
|
48
|
private CssLayout root = new CssLayout();
|
49
|
private TextField cacheField = new TextField();
|
50
|
private CssLayout detailsContainer = new CssLayout();
|
51
|
private TextField firstNameField = new TextField();
|
52
|
private TextField lastNameField = new TextField();
|
53
|
private TextField prefixField = new TextField();
|
54
|
private TextField suffixField = new TextField();
|
55
|
private SwitchButton unlockSwitch = new SwitchButton();
|
56
|
|
57
|
/**
|
58
|
* @param caption
|
59
|
*/
|
60
|
public PersonField(String caption) {
|
61
|
|
62
|
this();
|
63
|
setCaption(caption);
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* @param caption
|
68
|
*/
|
69
|
public PersonField() {
|
70
|
|
71
|
root.setPrimaryStyleName(PRIMARY_STYLE);
|
72
|
|
73
|
addStyledComponent(cacheField);
|
74
|
addStyledComponent(firstNameField);
|
75
|
addStyledComponent(lastNameField);
|
76
|
addStyledComponent(prefixField);
|
77
|
addStyledComponent(suffixField);
|
78
|
addStyledComponent(unlockSwitch);
|
79
|
|
80
|
addSizedComponent(root);
|
81
|
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
*
|
86
|
*/
|
87
|
private void checkUserPermissions(Person newValue) {
|
88
|
boolean userCanEdit = UserHelper.fromSession().userHasPermission(newValue, "DELETE", "UPDATE");
|
89
|
cacheField.setEnabled(userCanEdit);
|
90
|
firstNameField.setEnabled(userCanEdit);
|
91
|
lastNameField.setEnabled(userCanEdit);
|
92
|
prefixField.setEnabled(userCanEdit);
|
93
|
suffixField.setEnabled(userCanEdit);
|
94
|
}
|
95
|
|
96
|
private void setMode(Mode mode){
|
97
|
if(mode.equals(currentMode)){
|
98
|
return;
|
99
|
}
|
100
|
if(currentMode != null){
|
101
|
String removeMode = currentMode.toCssClass();
|
102
|
root.removeStyleName(removeMode);
|
103
|
}
|
104
|
String newMode = mode.toCssClass();
|
105
|
root.addStyleName(newMode);
|
106
|
currentMode = mode;
|
107
|
}
|
108
|
|
109
|
|
110
|
/**
|
111
|
* {@inheritDoc}
|
112
|
*/
|
113
|
@Override
|
114
|
protected Component initContent() {
|
115
|
|
116
|
root.addComponent(cacheField);
|
117
|
root.addComponent(unlockSwitch);
|
118
|
|
119
|
cacheField.setWidth(100, Unit.PERCENTAGE);
|
120
|
|
121
|
prefixField.setWidth(baseWidth, Unit.PERCENTAGE);
|
122
|
prefixField.setInputPrompt("Prefix");
|
123
|
|
124
|
firstNameField.setWidth(baseWidth * 3, Unit.PERCENTAGE);
|
125
|
firstNameField.setInputPrompt("First Name");
|
126
|
|
127
|
lastNameField.setWidth(baseWidth * 3, Unit.PERCENTAGE);
|
128
|
lastNameField.setInputPrompt("Last Name");
|
129
|
|
130
|
suffixField.setWidth(baseWidth, Unit.PERCENTAGE);
|
131
|
suffixField.setInputPrompt("Suffix");
|
132
|
|
133
|
detailsContainer.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
|
134
|
detailsContainer.addComponent(prefixField);
|
135
|
detailsContainer.addComponent(firstNameField);
|
136
|
detailsContainer.addComponent(lastNameField);
|
137
|
detailsContainer.addComponent(suffixField);
|
138
|
root.addComponent(detailsContainer);
|
139
|
|
140
|
unlockSwitch.addValueChangeListener(e -> {
|
141
|
if(refreshMode()){
|
142
|
switch (currentMode) {
|
143
|
case CACHE_MODE:
|
144
|
cacheField.focus();
|
145
|
break;
|
146
|
case DETAILS_MODE:
|
147
|
firstNameField.focus();
|
148
|
break;
|
149
|
default:
|
150
|
break;
|
151
|
|
152
|
}
|
153
|
}
|
154
|
});
|
155
|
unlockSwitch.setValueSetLister(e -> {
|
156
|
refreshMode();
|
157
|
});
|
158
|
|
159
|
addDefaultStyles();
|
160
|
setMode(Mode.DETAILS_MODE);
|
161
|
|
162
|
fieldGroup.bind(cacheField, "titleCache");
|
163
|
fieldGroup.bind(prefixField, "prefix");
|
164
|
fieldGroup.bind(firstNameField, "firstname");
|
165
|
fieldGroup.bind(lastNameField, "lastname");
|
166
|
fieldGroup.bind(suffixField, "suffix");
|
167
|
fieldGroup.bind(unlockSwitch, "protectedTitleCache");
|
168
|
fieldGroup.setBuffered(false);
|
169
|
|
170
|
return root;
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
*
|
175
|
* @return true if the mode has changed
|
176
|
*/
|
177
|
protected boolean refreshMode() {
|
178
|
Mode lastMode = currentMode;
|
179
|
setMode(unlockSwitch.getValue() ? Mode.CACHE_MODE: Mode.DETAILS_MODE);
|
180
|
return !lastMode.equals(currentMode);
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* {@inheritDoc}
|
185
|
*/
|
186
|
@Override
|
187
|
public Class<? extends Person> getType() {
|
188
|
return Person.class;
|
189
|
}
|
190
|
|
191
|
@Override
|
192
|
public void setValue(Person person){
|
193
|
super.setValue(person);
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* {@inheritDoc}
|
198
|
*/
|
199
|
@Override
|
200
|
protected void setInternalValue(Person newValue) {
|
201
|
super.setInternalValue(newValue);
|
202
|
fieldGroup.setItemDataSource(newValue);
|
203
|
checkUserPermissions(newValue);
|
204
|
}
|
205
|
|
206
|
@Override
|
207
|
protected void addDefaultStyles(){
|
208
|
cacheField.addStyleName("cache-field");
|
209
|
detailsContainer.addStyleName("details-fields");
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* {@inheritDoc}
|
214
|
*/
|
215
|
@Override
|
216
|
public FieldGroup getFieldGroup() {
|
217
|
return fieldGroup;
|
218
|
}
|
219
|
}
|