com.appspot.socialinquirer.client.view.HomeViewImpl.java Source code

Java tutorial

Introduction

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

import java.util.ArrayList;
import java.util.Date;

import com.appspot.socialinquirer.client.KeyProvider;
import com.appspot.socialinquirer.client.constant.EverScribeConstants;
import com.appspot.socialinquirer.shared.TextLanguage;
import com.appspot.socialinquirer.shared.dto.Classifier;
import com.appspot.socialinquirer.shared.dto.Message;
import com.appspot.socialinquirer.shared.dto.Question;
import com.appspot.socialinquirer.shared.dto.Quiz;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.SafeHtmlCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;

/**
 * The Class HomeViewImpl.
 */
public class HomeViewImpl extends Composite implements HomeView {

    /** The ui binder. */
    private static HomeViewUiBinder uiBinder = GWT.create(HomeViewUiBinder.class);

    /** The presenter. */
    private Presenter presenter;

    /** The questions table. */
    @UiField(provided = true)
    CellTable<Question> questionsTable;

    /** The questions paginator. */
    @UiField(provided = true)
    SimplePager questionsPaginator;

    /** The questions search box. */
    @UiField(provided = true)
    SuggestBox questionsSearchBox;

    /** The templates table. */
    @UiField(provided = true)
    CellTable<Message> templatesTable;

    /** The templates paginator. */
    @UiField(provided = true)
    SimplePager templatesPaginator;

    /** The data provider. */
    private ListDataProvider<Message> dataProvider;

    /** The questions search. */
    @UiField
    Button questionsSearch;

    /** The feeds. */
    @UiField
    HTMLPanel searchResults, introduction, feeds;

    // menu items
    /** The run code menu. */
    @UiField
    MenuItem viewMenu, analyzeMenu, summarizeMenu, suggestAnswerMenu, spellCheckMenu, speakMenu, runCodeMenu;

    /** The classifier bar. */
    @UiField
    MenuBar quizBar, languageBar, classifierBar;

    /** The constants. */
    private EverScribeConstants constants;

    /**
     * The Interface HomeViewUiBinder.
     */
    @UiTemplate("HomeView.ui.xml")
    interface HomeViewUiBinder extends UiBinder<Widget, HomeViewImpl> {
    }

    /**
     * Instantiates a new home view impl.
     *
     * @param constants the constants
     * @param oracle the oracle
     */
    public HomeViewImpl(final EverScribeConstants constants, SingleWordSuggestOracle oracle) {
        this.constants = constants;
        questionsTable = new CellTable<Question>();
        SimplePager.Resources ideasPagerResources = GWT.create(SimplePager.Resources.class);
        questionsPaginator = new SimplePager(TextLocation.CENTER, ideasPagerResources, false, 0, true);
        questionsPaginator.setDisplay(questionsTable);
        final SelectionModel<Question> questionsSelectionModel = new SingleSelectionModel<Question>(
                new KeyProvider<Question>());
        questionsTable.setSelectionModel(questionsSelectionModel,
                DefaultSelectionEventManager.<Question>createCheckboxManager());
        initTableColumns(questionsTable, questionsSelectionModel);
        questionsSearchBox = new SuggestBox(oracle);
        templatesTable = new CellTable<Message>();
        SimplePager.Resources linkedHubPagerResources = GWT.create(SimplePager.Resources.class);
        templatesPaginator = new SimplePager(TextLocation.CENTER, linkedHubPagerResources, false, 0, true);
        templatesPaginator.setPageSize(5);
        templatesTable.setPageSize(5);
        templatesPaginator.setDisplay(templatesTable);
        //       final SelectionModel<Message> linkedHubConnectionsSelectionModel = new MultiSelectionModel<Message>(
        //               new KeyProvider<Message>());
        //       templatesTable.setSelectionModel(linkedHubConnectionsSelectionModel,
        //               DefaultSelectionEventManager.<Message>createCheckboxManager());
        initTableColumns(templatesTable);

        initWidget(uiBinder.createAndBindUi(this));
        searchResults.setVisible(false);
        introduction.setVisible(true);
        feeds.setVisible(false);

        viewMenu.setCommand(new Command() {
            @Override
            public void execute() {
                presenter.onViewClicked(getSelectedQuestion());
            }
        });

        //       followMenu.setCommand(new Command() {
        //         @Override
        //         public void execute() {
        //            presenter.onFollowClicked(getSelectedQuestion());
        //         }
        //      });
        analyzeMenu.setCommand(new Command() {
            @Override
            public void execute() {
                presenter.onAnalyzeClicked(getSelectedQuestion());
            }
        });
        summarizeMenu.setCommand(new Command() {
            @Override
            public void execute() {
                presenter.onSummarizeClicked(getSelectedQuestion());
            }
        });
        suggestAnswerMenu.setCommand(new Command() {
            @Override
            public void execute() {
                presenter.onSuggestAnswerClicked(getSelectedQuestion());
            }
        });
        spellCheckMenu.setCommand(new Command() {
            @Override
            public void execute() {
                presenter.onSpellCheckClicked(getSelectedQuestion());
            }
        });
        speakMenu.setCommand(new Command() {
            @Override
            public void execute() {
                presenter.onSpeakClicked(getSelectedQuestion());
            }
        });
        runCodeMenu.setCommand(new Command() {
            @Override
            public void execute() {
                presenter.onRunCodeClicked(getSelectedQuestion());
            }
        });

        for (final TextLanguage language : TextLanguage.values()) {
            MenuItem menuItem = new MenuItem(language.name(), new Command() {

                @Override
                public void execute() {
                    presenter.onTranslateClicked(getSelectedQuestion(), language);
                }
            });
            menuItem.setEnabled(true);
            languageBar.addItem(menuItem);
        }
    }

    /* (non-Javadoc)
     * @see com.appspot.socialinquirer.client.view.HomeView#setClassifiers(java.util.ArrayList)
     */
    public void setClassifiers(ArrayList<Classifier> classifiers) {
        for (final Classifier item : classifiers) {
            MenuItem menuItem = new MenuItem(item.getName(), new Command() {

                @Override
                public void execute() {
                    presenter.onClassifyClicked(getSelectedQuestion(), item);
                }
            });
            menuItem.setEnabled(true);
            classifierBar.addItem(menuItem);
        }

    }

    /* (non-Javadoc)
     * @see com.appspot.socialinquirer.client.view.HomeView#setQuizzes(java.util.ArrayList)
     */
    public void setQuizzes(ArrayList<Quiz> quizzes) {
        for (final Quiz item : quizzes) {
            MenuItem menuItem = new MenuItem(item.getName(), new Command() {

                @Override
                public void execute() {
                    presenter.onAddToQuizClicked(getSelectedQuestion(), item);
                }
            });
            menuItem.setEnabled(true);
            quizBar.addItem(menuItem);
        }
    }

    /**
     * On questions search box changed.
     *
     * @param e the e
     */
    @UiHandler("questionsSearchBox")
    void onQuestionsSearchBoxChanged(KeyPressEvent e) {
        if (e.getCharCode() == ' ' || e.getCharCode() == KeyCodes.KEY_ENTER) {
            presenter.onQuestionsSearchPerformed(questionsSearchBox.getText());
        }
    }

    /**
     * On search clicked.
     *
     * @param e the e
     */
    @UiHandler("questionsSearch")
    void onSearchClicked(ClickEvent e) {
        presenter.onQuestionsSearchPerformed(questionsSearchBox.getText());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.appspot.linkedhub.client.view.ProjectView#setName(java.lang.String)
     */
    @Override
    public void setIntroduction(String introduction) {
        // introductionText.setHTML(introduction);
    }

    /* (non-Javadoc)
     * @see com.appspot.linkedhub.client.view.TasksView#setQuestions(java.util.List)
     */
    @Override
    public void setQuestions(ArrayList<Question> questions) {
        questionsTable.setRowData(questions);
        searchResults.setVisible(!questions.isEmpty());
        introduction.setVisible(questions.isEmpty());
        feeds.setVisible(false);
    }

    /* (non-Javadoc)
     * @see com.appspot.socialinquirer.client.view.HomeView#setMessages(java.util.ArrayList)
     */
    @Override
    public void setMessages(ArrayList<Message> message) {
        templatesTable.setRowData(message);
        searchResults.setVisible(false);
        introduction.setVisible(message.isEmpty());
        feeds.setVisible(!message.isEmpty());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.appspot.linkedhub.client.view.ProjectView#setPresenter(com.appspot.linkedhub.client.view.ProjectView.Presenter)
     */
    @Override
    public void setPresenter(Presenter listener) {
        this.presenter = listener;
    }

    /**
     * Gets the selected question.
     *
     * @return the selected question
     */
    private Question getSelectedQuestion() {
        for (Question question : questionsTable.getVisibleItems()) {
            if (questionsTable.getSelectionModel().isSelected(question)) {
                return question;
            }
        }

        return null;
    }

    /**
     * Inits the table columns.
     *
     * @param table the table
     * @param selectionModel the selection model
     */
    private void initTableColumns(CellTable<Question> table, final SelectionModel<Question> selectionModel) {
        // Checkbox column. This table will uses a checkbox column for selection.
        // Alternatively, you can call cellTable.setSelectionEnabled(true) to enable
        // mouse selection.
        Column<Question, Boolean> checkColumn = new Column<Question, Boolean>(new CheckboxCell(true, false)) {
            @Override
            public Boolean getValue(Question object) {
                // Get the value from the selection model.
                return selectionModel.isSelected(object);
            }
        };
        table.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));

        // Title.
        Column<Question, SafeHtml> titleColumn = new Column<Question, SafeHtml>(new SafeHtmlCell()) {
            @Override
            public SafeHtml getValue(Question object) {
                return SafeHtmlUtils.fromSafeConstant(object.getTitle());
            }
        };
        table.addColumn(titleColumn, constants.templatesTableColumnName());
        // Description.
        Column<Question, SafeHtml> textColumn = new Column<Question, SafeHtml>(new SafeHtmlCell()) {
            @Override
            public SafeHtml getValue(Question object) {
                return SafeHtmlUtils.fromSafeConstant(object.getContent());
            }
        };
        table.addColumn(textColumn, constants.templatesTableColumnSummary());
        // Due Date.
        Column<Question, Date> dueDateColumn = new Column<Question, Date>(new DateCell()) {
            @Override
            public Date getValue(Question object) {
                return object.getPublishedDate();
            }
        };
        table.addColumn(dueDateColumn, constants.templatesTableColumnTags());
    }

    /**
     * Inits the table columns.
     *
     * @param table the table
     */
    private void initTableColumns(CellTable<Message> table) {
        // Checkbox column. This table will uses a checkbox column for
        // selection.
        // Alternatively, you can call cellTable.setSelectionEnabled(true) to
        // enable
        // mouse selection.
        //       Column<Message, Boolean> checkColumn = new Column<Message, Boolean>(
        //           new CheckboxCell(true, false)) {
        //         @Override
        //         public Boolean getValue(Message object) {
        //           // Get the value from the selection model.
        //           return selectionModel.isSelected(object);
        //         }
        //       };
        //       table.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));

        // Name.
        Column<Message, String> nameColumn = new Column<Message, String>(new TextCell()) {
            @Override
            public String getValue(Message object) {
                return object.getName();
            }
        };
        table.addColumn(nameColumn, constants.templatesTableColumnName());
        // Headline.
        Column<Message, String> tagsColumn = new Column<Message, String>(new TextCell()) {
            @Override
            public String getValue(Message object) {
                StringBuilder builder = new StringBuilder();
                builder.append(object.getRecipient().getDisplayName());
                return builder.toString();

            }
        };
        table.addColumn(tagsColumn, constants.templatesTableColumnTags());
        // Description
        Column<Message, SafeHtml> descColumn = new Column<Message, SafeHtml>(new SafeHtmlCell()) {
            @Override
            public SafeHtml getValue(Message object) {
                SafeHtmlBuilder builder = new SafeHtmlBuilder();
                builder.appendHtmlConstant(object.getDescription());
                return builder.toSafeHtml();
            }
        };
        table.addColumn(descColumn, constants.templatesTableColumnSummary());
    }
}