Hi,

This message was supposed to be an answer to the "Menu Item and MVP"
discussion. But it does not appear, I probably made a mistake, so I
put it here in a new discussion.

The initial message posted by "mic" was :

>> Just sharing some thoughts....
>>
>> Would the MenuItem class need to implement HasClickHandler  so that
>> when a MenuItem is clicked, the action can be handled in the
>> presenter. At this time, the MenuItem needs an object that implements
>> the Command interface, so the view seems to know about the presenter.

>> I would like to know what others think about this.


This is probably a late answer to the initial question.
Anyway, I've made a quite simple implementation that solves the lack
of something like a HasClickHandlers dedicated to MenuItem.

Here I give you the solution I've built.


First, we need something like a "HasCommandHandlers" interface instead
of HasClickHandler. This will allow the presenter to handle a click on
a MenuItem.


public interface HasCommandHandlers extends HasHandlers {

        // equivalent to the addClickHandler in the HasClickHandlers
interface
        public HandlerRegistration addCommandHandler(CommandHandler handler);
}

We create a CommandHandler interface only to stay in line with the
naming convetion for the handlers.

public interface CommandHandler extends Command {
// this interface is empty
}

The Presenter :

        public interface Display {
                HasCommandHandlers getMenu1();
                HasCommandHandlers getMenu2();
                ...
        }

        public void build() {

                view.gerMenu1().addCommandHandler(new CommandHandler() {
                        public void execute() {
                                // do what you need
                        }
                });
        }

        ...
}

So this is standard MVP implementation.

Now the View.

We have to create a subclass of MenuItem implementing
MenuPresenter.HasCommandHandlers, so we will have a MenuItem
implementing the HasCommandHandlers interface just like a PushButton
implements HasClickHandlers.

        public class MyMenuItem extends MenuItem implements
HasCommandHandlers {

                // ----------------
                // extends MenuItem
                // ----------------

                public MyMenuItem(String text) {
                        super(text, (Command)null);
                }

                // ---------------------------
                // implements HasCommandHandlers
                // ---------------------------

                @Override
                public HandlerRegistration addCommandHandler(CommandHandler 
handler)
{

                        // call MenuItem setCommand() as the CommandHandler is 
a Command !
                        setCommand(handler);

                        return new HandlerRegistration() {

                                @Override
                                public void removeHandler() {
                                        // remove the command
                                        MyMenuItem.this.setCommand(null);
                                }

                        };
                }

                @Override
                public void fireEvent(GwtEvent<?> event) {
                        // do nothing because we do not receive events
                }
        }

The view as usual implements MenuPresenter.Display.

This seems to me a quite simple and straight solution.

Your comments are welcome !

HTH
Yves

-- 
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.

Reply via email to