Project

General

Profile

Download (3.97 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 java.util.Optional;
12

    
13
import com.vaadin.data.Container;
14
import com.vaadin.data.Property;
15
import com.vaadin.data.fieldgroup.FieldGroup;
16
import com.vaadin.ui.Button;
17
import com.vaadin.ui.Button.ClickListener;
18
import com.vaadin.ui.Component;
19
import com.vaadin.ui.CssLayout;
20
import com.vaadin.ui.ListSelect;
21
import com.vaadin.ui.themes.ValoTheme;
22

    
23
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
24
import eu.etaxonomy.cdm.vaadin.event.NestedButtonStateUpdater;
25

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

    
33
    private static final long serialVersionUID = 6277565876657520311L;
34

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

    
37
    private Class<V> type;
38

    
39
    private CssLayout container = new CssLayout();
40

    
41
    private ListSelect select;
42

    
43
    NestedButtonStateUpdater<V> buttonUpdater;
44

    
45
    private Button addButton = ButtonFactory.ADD_ITEM.createButton();
46
    private Button editButton  = ButtonFactory.EDIT_ITEM.createButton();
47

    
48
    public ToOneRelatedEntityListSelect(String caption, Class<V> type, Container dataSource){
49
        this.type = type;
50
        setCaption(caption);
51
        select = new ListSelect(caption, dataSource);
52
        addStyledComponents(select, addButton, editButton);
53
        addSizedComponent(select);
54
    }
55

    
56

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

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

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

    
84
    /**
85
     * {@inheritDoc}
86
     */
87
    @Override
88
    public Optional<FieldGroup> getFieldGroup() {
89
        return Optional.empty();
90
    }
91

    
92
    /**
93
     * @return the select
94
     * @deprecated list specific method should not be in
95
     * the interface
96
     */
97
    @Deprecated
98
    public ListSelect getSelect() {
99
        return select;
100
    }
101

    
102
    /**
103
     * {@inheritDoc}
104
     */
105
    @Override
106
    public void setPropertyDataSource(Property newDataSource) {
107
        select.setPropertyDataSource(newDataSource);
108
        if(buttonUpdater != null){
109
            buttonUpdater.updateButtons((V) select.getValue());
110
        }
111
    }
112

    
113
    @Override
114
    public Property getPropertyDataSource() {
115
        return select.getPropertyDataSource();
116
    }
117

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

    
126
    /**
127
     * {@inheritDoc}
128
     */
129
    @Override
130
    public void setAddButtonEnabled(boolean enabled) {
131
        addButton.setEnabled(enabled);
132
    }
133

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

    
142
    /**
143
     * {@inheritDoc}
144
     */
145
    @Override
146
    public void setEditButtonEnabled(boolean enabled) {
147
        editButton.setEnabled(enabled);
148
    }
149

    
150
    /**
151
     * {@inheritDoc}
152
     */
153
    @Override
154
    public void selectNewItem(V bean) {
155

    
156
      select.addItem(bean);
157
      select.select(bean);
158
    }
159

    
160

    
161
    /**
162
     * {@inheritDoc}
163
     */
164
    @Override
165
    public void setNestedButtonStateUpdater(NestedButtonStateUpdater<V> buttonUpdater) {
166
        this.buttonUpdater = buttonUpdater;
167
        select.addValueChangeListener(buttonUpdater);
168
    }
169

    
170

    
171
}
(17-17/19)