Written component "MyPanel extends VerticalPanel". I want to add an event to it, say 'onDataAdded' /

The code will look something like this:

MyPanel p = new MyPanel(); p.on('onDataAdded', new MyPanelDataAddedFunciton(){ private void execute(int dataAddedCount) { alert("" + dataAddedCount); } }); 

or

 MyPanel p = new MyPanel(){ private void onDataAdded(int dataAddedCount) { alert("" + dataAddedCount); } }); 

How to do it? Thank.

    2 answers 2

    Such processing schemes are no longer customary to do.

    The first version is absolutely terrible, the second one is just bad.

    For event handling, there is a fairly common pattern. It is customary to make an interface to the event listener.

     public interface MyControlListener { void onDataChanged(int dataAddedCount); } 

    The control itself should have a list of handlers:

     public class MyControl /*...*/ { private ArrayList<MyControlListener> listeners = new ArrayList<MyControlListener>(); public void addListener(MyControlListener listener) { listeners.add(listener); } public void removeListener(MyControlListener listener) { listeners.remove(listener); } private void fireListeners(int count) { for(MyControlListener listener : listeners) { listener.onDataChanged(count); } } /* ...... код может вызывать fireListeners(count) когда требуется уведомить слушателей */ } 

    After that, everyone who is interested in this event can subscribe to the event, for example, like this:

     class MyController implements MyControlListener { public void myInitMethod() { MyControl ctl = new MyControl(); ctl.addListener(this); } @Override public void onDataChanged(int count) { // тра-ла-ла } } 

    This is a common best practice and is used almost everywhere. Look, at least, at what is already there, at how existing events are made.

    • And you can show the simplest working example. So the idea seems a bit clear, but with an example it would be great! - Jenkamen

    I would inherit from GwtEvent (see examples with ScrollEvent, AttachEvent, ValueChangeEvent, and others) - let's say DataAddedEvent, as well as DataAddedHandler (inherited from EventHandler). Still create has-handlers for it:

     public interface HasDataAddedHandlers extends HasHandlers { HandlerRegistration addDataAddedHandler(DataAddedHandler handler); } 

    This interface should be implemented in your MyPanel. This scheme has the advantage that you can trigger your events through the same mechanism as the rest of the events in gwt, using the Widget class's addHandler (starting with gwt-2.1):

     public final <H extends EventHandler> HandlerRegistration addHandler( final H handler, GwtEvent.Type<H> type) 

    And use HandlerRegistration to remove a listener, as is customary in the GWT (no methods like removeListener are needed). And also do not need fireEvent, because the event is generated from the HandlerManager, you only need to implement a method in your DataAddedEvent class

     protected abstract void dispatch(H handler) 

    It is this scheme adopted in Gwt-2.1, which is slightly different from the traditional MVC, but not fundamentally