aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/ui/proto/ListBox.java
blob: bc952c1617d49eee0d8a1af64d2c09f3c981f862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright 2000-2014 Vaadin Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.ui.proto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.vaadin.server.communication.data.typed.CollectionDataSource;
import com.vaadin.server.communication.data.typed.DataProvider;
import com.vaadin.server.communication.data.typed.DataSource;
import com.vaadin.server.communication.data.typed.TypedDataGenerator;
import com.vaadin.shared.ui.proto.listbox.ListBoxConstants;
import com.vaadin.shared.ui.proto.listbox.ListBoxSelectRpc;
import com.vaadin.ui.AbstractComponent;

import elemental.json.JsonObject;

/**
 * ListBox is a simple select component typed to a data type.
 * 
 * @since
 */
public class ListBox<T> extends AbstractComponent {

    private static class DefaultNameProvider<T> implements NameProvider<T> {
        @Override
        public String getName(T value) {
            return value.toString();
        }
    }

    public interface NameProvider<T> extends Serializable {
        String getName(T value);
    }

    public interface ValueChange<T> extends Serializable {
        void valueChange(T value);
    }

    private T selected;
    private DataProvider<T> dataProvider;
    private Set<ValueChange<T>> listeners = new LinkedHashSet<ValueChange<T>>();

    public ListBox(Collection<T> data) {
        this(new CollectionDataSource<T>(data), new DefaultNameProvider<T>());
    }

    public ListBox(DataSource<T> data) {
        this(data, new DefaultNameProvider<T>());
    }

    public ListBox(Collection<T> data, NameProvider<T> nameProvider) {
        this(new CollectionDataSource<T>(data), nameProvider);
    }

    public ListBox(DataSource<T> data, NameProvider<T> nameProvider) {
        setDataSource(data, nameProvider);
        registerRpc(new ListBoxSelectRpc() {

            @Override
            public void select(String key) {
                if (key != null && !key.isEmpty()) {
                    // Key mapper gives the object represented by the given
                    // key communicated to the client-side
                    selected = dataProvider.getKeyMapper().get(key);
                    fireSelectionChange(selected);
                } else {
                    selected = null;
                }
            }
        });
    }

    public ListBox() {
        this(new CollectionDataSource<T>(new ArrayList<T>()),
                new DefaultNameProvider<T>());

    }

    public void setDataSource(Collection<T> data) {
        setDataSource(data, new DefaultNameProvider<T>());
    }

    public void setDataSource(DataSource<T> data) {
        setDataSource(data, new DefaultNameProvider<T>());
    }

    public void setDataSource(Collection<T> data, NameProvider<T> nameProvider) {
        setDataSource(new CollectionDataSource<T>(data), nameProvider);
    }

    public void setDataSource(DataSource<T> data,
            final NameProvider<T> nameProvider) {
        dataProvider = DataProvider.create(data, this);
        dataProvider.addDataGenerator(new TypedDataGenerator<T>() {

            @Override
            public void generateData(T bean, JsonObject rowData) {
                rowData.put(ListBoxConstants.NAME_KEY,
                        nameProvider.getName(bean));
            }

            @Override
            public void destroyData(T bean) {
                // No data needs to be destroyed.
            }
        });
    }

    public void addValueChangeListener(ValueChange<T> listener) {
        listeners.add(listener);
    }

    public void removeValueChangeListener(ValueChange<T> listener) {
        listeners.remove(listener);
    }

    public T getSelected() {
        return selected;
    }

    protected void fireSelectionChange(T selected) {
        List<ValueChange<T>> set = new ArrayList<ValueChange<T>>(listeners);
        for (ValueChange<T> listener : set) {
            listener.valueChange(selected);
        }
    }
}