I found out the problem. When I created the SimpleButtonPresenter in
the ContainerPresenter constructor, I created the SimpleButtonView but
it was not the one displayed on the screen. The one displayed on the
screen was the one created in the ContainerView. The fix:
In ContainerPresenter:
public interface Display {
Widget asWidget();
SimpleButtonPresenter.Display getSimpleButtonDisplay();
}
In the constructor
simpleButtonPresenter = new SimpleButtonPresenter(eventBus,
display.getSimpleButtonDisplay());
In ContainerView:
@Override
public Display getSimpleButtonDisplay() {
return sbv;
}
On Feb 7, 10:51 am, Sydney <[email protected]> wrote:
> I have a problem with click handlers that are not called. I use the
> MVP design explained in the article "Large scale application
> development and MVP". I have a simple view which contains one button.
> I also have another view that is just a container for that button.
> It's just to simulate the problem. For each view I have a presenter.
> In the AppController I have two ways to create the application, "test"
> which uses the SimpleButtonPresenter, and "container" which uses the
> ContainerPresenter. In the first case the handler is called but in the
> second case it's not. The only difference between the two cases if
> that in the second case, the button is inside a container. Do you have
> an idea where the problem comes from?
>
> AppController
>
> public class AppController implements Presenter,
> ValueChangeHandler<String> {
> private final HandlerManager eventBus;
> private HasWidgets container;
>
> public AppController(HandlerManager eventBus) {
> this.eventBus = eventBus;
> bind();
> }
>
> private void bind() {
> History.addValueChangeHandler(this);
> }
>
> public void go(final HasWidgets container) {
> this.container = container;
>
> if ("".equals(History.getToken())) {
> History.newItem("main");
> } else {
> History.fireCurrentHistoryState();
> }
> }
>
> public void onValueChange(ValueChangeEvent<String> event) {
> String token = event.getValue();
>
> if (token != null) {
> Presenter presenter = null;
>
> if (token.equals("main")) {
> } else if ("test".equals(token)) {
> presenter = new SimpleButtonPresenter(eventBus,
> new SimpleButtonView());
> } else if ("container".equals(token)) {
> presenter = new ContainerPresenter(eventBus,
> new ContainerView());
> }
>
> if (presenter != null) {
> presenter.go(container);
> }
> }
> }
>
> }
>
> The Views
>
> public class SimpleButtonView extends Composite implements
> SimpleButtonPresenter.Display {
>
> private final Button btnClick;
>
> public SimpleButtonView() {
> DecoratorPanel container = new DecoratorPanel();
> btnClick = new Button("Click Me");
> container.add(btnClick);
> this.initWidget(container);
> }
>
> @Override
> public Widget asWidget() {
> return this;
> }
>
> @Override
> public HasClickHandlers getButton() {
> return btnClick;
> }
>
> }
>
> public class ContainerView extends Composite implements
> ContainerPresenter.Display {
>
> public ContainerView() {
> DecoratorPanel container = new DecoratorPanel();
> SimpleButtonView sbv = new SimpleButtonView();
> container.add(sbv);
> this.initWidget(container);
> }
>
> @Override
> public Widget asWidget() {
> return this;
> }
>
> }
>
> The Presenters
>
> public class SimpleButtonPresenter implements Presenter {
>
> public interface Display {
> HasClickHandlers getButton();
>
> Widget asWidget();
> }
>
> private final HandlerManager eventBus;
> private final Display display;
>
> public SimpleButtonPresenter(HandlerManager eventBus, Display
> view) {
> this.eventBus = eventBus;
> this.display = view;
> }
>
> @Override
> public void go(HasWidgets container) {
> bind();
> container.clear();
> container.add(display.asWidget());
> }
>
> public void bind() {
> display.getButton().addClickHandler(new ClickHandler() {
>
> @Override
> public void onClick(ClickEvent event) {
> System.out.println("Click");
> }
>
> });
> }
>
> }
>
> public class ContainerPresenter implements Presenter {
>
> public interface Display {
> Widget asWidget();
> }
>
> private final HandlerManager eventBus;
> private final Display display;
> private final SimpleButtonPresenter simpleButtonPresenter;
>
> public ContainerPresenter(HandlerManager eventBus, Display view) {
> this.eventBus = eventBus;
> this.display = view;
> simpleButtonPresenter = new SimpleButtonPresenter(eventBus,
> new SimpleButtonView());
> }
>
> @Override
> public void go(HasWidgets container) {
> bind();
> container.clear();
> container.add(display.asWidget());
> }
>
> public void bind() {
> simpleButtonPresenter.bind();
> }
>
>
>
> }
--
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.