Project

General

Profile

Download (8.3 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.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
        buttonUpdater = new WeaklyRelatedEntityButtonUpdater(this);
65
        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
        filterablePagingProvider.clearIdCache();
132
        getSelect().reload();
133
    }
134

    
135

    
136
    /**
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
    public UUID getIdForValue(){
187
        return filterablePagingProvider.idFor(getValue());
188
    }
189

    
190

    
191
    /**
192
     * sets the selection to the <code>newFieldValue</code> only if the value can
193
     * be provided by the FilterablePagingProvider
194
     *
195
     */
196
    @Override
197
    public void setValue(String newFieldValue) throws com.vaadin.data.Property.ReadOnlyException, ConversionException {
198
        lazySelect.refresh();
199
        if(lazySelect.getOptions().contains(newFieldValue)){
200
            lazySelect.setValue(newFieldValue);
201
        }
202
        lazySelect.markAsDirty();
203
    }
204

    
205
    @Override
206
    public void setPropertyDataSource(Property newDataSource) {
207
        lazySelect.setPropertyDataSource(newDataSource);
208
        if(buttonUpdater != null){
209
            buttonUpdater.updateButtons(lazySelect.getValue());
210
        }
211
    }
212

    
213
    /**
214
     * {@inheritDoc}
215
     */
216
    @Override
217
    public Property getPropertyDataSource() {
218
        return lazySelect.getPropertyDataSource();
219
    }
220

    
221
    /**
222
     * {@inheritDoc}
223
     */
224
    @Override
225
    public void setReadOnly(boolean readOnly) {
226
        super.setReadOnly(readOnly);
227
        setDeepReadOnly(readOnly, getContent(), null);
228
        if(buttonUpdater != null){
229
         buttonUpdater.updateButtons(lazySelect.getValue());
230
        }
231
    }
232

    
233
    @Override
234
    public void updateButtons(){
235
        buttonUpdater.updateButtons(getValue());
236
    }
237

    
238
    /**
239
     * {@inheritDoc}
240
     * @deprecated NestedButtonStateUpdater should rather be instantiated in the RelatedEntityField instead of passing it as property
241
     */
242
    @Override
243
    @Deprecated
244
    public void setNestedButtonStateUpdater(NestedButtonStateUpdater<V> buttonUpdater) {
245
        // not needed
246
    }
247

    
248
    class WeaklyRelatedEntityButtonUpdater implements NestedButtonStateUpdater<String> {
249

    
250
        private static final long serialVersionUID = 4472031263172275012L;
251

    
252
        WeaklyRelatedEntityCombobox<V>  toOneRelatedEntityField;
253

    
254
        public WeaklyRelatedEntityButtonUpdater(WeaklyRelatedEntityCombobox<V> toOneRelatedEntityField){
255
            this.toOneRelatedEntityField = toOneRelatedEntityField;
256
            String stringValue = toOneRelatedEntityField.getValue();
257
            updateButtons(toOneRelatedEntityField.getValue());
258
            toOneRelatedEntityField.setEditButtonEnabled(false);
259
        }
260

    
261

    
262
        /**
263
         * {@inheritDoc}
264
         */
265
        @Override
266
        public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
267

    
268
            String value = (String)event.getProperty().getValue();
269
            updateButtons(value);
270
        }
271

    
272
        /**
273
         * @param value
274
         */
275
        @Override
276
        public void updateButtons(String value) {
277

    
278
            UUID uuid = null;
279
            if(value != null && filterablePagingProvider != null){
280
                uuid = filterablePagingProvider.idFor(value);
281
            }
282
            boolean userIsAllowedToUpdate = uuid != null && UserHelperAccess.userHelper().userHasPermission(type, uuid, CRUD.UPDATE);
283
            boolean userIsAllowedToCreate = UserHelperAccess.userHelper().userHasPermission(type, CRUD.CREATE);
284
            boolean isReadOnlyField = ((Field)toOneRelatedEntityField).isReadOnly();
285

    
286
            toOneRelatedEntityField.setAddButtonEnabled(!isReadOnlyField && userIsAllowedToCreate);
287
            toOneRelatedEntityField.setEditButtonEnabled(!isReadOnlyField && userIsAllowedToUpdate);
288
        }
289
    }
290

    
291

    
292

    
293
}
(18-18/19)