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

Java tutorial

Introduction

Here is the source code for com.appspot.socialinquirer.client.view.QuestionsViewImpl.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.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.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtml;
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.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.Composite;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;

/**
 * The Class QuestionsViewImpl.
 */
public class QuestionsViewImpl extends Composite implements QuestionsView {

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

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

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

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

    // 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 QuestionsListViewUiBinder.
     */
    @UiTemplate("QuestionsView.ui.xml")
    interface QuestionsListViewUiBinder extends UiBinder<Widget, QuestionsViewImpl> {
    }

    /**
     * Instantiates a new questions view impl.
     *
     * @param constants the constants
     */
    public QuestionsViewImpl(final EverScribeConstants constants) {
        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);
        initWidget(uiBinder.createAndBindUi(this));

        // menu commands
        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.linkedhub.client.view.ProjectView#setName(java.lang.String)
     */
    @Override
    public void setName(String name) {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.appspot.socialinquirer.client.view.QuestionsView#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.QuestionsView#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);
        }
    }

    /*
     * (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;
    }

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

    /**
     * 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());
        // Status.
        Column<Question, SafeHtml> statusColumn = new Column<Question, SafeHtml>(new SafeHtmlCell()) {
            @Override
            public SafeHtml getValue(Question object) {
                return SafeHtmlUtils.fromSafeConstant(object.getTags().toString());
            }
        };
        table.addColumn(statusColumn, constants.templatesTableColumnTags());
        Column<Question, SafeHtml> authorColumn = new Column<Question, SafeHtml>(new SafeHtmlCell()) {
            @Override
            public SafeHtml getValue(Question object) {
                return SafeHtmlUtils.fromSafeConstant(object.getAuthor());
            }
        };
        table.addColumn(authorColumn, constants.templatesTableColumnAuthor());

        // 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.templatesTableColumnPostedDate());
    }
}