Java tutorial
/* * 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.List; import com.appspot.socialinquirer.client.KeyProvider; import com.appspot.socialinquirer.client.constant.EverScribeConstants; import com.appspot.socialinquirer.shared.dto.Page; import com.google.gwt.cell.client.CheckboxCell; 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.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.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.Widget; import com.google.gwt.view.client.DefaultSelectionEventManager; import com.google.gwt.view.client.ListDataProvider; import com.google.gwt.view.client.MultiSelectionModel; import com.google.gwt.view.client.SelectionModel; /** * The Class PagesViewImpl. */ public class PagesViewImpl extends Composite implements PagesView { /** The ui binder. */ private static PagesViewUiBinder uiBinder = GWT.create(PagesViewUiBinder.class); /** The presenter. */ private Presenter presenter; /** The constants. */ private EverScribeConstants constants; /** The templates table. */ @UiField(provided = true) CellTable<Page> templatesTable; /** The templates paginator. */ @UiField(provided = true) SimplePager templatesPaginator; /** The data provider. */ private ListDataProvider<Page> dataProvider; /** * The Interface PagesViewUiBinder. */ @UiTemplate("PagesView.ui.xml") interface PagesViewUiBinder extends UiBinder<Widget, PagesViewImpl> { } /** * Instantiates a new pages view impl. * * @param constants the constants * @param master the master */ public PagesViewImpl(final EverScribeConstants constants, final boolean master) { this.constants = constants; templatesTable = new CellTable<Page>(); 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<Page> linkedHubConnectionsSelectionModel = new MultiSelectionModel<Page>( new KeyProvider<Page>()); templatesTable.setSelectionModel(linkedHubConnectionsSelectionModel, DefaultSelectionEventManager.<Page>createCheckboxManager()); initTableColumns(templatesTable, linkedHubConnectionsSelectionModel); initWidget(uiBinder.createAndBindUi(PagesViewImpl.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.PagesView#setPages(java.util.List) */ @Override public void setPages(List<Page> templates) { dataProvider = new ListDataProvider<Page>(templates); dataProvider.addDataDisplay(templatesTable); } /* (non-Javadoc) * @see com.appspot.socialinquirer.client.view.PagesView#addPages(java.util.List) */ @Override public void addPages(List<Page> templates) { dataProvider.getList().addAll(templates); } /** * Inits the table columns. * * @param table the table * @param selectionModel the selection model */ private void initTableColumns(CellTable<Page> table, final SelectionModel<Page> selectionModel) { // Checkbox column. This table will uses a checkbox column for // selection. // Alternatively, you can call cellTable.setSelectionEnabled(true) to // enable // mouse selection. Column<Page, Boolean> checkColumn = new Column<Page, Boolean>(new CheckboxCell(true, false)) { @Override public Boolean getValue(Page object) { // Get the value from the selection model. return selectionModel.isSelected(object); } }; table.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>")); // Name. Column<Page, String> nameColumn = new Column<Page, String>(new TextCell()) { @Override public String getValue(Page object) { return object.getName(); } }; table.addColumn(nameColumn, constants.templatesTableColumnName()); // Headline. Column<Page, String> tagsColumn = new Column<Page, String>(new TextCell()) { @Override public String getValue(Page object) { StringBuilder builder = new StringBuilder(); for (String tag : object.getTags()) { builder.append(tag).append(" "); } return builder.toString(); } }; table.addColumn(tagsColumn, constants.templatesTableColumnTags()); // Description Column<Page, SafeHtml> descColumn = new Column<Page, SafeHtml>(new SafeHtmlCell()) { @Override public SafeHtml getValue(Page object) { SafeHtmlBuilder builder = new SafeHtmlBuilder(); builder.appendHtmlConstant(object.getDescription()); return builder.toSafeHtml(); } }; table.addColumn(descColumn, constants.templatesTableColumnSummary()); } }