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.

AbstractDataProvider.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.data.provider;
  17. import java.lang.reflect.Method;
  18. import java.util.EventObject;
  19. import com.vaadin.data.provider.DataChangeEvent.DataRefreshEvent;
  20. import com.vaadin.event.EventRouter;
  21. import com.vaadin.shared.Registration;
  22. /**
  23. * Abstract data provider implementation which takes care of refreshing data
  24. * from the underlying data provider.
  25. *
  26. * @param <T>
  27. * data type
  28. * @param <F>
  29. * filter type
  30. *
  31. * @author Vaadin Ltd
  32. * @since 8.0
  33. *
  34. */
  35. public abstract class AbstractDataProvider<T, F> implements DataProvider<T, F> {
  36. private EventRouter eventRouter;
  37. @Override
  38. public Registration addDataProviderListener(
  39. DataProviderListener<T> listener) {
  40. return addListener(DataChangeEvent.class, listener,
  41. DataProviderListener.class.getMethods()[0]);
  42. }
  43. @Override
  44. public void refreshAll() {
  45. fireEvent(new DataChangeEvent<>(this));
  46. }
  47. @Override
  48. public void refreshItem(T item) {
  49. fireEvent(new DataRefreshEvent<>(this, item));
  50. }
  51. /**
  52. * Registers a new listener with the specified activation method to listen
  53. * events generated by this component. If the activation method does not
  54. * have any arguments the event object will not be passed to it when it's
  55. * called.
  56. *
  57. * @param eventType
  58. * the type of the listened event. Events of this type or its
  59. * subclasses activate the listener.
  60. * @param listener
  61. * the object instance who owns the activation method.
  62. * @param method
  63. * the activation method.
  64. * @return a registration for the listener
  65. */
  66. protected Registration addListener(Class<?> eventType,
  67. DataProviderListener<T> listener, Method method) {
  68. if (eventRouter == null) {
  69. eventRouter = new EventRouter();
  70. }
  71. return eventRouter.addListener(eventType, listener, method);
  72. }
  73. /**
  74. * Sends the event to all listeners.
  75. *
  76. * @param event
  77. * the Event to be sent to all listeners.
  78. */
  79. protected void fireEvent(EventObject event) {
  80. if (eventRouter != null) {
  81. eventRouter.fireEvent(event);
  82. }
  83. }
  84. }