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