Project

General

Profile

Download (7.6 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
import eu.etaxonomy.cdm.vaadin.ui.UIMessages;
31

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

    
40
    private static final long serialVersionUID = 6277565876657520311L;
41

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

    
44
    private Class<V> type;
45

    
46
    private CssLayout container = new CssLayout();
47

    
48
    private ReloadableLazyComboBox<V> lazySelect;
49

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

    
53
    private NestedButtonStateUpdater<V> buttonUpdater;
54

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

    
70

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

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

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

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

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

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

    
119
    }
120

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

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

    
137

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

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

    
154

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

    
163

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

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

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

    
185

    
186

    
187

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

    
201

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

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

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

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

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

    
249

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

    
256

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

    
263

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

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

    
275

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

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

    
287

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

    
294

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

    
301

    
302

    
303
}
(15-15/19)