Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DataCommunicatorConnector.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.client.connectors.data;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import com.vaadin.client.ServerConnector;
  22. import com.vaadin.client.data.AbstractRemoteDataSource;
  23. import com.vaadin.client.data.DataSource;
  24. import com.vaadin.client.extensions.AbstractExtensionConnector;
  25. import com.vaadin.server.data.DataCommunicator;
  26. import com.vaadin.shared.data.DataCommunicatorClientRpc;
  27. import com.vaadin.shared.data.DataCommunicatorConstants;
  28. import com.vaadin.shared.data.DataRequestRpc;
  29. import com.vaadin.shared.ui.Connect;
  30. import elemental.json.Json;
  31. import elemental.json.JsonArray;
  32. import elemental.json.JsonObject;
  33. /**
  34. * A connector for DataCommunicator class.
  35. *
  36. * @since
  37. */
  38. @Connect(DataCommunicator.class)
  39. public class DataCommunicatorConnector extends AbstractExtensionConnector {
  40. /**
  41. * Client-side {@link DataSource} implementation to be used with
  42. * {@link DataCommunicator}.
  43. */
  44. public class VaadinDataSource extends AbstractRemoteDataSource<JsonObject> {
  45. private Set<String> droppedKeys = new HashSet<>();
  46. protected VaadinDataSource() {
  47. registerRpc(DataCommunicatorClientRpc.class,
  48. new DataCommunicatorClientRpc() {
  49. @Override
  50. public void reset(int size) {
  51. resetDataAndSize(size);
  52. }
  53. @Override
  54. public void setData(int firstIndex, JsonArray data) {
  55. ArrayList<JsonObject> rows = new ArrayList<>(
  56. data.length());
  57. for (int i = 0; i < data.length(); i++) {
  58. JsonObject rowObject = data.getObject(i);
  59. rows.add(rowObject);
  60. }
  61. setRowData(firstIndex, rows);
  62. }
  63. @Override
  64. public void updateData(JsonArray data) {
  65. for (int i = 0; i < data.length(); ++i) {
  66. updateRowData(data.getObject(i));
  67. }
  68. }
  69. });
  70. }
  71. @Override
  72. protected void requestRows(int firstRowIndex, int numberOfRows,
  73. RequestRowsCallback<JsonObject> callback) {
  74. getRpcProxy(DataRequestRpc.class).requestRows(firstRowIndex,
  75. numberOfRows, 0, 0);
  76. JsonArray dropped = Json.createArray();
  77. int i = 0;
  78. for (String key : droppedKeys) {
  79. dropped.set(i++, key);
  80. }
  81. droppedKeys.clear();
  82. getRpcProxy(DataRequestRpc.class).dropRows(dropped);
  83. }
  84. @Override
  85. public String getRowKey(JsonObject row) {
  86. return row.getString(DataCommunicatorConstants.KEY);
  87. }
  88. @Override
  89. protected void onDropFromCache(int rowIndex, JsonObject removed) {
  90. droppedKeys.add(getRowKey(removed));
  91. super.onDropFromCache(rowIndex, removed);
  92. }
  93. /**
  94. * Updates row data based on row key.
  95. *
  96. * @param row
  97. * new row object
  98. */
  99. protected void updateRowData(JsonObject row) {
  100. int index = indexOfKey(getRowKey(row));
  101. if (index >= 0) {
  102. setRowData(index, Collections.singletonList(row));
  103. }
  104. }
  105. }
  106. private DataSource<JsonObject> ds = new VaadinDataSource();
  107. @Override
  108. protected void extend(ServerConnector target) {
  109. ServerConnector parent = getParent();
  110. if (parent instanceof HasDataSource) {
  111. ((HasDataSource) parent).setDataSource(ds);
  112. } else {
  113. assert false : "Parent not implementing HasDataSource";
  114. }
  115. }
  116. }