I want to create custom events and fire them in some part of the view such that some other part of the view is updated/removed/refreshed.
I have tried by extending the C
What you need:
1. A component that fires the event
2. A custom event class
3. A listener interface
4. And a listener implementation
Component from 1 need to have an list of event-listeners, and the methods: addListener; removeListener; and dispatchEvent. The dispatchEvent-method will iterate through the list of event-listeners and execute some method (see 4: the listener implementation). That method has as argument an event-object (see 2: the custom event class).
Essential code for 1:
protected List listeners;
protected synchronized void dispatchEvent(MyEvent event) {
if (listeners != null) {
for (MyListener listener : listeners) {
listener.myMethod(event);
}
}
}
public synchronized void addListener(MyListener listener) {
if (listeners == null) {
listeners = new ArrayList();
}
listeners.add(listener);
}
public synchronized void removeListener(MyListener listener) {
if (listeners == null) {
listeners = new ArrayList();
return;
}
listeners.remove(listener);
}
Essential code for 2:
public class MyEvent {
protected String eventType;
public MyEvent(String eventType) {
this.eventType = eventType;
}
//getters and setters
}
Essential part for 3:
public interface MyListener {
public void doSomething(MyEvent event);
}
Code for 4 (listener implementation)
public class MyImplementation implements MyListener {
@Override
public void doSomething(myEvent event) {
//do something concretes here
}
}
How the code flow works:
The dispatcher class will, after some interaction, "announce" to every event-listener that something has happened.
dispatchEvent(new MyEvent("ADD_USER"));
In a class you want to receive the event you need to instantiate the dispatcher class and listener implementation class, and add the listener to the dispatcher's list:
MyDispatcher disp = new MyDispatcher();
MyImplementation myImpl = new MyImplementation();
// Then add the event-listener to the dispatcher:
disp.addListener(myImpl);
Sure, some of these classes can be inner classes, and the code could be simplified or extended, but this is the general idea. This is also just plain Java and has nothing Vaadin-dependent.