Project

General

Profile

Download (7.67 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 org.vaadin.viritin.fields.LazyComboBox.FilterableCountProvider;
12
import org.vaadin.viritin.fields.LazyComboBox.FilterablePagingProvider;
13

    
14
import com.vaadin.data.Property;
15
import com.vaadin.data.Validator.InvalidValueException;
16
import com.vaadin.data.fieldgroup.FieldGroup;
17
import com.vaadin.data.util.converter.Converter.ConversionException;
18
import com.vaadin.server.AbstractErrorMessage.ContentMode;
19
import com.vaadin.server.ErrorMessage;
20
import com.vaadin.server.ErrorMessage.ErrorLevel;
21
import com.vaadin.server.UserError;
22
import com.vaadin.ui.Button;
23
import com.vaadin.ui.Button.ClickListener;
24
import com.vaadin.ui.Component;
25
import com.vaadin.ui.CssLayout;
26
import com.vaadin.ui.themes.ValoTheme;
27

    
28
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
29
import eu.etaxonomy.cdm.vaadin.event.NestedButtonStateUpdater;
30

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

    
39
    private static final long serialVersionUID = 6277565876657520311L;
40

    
41
    public static final String PRIMARY_STYLE = "v-related-entity-combobox";
42

    
43
    private Class<V> type;
44

    
45
    private CssLayout container = new CssLayout();
46

    
47
    private ReloadableLazyComboBox<V> lazySelect;
48

    
49
    private Button addButton = ButtonFactory.CREATE_NEW.createButton();
50
    private Button editButton = ButtonFactory.EDIT_ITEM.createButton();
51

    
52
    private NestedButtonStateUpdater<V> buttonUpdater;
53

    
54
    public ToOneRelatedEntityCombobox(String caption, Class<V> type){
55
        this.type = type;
56
        setCaption(caption);
57
        lazySelect = new ReloadableLazyComboBox<V>(type);
58
        lazySelect.setValidationVisible(false); // validation is to be shown for the ToOneRelatedEntityCombobox
59
        lazySelect.setRequiredError("Must be given");
60
        setRequiredError("Must be given");
61
        addStyledComponents(lazySelect, addButton, editButton);
62
        addSizedComponents(lazySelect, container);
63
        // lazySelect.setImmediate(true); // should cause immediate validation, however,
64
        // it does not work here, therefore we validate in the commitSelect() method during the commit
65
        lazySelect.addValueChangeListener(e -> {
66
            // update the itemContainer immediately so that the edit button acts on the chosen item
67
            commitSelect();
68
        });
69
    }
70

    
71

    
72
    /**
73
     * {@inheritDoc}
74
     */
75
    @Override
76
    protected Component initContent() {
77
        container.addComponents(lazySelect, addButton, editButton);
78
        setPrimaryStyleName(PRIMARY_STYLE);
79
        addDefaultStyles();
80
        return container;
81
    }
82

    
83
    /**
84
     * {@inheritDoc}
85
     */
86
    @Override
87
    public Class<? extends V> getType() {
88
        return type;
89
    }
90

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

    
99
    /**
100
     * {@inheritDoc}
101
     */
102
    @Override
103
    public FieldGroup getFieldGroup() {
104
        return null;
105
    }
106

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

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

    
120
    }
121

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

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

    
138

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

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

    
155

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

    
164

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

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

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

    
186

    
187

    
188

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

    
202

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

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

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

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

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

    
250

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

    
257

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

    
264

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

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

    
276

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

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

    
288

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

    
295

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

    
302

    
303

    
304
}
(15-15/19)