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

Java tutorial

Introduction

Here is the source code for com.appspot.socialinquirer.client.view.NetworkViewImpl.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 com.appspot.socialinquirer.client.KeyProvider;
import com.appspot.socialinquirer.client.constant.EverScribeConstants;
import com.appspot.socialinquirer.shared.dto.User;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
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.Window;
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 NetworkViewImpl.
 */
public class NetworkViewImpl extends Composite implements NetworkView {

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

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

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

    /**
     * The Interface NetworkViewUiBinder.
     */
    @UiTemplate("NetworkView.ui.xml")
    interface NetworkViewUiBinder extends UiBinder<Widget, NetworkViewImpl> {
    }

    /** The connections table. */
    @UiField(provided = true)
    CellTable<User> connectionsTable;

    /** The connections paginator. */
    @UiField(provided = true)
    SimplePager connectionsPaginator;

    /** The endorse menu. */
    @UiField
    MenuItem followMenu, endorseMenu;

    /** The page bar. */
    @UiField
    MenuBar pageBar;

    /**
     * Instantiates a new network view impl.
     *
     * @param constants the constants
     */
    public NetworkViewImpl(EverScribeConstants constants) {
        this.constants = constants;
        GWT.runAsync(new RunAsyncCallback() {
            public void onFailure(Throwable caught) {
                Window.alert("Code download failed");
            }

            public void onSuccess() {
                connectionsTable = new CellTable<User>();
                SimplePager.Resources linkedHubPagerResources = GWT.create(SimplePager.Resources.class);
                connectionsPaginator = new SimplePager(TextLocation.CENTER, linkedHubPagerResources, false, 0,
                        true);
                connectionsPaginator.setDisplay(connectionsTable);
                final SelectionModel<User> linkedHubConnectionsSelectionModel = new SingleSelectionModel<User>(
                        new KeyProvider<User>());
                connectionsTable.setSelectionModel(linkedHubConnectionsSelectionModel,
                        DefaultSelectionEventManager.<User>createCheckboxManager());
                initTableColumns(connectionsTable, linkedHubConnectionsSelectionModel);

                initWidget(uiBinder.createAndBindUi(NetworkViewImpl.this));
            }
        });

    }

    /* (non-Javadoc)
     * @see com.appspot.linkedhub.client.view.ConnectionsView#setName(java.lang.String)
     */
    @Override
    public void setName(String name) {
        // TODO Auto-generated method stub

    }

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

    /* (non-Javadoc)
     * @see com.appspot.linkedhub.client.view.ConnectionsView#setLinkedHubConnections(java.util.List)
     */
    @Override
    public void setConnections(ArrayList<User> linkedHubConnections) {
        connectionsTable.setRowData(linkedHubConnections);
    }

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

        // Name.
        Column<User, String> nameColumn = new Column<User, String>(new ClickableTextCell()) {
            @Override
            public String getValue(User object) {
                return object.getFullName();
            }
        };
        table.addColumn(nameColumn, constants.connectionsTableColumnName());
        // Headline.
        Column<User, String> headlineColumn = new Column<User, String>(new ClickableTextCell()) {
            @Override
            public String getValue(User object) {
                return String.valueOf(object.getReputation());
            }
        };
        table.addColumn(headlineColumn, constants.connectionsTableColumnReputation());
        // Summary.
        Column<User, String> projectsColumn = new Column<User, String>(new ClickableTextCell()) {
            @Override
            public String getValue(User object) {
                return String.valueOf(object.getAcceptRate());
            }
        };
        table.addColumn(projectsColumn, constants.connectionsTableColumnAcceptRate());
    }
}