Project

General

Profile

Download (4.89 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.fieldgroup.FieldGroup;
16
import com.vaadin.data.util.converter.Converter.ConversionException;
17
import com.vaadin.server.FontAwesome;
18
import com.vaadin.ui.Button;
19
import com.vaadin.ui.Button.ClickListener;
20
import com.vaadin.ui.Component;
21
import com.vaadin.ui.CssLayout;
22
import com.vaadin.ui.themes.ValoTheme;
23

    
24
/**
25
 * @author a.kohlbecker
26
 * @since May 24, 2017
27
 *
28
 */
29
public class ToOneRelatedEntityCombobox<V extends Object> extends CompositeCustomField<V>
30
    implements ToOneRelatedEntityField<V>, ReloadableSelect, EntitySupport<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 ReloadableLazyComboBox<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 ReloadableLazyComboBox<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 ReloadableLazyComboBox<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
    @Override
112
    public void reload() {
113
        getSelect().reload();
114
    }
115

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

    
124

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

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

    
141

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

    
150

    
151
    @Override
152
    public void replaceEntityValue(V bean){
153
        lazySelect.replaceEntityValue(bean);
154
    }
155

    
156
    @Override
157
    public void selectNewItem(V bean){
158
        setValue(bean);
159
    }
160

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

    
172

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

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

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

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

    
205

    
206
}
(12-12/14)