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.vaadin.component;
|
10
|
|
11
|
import java.util.Objects;
|
12
|
import java.util.Optional;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import org.vaadin.viritin.fields.LazyComboBox.FilterableCountProvider;
|
16
|
import org.vaadin.viritin.fields.LazyComboBox.FilterablePagingProvider;
|
17
|
|
18
|
import com.vaadin.data.Property;
|
19
|
import com.vaadin.data.fieldgroup.FieldGroup;
|
20
|
import com.vaadin.data.util.converter.Converter.ConversionException;
|
21
|
import com.vaadin.ui.Button;
|
22
|
import com.vaadin.ui.Button.ClickListener;
|
23
|
import com.vaadin.ui.Component;
|
24
|
import com.vaadin.ui.CssLayout;
|
25
|
import com.vaadin.ui.Field;
|
26
|
import com.vaadin.ui.themes.ValoTheme;
|
27
|
|
28
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
29
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
30
|
import eu.etaxonomy.cdm.service.FilterableStringRepresentationPagingProvider;
|
31
|
import eu.etaxonomy.cdm.service.UserHelperAccess;
|
32
|
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
|
33
|
import eu.etaxonomy.cdm.vaadin.event.NestedButtonStateUpdater;
|
34
|
|
35
|
/**
|
36
|
* @author a.kohlbecker
|
37
|
* @since May 24, 2017
|
38
|
*
|
39
|
*/
|
40
|
public class WeaklyRelatedEntityCombobox<V extends IdentifiableEntity<?>> extends CompositeCustomField<String>
|
41
|
implements WeaklyRelatedEntityField<V>, ReloadableSelect {
|
42
|
|
43
|
private static final long serialVersionUID = 6277565876657520311L;
|
44
|
|
45
|
public static final String PRIMARY_STYLE = "v-related-entity-combobox";
|
46
|
|
47
|
private Class<V> type;
|
48
|
|
49
|
private CssLayout container = new CssLayout();
|
50
|
|
51
|
private ReloadableLazyComboBox<String> lazySelect;
|
52
|
|
53
|
private Button addButton = ButtonFactory.CREATE_NEW.createButton();
|
54
|
private Button editButton = ButtonFactory.EDIT_ITEM.createButton();
|
55
|
|
56
|
private WeaklyRelatedEntityButtonUpdater buttonUpdater;
|
57
|
|
58
|
private FilterableStringRepresentationPagingProvider<UUID> filterablePagingProvider;
|
59
|
|
60
|
public WeaklyRelatedEntityCombobox(String caption, Class<V> type){
|
61
|
this.type = type;
|
62
|
setCaption(caption);
|
63
|
lazySelect = new ReloadableLazyComboBox<String>(String.class);
|
64
|
addStyledComponents(lazySelect, addButton, editButton);
|
65
|
addSizedComponents(lazySelect, container);
|
66
|
buttonUpdater = new WeaklyRelatedEntityButtonUpdater(this);
|
67
|
lazySelect.addValueChangeListener(buttonUpdater);
|
68
|
lazySelect.addValueChangeListener(e -> {
|
69
|
// update the itemContainer immediately so that the edit button acts on the chosen item
|
70
|
lazySelect.commit();
|
71
|
});
|
72
|
}
|
73
|
|
74
|
|
75
|
/**
|
76
|
* {@inheritDoc}
|
77
|
*/
|
78
|
@Override
|
79
|
protected Component initContent() {
|
80
|
container.addComponents(lazySelect, addButton, editButton);
|
81
|
setPrimaryStyleName(PRIMARY_STYLE);
|
82
|
addDefaultStyles();
|
83
|
return container;
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* {@inheritDoc}
|
88
|
*/
|
89
|
|
90
|
@Override
|
91
|
public Class<String> getType() {
|
92
|
return String.class;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* {@inheritDoc}
|
97
|
*/
|
98
|
@Override
|
99
|
protected void addDefaultStyles() {
|
100
|
container.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* {@inheritDoc}
|
105
|
*/
|
106
|
@Override
|
107
|
public Optional<FieldGroup> getFieldGroup() {
|
108
|
return Optional.empty();
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* @return the select
|
113
|
*/
|
114
|
public ReloadableLazyComboBox<String> getSelect() {
|
115
|
return lazySelect;
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* {@inheritDoc}
|
120
|
*/
|
121
|
public void loadFrom(FilterablePagingProvider<String> filterablePagingProvider, FilterableCountProvider filterableCountProvider, int pageLength) {
|
122
|
|
123
|
this.filterablePagingProvider = (FilterableStringRepresentationPagingProvider<UUID>) filterablePagingProvider;
|
124
|
lazySelect.loadFrom(filterablePagingProvider, filterableCountProvider, pageLength);
|
125
|
buttonUpdater.updateButtons(getValue());
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* reload the selected entity from the persistent storage
|
130
|
*/
|
131
|
@Override
|
132
|
public void reload() {
|
133
|
filterablePagingProvider.clearIdCache();
|
134
|
getSelect().reload();
|
135
|
}
|
136
|
|
137
|
|
138
|
/**
|
139
|
* {@inheritDoc}
|
140
|
*/
|
141
|
@Override
|
142
|
public void setAddButtonEnabled(boolean enabled) {
|
143
|
addButton.setEnabled(enabled);
|
144
|
}
|
145
|
|
146
|
|
147
|
/**
|
148
|
* {@inheritDoc}
|
149
|
*/
|
150
|
@Override
|
151
|
public void addClickListenerAddEntity(ClickListener listener) {
|
152
|
addButton.addClickListener(listener);
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* {@inheritDoc}
|
157
|
*/
|
158
|
@Override
|
159
|
public void setEditButtonEnabled(boolean enabled) {
|
160
|
editButton.setEnabled(enabled);
|
161
|
}
|
162
|
|
163
|
|
164
|
/**
|
165
|
* {@inheritDoc}
|
166
|
*/
|
167
|
@Override
|
168
|
public void addClickListenerEditEntity(ClickListener listener) {
|
169
|
editButton.addClickListener(listener);
|
170
|
}
|
171
|
|
172
|
@Override
|
173
|
public void selectNewItem(String bean){
|
174
|
setValue(bean);
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* Returns always currently selected item by
|
179
|
*
|
180
|
* {@inheritDoc}
|
181
|
*/
|
182
|
@Override
|
183
|
public String getValue() {
|
184
|
lazySelect.commit();
|
185
|
return lazySelect.getValue();
|
186
|
}
|
187
|
|
188
|
public UUID getIdForValue(){
|
189
|
return filterablePagingProvider.idFor(getValue());
|
190
|
}
|
191
|
|
192
|
|
193
|
/**
|
194
|
* sets the selection to the <code>newFieldValue</code> only if the value can
|
195
|
* be provided by the FilterablePagingProvider
|
196
|
*
|
197
|
*/
|
198
|
@Override
|
199
|
public void setValue(String newFieldValue) throws com.vaadin.data.Property.ReadOnlyException, ConversionException {
|
200
|
if(!Objects.equals(newFieldValue, lazySelect.getValue())){
|
201
|
lazySelect.refresh();
|
202
|
if(contains(newFieldValue)){
|
203
|
lazySelect.setValue(newFieldValue);
|
204
|
}
|
205
|
lazySelect.markAsDirty();
|
206
|
}
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* @param newFieldValue
|
211
|
* @return
|
212
|
*/
|
213
|
private boolean contains(String newFieldValue) {
|
214
|
UUID id = filterablePagingProvider.idFor(newFieldValue);
|
215
|
return id != null;
|
216
|
}
|
217
|
|
218
|
|
219
|
@Override
|
220
|
public boolean isValueInOptions(){
|
221
|
return lazySelect.getOptions().contains(lazySelect.getValue());
|
222
|
}
|
223
|
|
224
|
@Override
|
225
|
public void setPropertyDataSource(Property newDataSource) {
|
226
|
lazySelect.setPropertyDataSource(newDataSource);
|
227
|
if(buttonUpdater != null){
|
228
|
buttonUpdater.updateButtons(lazySelect.getValue());
|
229
|
}
|
230
|
}
|
231
|
|
232
|
/**
|
233
|
* {@inheritDoc}
|
234
|
*/
|
235
|
@Override
|
236
|
public Property getPropertyDataSource() {
|
237
|
return lazySelect.getPropertyDataSource();
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* {@inheritDoc}
|
242
|
*/
|
243
|
@Override
|
244
|
public void setReadOnly(boolean readOnly) {
|
245
|
super.setReadOnly(readOnly);
|
246
|
setDeepReadOnly(readOnly, getContent(), null);
|
247
|
if(buttonUpdater != null){
|
248
|
buttonUpdater.updateButtons(lazySelect.getValue());
|
249
|
}
|
250
|
}
|
251
|
|
252
|
@Override
|
253
|
public void updateButtons(){
|
254
|
buttonUpdater.updateButtons(getValue());
|
255
|
}
|
256
|
|
257
|
/**
|
258
|
* {@inheritDoc}
|
259
|
* @deprecated NestedButtonStateUpdater should rather be instantiated in the RelatedEntityField instead of passing it as property
|
260
|
*/
|
261
|
@Override
|
262
|
@Deprecated
|
263
|
public void setNestedButtonStateUpdater(NestedButtonStateUpdater<V> buttonUpdater) {
|
264
|
// not needed
|
265
|
}
|
266
|
|
267
|
class WeaklyRelatedEntityButtonUpdater implements NestedButtonStateUpdater<String> {
|
268
|
|
269
|
private static final long serialVersionUID = 4472031263172275012L;
|
270
|
|
271
|
WeaklyRelatedEntityCombobox<V> toOneRelatedEntityField;
|
272
|
|
273
|
public WeaklyRelatedEntityButtonUpdater(WeaklyRelatedEntityCombobox<V> toOneRelatedEntityField){
|
274
|
this.toOneRelatedEntityField = toOneRelatedEntityField;
|
275
|
String stringValue = toOneRelatedEntityField.getValue();
|
276
|
updateButtons(toOneRelatedEntityField.getValue());
|
277
|
toOneRelatedEntityField.setEditButtonEnabled(false);
|
278
|
}
|
279
|
|
280
|
|
281
|
/**
|
282
|
* {@inheritDoc}
|
283
|
*/
|
284
|
@Override
|
285
|
public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
|
286
|
|
287
|
String value = (String)event.getProperty().getValue();
|
288
|
updateButtons(value);
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* @param value
|
293
|
*/
|
294
|
@Override
|
295
|
public void updateButtons(String value) {
|
296
|
|
297
|
UUID uuid = null;
|
298
|
if(value != null && filterablePagingProvider != null){
|
299
|
uuid = filterablePagingProvider.idFor(value);
|
300
|
}
|
301
|
boolean userIsAllowedToUpdate = uuid != null && UserHelperAccess.userHelper().userHasPermission(type, uuid, CRUD.UPDATE);
|
302
|
boolean userIsAllowedToCreate = UserHelperAccess.userHelper().userHasPermission(type, CRUD.CREATE);
|
303
|
boolean isReadOnlyField = ((Field)toOneRelatedEntityField).isReadOnly();
|
304
|
|
305
|
toOneRelatedEntityField.setAddButtonEnabled(!isReadOnlyField && userIsAllowedToCreate);
|
306
|
toOneRelatedEntityField.setEditButtonEnabled(!isReadOnlyField && userIsAllowedToUpdate);
|
307
|
}
|
308
|
}
|
309
|
|
310
|
|
311
|
|
312
|
}
|