com.appspot.socialinquirer.client.activity.TopicsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.appspot.socialinquirer.client.activity.TopicsActivity.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.activity;

import gdurelle.tagcloud.client.tags.TagCloud;
import gdurelle.tagcloud.client.tags.WordTag;

import java.util.ArrayList;

import com.appspot.socialinquirer.client.ClientFactory;
import com.appspot.socialinquirer.client.constant.EverScribeConstants;
import com.appspot.socialinquirer.client.place.TopicsPlace;
import com.appspot.socialinquirer.client.util.UiUtils;
import com.appspot.socialinquirer.client.view.TopicsView;
import com.appspot.socialinquirer.shared.dto.Tag;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * The Class TopicsActivity.
 */
public class TopicsActivity extends AbstractActivity implements TopicsView.Presenter {
    // Used to obtain views, eventBus, placeController
    // Alternatively, could be injected via GIN
    /** The client factory. */
    private ClientFactory clientFactory;
    // Name that will be appended to "Hello,"
    /** The name. */
    private String name;

    /** The dashboard view. */
    private TopicsView dashboardView;

    /**
     * Instantiates a new topics activity.
     *
     * @param place the place
     * @param clientFactory the client factory
     */
    public TopicsActivity(TopicsPlace place, ClientFactory clientFactory) {
        this.name = place.getName();
        this.clientFactory = clientFactory;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
     *      com.google.gwt.event.shared.EventBus)
     */
    @Override
    public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
        final EverScribeConstants constants = clientFactory.getConstants();
        GWT.runAsync(new RunAsyncCallback() {
            public void onFailure(Throwable caught) {
                UiUtils.showErrorDialog(constants, constants.errorCodeDownloadFailed());
            }

            public void onSuccess() {
                dashboardView = clientFactory.getTopicsView();
                dashboardView.setName(name);
                // dashboardView.setUserTags(user.getTags());
                dashboardView.setPresenter(TopicsActivity.this);
                containerWidget.setWidget(dashboardView.asWidget());
                clientFactory.getUserService().getUserTags(50, new AsyncCallback<ArrayList<Tag>>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        UiUtils.showErrorDialog(constants, caught.getLocalizedMessage());
                    }

                    @Override
                    public void onSuccess(ArrayList<Tag> result) {
                        ArrayList<Tag> userTags = new ArrayList<Tag>();
                        ArrayList<Tag> suggestedTags = new ArrayList<Tag>();
                        for (Tag tag : result) {
                            if (tag.getKey() == null) {
                                suggestedTags.add(tag);
                            } else {
                                userTags.add(tag);
                            }
                        }
                        dashboardView.setActiveTags(userTags);
                        dashboardView.setSuggestedTags(suggestedTags);
                    }
                });
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.appspot.linkedhub.client.view.TagsView.Presenter#goTo(com.google.gwt.place.shared.Place)
     */
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

    /* (non-Javadoc)
     * @see com.appspot.socialinquirer.client.view.TopicsView.Presenter#onAddSelectedClicked(java.util.ArrayList)
     */
    @Override
    public void onAddSelectedClicked(ArrayList<Tag> tags) {
        clientFactory.getUserService().addUserTags(tags, new AsyncCallback<Void>() {

            @Override
            public void onFailure(Throwable caught) {
                UiUtils.showErrorDialog(clientFactory.getConstants(), caught.getLocalizedMessage());
            }

            @Override
            public void onSuccess(Void result) {
                // TODO Auto-generated method stub

            }
        });
    }

    /* (non-Javadoc)
     * @see com.appspot.socialinquirer.client.view.TopicsView.Presenter#onRemoveSelectedClicked(java.util.ArrayList)
     */
    @Override
    public void onRemoveSelectedClicked(ArrayList<Tag> tags) {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.appspot.socialinquirer.client.view.TopicsView.Presenter#onTagCloudClicked()
     */
    @Override
    public void onTagCloudClicked() {
        final EverScribeConstants constants = clientFactory.getConstants();
        clientFactory.getUserService().getUserTags(50, new AsyncCallback<ArrayList<Tag>>() {

            @Override
            public void onFailure(Throwable caught) {
                UiUtils.showErrorDialog(constants, caught.getLocalizedMessage());
            }

            @Override
            public void onSuccess(ArrayList<Tag> result) {
                TagCloud tagCloud = new TagCloud();
                tagCloud.setColored(true);

                for (Tag tag : result) {
                    WordTag word = new WordTag(tag.getTag());
                    word.setNumberOfOccurences(tag.getFreqency());
                    tagCloud.addWord(word);
                }

                final DialogBox dialogBox = new DialogBox();
                dialogBox.setText(constants.titleDialogBoxTagCloud());
                dialogBox.setAnimationEnabled(true);
                VerticalPanel dialogVPanel = new VerticalPanel();
                dialogVPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
                dialogVPanel.add(tagCloud);
                final Button closeButton = new Button(constants.closeButton());
                // We can set the id of a widget by accessing its
                // Element
                closeButton.getElement().setId("closeButton");
                dialogVPanel.add(closeButton);
                dialogBox.setWidget(dialogVPanel);

                closeButton.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        dialogBox.hide();
                    }
                });
                dialogBox.center();
            }
        });
    }
}