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
On Sep 10, 10:47 am, fonghuangyee <[email protected]> wrote:
> Thanks for ur advise.
> How about if i define a interface ( View Listener ), then my presenter
> implement the View Listener?
>
> Example :
>
> public class MyView extend Composite implement MyPresenter.View {
>
> public interface Listerner {
>
> public void onEdit(String id);
> }
>
> .....
> .....
>
> Listener listener;
>
> // a List of Button here
> button.addClickHandler(new ClickHandler(){
>
> public void onClick(ClickEvent event){
> listener.onEdit(id);
> }
> );
>
> }
>
> public class MyPresenter implement MyView.Listener {
>
> public interface View {
>
> }
>
> public void onEdit(String id){
> // Do something here.
> };
>
> }
>
> Please correct me if i am following the wrong way.
> Thanks.
>
> On Sep 7, 8:32 am, "Jason A. Beranek" <[email protected]> wrote:
>
> > One approach I've used is to put some logic in the View to forward a
> > different event than the purely GUI events to the Presenter. For
> > example, if you have a table or list of items which can be clicked,
> > and a click signifies selection of an item, use HasSelectionHandlers()
> > in the View, and translate in the View which item is selected by the
> > click. This seems to work for selection use cases, but it makes your
> > View in theMVPmore Supervising Controller (http://martinfowler.com/
> > eaaDev/SupervisingPresenter.html) than Passive View (http://
> > martinfowler.com/eaaDev/PassiveScreen.html).
>
> > If you were to need separate handling for each list item being
> > clicked, then you are likely looking at a Presenter per list item or
> > at having the View include a factory to add list items and return
> > handlers (i.e., HasClickHandlers addListItem() ). Any of these
> > approaches will work, it just depends on what you are looking to do
> > with each click handler.
>
> > V/r,
>
> > Jason
>
> > On Sep 6, 3:19 am, fonghuangyee <[email protected]> wrote:
>
> > > Hihi ,
> > > After i go through so manyMVPexample, i found that we always use
> > > presenter to get thoseHasClickHandlerfrom Display( Or View) to do
> > > some action. This way works nice for static clickable action, but if
> > > we have some dynamic button ( example a table listing with a list of
> > > clickable action ), then i have to get a list ofHasClickHandler
> > > from
> > > display? and every time i add new row to the table listing, i have to
> > > take care of the handler action, quite troublesome.
> > > any advice?
>
> > > Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---