Project

General

Profile

Download (10.3 KB) Statistics
| Branch: | Tag: | Revision:
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.component.TextFieldNFix;
30
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
31
import eu.etaxonomy.vaadin.component.CompositeCustomField;
32
import eu.etaxonomy.vaadin.component.SwitchButton;
33

    
34
/**
35
 * @author a.kohlbecker
36
 * @since May 11, 2017
37
 *
38
 */
39
public class PersonField extends CompositeCustomField<Person> {
40

    
41
    private static final long serialVersionUID = 8346575511284469356L;
42

    
43
    private static final String PRIMARY_STYLE = "v-person-field";
44

    
45
    /**
46
     * do not allow entities which are having only <code>null</code> values in all fields
47
     * {@link #getValue()} would return <code>null</code> in this case.
48
     */
49
    boolean allowNewEmptyEntity = true;
50

    
51
    private LazyComboBox<Person> personSelect = new LazyComboBox<Person>(Person.class);
52

    
53
    private Button personSelectConfirmButton = new Button("OK");
54
    private Button newPersonButton = new Button("New");
55

    
56
    private BeanFieldGroup<Person> fieldGroup = new BeanFieldGroup<>(Person.class);
57

    
58
    enum Mode {
59
        CACHE_MODE, DETAILS_MODE;
60

    
61
        public String toCssClass() {
62
            return name().toLowerCase().replace("_", "-");
63
        }
64
    }
65

    
66
    private Mode currentMode = null;
67

    
68
    private float baseWidth = 100 / 8;
69

    
70
    private CssLayout root = new CssLayout();
71
    private CssLayout selectOrNewContainer = new CssLayout();
72

    
73
    private TextField cacheField = new TextFieldNFix();
74
    private CssLayout detailsContainer = new CssLayout();
75
    private TextField firstNameField = new TextFieldNFix();
76
    private TextField lastNameField = new TextFieldNFix();
77
    private TextField prefixField = new TextFieldNFix();
78
    private TextField suffixField = new TextFieldNFix();
79
    private SwitchButton unlockSwitch = new SwitchButton();
80

    
81
    private boolean onCommit = false;
82

    
83

    
84
    /**
85
     * @param caption
86
     */
87
    public PersonField(String caption) {
88

    
89
        this();
90
        setCaption(caption);
91
    }
92

    
93
    /**
94
     * @param caption
95
     */
96
    public PersonField() {
97

    
98
        root.setPrimaryStyleName(PRIMARY_STYLE);
99

    
100
        // select existing or create new person
101
        addStyledComponents(personSelect, personSelectConfirmButton, newPersonButton);
102
        personSelect.addValueChangeListener(e -> {
103
            if(personSelect.getValue() != null){
104
                personSelectConfirmButton.setEnabled(true);
105
            }
106
        });
107
        personSelectConfirmButton.setEnabled(false);
108
        personSelectConfirmButton.addClickListener(e -> {
109
            setValue(personSelect.getValue());
110
            personSelect.clear();
111
        });
112
        selectOrNewContainer.addComponents(personSelect, personSelectConfirmButton, newPersonButton);
113
        newPersonButton.addClickListener(e -> {
114
            setValue(Person.NewInstance());
115
        });
116

    
117
        // edit person
118
        addStyledComponent(cacheField);
119
        addStyledComponent(firstNameField);
120
        addStyledComponent(lastNameField);
121
        addStyledComponent(prefixField);
122
        addStyledComponent(suffixField);
123
        addStyledComponent(unlockSwitch);
124

    
125
        addSizedComponent(root);
126
    }
127

    
128
    /**
129
     *
130
     */
131
    private void checkUserPermissions(Person newValue) {
132
        boolean userCanEdit = UserHelper.fromSession().userHasPermission(newValue, "DELETE", "UPDATE");
133
        boolean isUnsavedEnitity = newValue.getId() == 0;
134
        setEnabled(isUnsavedEnitity || userCanEdit);
135
    }
136

    
137
    private void setMode(Mode mode){
138
        if(mode.equals(currentMode)){
139
            return;
140
        }
141
        if(currentMode != null){
142
            String removeMode = currentMode.toCssClass();
143
            root.removeStyleName(removeMode);
144
        }
145
        String newMode = mode.toCssClass();
146
        root.addStyleName(newMode);
147
        currentMode = mode;
148
    }
149

    
150

    
151
    /**
152
     * {@inheritDoc}
153
     */
154
    @Override
155
    protected Component initContent() {
156

    
157
        selectOrNewContainer.setWidth(100, Unit.PERCENTAGE);
158
        personSelect.setWidthUndefined();
159

    
160
        root.addComponent(cacheField);
161
        root.addComponent(unlockSwitch);
162
        root.addComponent(selectOrNewContainer);
163

    
164
        cacheField.setWidth(100, Unit.PERCENTAGE);
165

    
166
        prefixField.setWidth(baseWidth, Unit.PERCENTAGE);
167
        prefixField.setInputPrompt("Prefix");
168

    
169
        firstNameField.setWidth(baseWidth * 3, Unit.PERCENTAGE);
170
        firstNameField.setInputPrompt("First Name");
171

    
172
        lastNameField.setWidth(baseWidth * 3, Unit.PERCENTAGE);
173
        lastNameField.setInputPrompt("Last Name");
174

    
175
        suffixField.setWidth(baseWidth, Unit.PERCENTAGE);
176
        suffixField.setInputPrompt("Suffix");
177

    
178
        detailsContainer.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
179
        detailsContainer.addComponent(prefixField);
180
        detailsContainer.addComponent(firstNameField);
181
        detailsContainer.addComponent(lastNameField);
182
        detailsContainer.addComponent(suffixField);
183
        root.addComponent(detailsContainer);
184

    
185
        unlockSwitch.addValueChangeListener(e -> {
186
            if(refreshMode()){
187
                switch (currentMode) {
188
                    case CACHE_MODE:
189
                        cacheField.focus();
190
                        break;
191
                    case DETAILS_MODE:
192
                        firstNameField.focus();
193
                        break;
194
                    default:
195
                        break;
196

    
197
                }
198
            }
199
        });
200
        unlockSwitch.setValueSetLister(e -> {
201
            refreshMode();
202
        });
203

    
204
        addDefaultStyles();
205
        setMode(Mode.DETAILS_MODE);
206

    
207
        fieldGroup.bind(cacheField, "titleCache");
208
        fieldGroup.bind(prefixField, "prefix");
209
        fieldGroup.bind(firstNameField, "firstname");
210
        fieldGroup.bind(lastNameField, "lastname");
211
        fieldGroup.bind(suffixField, "suffix");
212
        fieldGroup.bind(unlockSwitch, "protectedTitleCache");
213
        fieldGroup.setBuffered(false);
214

    
215
        updateVisibilities(getValue());
216

    
217
        return root;
218
    }
219

    
220
    /**
221
     *
222
     * @return true if the mode has changed
223
     */
224
    protected boolean refreshMode() {
225
        Mode lastMode = currentMode;
226
        setMode(unlockSwitch.getValue() ? Mode.CACHE_MODE: Mode.DETAILS_MODE);
227
        return !lastMode.equals(currentMode);
228
    }
229

    
230
    /**
231
     * {@inheritDoc}
232
     */
233
    @Override
234
    public Class<? extends Person> getType() {
235
        return Person.class;
236
    }
237

    
238
    @Override
239
    public void setValue(Person person){
240
//        if(person == null){
241
//            person = Person.NewInstance();
242
//        }
243
        super.setValue(person);
244
    }
245

    
246
    /**
247
     * {@inheritDoc}
248
     */
249
    @Override
250
    protected void setInternalValue(Person newValue) {
251

    
252
        super.setInternalValue(newValue);
253
        fieldGroup.setItemDataSource(newValue);
254
        checkUserPermissions(newValue);
255
        updateVisibilities(newValue);
256
    }
257

    
258
    /**
259
     *
260
     */
261
    private void updateVisibilities(Person person) {
262

    
263
        selectOrNewContainer.setVisible(person == null);
264

    
265
        detailsContainer.setVisible(person != null);
266
        unlockSwitch.setVisible(person != null);
267
        cacheField.setVisible(person != null);
268

    
269
    }
270

    
271
    @Override
272
    protected void addDefaultStyles(){
273
        cacheField.addStyleName("cache-field");
274
        detailsContainer.addStyleName("details-fields");
275
        unlockSwitch.addStyleName(Switch.DOM_STYLE);
276
    }
277

    
278
    /**
279
     * {@inheritDoc}
280
     */
281
    @Override
282
    public FieldGroup getFieldGroup() {
283
        return fieldGroup;
284
    }
285

    
286

    
287
    /**
288
     * {@inheritDoc}
289
     */
290
    @Override
291
    protected List<Field> nullValueCheckIgnoreFields() {
292

    
293
        List<Field>ignoreFields = super.nullValueCheckIgnoreFields();
294
        ignoreFields.add(unlockSwitch);
295

    
296
        if(unlockSwitch.getValue().booleanValue() == false){
297
            if(getValue().getId() == 0){
298
                // only it the entity is unsaved!
299
                ignoreFields.add(cacheField);
300
                cacheField.setValue(null);
301
            }
302
        }
303
        return ignoreFields;
304
    }
305

    
306
    /**
307
     * {@inheritDoc}
308
     */
309
    @Override
310
    public void commit() throws SourceException, InvalidValueException {
311

    
312
        super.commit();
313

    
314
        Person bean =  getValue();
315
        if(bean != null){
316
            boolean isUnsaved = bean.getId() == 0;
317
            if(isUnsaved && !(hasNullContent() && !allowNewEmptyEntity)){
318
                UserHelper.fromSession().createAuthorityForCurrentUser(bean, EnumSet.of(CRUD.UPDATE, CRUD.DELETE), null);
319
            }
320
        }
321
    }
322

    
323
    /**
324
     * {@inheritDoc}
325
     *
326
     * @return returns <code>null</code> in case the edited entity is unsaved and if
327
     * it only has null values.
328
     */
329
    @Override
330
    public Person getValue() {
331
        Person bean = super.getValue();
332
        if(bean == null){
333
            return null;
334
        }
335
       // boolean isUnsaved = bean.getId() == 0;
336
//        if(isUnsaved && hasNullContent() && !allowNewEmptyEntity) {
337
//            return null;
338
//        }
339
        return bean;
340
    }
341

    
342
    /**
343
     * @return the personSelect
344
     */
345
    public LazyComboBox<Person> getPersonSelect() {
346
        return personSelect;
347
    }
348

    
349
    /**
350
     * @param personSelect the personSelect to set
351
     */
352
    public void setPersonSelect(LazyComboBox<Person> personSelect) {
353
        this.personSelect = personSelect;
354
    }
355

    
356
    /**
357
     * @return the allowNewEmptyEntity
358
     */
359
    public boolean isAllowNewEmptyEntity() {
360
        return allowNewEmptyEntity;
361
    }
362

    
363
    /**
364
     * @param allowNewEmptyEntity the allowNewEmptyEntity to set
365
     */
366
    public void setAllowNewEmptyEntity(boolean allowNewEmptyEntity) {
367
        this.allowNewEmptyEntity = allowNewEmptyEntity;
368
    }
369

    
370

    
371

    
372
}
(3-3/5)