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(), EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
|
83
|
addComponent(nameLabel);
|
84
|
} else {
|
85
|
// no name in the registration! we only show the typified name as label
|
86
|
if(regDto.getTypifiedName() != null){
|
87
|
nameLabel = new Label(regDto.getTypifiedName().getLabel());
|
88
|
addComponent(nameLabel);
|
89
|
}
|
90
|
}
|
91
|
if(regDto.getOrderdTypeDesignationWorkingSets() != null){
|
92
|
for(TypedEntityReference<TypeDesignationBase<?>> baseEntityRef : regDto.getOrderdTypeDesignationWorkingSets().keySet()) {
|
93
|
TypeDesignationWorkingSet typeDesignationWorkingSet = regDto.getOrderdTypeDesignationWorkingSets().get(baseEntityRef);
|
94
|
String buttonLabel = SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType()) ? "Type": "NameType";
|
95
|
Button tdButton = new Button(buttonLabel + ":");
|
96
|
tdButton.setDescription("Edit the type designation working set");
|
97
|
tdButton.setEnabled(!isRegistrationLocked && UserHelper.fromSession().userHasPermission(baseEntityRef.getType(), baseEntityRef.getId(), CRUD.UPDATE));
|
98
|
addComponent(tdButton);
|
99
|
// Set<Integer> idSet = new HashSet<>();
|
100
|
// typeDesignationWorkingSet.getTypeDesignations().forEach(td -> idSet.add(td.getId()));
|
101
|
|
102
|
typeDesignationButtons.add(new TypeDesignationWorkingSetButton(
|
103
|
typeDesignationWorkingSet.getWorkingsetType(),
|
104
|
typeDesignationWorkingSet.getWorkingSetId(),
|
105
|
tdButton)
|
106
|
);
|
107
|
String labelText = typeDesignationWorkingSet.getRepresentation();
|
108
|
labelText = labelText.replaceAll("^[^:]+:", ""); // remove "Type:", "NameType:" from the beginning
|
109
|
Label label = new Label(labelText);
|
110
|
|
111
|
label.setWidthUndefined();
|
112
|
addComponent(label);
|
113
|
labels.add(label);
|
114
|
}
|
115
|
}
|
116
|
addTypeDesignationButton = new Button(FontAwesome.PLUS);
|
117
|
addTypeDesignationButton.setDescription("Add a new type designation workingset");
|
118
|
addTypeDesignationButton.setVisible(!isRegistrationLocked);
|
119
|
addComponent(addTypeDesignationButton);
|
120
|
|
121
|
//TODO make responsive and use specificIdentifier in case the space gets too narrow
|
122
|
identifierLink = new Link(regDto.getIdentifier(), new ExternalResource(regDto.getIdentifier()));
|
123
|
identifierLink.setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
|
124
|
|
125
|
addComponents(identifierLink);
|
126
|
|
127
|
iterator().forEachRemaining(c -> addStyledComponent(c));
|
128
|
addDefaultStyles();
|
129
|
|
130
|
}
|
131
|
|
132
|
public IdButton<TaxonName> getNameButton() {
|
133
|
return nameIdButton;
|
134
|
}
|
135
|
|
136
|
public List<TypeDesignationWorkingSetButton> getTypeDesignationButtons() {
|
137
|
return typeDesignationButtons;
|
138
|
}
|
139
|
|
140
|
public Button getAddTypeDesignationButton() {
|
141
|
return addTypeDesignationButton;
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* {@inheritDoc}
|
146
|
*/
|
147
|
@Override
|
148
|
protected void addDefaultStyles() {
|
149
|
addStyleName(STYLE_NAMES);
|
150
|
if(nameIdButton != null){
|
151
|
nameIdButton.getButton().addStyleName(DEFAULT_BUTTON_STYLES);
|
152
|
}
|
153
|
if(nameLabel != null){
|
154
|
nameLabel.addStyleName("v-disabled");
|
155
|
}
|
156
|
typeDesignationButtons.forEach(idb -> idb.getButton().addStyleName(DEFAULT_BUTTON_STYLES));
|
157
|
addTypeDesignationButton.addStyleName(DEFAULT_BUTTON_STYLES);
|
158
|
}
|
159
|
|
160
|
public class TypeDesignationWorkingSetButton {
|
161
|
private Integer id;
|
162
|
private TypeDesignationWorkingSetType type;
|
163
|
private Button button;
|
164
|
|
165
|
public TypeDesignationWorkingSetButton(TypeDesignationWorkingSetType type, Integer id, Button button){
|
166
|
this.type = type;
|
167
|
this.id = id;
|
168
|
this.button = button;
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* @return the id
|
173
|
*/
|
174
|
public Integer getId() {
|
175
|
return id;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* @return the button
|
180
|
*/
|
181
|
public Button getButton() {
|
182
|
return button;
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* @return the type
|
187
|
*/
|
188
|
public TypeDesignationWorkingSetType getType() {
|
189
|
return type;
|
190
|
}
|
191
|
|
192
|
}
|
193
|
|
194
|
public class IdButton<T> {
|
195
|
private Integer id;
|
196
|
private Class<T> entityType;
|
197
|
private Button button;
|
198
|
|
199
|
public IdButton(Class<T> type, Integer id, Button button){
|
200
|
this.entityType = type;
|
201
|
this.id = id;
|
202
|
this.button = button;
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* @return the id
|
207
|
*/
|
208
|
public Integer getId() {
|
209
|
return id;
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* @return the button
|
214
|
*/
|
215
|
public Button getButton() {
|
216
|
return button;
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* @return the type
|
221
|
*/
|
222
|
public Class<T> getType() {
|
223
|
return entityType;
|
224
|
}
|
225
|
|
226
|
}
|
227
|
|
228
|
}
|