Project

General

Profile

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

    
15
import com.vaadin.data.Property;
16
import com.vaadin.data.fieldgroup.FieldGroup;
17
import com.vaadin.data.util.converter.Converter.ConversionException;
18
import com.vaadin.server.FontAwesome;
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.themes.ValoTheme;
24

    
25
/**
26
 * @author a.kohlbecker
27
 * @since May 24, 2017
28
 *
29
 */
30
public class ToOneRelatedEntityCombobox<V extends Object> extends CompositeCustomField<V> implements ToOneRelatedEntityField<V> {
31

    
32
    private static final long serialVersionUID = 6277565876657520311L;
33

    
34
    public static final String PRIMARY_STYLE = "v-related-entity-combobox";
35

    
36
    private Class<V> type;
37

    
38
    private CssLayout container = new CssLayout();
39

    
40
    private LazyComboBox<V> lazySelect;
41

    
42
    private Button addButton = new Button(FontAwesome.PLUS);
43
    private Button editButton  = new Button(FontAwesome.EDIT);
44

    
45
    public ToOneRelatedEntityCombobox(String caption, Class<V> type){
46
        this.type = type;
47
        setCaption(caption);
48
        lazySelect = new LazyComboBox<V>(type);
49
        addStyledComponents(lazySelect, addButton, editButton);
50
        addSizedComponents(lazySelect, container);
51
        lazySelect.addValueChangeListener(e -> {
52
            // update the itemContainer immediately so that the edit button acts on the chosen item
53
            lazySelect.commit();
54
        });
55
    }
56

    
57

    
58
    /**
59
     * {@inheritDoc}
60
     */
61
    @Override
62
    protected Component initContent() {
63
        container.addComponents(lazySelect, addButton, editButton);
64
        setPrimaryStyleName(PRIMARY_STYLE);
65
        addDefaultStyles();
66
        return container;
67
    }
68

    
69
    /**
70
     * {@inheritDoc}
71
     */
72
    @Override
73
    public Class<? extends V> getType() {
74
        return type;
75
    }
76

    
77
    /**
78
     * {@inheritDoc}
79
     */
80
    @Override
81
    protected void addDefaultStyles() {
82
        container.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
83
    }
84

    
85
    /**
86
     * {@inheritDoc}
87
     */
88
    @Override
89
    public FieldGroup getFieldGroup() {
90
        return null;
91
    }
92

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

    
100
    /**
101
     * {@inheritDoc}
102
     */
103
    public void loadFrom(FilterablePagingProvider<V> filterablePagingProvider, FilterableCountProvider filterableCountProvider, int pageLength) {
104
        lazySelect.loadFrom(filterablePagingProvider, filterableCountProvider, pageLength);
105

    
106
    }
107

    
108
    /**
109
     * reload the selected entity from the persistent storage
110
     */
111
    public void reload() {
112
        getSelect().refresh();
113
        getSelect().discard(); // reload from data source
114

    
115
    }
116

    
117
    /**
118
     * {@inheritDoc}
119
     */
120
    @Override
121
    public void setAddButtonEnabled(boolean enabled) {
122
        addButton.setEnabled(enabled);
123
    }
124

    
125

    
126
    /**
127
     * {@inheritDoc}
128
     */
129
    @Override
130
    public void addClickListenerAddEntity(ClickListener listener) {
131
        addButton.addClickListener(listener);
132
    }
133

    
134
    /**
135
     * {@inheritDoc}
136
     */
137
    @Override
138
    public void setEditButtonEnabled(boolean enabled) {
139
        editButton.setEnabled(enabled);
140
    }
141

    
142

    
143
    /**
144
     * {@inheritDoc}
145
     */
146
    @Override
147
    public void addClickListenerEditEntity(ClickListener listener) {
148
        editButton.addClickListener(listener);
149
    }
150

    
151
    @Override
152
    public void selectNewItem(V bean){
153
        lazySelect.refresh();
154
        lazySelect.setValue(bean);
155
        lazySelect.markAsDirty();
156
    }
157

    
158

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

    
170

    
171
    /**
172
     * {@inheritDoc}
173
     */
174
    @Override
175
    public void setValue(V newFieldValue) throws com.vaadin.data.Property.ReadOnlyException, ConversionException {
176
        lazySelect.refresh();
177
        lazySelect.setValue(newFieldValue);
178
        lazySelect.markAsDirty();
179
    }
180

    
181
    @Override
182
    public void setPropertyDataSource(Property newDataSource) {
183
        lazySelect.setPropertyDataSource(newDataSource);
184
    }
185

    
186
    /**
187
     * {@inheritDoc}
188
     */
189
    @Override
190
    public Property getPropertyDataSource() {
191
        return lazySelect.getPropertyDataSource();
192
    }
193

    
194
    /**
195
     * {@inheritDoc}
196
     */
197
    @Override
198
    public void setReadOnly(boolean readOnly) {
199
        super.setReadOnly(readOnly);
200
        setDeepReadOnly(readOnly, getContent());
201
    }
202

    
203

    
204
}
(9-9/11)