Project

General

Profile

Download (4.46 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
     * {@inheritDoc}
110
     */
111
    @Override
112
    public void setAddButtonEnabled(boolean enabled) {
113
        addButton.setEnabled(enabled);
114
    }
115

    
116

    
117
    /**
118
     * {@inheritDoc}
119
     */
120
    @Override
121
    public void addClickListenerAddEntity(ClickListener listener) {
122
        addButton.addClickListener(listener);
123
    }
124

    
125
    /**
126
     * {@inheritDoc}
127
     */
128
    @Override
129
    public void setEditButtonEnabled(boolean enabled) {
130
        editButton.setEnabled(enabled);
131
    }
132

    
133

    
134
    /**
135
     * {@inheritDoc}
136
     */
137
    @Override
138
    public void addClickListenerEditEntity(ClickListener listener) {
139
        editButton.addClickListener(listener);
140
    }
141

    
142
    @Override
143
    public void selectNewItem(V bean){
144
        lazySelect.refresh();
145
        lazySelect.setValue(bean);
146
        lazySelect.markAsDirty();
147
    }
148

    
149

    
150
    /**
151
     * Returns always currently selected item by
152
     *
153
     * {@inheritDoc}
154
     */
155
    @Override
156
    public V getValue() {
157
        lazySelect.commit();
158
        return lazySelect.getValue();
159
    }
160

    
161

    
162
    /**
163
     * {@inheritDoc}
164
     */
165
    @Override
166
    public void setValue(V newFieldValue) throws com.vaadin.data.Property.ReadOnlyException, ConversionException {
167
        lazySelect.setValue(newFieldValue);
168
        lazySelect.refresh();
169
    }
170

    
171
    @Override
172
    public void setPropertyDataSource(Property newDataSource) {
173
        lazySelect.setPropertyDataSource(newDataSource);
174
    }
175

    
176
    /**
177
     * {@inheritDoc}
178
     */
179
    @Override
180
    public Property getPropertyDataSource() {
181
        return lazySelect.getPropertyDataSource();
182
    }
183

    
184

    
185

    
186
}
(9-9/11)