Package com.google.gwt.sample.contacts.client

Source Code of com.google.gwt.sample.contacts.client.AppController

package com.google.gwt.sample.contacts.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.sample.contacts.client.event.AddContactEvent;
import com.google.gwt.sample.contacts.client.event.AddContactEventHandler;
import com.google.gwt.sample.contacts.client.event.ContactUpdatedEvent;
import com.google.gwt.sample.contacts.client.event.ContactUpdatedEventHandler;
import com.google.gwt.sample.contacts.client.event.EditContactCancelledEvent;
import com.google.gwt.sample.contacts.client.event.EditContactCancelledEventHandler;
import com.google.gwt.sample.contacts.client.event.EditContactEvent;
import com.google.gwt.sample.contacts.client.event.EditContactEventHandler;
import com.google.gwt.sample.contacts.client.event.ShowContactAddressHandler;
import com.google.gwt.sample.contacts.client.event.ShowContactAdressEvent;
import com.google.gwt.sample.contacts.client.presenter.ContactAddressPresenter;
import com.google.gwt.sample.contacts.client.presenter.ContactsPresenter;
import com.google.gwt.sample.contacts.client.presenter.EditContactPresenter;
import com.google.gwt.sample.contacts.client.presenter.Presenter;
import com.google.gwt.sample.contacts.client.view.ContactAddressView;
import com.google.gwt.sample.contacts.client.view.ContactsView;
import com.google.gwt.sample.contacts.client.view.EditContactView;
import com.google.gwt.sample.contacts.shared.Contact;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;

public class AppController implements Presenter, ValueChangeHandler<String> {

  private final HandlerManager eventBus;

  private final ContactsServiceAsync rpcService;

  private HasWidgets container;

  public AppController(ContactsServiceAsync rpcService, HandlerManager eventBus) {

    this.eventBus = eventBus;
    this.rpcService = rpcService;
    bind();
  }

  private void bind() {

    History.addValueChangeHandler(this);

    eventBus.addHandler(AddContactEvent.TYPE, new AddContactEventHandler() {

      @Override
      public void onAddContact(AddContactEvent event) {

        doAddNewContact();
      }
    });

    eventBus.addHandler(EditContactEvent.TYPE, new EditContactEventHandler() {

      @Override
      public void onEditContact(EditContactEvent event) {

        doEditContact(event.getId());
      }
    });

    eventBus.addHandler(EditContactCancelledEvent.TYPE, new EditContactCancelledEventHandler() {

      @Override
      public void onEditContactCancelled(EditContactCancelledEvent event) {

        doEditContactCancelled();
      }
    });

    eventBus.addHandler(ContactUpdatedEvent.TYPE, new ContactUpdatedEventHandler() {

      @Override
      public void onContactUpdated(ContactUpdatedEvent event) {

        doContactUpdated();
      }
    });

    eventBus.addHandler(ShowContactAdressEvent.TYPE, new ShowContactAddressHandler() {

      @Override
      public void onShowAddress(ShowContactAdressEvent event) {

        doShowAddress(event.getContact());
      }

    });
  }

  private void doAddNewContact() {

    History.newItem("add");
  }

  private void doContactUpdated() {

    History.newItem("list");
  }

  private void doEditContact(String id) {

    History.newItem("edit", false);
    Presenter presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView(), id);
    presenter.go(container);
  }

  private void doEditContactCancelled() {

    History.newItem("list");
  }

  private void doShowAddress(Contact contact) {

    History.newItem("address");

    Presenter presenter = new ContactAddressPresenter(rpcService, eventBus, new ContactAddressView(), contact);
    presenter.go(container);
  }

  @Override
  public void go(final HasWidgets container) {

    this.container = container;

    if ("".equals(History.getToken())) {
      History.newItem("list");
    } else {
      History.fireCurrentHistoryState();
    }
  }

  @Override
  public void onValueChange(ValueChangeEvent<String> event) {

    String token = event.getValue();

    if (token != null) {
      Presenter presenter = null;

      if (token.equals("list")) {
        presenter = new ContactsPresenter(rpcService, eventBus, new ContactsView());
      } else if (token.equals("add")) {
        presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
      } else if (token.equals("edit")) {
        presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
      } else if (token.equals("address")) {
        // presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
      }

      if (presenter != null) {
        presenter.go(container);
      }
    }
  }
}
TOP

Related Classes of com.google.gwt.sample.contacts.client.AppController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.