Project

General

Profile

Download (3.32 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.fieldgroup.FieldGroup;
16
import com.vaadin.server.FontAwesome;
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
/**
24
 * @author a.kohlbecker
25
 * @since May 24, 2017
26
 *
27
 */
28
public class ToOneRelatedEntityCombobox<V extends Object> extends CompositeCustomField<V> implements ToOneRelatedEntityField<V> {
29

    
30
    private static final long serialVersionUID = 6277565876657520311L;
31

    
32
    public static final String PRIMARY_STYLE = "v-related-entity-combobox";
33

    
34
    private Class<V> type;
35

    
36
    private CssLayout container = new CssLayout();
37

    
38
    private LazyComboBox<V> lazySelect;
39

    
40
    private Button addButton = new Button(FontAwesome.PLUS);
41
    private Button editButton  = new Button(FontAwesome.EDIT);
42

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

    
55

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

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

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

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

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

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

    
104
    }
105

    
106
    /**
107
     * {@inheritDoc}
108
     */
109
    @Override
110
    public void addClickListenerAddEntity(ClickListener listener) {
111
        addButton.addClickListener(listener);
112
    }
113

    
114
    /**
115
     * {@inheritDoc}
116
     */
117
    @Override
118
    public void addClickListenerEditEntity(ClickListener listener) {
119
        editButton.addClickListener(listener);
120
    }
121

    
122
    @Override
123
    public void selectNewItem(V bean){
124
        lazySelect.refresh();
125
        lazySelect.setValue(bean);
126
        lazySelect.markAsDirty();
127
    }
128

    
129
}
(7-7/9)