/**
* Copyright 2010 Daniel Guermeur and Amy Unruh
*
* 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.
*
* See http://connectrapp.appspot.com/ for a demo, and links to more information
* about this app and the book that it accompanies.
*/
package com.casamind.adware.client.view;
import java.util.Comparator;
import com.casamind.adware.client.presenter.CompanyEditPresenter;
import com.casamind.adware.client.resources.DisplayMessages;
import com.casamind.adware.client.resources.UIConstants;
import com.casamind.adware.shared.AuthTypes;
import com.casamind.adware.shared.model.PublisherDTO;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionModel;
/**
* Demonstrate UIbinder Styles Separating UI and actions Leaving w/o saving
* warning
*
*/
public class CompanyEditView extends Composite implements
CompanyEditPresenter.Display, ValueChangeHandler<String>,
ChangeHandler, ClickHandler {
private static CompanyEditUiBinder uiBinder = GWT.create(CompanyEditUiBinder.class);
interface CompanyEditUiBinder extends UiBinder<Widget, CompanyEditView> {
}
private Column<PublisherDTO, String> clickableColumn;
private ListDataProvider<PublisherDTO> dataProvider;
private ProvidesKey<PublisherDTO> KEY_PROVIDER;
private final UIConstants constants = GWT.create(UIConstants.class);
private final DisplayMessages messages = GWT.create(DisplayMessages.class);
@UiField
CheckBox newsletterCheckbox, notificationCheckbox;
@UiField
TextBox authIdField, nameField, streetField, cityField, phoneField,
mobileField, faxField, emailField, websiteField;
@UiField
ListBox authProvidersList;
@UiField
Button cancelButton, saveButton, deleteButton;
@UiField(provided = true)
CellTable<PublisherDTO> cellTable;
@UiField(provided = true)
SimplePager pager;
@UiField
Anchor navigate;
public CompanyEditView() {
initCellTable();
initWidget(uiBinder.createAndBindUi(this));
initChangeHandlers();
authProvidersList.addItem(AuthTypes.GOOGLE);
// authProvidersList.addItem(AuthTypes.FACEBOOK);
// authProvidersList.addItem(AuthTypes.TWITTER);
}
private void initChangeHandlers() {
this.saveButton.setEnabled(false);
this.authIdField.addValueChangeHandler(this);
this.nameField.addValueChangeHandler(this);
this.streetField.addValueChangeHandler(this);
this.cityField.addValueChangeHandler(this);
this.phoneField.addValueChangeHandler(this);
this.mobileField.addValueChangeHandler(this);
this.faxField.addValueChangeHandler(this);
this.emailField.addValueChangeHandler(this);
this.websiteField.addValueChangeHandler(this);
this.newsletterCheckbox.addClickHandler(this);
this.notificationCheckbox.addClickHandler(this);
this.authProvidersList.addChangeHandler(this);
}
private void dtoStateChanged() {
this.saveButton.setEnabled(isValideInput());
}
private boolean isValideInput() {
return !"".equals(this.authIdField.getValue().trim()) && !"".equals(this.nameField.getValue().trim());
}
private void initCellTable() {
this.KEY_PROVIDER = new ProvidesKey<PublisherDTO>() {
public Object getKey(PublisherDTO item) {
return item == null ? null : item.getId();
}
};
this.cellTable = new CellTable<PublisherDTO>(KEY_PROVIDER);
this.dataProvider = new ListDataProvider<PublisherDTO>();
// Set the message to display when the table is empty.
this.cellTable.setEmptyTableWidget(new Label(messages.noResultsFound()));
// Attach a column sort handler to the ListDataProvider to sort the
// list.
ListHandler<PublisherDTO> sortHandler = new ListHandler<PublisherDTO>(this.dataProvider.getList());
cellTable.addColumnSortHandler(sortHandler);
// Create a Pager to control the table.
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
pager.setDisplay(cellTable);
// Add a selection model so we can select cells.
final SelectionModel<PublisherDTO> selectionModel = new MultiSelectionModel<PublisherDTO>(this.KEY_PROVIDER);
cellTable.setSelectionModel(selectionModel, DefaultSelectionEventManager.<PublisherDTO> createCheckboxManager());
// Initialize the columns.
initTableColumns(selectionModel, sortHandler);
}
private void initTableColumns(SelectionModel<PublisherDTO> selectionModel, ListHandler<PublisherDTO> sortHandler) {
this.cellTable.setWidth("100%", true);
// Name.
Column<PublisherDTO, String> nameColumn = new Column<PublisherDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(PublisherDTO object) {
return object.getDisplayName();
}
};
nameColumn.setSortable(true);
sortHandler.setComparator(nameColumn, new Comparator<PublisherDTO>() {
public int compare(PublisherDTO o1, PublisherDTO o2) {
return o1.getDisplayName().compareTo(o2.getDisplayName());
}
});
this.cellTable.addColumn(nameColumn, this.constants.name());
this.clickableColumn = nameColumn;
// Unique Id
Column<PublisherDTO, String> uniqueIdColumn = new Column<PublisherDTO, String>(new TextCell()) {
@Override
public String getValue(PublisherDTO object) {
return object.getLogin();
}
};
uniqueIdColumn.setSortable(true);
sortHandler.setComparator(uniqueIdColumn, new Comparator<PublisherDTO>() {
public int compare(PublisherDTO o1, PublisherDTO o2) {
return o1.getUniqueId().compareTo(o2.getUniqueId());
}
});
this.cellTable.addColumn(uniqueIdColumn, this.constants.uniqueId());
// Phone
Column<PublisherDTO, String> phoneColumn = new Column<PublisherDTO, String>(new TextCell()) {
@Override
public String getValue(PublisherDTO object) {
return object.getPhone();
}
};
phoneColumn.setSortable(true);
sortHandler.setComparator(phoneColumn, new Comparator<PublisherDTO>() {
public int compare(PublisherDTO o1, PublisherDTO o2) {
return o1.getPhone().compareTo(o2.getPhone());
}
});
this.cellTable.addColumn(phoneColumn, this.constants.phone());
// Email
Column<PublisherDTO, String> emailColumn = new Column<PublisherDTO, String>(new TextCell()) {
@Override
public String getValue(PublisherDTO object) {
return object.getEmail();
}
};
emailColumn.setSortable(true);
sortHandler.setComparator(emailColumn, new Comparator<PublisherDTO>() {
public int compare(PublisherDTO o1, PublisherDTO o2) {
return o1.getEmail().compareTo(o2.getEmail());
}
});
this.cellTable.addColumn(emailColumn, this.constants.email());
// Produits.
Column<PublisherDTO, Number> productColumn = new Column<PublisherDTO, Number>(new NumberCell()) {
@Override
public Number getValue(PublisherDTO object) {
return object.getProducts() == null ? 0 : object.getProducts().size();
}
};
productColumn.setSortable(true);
this.cellTable.addColumn(productColumn, constants.products());
this.cellTable.setColumnWidth(productColumn, 60, Unit.PT);
// Slots.
Column<PublisherDTO, Number> slotColumn = new Column<PublisherDTO, Number>(new NumberCell()) {
@Override
public Number getValue(PublisherDTO object) {
return object.getSlots() == null ? 0 : object.getSlots().size();
}
};
slotColumn.setSortable(true);
this.cellTable.addColumn(slotColumn, constants.schedules());
this.cellTable.setColumnWidth(slotColumn, 60, Unit.PT);
}
@Override
protected void onLoad() {
super.onLoad();
nameField.setFocus(true);
}
@Override
public Widget asWidget() {
return this;
}
public boolean isPermanentView() {
return false;
}
@Override
public HasClickHandlers getCancelButton() {
return cancelButton;
}
@Override
public HasClickHandlers getSaveButton() {
return saveButton;
}
@Override
public HasClickHandlers getDeleteButton() {
return deleteButton;
}
@Override
public HasValue<String> getName() {
return nameField;
}
@Override
public HasValue<String> getStreet() {
return streetField;
}
@Override
public HasValue<String> getCity() {
return cityField;
}
@Override
public HasValue<String> getPhone() {
return phoneField;
}
@Override
public HasValue<String> getMobile() {
return mobileField;
}
@Override
public HasValue<String> getFax() {
return faxField;
}
@Override
public HasValue<String> getEmail() {
return emailField;
}
@Override
public HasValue<String> getWebsite() {
return websiteField;
}
@Override
public HasValue<String> getLogin() {
return authIdField;
}
@Override
public ListBox getServicesList() {
return authProvidersList;
}
@Override
public HasData<PublisherDTO> getPubisherGrid() {
return this.cellTable;
}
@Override
public ListDataProvider<PublisherDTO> getDataProvider() {
return this.dataProvider;
}
@Override
public void addDataDisplay(HasData<PublisherDTO> display) {
this.dataProvider.addDataDisplay(display);
}
@Override
public HasValue<Boolean> isReceiveNewsLetter() {
return this.newsletterCheckbox;
}
@Override
public HasValue<Boolean> isReceiveNotifications() {
return this.notificationCheckbox;
}
@Override
public HasClickHandlers getNavigationLink() {
return this.navigate;
}
@Override
public void hideNavigationLink() {
this.navigate.setVisible(false);
}
@Override
public Column<PublisherDTO, String> getClickableColumn() {
return this.clickableColumn;
}
@Override
public void onChange(ChangeEvent event) {
this.dtoStateChanged();
}
@Override
public void onValueChange(ValueChangeEvent<String> event) {
this.dtoStateChanged();
}
@Override
public void onClick(ClickEvent event) {
this.dtoStateChanged();
}
}