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.

SimpleDataChangeHandler.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.data;
  17. import java.util.function.Consumer;
  18. import com.google.gwt.core.client.Scheduler;
  19. import com.vaadin.shared.Range;
  20. /**
  21. * Helper class for creating a {@link DataChangeHandler} for a Widget that does
  22. * not support lazy loading.
  23. *
  24. * @author Vaadin Ltd
  25. * @since 8.0
  26. */
  27. public class SimpleDataChangeHandler implements DataChangeHandler {
  28. /**
  29. * Class to request the data source to get the full data set.
  30. */
  31. private static class DelayedResetScheduler {
  32. private final DataSource<?> dataSource;
  33. private boolean scheduled = false;
  34. public DelayedResetScheduler(DataSource<?> dataSource) {
  35. this.dataSource = dataSource;
  36. }
  37. public void schedule() {
  38. if (scheduled) {
  39. return;
  40. }
  41. Scheduler.get().scheduleFinally(() -> {
  42. dataSource.ensureAvailability(0, dataSource.size());
  43. scheduled = false;
  44. });
  45. scheduled = true;
  46. }
  47. public int getExpectedSize() {
  48. return dataSource.size();
  49. }
  50. public boolean isScheduled() {
  51. return scheduled;
  52. }
  53. }
  54. private final DelayedResetScheduler scheduler;
  55. private final Consumer<Range> refreshMethod;
  56. SimpleDataChangeHandler(DataSource<?> dataSource,
  57. Consumer<Range> refreshMethod) {
  58. scheduler = new DelayedResetScheduler(dataSource);
  59. this.refreshMethod = refreshMethod;
  60. }
  61. @Override
  62. public void dataUpdated(int firstRowIndex, int numberOfRows) {
  63. scheduler.schedule();
  64. }
  65. @Override
  66. public void dataRemoved(int firstRowIndex, int numberOfRows) {
  67. scheduler.schedule();
  68. }
  69. @Override
  70. public void dataAdded(int firstRowIndex, int numberOfRows) {
  71. scheduler.schedule();
  72. }
  73. @Override
  74. public void dataAvailable(int firstRowIndex, int numberOfRows) {
  75. if (!scheduler.isScheduled() && firstRowIndex == 0
  76. && numberOfRows == scheduler.getExpectedSize()) {
  77. // All data should now be available.
  78. refreshMethod.accept(Range.withLength(firstRowIndex, numberOfRows));
  79. } else {
  80. scheduler.schedule();
  81. }
  82. }
  83. @Override
  84. public void resetDataAndSize(int newSize) {
  85. scheduler.schedule();
  86. }
  87. }