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.

RpcDataProviderExtension.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2000-2013 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.data;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.List;
  20. import com.vaadin.data.Container.Indexed;
  21. import com.vaadin.server.AbstractExtension;
  22. import com.vaadin.shared.data.DataProviderRpc;
  23. import com.vaadin.shared.data.DataProviderState;
  24. import com.vaadin.shared.data.DataRequestRpc;
  25. import com.vaadin.ui.components.grid.Grid;
  26. /**
  27. * Provides Vaadin server-side container data source to a
  28. * {@link com.vaadin.client.ui.grid.GridConnector}. This is currently
  29. * implemented as an Extension hardcoded to support a specific connector type.
  30. * This will be changed once framework support for something more flexible has
  31. * been implemented.
  32. *
  33. * @since 7.2
  34. * @author Vaadin Ltd
  35. */
  36. public class RpcDataProviderExtension extends AbstractExtension {
  37. private final Indexed container;
  38. /**
  39. * Creates a new data provider using the given container.
  40. *
  41. * @param container
  42. * the container to make available
  43. */
  44. public RpcDataProviderExtension(Indexed container) {
  45. this.container = container;
  46. // TODO support for reacting to events from the container added later
  47. registerRpc(new DataRequestRpc() {
  48. @Override
  49. public void requestRows(int firstRow, int numberOfRows) {
  50. pushRows(firstRow, numberOfRows);
  51. }
  52. });
  53. getState().containerSize = container.size();
  54. }
  55. private void pushRows(int firstRow, int numberOfRows) {
  56. List<?> itemIds = container.getItemIds(firstRow, numberOfRows);
  57. Collection<?> propertyIds = container.getContainerPropertyIds();
  58. List<String[]> rows = new ArrayList<String[]>(itemIds.size());
  59. for (Object itemId : itemIds) {
  60. Item item = container.getItem(itemId);
  61. String[] row = new String[propertyIds.size()];
  62. int i = 0;
  63. for (Object propertyId : propertyIds) {
  64. Object value = item.getItemProperty(propertyId).getValue();
  65. String stringValue = String.valueOf(value);
  66. row[i++] = stringValue;
  67. }
  68. rows.add(row);
  69. }
  70. getRpcProxy(DataProviderRpc.class).setRowData(firstRow, rows);
  71. }
  72. @Override
  73. protected DataProviderState getState() {
  74. return (DataProviderState) super.getState();
  75. }
  76. /**
  77. * Makes the data source available to the given {@link Grid} component.
  78. *
  79. * @param component
  80. * the remote data grid component to extend
  81. */
  82. public void extend(Grid component) {
  83. super.extend(component);
  84. }
  85. }