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.data.util.BeanItem;
|
14
|
import com.vaadin.server.UserError;
|
15
|
import com.vaadin.ui.Component;
|
16
|
import com.vaadin.ui.CssLayout;
|
17
|
|
18
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
19
|
import eu.etaxonomy.cdm.model.agent.Person;
|
20
|
import eu.etaxonomy.cdm.model.agent.Team;
|
21
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
22
|
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
|
23
|
import eu.etaxonomy.vaadin.component.CompositeCustomField;
|
24
|
import eu.etaxonomy.vaadin.component.ToManyRelatedEntitiesListSelect;
|
25
|
import eu.etaxonomy.vaadin.component.SwitchableTextField;
|
26
|
|
27
|
/**
|
28
|
* @author a.kohlbecker
|
29
|
* @since May 11, 2017
|
30
|
*
|
31
|
*/
|
32
|
public class TeamOrPersonField extends CompositeCustomField<TeamOrPersonBase<?>> {
|
33
|
|
34
|
private static final long serialVersionUID = 660806402243118112L;
|
35
|
|
36
|
private static final String PRIMARY_STYLE = "v-team-or-person-field";
|
37
|
|
38
|
private CssLayout root = new CssLayout();
|
39
|
private CssLayout compositeWrapper = new CssLayout();
|
40
|
|
41
|
// Fields for case when value is a Person
|
42
|
private PersonField personField = new PersonField();
|
43
|
|
44
|
// Fields for case when value is a Team
|
45
|
private SwitchableTextField titleField = new SwitchableTextField("Team (bibliographic)");
|
46
|
private SwitchableTextField nomenclaturalTitleField = new SwitchableTextField("Team (nomenclatural)");
|
47
|
private ToManyRelatedEntitiesListSelect<Person, PersonField> personsListEditor = new ToManyRelatedEntitiesListSelect<Person, PersonField>(Person.class, PersonField.class, "Teammembers");
|
48
|
|
49
|
private BeanFieldGroup<Team> fieldGroup = new BeanFieldGroup<>(Team.class);
|
50
|
|
51
|
public TeamOrPersonField(String caption){
|
52
|
|
53
|
setCaption(caption);
|
54
|
|
55
|
addStyledComponent(personField);
|
56
|
addStyledComponent(titleField);
|
57
|
addStyledComponent(nomenclaturalTitleField);
|
58
|
addStyledComponent(personsListEditor);
|
59
|
|
60
|
addSizedComponent(root);
|
61
|
addSizedComponent(compositeWrapper);
|
62
|
addSizedComponent(personField);
|
63
|
addSizedComponent(titleField);
|
64
|
addSizedComponent(nomenclaturalTitleField);
|
65
|
addSizedComponent(personsListEditor);
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* {@inheritDoc}
|
70
|
*/
|
71
|
@Override
|
72
|
protected Component initContent() {
|
73
|
root.setPrimaryStyleName(PRIMARY_STYLE);
|
74
|
compositeWrapper.setStyleName("margin-wrapper");
|
75
|
root.addComponent(compositeWrapper);
|
76
|
return root;
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* {@inheritDoc}
|
81
|
*/
|
82
|
@Override
|
83
|
public Class getType() {
|
84
|
return TeamOrPersonBase.class;
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* {@inheritDoc}
|
89
|
*/
|
90
|
@Override
|
91
|
protected void setInternalValue(TeamOrPersonBase<?> newValue) {
|
92
|
|
93
|
super.setInternalValue(newValue);
|
94
|
|
95
|
newValue = HibernateProxyHelper.deproxy(newValue);
|
96
|
|
97
|
if(newValue == null) {
|
98
|
return;
|
99
|
}
|
100
|
|
101
|
if(Person.class.isAssignableFrom(newValue.getClass())){
|
102
|
// value is a Person:
|
103
|
compositeWrapper.addComponent(personField);
|
104
|
|
105
|
personField.setValue((Person) newValue);
|
106
|
personField.registerParentFieldGroup(fieldGroup);
|
107
|
|
108
|
}
|
109
|
else if(Team.class.isAssignableFrom(newValue.getClass())){
|
110
|
// otherwise it a Team
|
111
|
|
112
|
compositeWrapper.addComponents(titleField, nomenclaturalTitleField, personsListEditor);
|
113
|
|
114
|
titleField.bindTo(fieldGroup, "titleCache", "protectedTitleCache");
|
115
|
nomenclaturalTitleField.bindTo(fieldGroup, "nomenclaturalTitle", "protectedNomenclaturalTitleCache");
|
116
|
fieldGroup.bind(personsListEditor, "teamMembers");
|
117
|
|
118
|
fieldGroup.setItemDataSource(new BeanItem<Team>((Team)newValue));
|
119
|
personsListEditor.registerParentFieldGroup(fieldGroup);
|
120
|
|
121
|
|
122
|
} else {
|
123
|
setComponentError(new UserError("TeamOrPersonField Error: Unsupported value type: " + newValue.getClass().getName()));
|
124
|
}
|
125
|
|
126
|
checkUserPermissions(newValue);
|
127
|
}
|
128
|
|
129
|
private void checkUserPermissions(TeamOrPersonBase<?> newValue) {
|
130
|
boolean userCanEdit = UserHelper.fromSession().userHasPermission(newValue, "DELETE", "UPDATE");
|
131
|
titleField.setEnabled(userCanEdit);
|
132
|
nomenclaturalTitleField.setEnabled(userCanEdit);
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* {@inheritDoc}
|
137
|
*/
|
138
|
@Override
|
139
|
protected void addDefaultStyles() {
|
140
|
// no default styles here
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* {@inheritDoc}
|
145
|
*/
|
146
|
@Override
|
147
|
public FieldGroup getFieldGroup() {
|
148
|
return fieldGroup;
|
149
|
}
|
150
|
|
151
|
public Component[] getCachFields(){
|
152
|
return new Component[]{titleField, nomenclaturalTitleField};
|
153
|
}
|
154
|
|
155
|
|
156
|
|
157
|
}
|