Project

General

Profile

Download (7.44 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
public class ToOneRelatedEntityCombobox<V extends Object> extends CompositeCustomField<V>
39
    implements ToOneRelatedEntityField<V>, ReloadableSelect, EntitySupport<V> {
40

    
41
    private static final long serialVersionUID = 1477855047187199796L;
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<V> lazySelect;
50

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

    
54
    private NestedButtonStateUpdater<V> buttonUpdater;
55

    
56
    public ToOneRelatedEntityCombobox(String caption, Class<V> type){
57
        this.type = type;
58
        setCaption(caption);
59
        lazySelect = new ReloadableLazyComboBox<V>(type);
60
        lazySelect.setRequiredError(UIMessages.REQUIRED_SELECT_MISSING);
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
    @Override
72
    protected Component initContent() {
73
        container.addComponents(lazySelect, addButton, editButton);
74
        setPrimaryStyleName(PRIMARY_STYLE);
75
        addDefaultStyles();
76
        return container;
77
    }
78

    
79
    @Override
80
    public Class<? extends V> getType() {
81
        return type;
82
    }
83

    
84
    @Override
85
    protected void addDefaultStyles() {
86
        container.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
87
    }
88

    
89
    @Override
90
    public Optional<FieldGroup> getFieldGroup() {
91
        return Optional.empty();
92
    }
93

    
94
    /**
95
     * @return the select
96
     */
97
    public ReloadableLazyComboBox<V> getSelect() {
98
        return lazySelect;
99
    }
100

    
101
    public void selectFirst() {
102
        lazySelect.selectFirst();
103
    }
104

    
105
    public void loadFrom(FilterablePagingProvider<V> filterablePagingProvider, FilterableCountProvider filterableCountProvider, int pageLength) {
106
        lazySelect.loadFrom(filterablePagingProvider, filterableCountProvider, pageLength);
107

    
108
    }
109

    
110
    /**
111
     * reload the selected entity from the persistent storage
112
     */
113
    @Override
114
    public void reload() {
115
        getSelect().reload();
116
    }
117

    
118
    @Override
119
    public void setAddButtonEnabled(boolean enabled) {
120
        addButton.setEnabled(enabled);
121
    }
122

    
123
    @Override
124
    public void addClickListenerAddEntity(ClickListener listener) {
125
        addButton.addClickListener(listener);
126
    }
127

    
128
    @Override
129
    public void setEditButtonEnabled(boolean enabled) {
130
        editButton.setEnabled(enabled);
131
    }
132

    
133
    @Override
134
    public void addClickListenerEditEntity(ClickListener listener) {
135
        editButton.addClickListener(listener);
136
    }
137

    
138
    @Override
139
    public void addValueChangeListener(ValueChangeListener listener) {
140
        lazySelect.addValueChangeListener(listener);
141
    }
142

    
143
    @Override
144
    public void removeValueChangeListener(ValueChangeListener listener) {
145
        lazySelect.removeValueChangeListener(listener);
146
    }
147

    
148
    @Override
149
    public void replaceEntityValue(V bean){
150
        lazySelect.replaceEntityValue(bean);
151
    }
152

    
153
    @Override
154
    public void selectNewItem(V bean){
155
        setValue(bean);
156
    }
157

    
158
    /**
159
     * Returns always currently selected item by
160
     */
161
    @Override
162
    public V getValue() {
163
        commitSelect();
164
        return lazySelect.getValue();
165
    }
166

    
167
    public void commitSelect() {
168
        try {
169
            setComponentError(null);
170
            lazySelect.commit();
171
        } catch (InvalidValueException ex){
172
            UserError componentError = new UserError(ex.getHtmlMessage(), ContentMode.HTML, ErrorLevel.ERROR);
173
            lazySelect.setComponentError(componentError);
174
        }
175
    }
176

    
177
    @Override
178
    public void setValue(V newFieldValue) throws com.vaadin.data.Property.ReadOnlyException, ConversionException {
179
        lazySelect.refresh();
180
        lazySelect.setValue(newFieldValue);
181
        lazySelect.markAsDirty();
182
    }
183

    
184
    @Override
185
    public void setPropertyDataSource(Property newDataSource) {
186
        lazySelect.setPropertyDataSource(newDataSource);
187
        if(buttonUpdater != null){
188
            buttonUpdater.updateButtons(lazySelect.getValue());
189
        }
190
    }
191

    
192
    @Override
193
    public Property getPropertyDataSource() {
194
        return lazySelect.getPropertyDataSource();
195
    }
196

    
197
    @Override
198
    public void setReadOnly(boolean readOnly) {
199
        super.setReadOnly(readOnly);
200
        setDeepReadOnly(readOnly, getContent(), null);
201
        if(buttonUpdater != null){
202
            buttonUpdater.updateButtons(lazySelect.getValue());
203
        }
204
    }
205

    
206
    @Override
207
    public void setNestedButtonStateUpdater(NestedButtonStateUpdater<V> buttonUpdater) {
208
        this.buttonUpdater = buttonUpdater;
209
        lazySelect.addValueChangeListener(buttonUpdater);
210
    }
211

    
212
    @Override
213
    public void validate() throws InvalidValueException {
214
        lazySelect.validate();
215
    }
216

    
217
    @Override
218
    public ErrorMessage getErrorMessage() {
219
        ErrorMessage errorMessage = lazySelect.getErrorMessage();
220
        return errorMessage;
221
    }
222

    
223
    @Override
224
    public boolean isValid() {
225
        return lazySelect.isValid();
226
    }
227

    
228
    @Override
229
    public void setRequired(boolean required) {
230
        super.setRequired(required);
231
        lazySelect.setRequired(required);
232
    }
233

    
234
    @Override
235
    public boolean isRequired() {
236
        return lazySelect.isRequired();
237
    }
238

    
239
    @Override
240
    public void setImmediate(boolean immediate) {
241
        super.setImmediate(immediate);
242
        lazySelect.setImmediate(immediate);
243
    }
244

    
245
    @Override
246
    public void commit() throws SourceException, InvalidValueException {
247
        lazySelect.commit(); // we must not use the commitSelect() here to allow InvalidValueException to be handled by the caller
248
        super.commit();
249
    }
250

    
251
    @Override
252
    public void setComponentError(ErrorMessage componentError) {
253
        lazySelect.setComponentError(componentError);
254
        super.setComponentError(componentError);
255
    }
256

    
257
}
(15-15/19)