aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java
blob: c4405f06b12c5663fdc7480c69ed59a6bc4217f6 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright 2000-2016 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;

import java.lang.reflect.Method;
import java.util.Objects;
import java.util.Optional;

import com.vaadin.data.HasValue;
import com.vaadin.event.selection.SingleSelectionChangeEvent;
import com.vaadin.event.selection.SingleSelectionListener;
import com.vaadin.server.data.DataCommunicator;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.data.selection.SelectionModel.Single;
import com.vaadin.shared.data.selection.SelectionServerRpc;
import com.vaadin.shared.ui.AbstractSingleSelectState;
import com.vaadin.util.ReflectTools;

/**
 * An abstract base class for listing components that only support single
 * selection and no lazy loading of data items.
 *
 * @author Vaadin Ltd.
 *
 * @param <T>
 *            the item date type
 *
 * @see com.vaadin.shared.data.selection.SelectionModel.Single
 *
 * @since 8.0
 */
public abstract class AbstractSingleSelect<T> extends
        AbstractListing<T, AbstractSingleSelect<T>.AbstractSingleSelection>
        implements HasValue<T> {

    /**
     * A base class for single selection model implementations. Listens to
     * {@code SelectionServerRpc} invocations to track selection requests by the
     * client. Maintaining the selection state and communicating it from the
     * server to the client is the responsibility of the implementing class.
     */
    public abstract class AbstractSingleSelection
            implements SelectionModel.Single<T> {

        /**
         * Creates a new {@code SimpleSingleSelection} instance.
         */
        public AbstractSingleSelection() {
            registerRpc(new SelectionServerRpc() {

                @Override
                public void select(String key) {
                    setSelectedFromClient(key);
                }

                @Override
                public void deselect(String key) {
                    if (isKeySelected(key)) {
                        setSelectedFromClient(null);
                    }
                }
            });
        }

        @Override
        public void deselect(T item) {
            Objects.requireNonNull(item, "deselected item cannot be null");
            if (isSelected(item)) {
                setSelectedFromServer(null);
            }
        }

        @Override
        public void select(T item) {
            Objects.requireNonNull(item, "selected item cannot be null");
            setSelectedFromServer(item);
        }

        /**
         * Returns the communication key of the selected item or {@code null} if
         * no item is selected.
         *
         * @return the key of the selected item if any, {@code null} otherwise.
         */
        protected abstract String getSelectedKey();

        /**
         * Sets the selected item based on the given communication key. If the
         * key is {@code null}, clears the current selection if any.
         *
         * @param key
         *            the key of the selected item or {@code null} to clear
         *            selection
         */
        protected abstract void doSetSelectedKey(String key);

        /**
         * Sets the selection based on a client request. Does nothing if the
         * select component is {@linkplain Component#isReadOnly()} or if the
         * selection would not change. Otherwise updates the selection and fires
         * a selection change event with {@code isUserOriginated == true}.
         *
         * @param key
         *            the key of the item to select or {@code null} to clear
         *            selection
         */
        protected void setSelectedFromClient(String key) {
            if (isReadOnly()) {
                return;
            }
            if (isKeySelected(key)) {
                return;
            }

            doSetSelectedKey(key);
            fireEvent(new SingleSelectionChangeEvent<>(
                    AbstractSingleSelect.this, true));
        }

        /**
         * Sets the selection based on server API call. Does nothing if the
         * selection would not change; otherwise updates the selection and fires
         * a selection change event with {@code isUserOriginated == false}.
         *
         * @param item
         *            the item to select or {@code null} to clear selection
         */
        protected void setSelectedFromServer(T item) {
            // TODO creates a key if item not in data source
            String key = itemToKey(item);

            if (isKeySelected(key) || isSelected(item)) {
                return;
            }

            doSetSelectedKey(key);
            fireEvent(new SingleSelectionChangeEvent<>(
                    AbstractSingleSelect.this, false));
        }

        /**
         * Returns whether the given key maps to the currently selected item.
         *
         * @param key
         *            the key to test or {@code null} to test whether nothing is
         *            selected
         * @return {@code true} if the key equals the key of the currently
         *         selected item (or {@code null} if no selection),
         *         {@code false} otherwise.
         */
        protected boolean isKeySelected(String key) {
            return Objects.equals(key, getSelectedKey());
        }

        /**
         * Returns the communication key assigned to the given item.
         *
         * @param item
         *            the item whose key to return
         * @return the assigned key
         */
        protected String itemToKey(T item) {
            if (item == null) {
                return null;
            } else {
                // TODO creates a key if item not in data source
                return getDataCommunicator().getKeyMapper().key(item);
            }
        }

        /**
         * Returns the item that the given key is assigned to, or {@code null}
         * if there is no such item.
         *
         * @param key
         *            the key whose item to return
         * @return the associated item if any, {@code null} otherwise.
         */
        protected T keyToItem(String key) {
            return getDataCommunicator().getKeyMapper().get(key);
        }
    }

    /**
     * A simple single selection model using the {@code AbstractSingleSelect}
     * RPC and state to communicate with the client. Has no client-side
     * counterpart; the listing connector is expected to handle selection.
     * Client-to-server selection is passed via {@link SelectionServerRpc} and
     * server-to-client via {@link AbstractSingleSelectState#selectedItemKey}.
     */
    protected class SimpleSingleSelection extends AbstractSingleSelection {

        /**
         * Creates a new {@code SimpleSingleSelection}.
         */
        public SimpleSingleSelection() {
        }

        @Override
        public Optional<T> getSelectedItem() {
            return Optional.ofNullable(keyToItem(getSelectedKey()));
        }

        @Override
        protected String getSelectedKey() {
            return getState(false).selectedItemKey;
        }

        @Override
        protected void doSetSelectedKey(String key) {
            getState().selectedItemKey = key;
        }
    }

    @Deprecated
    private static final Method SELECTION_CHANGE_METHOD = ReflectTools
            .findMethod(SingleSelectionListener.class, "accept",
                    SingleSelectionChangeEvent.class);

    /**
     * Creates a new {@code AbstractListing} with a default data communicator.
     * <p>
     * <strong>Note:</strong> This constructor does not set a selection model
     * for the new listing. The invoking constructor must explicitly call
     * {@link #setSelectionModel(SelectionModel)} with an
     * {@link AbstractSingleSelect.AbstractSingleSelection} .
     */
    protected AbstractSingleSelect() {
    }

    /**
     * Creates a new {@code AbstractSingleSelect} with the given custom data
     * communicator.
     * <p>
     * <strong>Note:</strong> This method is for creating an
     * {@code AbstractSingleSelect} with a custom communicator. In the common
     * case {@link AbstractSingleSelect#AbstractSingleSelect()} should be used.
     * <p>
     * <strong>Note:</strong> This constructor does not set a selection model
     * for the new listing. The invoking constructor must explicitly call
     * {@link #setSelectionModel(SelectionModel)} with an
     * {@link AbstractSingleSelect.AbstractSingleSelection} .
     *
     * @param dataCommunicator
     *            the data communicator to use, not null
     */
    protected AbstractSingleSelect(DataCommunicator<T> dataCommunicator) {
        super(dataCommunicator);
    }

    /**
     * Adds a selection listener to this select. The listener is called when the
     * value of this select is changed either by the user or programmatically.
     *
     * @param listener
     *            the value change listener, not null
     * @return a registration for the listener
     */
    public Registration addSelectionListener(
            SingleSelectionListener<T> listener) {
        addListener(SingleSelectionChangeEvent.class, listener,
                SELECTION_CHANGE_METHOD);
        return () -> removeListener(SingleSelectionChangeEvent.class, listener);
    }

    /**
     * Returns the currently selected item, or an empty optional if no item is
     * selected.
     *
     * @return an optional of the selected item if any, an empty optional
     *         otherwise
     */
    public Optional<T> getSelectedItem() {
        return getSelectionModel().getSelectedItem();
    }

    /**
     * Sets the current selection to the given item or clears selection if given
     * {@code null}.
     *
     * @param item
     *            the item to select or {@code null} to clear selection
     */
    public void setSelectedItem(T item) {
        getSelectionModel().setSelectedItem(item);
    }

    /**
     * Returns the current value of this object which is the currently selected
     * item.
     * <p>
     * The call is delegated to {@link #getSelectedItem()}
     *
     * @return the current selection, may be {@code null}
     * 
     * @see #getSelectedItem()
     * @see Single#getSelectedItem
     */
    @Override
    public T getValue() {
        return getSelectedItem().orElse(null);
    }

    /**
     * Sets the value of this object which is an item to select. If the new
     * value is not equal to {@code getValue()}, fires a value change event. If
     * value is {@code null} then it deselects currently selected item.
     * <p>
     * The call is delegated to {@link #setSelectedItem(Object)}.
     * 
     * @see #setSelectedItem(Object)
     * @see Single#setSelectedItem(Object)
     *
     * @param value
     *            the item to select or {@code null} to clear selection
     */
    @Override
    public void setValue(T value) {
        setSelectedItem(value);
    }

    @Override
    public Registration addValueChangeListener(
            HasValue.ValueChangeListener<T> listener) {
        return addSelectionListener(event -> listener.accept(
                new ValueChangeEvent<>(this, event.isUserOriginated())));
    }

    @Override
    protected AbstractSingleSelectState getState() {
        return (AbstractSingleSelectState) super.getState();
    }

    @Override
    protected AbstractSingleSelectState getState(boolean markAsDirty) {
        return (AbstractSingleSelectState) super.getState(markAsDirty);
    }
}