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

Java tutorial

Introduction

Here is the source code for com.appspot.socialinquirer.client.view.QuestionViewImpl.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.Answer;
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.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.DisclosurePanel;
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.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;

/**
 * The Class QuestionViewImpl.
 */
public class QuestionViewImpl extends Composite implements QuestionView {

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

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

    /** The question. */
    private Question question;

    /** The answers. */
    @UiField
    DisclosurePanel answers;

    /** The answers table. */
    @UiField(provided = true)
    CellTable<Answer> answersTable;

    /** The answers paginator. */
    @UiField(provided = true)
    SimplePager answersPaginator;

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

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

    /** The question content. */
    @UiField
    HTML questionTitle, questionPostedDate, questionContent;

    /** The endorse question author menu. */
    @UiField
    MenuItem questionAuthor, followQuestionAuthorMenu, endorseQuestionAuthorMenu;

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

    /**
     * The Interface QuestionViewUiBinder.
     */
    @UiTemplate("QuestionView.ui.xml")
    interface QuestionViewUiBinder extends UiBinder<Widget, QuestionViewImpl> {
    }

    /**
     * Instantiates a new question view impl.
     *
     * @param constants the constants
     */
    public QuestionViewImpl(final EverScribeConstants constants) {
        this.constants = constants;
        answersTable = new CellTable<Answer>();
        SimplePager.Resources ideasPagerResources = GWT.create(SimplePager.Resources.class);
        answersPaginator = new SimplePager(TextLocation.CENTER, ideasPagerResources, false, 0, true);
        answersPaginator.setDisplay(answersTable);
        final SelectionModel<Answer> answerSelectionModel = new SingleSelectionModel<Answer>(
                new KeyProvider<Answer>());
        answersTable.setSelectionModel(answerSelectionModel,
                DefaultSelectionEventManager.<Answer>createCheckboxManager());
        initTableColumns(answersTable, answerSelectionModel);

        initWidget(uiBinder.createAndBindUi(this));

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

        followQuestionAuthorMenu.setCommand(new Command() {
            @Override
            public void execute() {
                presenter.onFollowUserClicked(questionAuthor.getText());
            }
        });

        //       followAnswerAuthorMenu.setCommand(new Command() {
        //         @Override
        //         public void execute() {
        //            presenter.onFollowUserClicked(answerAuthor.getText());
        //         }
        //      });

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

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

        answers.setAnimationEnabled(true);
    }

    /*
     * (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.QuestionView#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(question, item);
                }
            });
            menuItem.setEnabled(true);
            classifierBar.addItem(menuItem);
        }

    }

    /* (non-Javadoc)
     * @see com.appspot.socialinquirer.client.view.QuestionView#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(question, 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.socialinquirer.client.view.QuestionView#setQuestion(com.appspot.socialinquirer.shared.dto.Question)
     */

    @Override
    public void setQuestion(Question question) {
        this.question = question;
        if (question != null) {
            questionTitle.setHTML(question.getTitle());
            questionPostedDate.setHTML(question.getPublishedDate().toString());
            questionContent.setHTML(question.getContent());
            questionAuthor.setText(question.getAuthor());
            answersTable.setRowData(question.getAnswers());
        }
    }

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

        // Description.
        Column<Answer, SafeHtml> textColumn = new Column<Answer, SafeHtml>(new SafeHtmlCell()) {
            @Override
            public SafeHtml getValue(Answer object) {
                return SafeHtmlUtils.fromSafeConstant(object.getContent());
            }
        };
        table.addColumn(textColumn, constants.templatesTableColumnSummary());
        // Status.
        Column<Answer, SafeHtml> authorColumn = new Column<Answer, SafeHtml>(new SafeHtmlCell()) {
            @Override
            public SafeHtml getValue(Answer object) {
                return SafeHtmlUtils.fromSafeConstant(object.getAuthor());
            }
        };
        table.addColumn(authorColumn, constants.templatesTableColumnAuthor());
        // Due Date.
        Column<Answer, Date> dueDateColumn = new Column<Answer, Date>(new DateCell()) {
            @Override
            public Date getValue(Answer object) {
                return object.getPublishedDate();
            }
        };
        table.addColumn(dueDateColumn, constants.templatesTableColumnPostedDate());
    }

    /* (non-Javadoc)
     * @see com.appspot.socialinquirer.client.view.QuestionView#getQuestion()
     */
    @Override
    public Question getQuestion() {
        return question;
    }
}