Package com.metadot.book.connectr.client

Source Code of com.metadot.book.connectr.client.AppController

/**
* Copyright 2010 Daniel Guermeur and Amy Unruh
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*
*   See http://connectrapp.appspot.com/ for a demo, and links to more information
*   about this app and the book that it accompanies.
*/
package com.metadot.book.connectr.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.History;
import com.metadot.book.connectr.client.event.FriendAddEvent;
import com.metadot.book.connectr.client.event.FriendAddEventHandler;
import com.metadot.book.connectr.client.event.FriendEditCancelledEvent;
import com.metadot.book.connectr.client.event.FriendEditCancelledEventHandler;
import com.metadot.book.connectr.client.event.FriendEditEvent;
import com.metadot.book.connectr.client.event.FriendEditEventHandler;
import com.metadot.book.connectr.client.event.FriendUpdatedEvent;
import com.metadot.book.connectr.client.event.FriendUpdatedEventHandler;
import com.metadot.book.connectr.client.event.LogoutEvent;
import com.metadot.book.connectr.client.event.LogoutEventHandler;
import com.metadot.book.connectr.client.event.ProfileEditEvent;
import com.metadot.book.connectr.client.event.ProfileEditEventHandler;
import com.metadot.book.connectr.client.event.ShowFriendPopupEvent;
import com.metadot.book.connectr.client.event.ShowFriendPopupEventHandler;
import com.metadot.book.connectr.client.presenter.FriendEditPresenter;
import com.metadot.book.connectr.client.presenter.FriendPopupPresenter;
import com.metadot.book.connectr.client.presenter.MessageListPresenter;
import com.metadot.book.connectr.client.presenter.Presenter;
import com.metadot.book.connectr.client.service.FriendsServiceAsync;
import com.metadot.book.connectr.client.service.MessagesService;
import com.metadot.book.connectr.client.service.MessagesServiceAsync;
import com.metadot.book.connectr.client.view.FriendEditView;
import com.metadot.book.connectr.client.view.FriendPopupView;
import com.metadot.book.connectr.client.view.MessageListView;

public class AppController implements ValueChangeHandler<String> {
  private final SimpleEventBus eventBus;
  private final FriendsServiceAsync friendService;
  private final MessagesServiceAsync messagesService = GWT.create(MessagesService.class);
  private String currentFriendId;
  private MessageListPresenter messageListPresenter;

  public AppController(FriendsServiceAsync rpcService, SimpleEventBus eventBus) {
    this.eventBus = eventBus;
    this.friendService = rpcService;
    bind();
  }

  private void bind() {
    History.addValueChangeHandler(this);

    eventBus.addHandler(ProfileEditEvent.TYPE, new ProfileEditEventHandler() {
      @Override
      public void onEditProfile(ProfileEditEvent event) {
        doProfileEdit();
      }
    });

    eventBus.addHandler(FriendAddEvent.TYPE, new FriendAddEventHandler() {
      public void onAddFriend(FriendAddEvent event) {
        doAddNewFriend();
      }
    });

    eventBus.addHandler(ShowFriendPopupEvent.TYPE, new ShowFriendPopupEventHandler() {
      public void onShowFriendPopup(ShowFriendPopupEvent event) {
        FriendPopupPresenter friendPopupPresenter = new FriendPopupPresenter(friendService, eventBus,
            new FriendPopupView(event.getFriend().getDisplayName(), event.getClickPoint()), event.getFriend());
        friendPopupPresenter.go();
      }
    });

    eventBus.addHandler(FriendEditEvent.TYPE, new FriendEditEventHandler() {
      public void onEditFriend(FriendEditEvent event) {
        GWT.log("AppController: Friend edit event received. Id: " + event.getId());
        doEditFriend(event.getId());
      }
    });

    eventBus.addHandler(FriendEditCancelledEvent.TYPE, new FriendEditCancelledEventHandler() {
      public void onEditFriendCancelled(FriendEditCancelledEvent event) {
        doEditFriendCancelled();
      }
    });

    eventBus.addHandler(FriendUpdatedEvent.TYPE, new FriendUpdatedEventHandler() {
      public void onFriendUpdated(FriendUpdatedEvent event) {
        doFriendUpdated();
      }
    });

    eventBus.addHandler(LogoutEvent.TYPE, new LogoutEventHandler() {
      @Override public void onLogout(LogoutEvent event) {
        GWT.log("AppController: Logout event received");
        doLogout();
      }
    });
  }

  private void doAddNewFriend() {
    History.newItem("add");
  }

  private void doEditFriend(String id) {
    currentFriendId = id;
    History.newItem("edit");
  }

  private void doEditFriendCancelled() {
    History.newItem("list");
  }
 
  private void doProfileEdit() {
    History.newItem("HelloPlace");
  }

  private void doFriendUpdated() {
    History.newItem("list");
  }

  private void doLogout() {
    History.newItem("login");
  }

  public void go() {

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

  public void onValueChange(ValueChangeEvent<String> event) {
    String token = event.getValue();

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

      if (token.equals("list")) {
        presenter = getMessageListPresenter();
        presenter.go(ConnectrApp.get().getMainPanel());
       
        return;

      } else if (token.equals("add")) {
        presenter = new FriendEditPresenter(friendService, eventBus, new FriendEditView(), null);
        presenter.go(ConnectrApp.get().getMainPanel());

        return;

      } else if (token.equals("login")) {
        ConnectrApp.get().showLoginView();
       
        return;

      } else if (token.equals("edit")) {
        presenter = new FriendEditPresenter(friendService, eventBus, new FriendEditView(), currentFriendId);
        presenter.go(ConnectrApp.get().getMainPanel());

        return;
      }
    }

  }

  private MessageListPresenter getMessageListPresenter() {
    if (messageListPresenter == null){
      GWT.log("AppController: Creating MessageListPresenter");
      messageListPresenter = new MessageListPresenter(messagesService, eventBus, new MessageListView());
    }

    return messageListPresenter;
  }
 
}
TOP

Related Classes of com.metadot.book.connectr.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.