/
NativeSelectConnector.java
102 lines (88 loc) · 3.32 KB
/
NativeSelectConnector.java
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
/*
* 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.client.ui.nativeselect;
import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.connectors.AbstractListingConnector;
import com.vaadin.client.data.DataSource;
import com.vaadin.client.ui.VNativeSelect;
import com.vaadin.shared.Range;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.ui.Connect;
import elemental.json.JsonObject;
/**
* The client-side connector for the {@code NativeSelect} component.
*
* @author Vaadin Ltd.
*
* @see com.vaadin.ui.NativeSelect
* @see com.vaadin.client.ui.VNativeSelect
*
* @since 8.0
*/
@Connect(com.vaadin.ui.NativeSelect.class)
public class NativeSelectConnector extends
AbstractListingConnector<SelectionModel<?>> {
private Registration dataChangeRegistration;
@Override
public VNativeSelect getWidget() {
return (VNativeSelect) super.getWidget();
}
@Override
public void setDataSource(DataSource<JsonObject> dataSource) {
if (dataChangeRegistration != null) {
dataChangeRegistration.remove();
}
dataChangeRegistration = dataSource.addDataChangeHandler(
this::onDataChange);
super.setDataSource(dataSource);
}
@OnStateChange("readOnly")
@SuppressWarnings("deprecation")
void updateWidgetReadOnly() {
getWidget().setEnabled(isEnabled() && !isReadOnly());
}
/**
* A data change handler registered to the data source. Updates the data
* items and selection status when the data source notifies of new changes
* from the server side.
*
* @param range
* the new range of data items
*/
private void onDataChange(Range range) {
assert range.getStart() == 0 && range.getEnd() == getDataSource()
.size() : "NativeSelect only supports full updates, but got range "
+ range;
final VNativeSelect select = getWidget();
final int itemCount = select.getItemCount();
for (int i = range.getStart(); i < range.getEnd(); i++) {
final JsonObject row = getDataSource().getRow(i);
if (i < itemCount) {
// Reuse and update an existing item
select.setItemText(i, getRowData(row).asString());
select.setValue(i, getRowKey(row));
} else {
// Add new items if the new dataset is larger than the old
select.addItem(getRowData(row).asString(), getRowKey(row));
}
}
for (int i = select.getItemCount() - 1; i >= range.getEnd(); i--) {
// Remove extra items if the new dataset is smaller than the old
select.removeItem(i);
}
}
}