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.event;
|
10
|
|
11
|
import com.vaadin.data.Property.ValueChangeEvent;
|
12
|
import com.vaadin.ui.Field;
|
13
|
|
14
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
15
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
16
|
import eu.etaxonomy.cdm.service.UserHelperAccess;
|
17
|
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
|
18
|
|
19
|
/**
|
20
|
* @author a.kohlbecker
|
21
|
* @since 19.10.2017
|
22
|
*
|
23
|
*/
|
24
|
public class ToOneRelatedEntityButtonUpdater<CDM extends CdmBase> implements NestedButtonStateUpdater<CDM> {
|
25
|
|
26
|
private static final long serialVersionUID = 4472031263172275012L;
|
27
|
|
28
|
ToOneRelatedEntityField<CDM> toOneRelatedEntityField;
|
29
|
|
30
|
private Class<? extends CDM> type;
|
31
|
|
32
|
|
33
|
public ToOneRelatedEntityButtonUpdater(ToOneRelatedEntityField<CDM> toOneRelatedEntityField){
|
34
|
this.toOneRelatedEntityField = toOneRelatedEntityField;
|
35
|
this.type = toOneRelatedEntityField.getType();
|
36
|
updateButtons(((Field<CDM>)toOneRelatedEntityField).getValue());
|
37
|
toOneRelatedEntityField.setEditButtonEnabled(false);
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* {@inheritDoc}
|
42
|
*/
|
43
|
@Override
|
44
|
public void valueChange(ValueChangeEvent event) {
|
45
|
|
46
|
CDM value = (CDM)event.getProperty().getValue();
|
47
|
updateButtons(value);
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* @param value
|
52
|
*/
|
53
|
@Override
|
54
|
public void updateButtons(CDM value) {
|
55
|
|
56
|
boolean userIsAllowedToUpdate = value != null && UserHelperAccess.userHelper().userHasPermission(value, CRUD.UPDATE);
|
57
|
boolean userIsAllowedToCreate = UserHelperAccess.userHelper().userHasPermission(type, CRUD.CREATE);
|
58
|
boolean isReadOnlyField = ((Field)toOneRelatedEntityField).isReadOnly();
|
59
|
|
60
|
toOneRelatedEntityField.setAddButtonEnabled(!isReadOnlyField && userIsAllowedToCreate);
|
61
|
toOneRelatedEntityField.setEditButtonEnabled(!isReadOnlyField && userIsAllowedToUpdate);
|
62
|
|
63
|
}
|
64
|
|
65
|
}
|