/
RpcDataSourceConnector.java
253 lines (221 loc) · 9.36 KB
/
RpcDataSourceConnector.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
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
/*
* 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.client.connectors;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.vaadin.client.ServerConnector;
import com.vaadin.client.data.AbstractRemoteDataSource;
import com.vaadin.client.extensions.AbstractExtensionConnector;
import com.vaadin.shared.data.DataProviderRpc;
import com.vaadin.shared.data.DataRequestRpc;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.grid.GridState;
import com.vaadin.shared.ui.grid.Range;
import elemental.json.Json;
import elemental.json.JsonArray;
import elemental.json.JsonObject;
/**
* Connects a Vaadin server-side container data source to a Grid. This is
* currently implemented as an Extension hardcoded to support a specific
* connector type. This will be changed once framework support for something
* more flexible has been implemented.
*
* @since 7.4
* @author Vaadin Ltd
*/
@Connect(com.vaadin.data.RpcDataProviderExtension.class)
public class RpcDataSourceConnector extends AbstractExtensionConnector {
/**
* A callback interface to let {@link GridConnector} know that detail
* visibilities might have changed.
*
* @since 7.5.0
* @author Vaadin Ltd
*/
interface DetailsListener {
/**
* A request to verify (and correct) the visibility for a row, given
* updated metadata.
*
* @param rowIndex
* the index of the row that should be checked
* @param row
* the row object to check visibility for
* @see GridState#JSONKEY_DETAILS_VISIBLE
*/
void reapplyDetailsVisibility(int rowIndex, JsonObject row);
}
public class RpcDataSource extends AbstractRemoteDataSource<JsonObject> {
protected RpcDataSource() {
registerRpc(DataProviderRpc.class, new DataProviderRpc() {
@Override
public void setRowData(int firstRow, JsonArray rowArray) {
ArrayList<JsonObject> rows = new ArrayList<JsonObject>(
rowArray.length());
for (int i = 0; i < rowArray.length(); i++) {
JsonObject rowObject = rowArray.getObject(i);
rows.add(rowObject);
}
RpcDataSource.this.setRowData(firstRow, rows);
}
@Override
public void removeRowData(int firstRow, int count) {
RpcDataSource.this.removeRowData(firstRow, count);
}
@Override
public void insertRowData(int firstRow, int count) {
RpcDataSource.this.insertRowData(firstRow, count);
}
@Override
public void resetDataAndSize(int size) {
RpcDataSource.this.resetDataAndSize(size);
}
@Override
public void updateRowData(JsonArray rowArray) {
for (int i = 0; i < rowArray.length(); ++i) {
RpcDataSource.this.updateRowData(rowArray.getObject(i));
}
}
});
}
private DataRequestRpc rpcProxy = getRpcProxy(DataRequestRpc.class);
private DetailsListener detailsListener;
private JsonArray droppedRowKeys = Json.createArray();
@Override
protected void requestRows(int firstRowIndex, int numberOfRows,
RequestRowsCallback<JsonObject> callback) {
if (droppedRowKeys.length() > 0) {
rpcProxy.dropRows(droppedRowKeys);
droppedRowKeys = Json.createArray();
}
/*
* If you're looking at this code because you want to learn how to
* use AbstactRemoteDataSource, please look somewhere else instead.
*
* We're not doing things in the conventional way with the callback
* here since Vaadin doesn't directly support RPC with return
* values. We're instead asking the server to push us some data, and
* when we receive pushed data, we just push it along to the
* underlying cache in the same way no matter if it was a genuine
* push or just a result of us requesting rows.
*/
Range cached = getCachedRange();
rpcProxy.requestRows(firstRowIndex, numberOfRows,
cached.getStart(), cached.length());
/*
* Show the progress indicator if there is a pending data request
* and some of the visible rows are being requested. The RPC in
* itself will not trigger the indicator since it might just fetch
* some rows in the background to fill the cache.
*
* The indicator will be hidden by the framework when the response
* is received (unless another request is already on its way at that
* point).
*/
if (getRequestedAvailability().intersects(
Range.withLength(firstRowIndex, numberOfRows))) {
getConnection().getLoadingIndicator().ensureTriggered();
}
}
@Override
public void ensureAvailability(int firstRowIndex, int numberOfRows) {
super.ensureAvailability(firstRowIndex, numberOfRows);
/*
* We trigger the indicator already at this point since the actual
* RPC will not be sent right away when waiting for the response to
* a previous request.
*
* Only triggering here would not be enough since the check that
* sets isWaitingForData is deferred. We don't want to trigger the
* loading indicator here if we don't know that there is actually a
* request going on since some other bug might then cause the
* loading indicator to not be hidden.
*/
if (isWaitingForData()
&& !Range.withLength(firstRowIndex, numberOfRows)
.isSubsetOf(getCachedRange())) {
getConnection().getLoadingIndicator().ensureTriggered();
}
}
@Override
public String getRowKey(JsonObject row) {
if (row.hasKey(GridState.JSONKEY_ROWKEY)) {
return row.getString(GridState.JSONKEY_ROWKEY);
} else {
return null;
}
}
public RowHandle<JsonObject> getHandleByKey(Object key) {
JsonObject row = Json.createObject();
row.put(GridState.JSONKEY_ROWKEY, (String) key);
return new RowHandleImpl(row, key);
}
@Override
protected void unpinHandle(RowHandleImpl handle) {
// Row data is no longer available after it has been unpinned.
String key = getRowKey(handle.getRow());
super.unpinHandle(handle);
if (!handle.isPinned()) {
if (indexOfKey(key) == -1) {
// Row out of view has been unpinned. drop it
droppedRowKeys.set(droppedRowKeys.length(), key);
}
}
}
void setDetailsListener(DetailsListener detailsListener) {
this.detailsListener = detailsListener;
}
@Override
protected void setRowData(int firstRowIndex, List<JsonObject> rowData) {
super.setRowData(firstRowIndex, rowData);
/*
* Intercepting details information from the data source, rerouting
* them back to the GridConnector (as a details listener)
*/
for (int i = 0; i < rowData.size(); i++) {
detailsListener.reapplyDetailsVisibility(firstRowIndex + i,
rowData.get(i));
}
}
/**
* Updates row data based on row key.
*
* @since 7.6
* @param row
* new row object
*/
protected void updateRowData(JsonObject row) {
int index = indexOfKey(getRowKey(row));
if (index >= 0) {
setRowData(index, Collections.singletonList(row));
}
}
@Override
protected void onDropFromCache(int rowIndex, JsonObject row) {
if (!((RowHandleImpl) getHandle(row)).isPinned()) {
droppedRowKeys.set(droppedRowKeys.length(), getRowKey(row));
}
}
}
private final RpcDataSource dataSource = new RpcDataSource();
@Override
protected void extend(ServerConnector target) {
GridConnector gridConnector = (GridConnector) target;
dataSource.setDetailsListener(gridConnector.getDetailsListener());
gridConnector.setDataSource(dataSource);
}
}