Project

General

Profile

Download (7.64 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.Optional;
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.Validator.InvalidValueException;
18
import com.vaadin.data.fieldgroup.FieldGroup;
19
import com.vaadin.data.util.converter.Converter.ConversionException;
20
import com.vaadin.server.AbstractErrorMessage.ContentMode;
21
import com.vaadin.server.ErrorMessage;
22
import com.vaadin.server.ErrorMessage.ErrorLevel;
23
import com.vaadin.server.UserError;
24
import com.vaadin.ui.Button;
25
import com.vaadin.ui.Button.ClickListener;
26
import com.vaadin.ui.Component;
27
import com.vaadin.ui.CssLayout;
28
import com.vaadin.ui.themes.ValoTheme;
29

    
30
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
31
import eu.etaxonomy.cdm.vaadin.event.NestedButtonStateUpdater;
32
import eu.etaxonomy.cdm.vaadin.ui.UIMessages;
33

    
34
/**
35
 * @author a.kohlbecker
36
 * @since May 24, 2017
37
 *
38
 */
39
public class ToOneRelatedEntityCombobox<V extends Object> extends CompositeCustomField<V>
40
    implements ToOneRelatedEntityField<V>, ReloadableSelect, EntitySupport<V> {
41

    
42
    private static final long serialVersionUID = 6277565876657520311L;
43

    
44
    public static final String PRIMARY_STYLE = "v-related-entity-combobox";
45

    
46
    private Class<V> type;
47

    
48
    private CssLayout container = new CssLayout();
49

    
50
    private ReloadableLazyComboBox<V> lazySelect;
51

    
52
    private Button addButton = ButtonFactory.CREATE_NEW.createButton();
53
    private Button editButton = ButtonFactory.EDIT_ITEM.createButton();
54

    
55
    private NestedButtonStateUpdater<V> buttonUpdater;
56

    
57
    public ToOneRelatedEntityCombobox(String caption, Class<V> type){
58
        this.type = type;
59
        setCaption(caption);
60
        lazySelect = new ReloadableLazyComboBox<V>(type);
61
        lazySelect.setRequiredError(UIMessages.REQUIRED_SELECT_MISSING);
62
        addStyledComponents(lazySelect, addButton, editButton);
63
        addSizedComponents(lazySelect, container);
64
        // lazySelect.setImmediate(true); // should cause immediate validation, however,
65
        // it does not work here, therefore we validate in the commitSelect() method during the commit
66
        lazySelect.addValueChangeListener(e -> {
67
            // update the itemContainer immediately so that the edit button acts on the chosen item
68
            commitSelect();
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
    @Override
88
    public Class<? extends V> getType() {
89
        return type;
90
    }
91

    
92
    /**
93
     * {@inheritDoc}
94
     */
95
    @Override
96
    protected void addDefaultStyles() {
97
        container.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
98
    }
99

    
100
    /**
101
     * {@inheritDoc}
102
     */
103
    @Override
104
    public Optional<FieldGroup> getFieldGroup() {
105
        return Optional.empty();
106
    }
107

    
108
    /**
109
     * @return the select
110
     */
111
    public ReloadableLazyComboBox<V> getSelect() {
112
        return lazySelect;
113
    }
114

    
115
    /**
116
     * {@inheritDoc}
117
     */
118
    public void loadFrom(FilterablePagingProvider<V> filterablePagingProvider, FilterableCountProvider filterableCountProvider, int pageLength) {
119
        lazySelect.loadFrom(filterablePagingProvider, filterableCountProvider, pageLength);
120

    
121
    }
122

    
123
    /**
124
     * reload the selected entity from the persistent storage
125
     */
126
    @Override
127
    public void reload() {
128
        getSelect().reload();
129
    }
130

    
131
    /**
132
     * {@inheritDoc}
133
     */
134
    @Override
135
    public void setAddButtonEnabled(boolean enabled) {
136
        addButton.setEnabled(enabled);
137
    }
138

    
139

    
140
    /**
141
     * {@inheritDoc}
142
     */
143
    @Override
144
    public void addClickListenerAddEntity(ClickListener listener) {
145
        addButton.addClickListener(listener);
146
    }
147

    
148
    /**
149
     * {@inheritDoc}
150
     */
151
    @Override
152
    public void setEditButtonEnabled(boolean enabled) {
153
        editButton.setEnabled(enabled);
154
    }
155

    
156

    
157
    /**
158
     * {@inheritDoc}
159
     */
160
    @Override
161
    public void addClickListenerEditEntity(ClickListener listener) {
162
        editButton.addClickListener(listener);
163
    }
164

    
165

    
166
    @Override
167
    public void replaceEntityValue(V bean){
168
        lazySelect.replaceEntityValue(bean);
169
    }
170

    
171
    @Override
172
    public void selectNewItem(V bean){
173
        setValue(bean);
174
    }
175

    
176
    /**
177
     * Returns always currently selected item by
178
     *
179
     * {@inheritDoc}
180
     */
181
    @Override
182
    public V getValue() {
183
        commitSelect();
184
        return lazySelect.getValue();
185
    }
186

    
187

    
188

    
189

    
190
    /**
191
     *
192
     */
193
    public void commitSelect() {
194
        try {
195
            setComponentError(null);
196
            lazySelect.commit();
197
        } catch (InvalidValueException ex){
198
            UserError componentError = new UserError(ex.getHtmlMessage(), ContentMode.HTML, ErrorLevel.ERROR);
199
            lazySelect.setComponentError(componentError);
200
        }
201
    }
202

    
203

    
204
    /**
205
     * {@inheritDoc}
206
     */
207
    @Override
208
    public void setValue(V newFieldValue) throws com.vaadin.data.Property.ReadOnlyException, ConversionException {
209
        lazySelect.refresh();
210
        lazySelect.setValue(newFieldValue);
211
        lazySelect.markAsDirty();
212
    }
213

    
214
    @Override
215
    public void setPropertyDataSource(Property newDataSource) {
216
        lazySelect.setPropertyDataSource(newDataSource);
217
        if(buttonUpdater != null){
218
            buttonUpdater.updateButtons(lazySelect.getValue());
219
        }
220
    }
221

    
222
    /**
223
     * {@inheritDoc}
224
     */
225
    @Override
226
    public Property getPropertyDataSource() {
227
        return lazySelect.getPropertyDataSource();
228
    }
229

    
230
    /**
231
     * {@inheritDoc}
232
     */
233
    @Override
234
    public void setReadOnly(boolean readOnly) {
235
        super.setReadOnly(readOnly);
236
        setDeepReadOnly(readOnly, getContent(), null);
237
        if(buttonUpdater != null){
238
            buttonUpdater.updateButtons(lazySelect.getValue());
239
        }
240
    }
241

    
242
    /**
243
     * {@inheritDoc}
244
     */
245
    @Override
246
    public void setNestedButtonStateUpdater(NestedButtonStateUpdater<V> buttonUpdater) {
247
        this.buttonUpdater = buttonUpdater;
248
        lazySelect.addValueChangeListener(buttonUpdater);
249
    }
250

    
251

    
252
    @Override
253
    public void validate() throws InvalidValueException {
254
        super.validate();
255
        lazySelect.validate();
256
    }
257

    
258

    
259
    @Override
260
    public ErrorMessage getErrorMessage() {
261
        ErrorMessage errorMessage = lazySelect.getErrorMessage();
262
        return errorMessage;
263
    }
264

    
265

    
266
    @Override
267
    public boolean isValid() {
268
        return lazySelect.isValid();
269
    }
270

    
271
    @Override
272
    public void setRequired(boolean required) {
273
        super.setRequired(required);
274
        lazySelect.setRequired(required);
275
    }
276

    
277

    
278
    @Override
279
    public boolean isRequired() {
280
        return lazySelect.isRequired();
281
    }
282

    
283
    @Override
284
    public void setImmediate(boolean immediate) {
285
        super.setImmediate(immediate);
286
        lazySelect.setImmediate(immediate);
287
    }
288

    
289

    
290
    @Override
291
    public void commit() throws SourceException, InvalidValueException {
292
        lazySelect.commit(); // we must not use the commitSelect() here to allow InvalidValueException to be handled by the caller
293
        super.commit();
294
    }
295

    
296

    
297
    @Override
298
    public void setComponentError(ErrorMessage componentError) {
299
        lazySelect.setComponentError(componentError);
300
        super.setComponentError(componentError);
301
    }
302

    
303

    
304

    
305
}
(15-15/19)