Project

General

Profile

Download (4.93 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.ui.Button;
18
import com.vaadin.ui.Button.ClickListener;
19
import com.vaadin.ui.Component;
20
import com.vaadin.ui.CssLayout;
21
import com.vaadin.ui.themes.ValoTheme;
22

    
23
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
24

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

    
33
    private static final long serialVersionUID = 6277565876657520311L;
34

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

    
37
    private Class<V> type;
38

    
39
    private CssLayout container = new CssLayout();
40

    
41
    private ReloadableLazyComboBox<V> lazySelect;
42

    
43
    private Button addButton = ButtonFactory.CREATE_NEW.createButton();
44
    private Button editButton = ButtonFactory.EDIT_ITEM.createButton();
45

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

    
58

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

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

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

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

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

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

    
107
    }
108

    
109
    /**
110
     * reload the selected entity from the persistent storage
111
     */
112
    @Override
113
    public void reload() {
114
        getSelect().reload();
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

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

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

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

    
173

    
174
    /**
175
     * {@inheritDoc}
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
    }
188

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

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

    
206

    
207
}
(15-15/17)