You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RpcDataSourceConnector.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.client.connectors;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import com.vaadin.client.ServerConnector;
  21. import com.vaadin.client.data.AbstractRemoteDataSource;
  22. import com.vaadin.client.extensions.AbstractExtensionConnector;
  23. import com.vaadin.shared.Range;
  24. import com.vaadin.shared.data.DataProviderRpc;
  25. import com.vaadin.shared.data.DataRequestRpc;
  26. import com.vaadin.shared.ui.Connect;
  27. import com.vaadin.v7.shared.ui.grid.GridState;
  28. import elemental.json.Json;
  29. import elemental.json.JsonArray;
  30. import elemental.json.JsonObject;
  31. /**
  32. * Connects a Vaadin server-side container data source to a Grid. This is
  33. * currently implemented as an Extension hardcoded to support a specific
  34. * connector type. This will be changed once framework support for something
  35. * more flexible has been implemented.
  36. *
  37. * @since 7.4
  38. * @author Vaadin Ltd
  39. */
  40. @Connect(com.vaadin.v7.server.communication.data.RpcDataProviderExtension.class)
  41. public class RpcDataSourceConnector extends AbstractExtensionConnector {
  42. /**
  43. * A callback interface to let {@link GridConnector} know that detail
  44. * visibilities might have changed.
  45. *
  46. * @since 7.5.0
  47. * @author Vaadin Ltd
  48. */
  49. interface DetailsListener {
  50. /**
  51. * A request to verify (and correct) the visibility for a row, given
  52. * updated metadata.
  53. *
  54. * @param rowIndex
  55. * the index of the row that should be checked
  56. * @param row
  57. * the row object to check visibility for
  58. * @see GridState#JSONKEY_DETAILS_VISIBLE
  59. */
  60. void reapplyDetailsVisibility(int rowIndex, JsonObject row);
  61. }
  62. public class RpcDataSource extends AbstractRemoteDataSource<JsonObject> {
  63. protected RpcDataSource() {
  64. registerRpc(DataProviderRpc.class, new DataProviderRpc() {
  65. @Override
  66. public void setRowData(int firstRow, JsonArray rowArray) {
  67. ArrayList<JsonObject> rows = new ArrayList<>(
  68. rowArray.length());
  69. for (int i = 0; i < rowArray.length(); i++) {
  70. JsonObject rowObject = rowArray.getObject(i);
  71. rows.add(rowObject);
  72. }
  73. RpcDataSource.this.setRowData(firstRow, rows);
  74. }
  75. @Override
  76. public void removeRowData(int firstRow, int count) {
  77. RpcDataSource.this.removeRowData(firstRow, count);
  78. }
  79. @Override
  80. public void insertRowData(int firstRow, int count) {
  81. RpcDataSource.this.insertRowData(firstRow, count);
  82. }
  83. @Override
  84. public void resetDataAndSize(int size) {
  85. RpcDataSource.this.resetDataAndSize(size);
  86. }
  87. @Override
  88. public void updateRowData(JsonArray rowArray) {
  89. for (int i = 0; i < rowArray.length(); ++i) {
  90. RpcDataSource.this.updateRowData(rowArray.getObject(i));
  91. }
  92. }
  93. });
  94. }
  95. private DataRequestRpc rpcProxy = getRpcProxy(DataRequestRpc.class);
  96. private DetailsListener detailsListener;
  97. private JsonArray droppedRowKeys = Json.createArray();
  98. @Override
  99. protected void requestRows(int firstRowIndex, int numberOfRows,
  100. RequestRowsCallback<JsonObject> callback) {
  101. if (droppedRowKeys.length() > 0) {
  102. rpcProxy.dropRows(droppedRowKeys);
  103. droppedRowKeys = Json.createArray();
  104. }
  105. /*
  106. * If you're looking at this code because you want to learn how to
  107. * use AbstactRemoteDataSource, please look somewhere else instead.
  108. *
  109. * We're not doing things in the conventional way with the callback
  110. * here since Vaadin doesn't directly support RPC with return
  111. * values. We're instead asking the server to push us some data, and
  112. * when we receive pushed data, we just push it along to the
  113. * underlying cache in the same way no matter if it was a genuine
  114. * push or just a result of us requesting rows.
  115. */
  116. Range cached = getCachedRange();
  117. rpcProxy.requestRows(firstRowIndex, numberOfRows, cached.getStart(),
  118. cached.length());
  119. /*
  120. * Show the progress indicator if there is a pending data request
  121. * and some of the visible rows are being requested. The RPC in
  122. * itself will not trigger the indicator since it might just fetch
  123. * some rows in the background to fill the cache.
  124. *
  125. * The indicator will be hidden by the framework when the response
  126. * is received (unless another request is already on its way at that
  127. * point).
  128. */
  129. if (getRequestedAvailability().intersects(
  130. Range.withLength(firstRowIndex, numberOfRows))) {
  131. getConnection().getLoadingIndicator().ensureTriggered();
  132. }
  133. }
  134. @Override
  135. public void ensureAvailability(int firstRowIndex, int numberOfRows) {
  136. super.ensureAvailability(firstRowIndex, numberOfRows);
  137. /*
  138. * We trigger the indicator already at this point since the actual
  139. * RPC will not be sent right away when waiting for the response to
  140. * a previous request.
  141. *
  142. * Only triggering here would not be enough since the check that
  143. * sets isWaitingForData is deferred. We don't want to trigger the
  144. * loading indicator here if we don't know that there is actually a
  145. * request going on since some other bug might then cause the
  146. * loading indicator to not be hidden.
  147. */
  148. if (isWaitingForData()
  149. && !Range.withLength(firstRowIndex, numberOfRows)
  150. .isSubsetOf(getCachedRange())) {
  151. getConnection().getLoadingIndicator().ensureTriggered();
  152. }
  153. }
  154. @Override
  155. public String getRowKey(JsonObject row) {
  156. if (row.hasKey(GridState.JSONKEY_ROWKEY)) {
  157. return row.getString(GridState.JSONKEY_ROWKEY);
  158. } else {
  159. return null;
  160. }
  161. }
  162. public RowHandle<JsonObject> getHandleByKey(Object key) {
  163. JsonObject row = Json.createObject();
  164. row.put(GridState.JSONKEY_ROWKEY, (String) key);
  165. return new RowHandleImpl(row, key);
  166. }
  167. @Override
  168. protected void unpinHandle(RowHandleImpl handle) {
  169. // Row data is no longer available after it has been unpinned.
  170. String key = getRowKey(handle.getRow());
  171. super.unpinHandle(handle);
  172. if (!handle.isPinned()) {
  173. if (indexOfKey(key) == -1) {
  174. // Row out of view has been unpinned. drop it
  175. droppedRowKeys.set(droppedRowKeys.length(), key);
  176. }
  177. }
  178. }
  179. void setDetailsListener(DetailsListener detailsListener) {
  180. this.detailsListener = detailsListener;
  181. }
  182. @Override
  183. protected void setRowData(int firstRowIndex, List<JsonObject> rowData) {
  184. super.setRowData(firstRowIndex, rowData);
  185. /*
  186. * Intercepting details information from the data source, rerouting
  187. * them back to the GridConnector (as a details listener)
  188. */
  189. for (int i = 0; i < rowData.size(); i++) {
  190. detailsListener.reapplyDetailsVisibility(firstRowIndex + i,
  191. rowData.get(i));
  192. }
  193. }
  194. /**
  195. * Updates row data based on row key.
  196. *
  197. * @since 7.6
  198. * @param row
  199. * new row object
  200. */
  201. protected void updateRowData(JsonObject row) {
  202. int index = indexOfKey(getRowKey(row));
  203. if (index >= 0) {
  204. setRowData(index, Collections.singletonList(row));
  205. }
  206. }
  207. @Override
  208. protected void onDropFromCache(int rowIndex, JsonObject row) {
  209. if (!isPinned(row)) {
  210. droppedRowKeys.set(droppedRowKeys.length(), getRowKey(row));
  211. }
  212. }
  213. @Override
  214. protected boolean canFetchData() {
  215. return isEnabled();
  216. }
  217. }
  218. private final RpcDataSource dataSource = new RpcDataSource();
  219. @Override
  220. protected void extend(ServerConnector target) {
  221. GridConnector gridConnector = (GridConnector) target;
  222. dataSource.setDetailsListener(gridConnector.getDetailsListener());
  223. gridConnector.setDataSource(dataSource);
  224. }
  225. }