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.registration;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.EnumSet;
|
13
|
import java.util.List;
|
14
|
|
15
|
import com.vaadin.server.ExternalResource;
|
16
|
import com.vaadin.server.FontAwesome;
|
17
|
import com.vaadin.ui.Button;
|
18
|
import com.vaadin.ui.Label;
|
19
|
import com.vaadin.ui.Link;
|
20
|
import com.vaadin.ui.themes.ValoTheme;
|
21
|
|
22
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
23
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
24
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
25
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
26
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
27
|
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
|
28
|
import eu.etaxonomy.cdm.vaadin.security.PermissionDebugUtils;
|
29
|
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
|
30
|
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet;
|
31
|
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
|
32
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
|
33
|
import eu.etaxonomy.vaadin.component.CompositeStyledComponent;
|
34
|
|
35
|
/**
|
36
|
* @author a.kohlbecker
|
37
|
* @since May 19, 2017
|
38
|
*
|
39
|
*/
|
40
|
public class RegistrationItemEditButtonGroup extends CompositeStyledComponent {
|
41
|
|
42
|
|
43
|
/**
|
44
|
*
|
45
|
*/
|
46
|
private static final String DEFAULT_BUTTON_STYLES = "";
|
47
|
|
48
|
private static final long serialVersionUID = -5059163772392864050L;
|
49
|
|
50
|
public static final String STYLE_NAMES = "edit-button-group " + ValoTheme.LAYOUT_COMPONENT_GROUP;
|
51
|
|
52
|
private IdButton<TaxonName> nameIdButton = null;
|
53
|
|
54
|
private List<TypeDesignationWorkingSetButton> typeDesignationButtons = new ArrayList<>();
|
55
|
|
56
|
private List<Label> labels = new ArrayList<>();
|
57
|
|
58
|
private Button addTypeDesignationButton;
|
59
|
|
60
|
private Label nameLabel = null;
|
61
|
|
62
|
private Link identifierLink;
|
63
|
|
64
|
public RegistrationItemEditButtonGroup(RegistrationDTO regDto) {
|
65
|
|
66
|
boolean isRegistrationLocked = EnumSet.of(
|
67
|
RegistrationStatus.PUBLISHED, RegistrationStatus.REJECTED)
|
68
|
.contains(regDto.getStatus());
|
69
|
|
70
|
setWidth(100, Unit.PERCENTAGE);
|
71
|
|
72
|
if(regDto.getName() != null){
|
73
|
Button nameButton = new Button("Name:");
|
74
|
nameButton.setDescription("Edit the Name");
|
75
|
nameIdButton = new IdButton<TaxonName>(TaxonName.class, regDto.getName().getId(), nameButton);
|
76
|
Label nameLabel = new Label(regDto.getName().getLabel());
|
77
|
nameLabel.setWidthUndefined();
|
78
|
boolean userHasPermission = UserHelper.fromSession().userHasPermission(regDto.registration().getName(), CRUD.UPDATE);
|
79
|
nameButton.setEnabled(!isRegistrationLocked && userHasPermission);
|
80
|
|
81
|
addComponent(nameIdButton.getButton());
|
82
|
PermissionDebugUtils.fromSession().addGainPerEntityPermissionButton(this, TaxonName.class, regDto.getName().getId(),
|
83
|
EnumSet.of(CRUD.UPDATE, CRUD.DELETE), null);
|
84
|
addComponent(nameLabel);
|
85
|
} else {
|
86
|
// no name in the registration! we only show the typified name as label
|
87
|
if(regDto.getTypifiedName() != null){
|
88
|
nameLabel = new Label(regDto.getTypifiedName().getLabel());
|
89
|
addComponent(nameLabel);
|
90
|
}
|
91
|
}
|
92
|
if(regDto.getOrderdTypeDesignationWorkingSets() != null){
|
93
|
for(TypedEntityReference<TypeDesignationBase<?>> baseEntityRef : regDto.getOrderdTypeDesignationWorkingSets().keySet()) {
|
94
|
TypeDesignationWorkingSet typeDesignationWorkingSet = regDto.getOrderdTypeDesignationWorkingSets().get(baseEntityRef);
|
95
|
String buttonLabel = SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType()) ? "Type": "NameType";
|
96
|
Button tdButton = new Button(buttonLabel + ":");
|
97
|
tdButton.setDescription("Edit the type designation working set");
|
98
|
tdButton.setEnabled(!isRegistrationLocked && UserHelper.fromSession().userHasPermission(baseEntityRef.getType(), baseEntityRef.getId(), CRUD.UPDATE));
|
99
|
addComponent(tdButton);
|
100
|
|
101
|
PermissionDebugUtils.fromSession().addGainPerEntityPermissionButton(this, SpecimenOrObservationBase.class,
|
102
|
baseEntityRef.getId(), EnumSet.of(CRUD.UPDATE, CRUD.DELETE), RegistrationStatus.PREPARATION.name());
|
103
|
|
104
|
typeDesignationButtons.add(new TypeDesignationWorkingSetButton(
|
105
|
typeDesignationWorkingSet.getWorkingsetType(),
|
106
|
typeDesignationWorkingSet.getWorkingSetId(),
|
107
|
tdButton)
|
108
|
);
|
109
|
String labelText = typeDesignationWorkingSet.getRepresentation();
|
110
|
labelText = labelText.replaceAll("^[^:]+:", ""); // remove "Type:", "NameType:" from the beginning
|
111
|
Label label = new Label(labelText);
|
112
|
|
113
|
label.setWidthUndefined();
|
114
|
addComponent(label);
|
115
|
labels.add(label);
|
116
|
}
|
117
|
}
|
118
|
addTypeDesignationButton = new Button(FontAwesome.PLUS);
|
119
|
addTypeDesignationButton.setDescription("Add a new type designation workingset");
|
120
|
addTypeDesignationButton.setVisible(!isRegistrationLocked);
|
121
|
addComponent(addTypeDesignationButton);
|
122
|
|
123
|
//TODO make responsive and use specificIdentifier in case the space gets too narrow
|
124
|
identifierLink = new Link(regDto.getIdentifier(), new ExternalResource(regDto.getIdentifier()));
|
125
|
identifierLink.setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
|
126
|
|
127
|
addComponents(identifierLink);
|
128
|
|
129
|
iterator().forEachRemaining(c -> addStyledComponent(c));
|
130
|
addDefaultStyles();
|
131
|
|
132
|
}
|
133
|
|
134
|
public IdButton<TaxonName> getNameButton() {
|
135
|
return nameIdButton;
|
136
|
}
|
137
|
|
138
|
public List<TypeDesignationWorkingSetButton> getTypeDesignationButtons() {
|
139
|
return typeDesignationButtons;
|
140
|
}
|
141
|
|
142
|
public Button getAddTypeDesignationButton() {
|
143
|
return addTypeDesignationButton;
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* {@inheritDoc}
|
148
|
*/
|
149
|
@Override
|
150
|
protected void addDefaultStyles() {
|
151
|
addStyleName(STYLE_NAMES);
|
152
|
if(nameIdButton != null){
|
153
|
nameIdButton.getButton().addStyleName(DEFAULT_BUTTON_STYLES);
|
154
|
}
|
155
|
if(nameLabel != null){
|
156
|
nameLabel.addStyleName("v-disabled");
|
157
|
}
|
158
|
typeDesignationButtons.forEach(idb -> idb.getButton().addStyleName(DEFAULT_BUTTON_STYLES));
|
159
|
addTypeDesignationButton.addStyleName(DEFAULT_BUTTON_STYLES);
|
160
|
}
|
161
|
|
162
|
public class TypeDesignationWorkingSetButton {
|
163
|
private Integer id;
|
164
|
private TypeDesignationWorkingSetType type;
|
165
|
private Button button;
|
166
|
|
167
|
public TypeDesignationWorkingSetButton(TypeDesignationWorkingSetType type, Integer id, Button button){
|
168
|
this.type = type;
|
169
|
this.id = id;
|
170
|
this.button = button;
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* @return the id
|
175
|
*/
|
176
|
public Integer getId() {
|
177
|
return id;
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* @return the button
|
182
|
*/
|
183
|
public Button getButton() {
|
184
|
return button;
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* @return the type
|
189
|
*/
|
190
|
public TypeDesignationWorkingSetType getType() {
|
191
|
return type;
|
192
|
}
|
193
|
|
194
|
}
|
195
|
|
196
|
public class IdButton<T> {
|
197
|
private Integer id;
|
198
|
private Class<T> entityType;
|
199
|
private Button button;
|
200
|
|
201
|
public IdButton(Class<T> type, Integer id, Button button){
|
202
|
this.entityType = type;
|
203
|
this.id = id;
|
204
|
this.button = button;
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* @return the id
|
209
|
*/
|
210
|
public Integer getId() {
|
211
|
return id;
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* @return the button
|
216
|
*/
|
217
|
public Button getButton() {
|
218
|
return button;
|
219
|
}
|
220
|
|
221
|
/**
|
222
|
* @return the type
|
223
|
*/
|
224
|
public Class<T> getType() {
|
225
|
return entityType;
|
226
|
}
|
227
|
|
228
|
}
|
229
|
|
230
|
}
|