com.vaadin.data.provider.AbstractDataProvider Maven / Gradle / Ivy
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See for the full
* license.
*/
package com.vaadin.data.provider;
import java.lang.reflect.Method;
import java.util.EventObject;
import com.vaadin.data.provider.DataChangeEvent.DataRefreshEvent;
import com.vaadin.event.EventRouter;
import com.vaadin.shared.Registration;
/**
* Abstract data provider implementation which takes care of refreshing data
* from the underlying data provider.
*
* @param
* data type
* @param
* filter type
*
* @author Vaadin Ltd
* @since 8.0
*
*/
public abstract class AbstractDataProvider implements DataProvider {
private EventRouter eventRouter;
@Override
public Registration addDataProviderListener(
DataProviderListener listener) {
return addListener(DataChangeEvent.class, listener,
DataProviderListener.class.getMethods()[0]);
}
@Override
public void refreshAll() {
fireEvent(new DataChangeEvent<>(this));
}
@Override
public void refreshItem(T item) {
fireEvent(new DataRefreshEvent<>(this, item));
}
/**
* Registers a new listener with the specified activation method to listen
* events generated by this component. If the activation method does not
* have any arguments the event object will not be passed to it when it's
* called.
*
* @param eventType
* the type of the listened event. Events of this type or its
* subclasses activate the listener.
* @param listener
* the object instance who owns the activation method.
* @param method
* the activation method.
* @return a registration for the listener
*/
protected Registration addListener(Class> eventType,
DataProviderListener listener, Method method) {
if (eventRouter == null) {
eventRouter = new EventRouter();
}
return eventRouter.addListener(eventType, listener, method);
}
/**
* Sends the event to all listeners.
*
* @param event
* the Event to be sent to all listeners.
*/
protected void fireEvent(EventObject event) {
if (eventRouter != null) {
eventRouter.fireEvent(event);
}
}
}