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

Java tutorial

Introduction

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

import com.appspot.socialinquirer.client.KeyProvider;
import com.appspot.socialinquirer.client.constant.EverScribeConstants;
import com.appspot.socialinquirer.shared.dto.Answer;
import com.appspot.socialinquirer.shared.dto.Question;
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.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
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 QuizViewImpl.
 */
public class QuizViewImpl extends Composite implements QuizView {

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

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

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

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

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

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

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

    /**
     * The Interface QuizViewUiBinder.
     */
    @UiTemplate("QuizView.ui.xml")
    interface QuizViewUiBinder extends UiBinder<Widget, QuizViewImpl> {
    }

    /**
     * Instantiates a new quiz view impl.
     *
     * @param constants the constants
     */
    public QuizViewImpl(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));
    }

    /*
     * (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.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.QuizView#setQuestion(com.appspot.socialinquirer.shared.dto.Question)
     */

    @Override
    public void setQuestion(Question question) {
        this.question = question;
        if (question != null) {
            questionTitle.setHTML(question.getTitle());
            questionContent.setHTML(question.getContent());
            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.QuizView#getQuestion()
     */
    @Override
    public Question getQuestion() {
        return question;
    }
}