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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 2000-2014 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;
  17. import java.util.ArrayList;
  18. import com.google.gwt.json.client.JSONArray;
  19. import com.google.gwt.json.client.JSONObject;
  20. import com.google.gwt.json.client.JSONParser;
  21. import com.google.gwt.json.client.JSONString;
  22. import com.google.gwt.json.client.JSONValue;
  23. import com.vaadin.client.ServerConnector;
  24. import com.vaadin.client.data.AbstractRemoteDataSource;
  25. import com.vaadin.client.extensions.AbstractExtensionConnector;
  26. import com.vaadin.shared.data.DataProviderRpc;
  27. import com.vaadin.shared.data.DataProviderState;
  28. import com.vaadin.shared.data.DataRequestRpc;
  29. import com.vaadin.shared.ui.Connect;
  30. import com.vaadin.shared.ui.grid.GridState;
  31. import com.vaadin.shared.ui.grid.Range;
  32. /**
  33. * Connects a Vaadin server-side container data source to a Grid. This is
  34. * currently implemented as an Extension hardcoded to support a specific
  35. * connector type. This will be changed once framework support for something
  36. * more flexible has been implemented.
  37. *
  38. * @since
  39. * @author Vaadin Ltd
  40. */
  41. @Connect(com.vaadin.data.RpcDataProviderExtension.class)
  42. public class RpcDataSourceConnector extends AbstractExtensionConnector {
  43. public class RpcDataSource extends AbstractRemoteDataSource<JSONObject> {
  44. protected RpcDataSource() {
  45. registerRpc(DataProviderRpc.class, new DataProviderRpc() {
  46. @Override
  47. public void setRowData(int firstRow, String rowsJson) {
  48. JSONValue parsedJson = JSONParser.parseStrict(rowsJson);
  49. JSONArray rowArray = parsedJson.isArray();
  50. assert rowArray != null : "Was unable to parse JSON into an array: "
  51. + parsedJson;
  52. ArrayList<JSONObject> rows = new ArrayList<JSONObject>(
  53. rowArray.size());
  54. for (int i = 0; i < rowArray.size(); i++) {
  55. JSONValue rowValue = rowArray.get(i);
  56. JSONObject rowObject = rowValue.isObject();
  57. assert rowObject != null : "Was unable to parse JSON into an object: "
  58. + rowValue;
  59. rows.add(rowObject);
  60. }
  61. dataSource.setRowData(firstRow, rows);
  62. }
  63. @Override
  64. public void removeRowData(int firstRow, int count) {
  65. dataSource.removeRowData(firstRow, count);
  66. }
  67. @Override
  68. public void insertRowData(int firstRow, int count) {
  69. dataSource.insertRowData(firstRow, count);
  70. }
  71. @Override
  72. public void resetDataAndSize(int size) {
  73. dataSource.resetDataAndSize(size);
  74. }
  75. });
  76. }
  77. private DataRequestRpc rpcProxy = getRpcProxy(DataRequestRpc.class);
  78. @Override
  79. protected void requestRows(int firstRowIndex, int numberOfRows) {
  80. Range cached = getCachedRange();
  81. rpcProxy.requestRows(firstRowIndex, numberOfRows,
  82. cached.getStart(), cached.length());
  83. }
  84. @Override
  85. public String getRowKey(JSONObject row) {
  86. JSONString string = row.get(GridState.JSONKEY_ROWKEY).isString();
  87. if (string != null) {
  88. return string.stringValue();
  89. } else {
  90. return null;
  91. }
  92. }
  93. public RowHandle<JSONObject> getHandleByKey(Object key) {
  94. JSONObject row = new JSONObject();
  95. row.put(GridState.JSONKEY_ROWKEY, new JSONString((String) key));
  96. return new RowHandleImpl(row, key);
  97. }
  98. @Override
  99. public int size() {
  100. return getState().containerSize;
  101. }
  102. @Override
  103. protected void pinHandle(RowHandleImpl handle) {
  104. // Server only knows if something is pinned or not. No need to pin
  105. // multiple times.
  106. boolean pinnedBefore = handle.isPinned();
  107. super.pinHandle(handle);
  108. if (!pinnedBefore) {
  109. rpcProxy.setPinned(getRowKey(handle.getRow()), true);
  110. }
  111. }
  112. @Override
  113. protected void unpinHandle(RowHandleImpl handle) {
  114. // Row data is no longer available after it has been unpinned.
  115. String key = getRowKey(handle.getRow());
  116. super.unpinHandle(handle);
  117. if (!handle.isPinned()) {
  118. rpcProxy.setPinned(key, false);
  119. }
  120. }
  121. }
  122. private final RpcDataSource dataSource = new RpcDataSource();
  123. @Override
  124. protected void extend(ServerConnector target) {
  125. ((GridConnector) target).setDataSource(dataSource);
  126. }
  127. @Override
  128. public DataProviderState getState() {
  129. return (DataProviderState) super.getState();
  130. }
  131. }