Project

General

Profile

Download (9.98 KB) Statistics
| Branch: | Tag: | Revision:
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.Validator.InvalidValueException;
20
import com.vaadin.data.fieldgroup.FieldGroup;
21
import com.vaadin.data.util.converter.Converter.ConversionException;
22
import com.vaadin.server.AbstractErrorMessage.ContentMode;
23
import com.vaadin.server.ErrorMessage.ErrorLevel;
24
import com.vaadin.server.UserError;
25
import com.vaadin.ui.Button;
26
import com.vaadin.ui.Button.ClickListener;
27
import com.vaadin.ui.Component;
28
import com.vaadin.ui.CssLayout;
29
import com.vaadin.ui.Field;
30
import com.vaadin.ui.themes.ValoTheme;
31

    
32
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
33
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
34
import eu.etaxonomy.cdm.service.FilterableStringRepresentationPagingProvider;
35
import eu.etaxonomy.cdm.service.UserHelperAccess;
36
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
37
import eu.etaxonomy.cdm.vaadin.event.NestedButtonStateUpdater;
38

    
39
/**
40
 * @author a.kohlbecker
41
 * @since May 24, 2017
42
 *
43
 */
44
public class WeaklyRelatedEntityCombobox<V extends IdentifiableEntity<?>> extends CompositeCustomField<String>
45
    implements WeaklyRelatedEntityField<V>, ReloadableSelect {
46

    
47
    private static final long serialVersionUID = 6277565876657520311L;
48

    
49
    public static final String PRIMARY_STYLE = "v-related-entity-combobox";
50

    
51
    private Class<V> type;
52

    
53
    private CssLayout container = new CssLayout();
54

    
55
    private ReloadableLazyComboBox<String> lazySelect;
56

    
57
    private Button addButton = ButtonFactory.CREATE_NEW.createButton();
58
    private Button editButton = ButtonFactory.EDIT_ITEM.createButton();
59

    
60
    private WeaklyRelatedEntityButtonUpdater buttonUpdater;
61

    
62
    private FilterableStringRepresentationPagingProvider<UUID> filterablePagingProvider;
63

    
64
    public WeaklyRelatedEntityCombobox(String caption, Class<V> type){
65
        this.type = type;
66
        setCaption(caption);
67
        lazySelect = new ReloadableLazyComboBox<String>(String.class);
68
        addStyledComponents(lazySelect, addButton, editButton);
69
        addSizedComponents(lazySelect, container);
70
        buttonUpdater = new WeaklyRelatedEntityButtonUpdater(this);
71
        lazySelect.addValueChangeListener(buttonUpdater);
72
        lazySelect.setValidationVisible(true);
73
        lazySelect.addValueChangeListener(e -> {
74
            // update the itemContainer immediately so that the edit button acts on the chosen item
75
            // TODO In contrast to ToOneRelatedEntityCombobox where getValue() is overwritten to call
76
            // commitSelect() calling this method would most probably remove all strings witch do not have a
77
            // weakly related entity. Such behavior would be very unfriendly to users.
78
            try {
79
              lazySelect.commit();
80
            } catch (InvalidValueException ie){
81
                /* Ignore here */
82
            }
83
        });
84
    }
85

    
86

    
87
    /**
88
     * {@inheritDoc}
89
     */
90
    @Override
91
    protected Component initContent() {
92
        container.addComponents(lazySelect, addButton, editButton);
93
        setPrimaryStyleName(PRIMARY_STYLE);
94
        addDefaultStyles();
95
        return container;
96
    }
97

    
98
    /**
99
     * {@inheritDoc}
100
     */
101

    
102
    @Override
103
    public Class<String> getType() {
104
        return String.class;
105
    }
106

    
107
    /**
108
     * {@inheritDoc}
109
     */
110
    @Override
111
    protected void addDefaultStyles() {
112
        container.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
113
    }
114

    
115
    /**
116
     * {@inheritDoc}
117
     */
118
    @Override
119
    public Optional<FieldGroup> getFieldGroup() {
120
        return Optional.empty();
121
    }
122

    
123
    /**
124
     * @return the select
125
     */
126
    public ReloadableLazyComboBox<String> getSelect() {
127
        return lazySelect;
128
    }
129

    
130
    /**
131
     * {@inheritDoc}
132
     */
133
    public void loadFrom(FilterablePagingProvider<String> filterablePagingProvider, FilterableCountProvider filterableCountProvider, int pageLength) {
134

    
135
        this.filterablePagingProvider = (FilterableStringRepresentationPagingProvider<UUID>) filterablePagingProvider;
136
        lazySelect.loadFrom(filterablePagingProvider, filterableCountProvider, pageLength);
137
        buttonUpdater.updateButtons(getValue());
138
    }
139

    
140
    /**
141
     * reload the selected entity from the persistent storage
142
     */
143
    @Override
144
    public void reload() {
145
        filterablePagingProvider.clearIdCache();
146
        getSelect().reload();
147
    }
148

    
149

    
150
    /**
151
     * {@inheritDoc}
152
     */
153
    @Override
154
    public void setAddButtonEnabled(boolean enabled) {
155
        addButton.setEnabled(enabled);
156
    }
157

    
158

    
159
    /**
160
     * {@inheritDoc}
161
     */
162
    @Override
163
    public void addClickListenerAddEntity(ClickListener listener) {
164
        addButton.addClickListener(listener);
165
    }
166

    
167
    /**
168
     * {@inheritDoc}
169
     */
170
    @Override
171
    public void setEditButtonEnabled(boolean enabled) {
172
        editButton.setEnabled(enabled);
173
    }
174

    
175

    
176
    /**
177
     * {@inheritDoc}
178
     */
179
    @Override
180
    public void addClickListenerEditEntity(ClickListener listener) {
181
        editButton.addClickListener(listener);
182
    }
183

    
184
    @Override
185
    public void selectNewItem(String bean){
186
        setValue(bean);
187
    }
188

    
189

    
190
    public UUID getIdForValue(){
191
        return filterablePagingProvider.idFor(getValue());
192
    }
193

    
194

    
195
    /**
196
     * sets the selection to the <code>newFieldValue</code> only if the value can
197
     * be provided by the FilterablePagingProvider
198
     *
199
     */
200
    @Override
201
    public void setValue(String newFieldValue) throws com.vaadin.data.Property.ReadOnlyException, ConversionException {
202
        if(!Objects.equals(newFieldValue, lazySelect.getValue())){
203
            if(contains(newFieldValue)){
204
                lazySelect.setValue(newFieldValue);
205
                lazySelect.markAsDirty();
206
            }
207
        }
208
    }
209

    
210
    @Override
211
    public String getValue() {
212
        return lazySelect.getValue();
213

    
214
    }
215

    
216
    /**
217
     * @param newFieldValue
218
     * @return
219
     */
220
    private boolean contains(String newFieldValue) {
221
        UUID id = filterablePagingProvider.idFor(newFieldValue);
222
        return id != null;
223
    }
224

    
225

    
226
    @Override
227
    public boolean isValueInOptions(){
228
        return lazySelect.getOptions().contains(lazySelect.getValue());
229
    }
230

    
231
    @Override
232
    public void setPropertyDataSource(Property newDataSource) {
233
        lazySelect.setPropertyDataSource(newDataSource);
234
        if(buttonUpdater != null){
235
            buttonUpdater.updateButtons(lazySelect.getValue());
236
        }
237
    }
238

    
239
    /**
240
     * {@inheritDoc}
241
     */
242
    @Override
243
    public Property getPropertyDataSource() {
244
        return lazySelect.getPropertyDataSource();
245
    }
246

    
247
    /**
248
     * {@inheritDoc}
249
     */
250
    @Override
251
    public void setReadOnly(boolean readOnly) {
252
        super.setReadOnly(readOnly);
253
        setDeepReadOnly(readOnly, getContent(), null);
254
        if(buttonUpdater != null){
255
         buttonUpdater.updateButtons(lazySelect.getValue());
256
        }
257
    }
258

    
259
    @Override
260
    public void setRequired(boolean required) {
261
        super.setRequired(required);
262
        lazySelect.setRequired(required);
263
    }
264

    
265
    @Override
266
    public void setImmediate(boolean immediate){
267
        super.setImmediate(immediate);
268
        lazySelect.setImmediate(immediate);
269
    }
270

    
271
    @Override
272
    public void updateButtons(){
273
        buttonUpdater.updateButtons(getValue());
274
    }
275

    
276
    @Override
277
    public void commit() throws SourceException, InvalidValueException {
278
        lazySelect.commit();
279
    }
280

    
281
    /**
282
    *
283
    */
284
   public void commitSelect() {
285
       try {
286
           setComponentError(null);
287
           lazySelect.commit();
288
       } catch (InvalidValueException ex){
289
           UserError componentError = new UserError(ex.getHtmlMessage(), ContentMode.HTML, ErrorLevel.ERROR);
290
           lazySelect.setComponentError(componentError);
291
       }
292
   }
293

    
294

    
295

    
296
    /**
297
     * {@inheritDoc}
298
     * @deprecated NestedButtonStateUpdater should rather be instantiated in the RelatedEntityField instead of passing it as property
299
     */
300
    @Override
301
    @Deprecated
302
    public void setNestedButtonStateUpdater(NestedButtonStateUpdater<V> buttonUpdater) {
303
        // not needed
304
    }
305

    
306
    class WeaklyRelatedEntityButtonUpdater implements NestedButtonStateUpdater<String> {
307

    
308
        private static final long serialVersionUID = 4472031263172275012L;
309

    
310
        WeaklyRelatedEntityCombobox<V>  toOneRelatedEntityField;
311

    
312
        public WeaklyRelatedEntityButtonUpdater(WeaklyRelatedEntityCombobox<V> toOneRelatedEntityField){
313
            this.toOneRelatedEntityField = toOneRelatedEntityField;
314
            updateButtons(toOneRelatedEntityField.getValue());
315
            toOneRelatedEntityField.setEditButtonEnabled(false);
316
        }
317

    
318

    
319
        /**
320
         * {@inheritDoc}
321
         */
322
        @Override
323
        public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
324

    
325
            String value = (String)event.getProperty().getValue();
326
            updateButtons(value);
327
        }
328

    
329
        /**
330
         * @param value
331
         */
332
        @Override
333
        public void updateButtons(String value) {
334

    
335
            UUID uuid = null;
336
            if(value != null && filterablePagingProvider != null){
337
                uuid = filterablePagingProvider.idFor(value);
338
            }
339
            boolean userIsAllowedToUpdate = uuid != null && UserHelperAccess.userHelper().userHasPermission(type, uuid, CRUD.UPDATE);
340
            boolean userIsAllowedToCreate = UserHelperAccess.userHelper().userHasPermission(type, CRUD.CREATE);
341
            boolean isReadOnlyField = ((Field)toOneRelatedEntityField).isReadOnly();
342

    
343
            toOneRelatedEntityField.setAddButtonEnabled(!isReadOnlyField && userIsAllowedToCreate);
344
            toOneRelatedEntityField.setEditButtonEnabled(!isReadOnlyField && userIsAllowedToUpdate);
345
        }
346
    }
347

    
348
}
(18-18/19)