I take a little different approach, but you have most of the
fundamentals. The characteristic interface (i.e., Listener in your
example) shouldn't be defined as part of a particular instance of
MyPresenter.View, as that ties the Presenter directly to a specific
view implementation. Also, I would recommend following the Handler
pattern in GWT, as it allows you to utilize some of the Handler
management functions.
Taking your example, here's a possible approach using
SelectionHandler<String> instead of MyView.Listener:
public class MyView extend Composite implements MyPresenter.View,
HasSelectionHandlers<String> {
HasSelectionHandlers<String> getSelectionSource() {
return this;
}
...
//Do something with a click event (called by the ClickHandler)
void doClick( CLickEvent event ) {
//Retrieve the selection (i.e., item to edit) by processing the
ClickEvent
String selection = getSelectionFromClickEvent( event );
SelectionEvent.fire(this, selection);
}
}
public class MyPresenter implement MyView.Listener {
public interface View {
HasSelectionHandlers<String> getSelectionSource();
}
...
public void bind(View display){
display.getSelectionSource().addSelectionHandler(new
SelectionHandler<String>(){
public void onSelection(SelectionEvent<String> event) {
//Do Something in response to Selection
}
});
};
}
Note that in the example, even though MyView implements the
HasSelectionHandler<String> interface directly, the Presenter only
accesses an instance that HasSelectionHandlers<String> through
View.getSelectionSource(). This allows different View implementations
to implement different sources for the SelectionEvent, reducing
potential coupling between the view and presenter. Hope that helps.
-Jason