package com.casamind.adware.client.view;
import java.util.Comparator;
import com.casamind.adware.client.presenter.PublisherEditPresenter;
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.ProductSummaryDTO;
import com.casamind.adware.shared.model.SlotSummaryDTO;
import com.google.gwt.cell.client.NumberCell;
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.i18n.client.DateTimeFormat;
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;
public class PublisherEditView extends Composite implements
PublisherEditPresenter.Display, ValueChangeHandler<String>,
ChangeHandler, ClickHandler {
private static PublisherEditViewUiBinder uiBinder = GWT.create(PublisherEditViewUiBinder.class);
interface PublisherEditViewUiBinder extends
UiBinder<Widget, PublisherEditView> {
}
private Column<SlotSummaryDTO, String> slotClickableColumn;
private Column<ProductSummaryDTO, String> productClickableColumn;
private ListDataProvider<SlotSummaryDTO> slotDataProvider;
private ListDataProvider<ProductSummaryDTO> productDataProvider;
private ProvidesKey<SlotSummaryDTO> SLOT_KEY_PROVIDER;
private ProvidesKey<ProductSummaryDTO> PRODUCT_KEY_PROVIDER;
private final UIConstants constants = GWT.create(UIConstants.class);
private final DisplayMessages messages = GWT.create(DisplayMessages.class);
@UiField
CheckBox newsletterCheckbox, notificationCheckbox;
@UiField
TextBox firstnameField, lastnameField, emailField, phoneField, authIdField;
@UiField
ListBox authProvidersList, companyList;
@UiField
Button cancelButton, saveButton, deleteButton;
@UiField(provided = true)
CellTable<SlotSummaryDTO> slotGrid;
@UiField(provided = true)
CellTable<ProductSummaryDTO> productGrid;
@UiField(provided = true)
SimplePager slotPager, productPager;
@UiField
Anchor navigate;
public PublisherEditView() {
initCellTables();
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.firstnameField.addValueChangeHandler(this);
this.lastnameField.addValueChangeHandler(this);
this.emailField.addValueChangeHandler(this);
this.phoneField.addChangeHandler(this);
this.authIdField.addValueChangeHandler(this);
this.newsletterCheckbox.addClickHandler(this);
this.notificationCheckbox.addClickHandler(this);
this.authProvidersList.addChangeHandler(this);
this.companyList.addChangeHandler(this);
}
private void dtoStateChanged() {
this.saveButton.setEnabled(isValideInput());
}
private boolean isValideInput() {
return !"".equals(this.authIdField.getValue().trim()) && !"".equals(this.lastnameField.getValue().trim()) && !"".equals(this.firstnameField.getValue().trim());
}
private void initCellTables() {
initProductTable();
initSlotTable();
}
private void initSlotTable() {
this.SLOT_KEY_PROVIDER = new ProvidesKey<SlotSummaryDTO>() {
public Object getKey(SlotSummaryDTO item) {
return item == null ? null : item.getId();
}
};
this.slotGrid = new CellTable<SlotSummaryDTO>(SLOT_KEY_PROVIDER);
this.slotDataProvider = new ListDataProvider<SlotSummaryDTO>();
// Set the message to display when the table is empty.
this.slotGrid.setEmptyTableWidget(new Label(this.messages.noResultsFound()));
// Attach a column sort handler to the ListDataProvider to sort the
// list.
ListHandler<SlotSummaryDTO> sortHandler = new ListHandler<SlotSummaryDTO>(this.slotDataProvider.getList());
this.slotGrid.addColumnSortHandler(sortHandler);
// Create a Pager to control the table.
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
this.slotPager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
this.slotPager.setDisplay(this.slotGrid);
// Add a selection model so we can select cells.
final SelectionModel<SlotSummaryDTO> selectionModel = new MultiSelectionModel<SlotSummaryDTO>(this.SLOT_KEY_PROVIDER);
this.slotGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<SlotSummaryDTO> createCheckboxManager());
// Initialize the columns.
initSlotTableColumns(selectionModel, sortHandler);
}
private void initProductTable() {
this.PRODUCT_KEY_PROVIDER = new ProvidesKey<ProductSummaryDTO>() {
public Object getKey(ProductSummaryDTO item) {
return item == null ? null : item.getId();
}
};
this.productGrid = new CellTable<ProductSummaryDTO>(this.PRODUCT_KEY_PROVIDER);
this.productDataProvider = new ListDataProvider<ProductSummaryDTO>();
// Set the message to display when the table is empty.
this.productGrid.setEmptyTableWidget(new Label(this.messages.noResultsFound()));
// Attach a column sort handler to the ListDataProvider to sort the
// list.
ListHandler<ProductSummaryDTO> sortHandler = new ListHandler<ProductSummaryDTO>(this.productDataProvider.getList());
this.productGrid.addColumnSortHandler(sortHandler);
// Create a Pager to control the table.
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
this.productPager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
this.productPager.setDisplay(this.productGrid);
// Add a selection model so we can select cells.
final SelectionModel<ProductSummaryDTO> selectionModel = new MultiSelectionModel<ProductSummaryDTO>(this.PRODUCT_KEY_PROVIDER);
this.productGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<ProductSummaryDTO> createCheckboxManager());
// Initialize the columns.
initProductTableColumns(selectionModel, sortHandler);
}
private void initSlotTableColumns(final SelectionModel<SlotSummaryDTO> selectionModel, ListHandler<SlotSummaryDTO> sortHandler) {
this.slotGrid.setWidth("100%", true);
// Start date.
Column<SlotSummaryDTO, String> startDateColumn = new Column<SlotSummaryDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(SlotSummaryDTO object) {
return DateTimeFormat.getFormat(constants.dateFormat()).format(object.getStartDate());
}
};
startDateColumn.setSortable(true);
sortHandler.setComparator(startDateColumn, new Comparator<SlotSummaryDTO>() {
public int compare(SlotSummaryDTO o1, SlotSummaryDTO o2) {
return o1.getStartDate().compareTo(o2.getStartDate());
}
});
this.slotGrid.addColumn(startDateColumn, this.constants.startDate());
this.slotClickableColumn = startDateColumn;
// Price.
Column<SlotSummaryDTO, Number> priceColumn = new Column<SlotSummaryDTO, Number>(new NumberCell()) {
@Override
public Number getValue(SlotSummaryDTO object) {
return object.getPrice();
}
};
priceColumn.setSortable(true);
sortHandler.setComparator(priceColumn, new Comparator<SlotSummaryDTO>() {
public int compare(SlotSummaryDTO o1, SlotSummaryDTO o2) {
return (int) (o2.getPrice() - o1.getPrice());
}
});
this.slotGrid.addColumn(priceColumn, this.constants.price());
this.slotGrid.setColumnWidth(priceColumn, 50, Unit.PT);
}
private void initProductTableColumns(SelectionModel<ProductSummaryDTO> selectionModel, ListHandler<ProductSummaryDTO> sortHandler) {
this.productGrid.setWidth("100%", true);
// Name
Column<ProductSummaryDTO, String> nameColumn = new Column<ProductSummaryDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(ProductSummaryDTO object) {
return object.getDisplayName();
}
};
nameColumn.setSortable(true);
sortHandler.setComparator(nameColumn, new Comparator<ProductSummaryDTO>() {
public int compare(ProductSummaryDTO o1, ProductSummaryDTO o2) {
return o1.getDisplayName().compareTo(o2.getDisplayName());
}
});
this.productGrid.addColumn(nameColumn, this.constants.name());
this.productClickableColumn = nameColumn;
// Schedules.
Column<ProductSummaryDTO, Number> nbColumn = new Column<ProductSummaryDTO, Number>(new NumberCell()) {
@Override
public Number getValue(ProductSummaryDTO object) {
return object.getNbSlots();
}
};
nbColumn.setSortable(true);
sortHandler.setComparator(nbColumn, new Comparator<ProductSummaryDTO>() {
public int compare(ProductSummaryDTO o1, ProductSummaryDTO o2) {
return (int) (o2.getNbSlots() - o1.getNbSlots());
}
});
this.productGrid.addColumn(nbColumn, this.constants.nbSchedules());
this.productGrid.setColumnWidth(nbColumn, 50, Unit.PT);
}
public boolean isPermanentView() {
return false;
}
@Override
public HasValue<String> getLogin() {
return this.authIdField;
}
@Override
public ListBox getServicesList() {
return this.authProvidersList;
}
@Override
public HasValue<String> getFirstname() {
return this.firstnameField;
}
@Override
public HasValue<String> getLastname() {
return this.lastnameField;
}
@Override
public HasValue<String> getEmail() {
return this.emailField;
}
@Override
public HasValue<String> getPhone() {
return this.phoneField;
}
@Override
public HasClickHandlers getSaveButton() {
return this.saveButton;
}
@Override
public HasClickHandlers getDeleteButton() {
return this.deleteButton;
}
@Override
public HasClickHandlers getCancelButton() {
return this.cancelButton;
}
@Override
public Widget asWidget() {
return this;
}
@Override
public void addProductDataDisplay(HasData<ProductSummaryDTO> display) {
this.productDataProvider.addDataDisplay(display);
}
@Override
public void addSlotDataDisplay(HasData<SlotSummaryDTO> display) {
this.slotDataProvider.addDataDisplay(display);
}
@Override
public HasData<ProductSummaryDTO> getProductGrid() {
return this.productGrid;
}
@Override
public HasData<SlotSummaryDTO> getSlotGrid() {
return this.slotGrid;
}
@Override
public ListDataProvider<ProductSummaryDTO> getProductDataProvider() {
return this.productDataProvider;
}
@Override
public ListDataProvider<SlotSummaryDTO> getSlotDataProvider() {
return this.slotDataProvider;
}
@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 ListBox getCompanyList() {
return this.companyList;
}
@Override
public Column<ProductSummaryDTO, String> getProductClickableColumn() {
return this.productClickableColumn;
}
@Override
public Column<SlotSummaryDTO, String> getSlotClickableColumn() {
return this.slotClickableColumn;
}
@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();
}
}