com.appspot.socialinquirer.client.SocialInquirer.java Source code

Java tutorial

Introduction

Here is the source code for com.appspot.socialinquirer.client.SocialInquirer.java

Source

/*
 * Copyright 2012 Nabeel Mukhtar 
 * 
 * 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. 
 * 
 */
package com.appspot.socialinquirer.client;

import com.appspot.socialinquirer.client.constant.EverScribeConstants;
import com.appspot.socialinquirer.client.menu.HistoryCommand;
import com.appspot.socialinquirer.client.menu.LoginCommand;
import com.appspot.socialinquirer.client.menu.LogoutCommand;
import com.appspot.socialinquirer.client.place.HomePlace;
import com.appspot.socialinquirer.client.rpc.UserRpcServiceAsync;
import com.appspot.socialinquirer.client.util.UiUtils;
import com.appspot.socialinquirer.shared.dto.User;
import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.search.client.SearchUtils;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;

/**
 * The Class SocialInquirer.
 */
public class SocialInquirer implements EntryPoint {

    // /** The dashboard place. */
    // private Place dashboardPlace = new DashboardPlace(HistoryToken.Dashboard
    // .name());

    /** The home place. */
    private Place homePlace = new HomePlace(HistoryToken.Home.name());

    /** The app widget. */
    private SimplePanel appWidget = new SimplePanel();

    /** The Constant clientFactory. */
    private static final ClientFactory clientFactory = GWT.create(ClientFactory.class);

    /*
     * (non-Javadoc)
     * 
     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
     */
    public void onModuleLoad() {
        final EventBus eventBus = clientFactory.getEventBus();
        final PlaceController placeController = clientFactory.getPlaceController();
        final UserRpcServiceAsync userService = clientFactory.getUserService();
        final EverScribeConstants constants = clientFactory.getConstants();
        GWT.runAsync(new RunAsyncCallback() {
            public void onFailure(Throwable caught) {
                UiUtils.showErrorDialog(constants, constants.errorCodeDownloadFailed());
            }

            public void onSuccess() {

                // Start ActivityManager for the main widget with our
                // ActivityMapper
                ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
                ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
                // TODO-NM: Fix this.
                activityManager.setDisplay(appWidget);

                // Start PlaceHistoryHandler with our PlaceHistoryMapper
                final AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
                final PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);

                userService.getRandomQuote(new AsyncCallback<String>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        RootPanel.get("quots").add(new HTML(
                                "<p>There is nothing more powerful than an idea whose time has come.</p>", true));
                    }

                    @Override
                    public void onSuccess(String result) {
                        RootPanel.get("quots").add(new HTML("<p>" + result + "</p>", true));
                    }
                });

                userService.getCurrentUser(new AsyncCallback<User>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        MenuBar menu = createMenuBar(constants, null);

                        RootPanel.get("menu").add(menu);

                        RootPanel.get("content").add(appWidget);
                        // RootPanel.get("loginPanel").add(new HTML("<a
                        // href=\"/Auth?command=EvernoteAuth\"
                        // class=\"login\">Login with Evernote</a>"));

                        // handle exception;
                        historyHandler.register(placeController, eventBus, homePlace);
                        // Goes to place represented on URL or default place
                        historyHandler.handleCurrentHistory();
                    }

                    @Override
                    public void onSuccess(User result) {
                        RootPanel.get("content").add(appWidget);

                        if (result != null) {
                            MenuBar menu = createMenuBar(constants, result);

                            RootPanel.get("menu").add(menu);
                            clientFactory.setUser(result);
                            historyHandler.register(placeController, eventBus, homePlace);
                        } else {
                            MenuBar menu = createMenuBar(constants, null);

                            RootPanel.get("menu").add(menu);
                            historyHandler.register(placeController, eventBus, homePlace);
                        }
                        // Goes to place represented on URL or default place
                        historyHandler.handleCurrentHistory();
                    }
                });
            }
        });
        SearchUtils.loadSearchApi(new Runnable() {
            public void run() {
            }
        });
    }

    /**
     * Creates the menu bar.
     *
     * @param constants the constants
     * @param user the user
     * @return the menu bar
     */
    private MenuBar createMenuBar(final EverScribeConstants constants, final User user) {
        final boolean enabled = (user != null);
        final MenuBar menu = new MenuBar();
        GWT.runAsync(new RunAsyncCallback() {
            public void onFailure(Throwable caught) {
                UiUtils.showErrorDialog(constants, constants.errorCodeDownloadFailed());
            }

            public void onSuccess() {
                menu.setAutoOpen(true);
                menu.setAnimationEnabled(true);

                MenuItem homeMenuItem = new MenuItem(constants.homeMenuName(),
                        new HistoryCommand(HistoryToken.Home));
                homeMenuItem.setEnabled(enabled);
                menu.addItem(homeMenuItem);

                MenuBar networkMenuItem = new MenuBar(true);
                networkMenuItem.setAnimationEnabled(true);
                menu.addItem(new MenuItem(constants.networkMenuName(), networkMenuItem));
                MenuItem nfMenuItem = new MenuItem(constants.followersMenuName(),
                        new HistoryCommand(HistoryToken.Network_Followers));
                nfMenuItem.setEnabled(enabled);
                networkMenuItem.addItem(nfMenuItem);
                nfMenuItem = new MenuItem(constants.followingMenuName(),
                        new HistoryCommand(HistoryToken.Network_Following));
                nfMenuItem.setEnabled(enabled);
                networkMenuItem.addItem(nfMenuItem);
                nfMenuItem = new MenuItem(constants.recommendedMenuName(),
                        new HistoryCommand(HistoryToken.Network_Recommended));
                nfMenuItem.setEnabled(enabled);
                networkMenuItem.addItem(nfMenuItem);

                MenuBar activityMenuItem = new MenuBar(true);
                activityMenuItem.setAnimationEnabled(true);
                menu.addItem(new MenuItem(constants.activityMenuName(), activityMenuItem));
                MenuItem aMenuItem = new MenuItem(constants.tasksMenuName(),
                        new HistoryCommand(HistoryToken.Activity_Tasks));
                aMenuItem.setEnabled(enabled);
                activityMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.pollMenuName(), new HistoryCommand(HistoryToken.Activity_Polls));
                aMenuItem.setEnabled(enabled);
                activityMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.quizMenuName(),
                        new HistoryCommand(HistoryToken.Activity_Quizzes));
                aMenuItem.setEnabled(enabled);
                activityMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.pagesMenuName(),
                        new HistoryCommand(HistoryToken.Activity_Pages));
                aMenuItem.setEnabled(enabled);
                activityMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.messagesMenuName(),
                        new HistoryCommand(HistoryToken.Activity_Messages));
                aMenuItem.setEnabled(enabled);
                activityMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.endorsementsMenuName(),
                        new HistoryCommand(HistoryToken.Activity_Endorsements));
                aMenuItem.setEnabled(enabled);
                activityMenuItem.addItem(aMenuItem);

                //            MenuBar pagesMenuItem = new MenuBar(true);
                //            pagesMenuItem.setAnimationEnabled(true);
                //            menu.addItem(new MenuItem(constants.pagesMenuName(),
                //                  pagesMenuItem));
                //            aMenuItem = new MenuItem(constants
                //                  .followingMenuName(), new HistoryCommand(HistoryToken.Pages_Following));
                //            aMenuItem.setEnabled(enabled);
                //            pagesMenuItem.addItem(aMenuItem);
                //            aMenuItem = new MenuItem(constants
                //                  .ownedMenuName(), new HistoryCommand(HistoryToken.Pages_Created));
                //            aMenuItem.setEnabled(enabled);
                //            pagesMenuItem.addItem(aMenuItem);
                //            aMenuItem = new MenuItem(constants
                //                  .recommendedMenuName(), new HistoryCommand(HistoryToken.Pages_Recommended));
                //            aMenuItem.setEnabled(enabled);
                //            pagesMenuItem.addItem(aMenuItem);

                MenuBar questionsMenuItem = new MenuBar(true);
                questionsMenuItem.setAnimationEnabled(true);
                menu.addItem(new MenuItem(constants.questionsMenuName(), questionsMenuItem));
                aMenuItem = new MenuItem(constants.askedMenuName(),
                        new HistoryCommand(HistoryToken.Questions_Asked));
                aMenuItem.setEnabled(enabled);
                questionsMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.answeredMenuName(),
                        new HistoryCommand(HistoryToken.Questions_Answered));
                aMenuItem.setEnabled(enabled);
                questionsMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.followingMenuName(),
                        new HistoryCommand(HistoryToken.Questions_Following));
                aMenuItem.setEnabled(enabled);
                questionsMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.recommendedMenuName(),
                        new HistoryCommand(HistoryToken.Questions_Recommended));
                aMenuItem.setEnabled(enabled);
                questionsMenuItem.addItem(aMenuItem);

                MenuBar tagsMenuItem = new MenuBar(true);
                tagsMenuItem.setAnimationEnabled(true);
                menu.addItem(new MenuItem(constants.tagsMenuName(), tagsMenuItem));
                aMenuItem = new MenuItem(constants.ownedMenuName(), new HistoryCommand(HistoryToken.Topics_Active));
                aMenuItem.setEnabled(enabled);
                tagsMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.followingMenuName(),
                        new HistoryCommand(HistoryToken.Topics_Following));
                aMenuItem.setEnabled(enabled);
                tagsMenuItem.addItem(aMenuItem);
                aMenuItem = new MenuItem(constants.recommendedMenuName(),
                        new HistoryCommand(HistoryToken.Topics_Recommended));
                aMenuItem.setEnabled(enabled);
                tagsMenuItem.addItem(aMenuItem);

                MenuBar accountMenu = new MenuBar(true);
                accountMenu.setAnimationEnabled(true);
                menu.addItem(new MenuItem(constants.accountMenuName(), accountMenu));

                MenuItem loginMenuItem = new MenuItem(constants.loginMenuName(), new LoginCommand(constants));
                loginMenuItem.setEnabled(!enabled);
                accountMenu.addItem(loginMenuItem);
                MenuItem logoutMenuItem = new MenuItem(constants.logoutMenuName(), new LogoutCommand(constants));
                logoutMenuItem.setEnabled(enabled);
                accountMenu.addItem(logoutMenuItem);

                MenuItem settingsMenuItem = new MenuItem(constants.settingsMenuName(),
                        new HistoryCommand(HistoryToken.Settings));
                settingsMenuItem.setEnabled(enabled);
                accountMenu.addItem(settingsMenuItem);
            }
        });
        return menu;
    }
}