package com.casamind.adware.client.view;
import java.util.Comparator;
import com.casamind.adware.client.presenter.SlotEditPresenter;
import com.casamind.adware.client.resources.DisplayMessages;
import com.casamind.adware.client.resources.UIConstants;
import com.casamind.adware.shared.SlotStatus;
import com.casamind.adware.shared.model.ProductSummaryDTO;
import com.google.gwt.core.client.GWT;
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.event.shared.HasHandlers;
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.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasText;
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.RadioButton;
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 SlotEditView extends Composite implements HasHandlers,
SlotEditPresenter.Display, ValueChangeHandler<String>, ChangeHandler,
ClickHandler {
private static SlotEditViewUiBinder uiBinder = GWT.create(SlotEditViewUiBinder.class);
interface SlotEditViewUiBinder extends UiBinder<Widget, SlotEditView> {
}
private Column<ProductSummaryDTO, String> selectedProductsClickableColumn;
private Column<ProductSummaryDTO, String> selectedProductsDeleteColumn;
private Column<ProductSummaryDTO, String> otherProductsClickableColumn;
private Column<ProductSummaryDTO, String> otherProductsDeleteColumnn;
private ListDataProvider<ProductSummaryDTO> otherProductsDataProvider;
private ListDataProvider<ProductSummaryDTO> selectedProductsDataProvider;
private ProvidesKey<ProductSummaryDTO> OTHER_PRODUCTS_KEY_PROVIDER;
private ProvidesKey<ProductSummaryDTO> SELECTED_PRODUCTS_KEY_PROVIDER;
private final UIConstants constants = GWT.create(UIConstants.class);
private final DisplayMessages messages = GWT.create(DisplayMessages.class);
@UiField
RadioButton altButton, seqButton;
@UiField
Label startDateField, endDateField, numberOfSlotsField, priceField,
statusField;
@UiField
ListBox publisherList, companyList;
@UiField
Button cancelButton, saveButton, deleteButton;
@UiField(provided = true)
CellTable<ProductSummaryDTO> selectedProductsGrid, otherProductsGrid;
@UiField(provided = true)
SimplePager selectedProductsPager, otherProductsPager;
public SlotEditView() {
initCellTables();
initWidget(uiBinder.createAndBindUi(this));
initChangeHandlers();
}
private void initChangeHandlers() {
this.saveButton.setEnabled(false);
this.altButton.addClickHandler(this);
this.seqButton.addClickHandler(this);
this.publisherList.addChangeHandler(this);
this.companyList.addChangeHandler(this);
}
private void dtoStateChanged() {
this.saveButton.setEnabled(isValideInput());
}
private boolean isValideInput() {
return this.selectedProductsDataProvider.getList().size() > 0;
}
private void initCellTables() {
initSelectedProductsTable();
initOtherProductsTable();
}
private void initSelectedProductsTable() {
this.SELECTED_PRODUCTS_KEY_PROVIDER = new ProvidesKey<ProductSummaryDTO>() {
public Object getKey(ProductSummaryDTO item) {
return item == null ? null : item.getId();
}
};
this.selectedProductsGrid = new CellTable<ProductSummaryDTO>(SELECTED_PRODUCTS_KEY_PROVIDER);
this.selectedProductsDataProvider = new ListDataProvider<ProductSummaryDTO>();
// Set the message to display when the table is empty.
this.selectedProductsGrid.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.selectedProductsDataProvider.getList());
selectedProductsGrid.addColumnSortHandler(sortHandler);
// Create a Pager to control the table.
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
selectedProductsPager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
selectedProductsPager.setDisplay(selectedProductsGrid);
// Add a selection model so we can select cells.
final SelectionModel<ProductSummaryDTO> selectionModel = new MultiSelectionModel<ProductSummaryDTO>(this.SELECTED_PRODUCTS_KEY_PROVIDER);
selectedProductsGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<ProductSummaryDTO> createCheckboxManager());
// Initialize the columns.
initSelectedProductsTableColumns(selectionModel, sortHandler);
}
private void initOtherProductsTable() {
this.OTHER_PRODUCTS_KEY_PROVIDER = new ProvidesKey<ProductSummaryDTO>() {
public Object getKey(ProductSummaryDTO item) {
return item == null ? null : item.getId();
}
};
this.otherProductsGrid = new CellTable<ProductSummaryDTO>(this.OTHER_PRODUCTS_KEY_PROVIDER);
this.otherProductsDataProvider = new ListDataProvider<ProductSummaryDTO>();
// Set the message to display when the table is empty.
this.otherProductsGrid.setEmptyTableWidget(new Label(messages.noResultsFound()));
// Attach a column sort handler to the ListDataProvider to sort the
// list.
ListHandler<ProductSummaryDTO> sortHandler = new ListHandler<ProductSummaryDTO>(this.otherProductsDataProvider.getList());
this.otherProductsGrid.addColumnSortHandler(sortHandler);
// Create a Pager to control the table.
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
this.otherProductsPager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
this.otherProductsPager.setDisplay(otherProductsGrid);
// Add a selection model so we can select cells.
final SelectionModel<ProductSummaryDTO> selectionModel = new MultiSelectionModel<ProductSummaryDTO>(this.OTHER_PRODUCTS_KEY_PROVIDER);
this.otherProductsGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<ProductSummaryDTO> createCheckboxManager());
// Initialize the columns.
initOtherProductsTableColumns(selectionModel, sortHandler);
}
private void initSelectedProductsTableColumns(SelectionModel<ProductSummaryDTO> selectionModel, ListHandler<ProductSummaryDTO> sortHandler) {
this.selectedProductsGrid.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.selectedProductsGrid.addColumn(nameColumn, this.constants.name());
this.selectedProductsClickableColumn = nameColumn;
// Delete
Column<ProductSummaryDTO, String> deleteColumn = new Column<ProductSummaryDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(ProductSummaryDTO object) {
return constants.delete();
}
};
deleteColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_END);
deleteColumn.setSortable(true);
this.selectedProductsGrid.addColumn(deleteColumn, "");
this.selectedProductsDeleteColumn = deleteColumn;
}
private void initOtherProductsTableColumns(SelectionModel<ProductSummaryDTO> selectionModel, ListHandler<ProductSummaryDTO> sortHandler) {
this.otherProductsGrid.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.otherProductsGrid.addColumn(nameColumn, this.constants.name());
this.otherProductsClickableColumn = nameColumn;
// Delete
Column<ProductSummaryDTO, String> deleteColumn = new Column<ProductSummaryDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(ProductSummaryDTO object) {
return constants.add();
}
};
deleteColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_END);
deleteColumn.setSortable(true);
this.otherProductsGrid.addColumn(deleteColumn, "");
this.otherProductsDeleteColumnn = deleteColumn;
}
public boolean isPermanentView() {
return false;
}
@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();
}
@Override
public HasText getStartDate() {
return this.startDateField;
}
@Override
public HasText getEndDate() {
return this.endDateField;
}
@Override
public HasText getNumberOfSlots() {
return this.numberOfSlotsField;
}
@Override
public HasText getPrice() {
return this.priceField;
}
@Override
public HasValue<Boolean> getSeq() {
return this.seqButton;
}
@Override
public HasValue<Boolean> getAlt() {
return this.altButton;
}
@Override
public HasClickHandlers getSaveButton() {
return this.saveButton;
}
@Override
public HasClickHandlers getDeleteButton() {
return this.deleteButton;
}
@Override
public HasClickHandlers getCancelButton() {
return this.cancelButton;
}
@Override
public ListBox getPublisherList() {
return this.publisherList;
}
@Override
public ListBox getCompanyList() {
return this.companyList;
}
@Override
public HasData<ProductSummaryDTO> getSelectedProductsGrid() {
return this.selectedProductsGrid;
}
@Override
public HasData<ProductSummaryDTO> getOtherProductsGrid() {
return this.otherProductsGrid;
}
@Override
public Column<ProductSummaryDTO, String> getSelectedProductsDeleteColumn() {
return this.selectedProductsDeleteColumn;
}
@Override
public Column<ProductSummaryDTO, String> getSelectedProductsClickableColumn() {
return this.selectedProductsClickableColumn;
}
@Override
public Column<ProductSummaryDTO, String> getOtherProductsDeleteColumn() {
return this.otherProductsDeleteColumnn;
}
@Override
public Column<ProductSummaryDTO, String> getOtherProductsClickableColumn() {
return this.otherProductsClickableColumn;
}
@Override
public ListDataProvider<ProductSummaryDTO> getSelectedProductsDataProvider() {
return this.selectedProductsDataProvider;
}
@Override
public ListDataProvider<ProductSummaryDTO> getOtherProductsDataProvider() {
return this.otherProductsDataProvider;
}
@Override
public void addSelectedProductsDataDisplay(HasData<ProductSummaryDTO> display) {
if(!this.selectedProductsDataProvider.getDataDisplays().contains(display)){
this.selectedProductsDataProvider.addDataDisplay(display);
}
}
@Override
public void addOtherProductsDataDisplay(HasData<ProductSummaryDTO> display) {
if(!this.otherProductsDataProvider.getDataDisplays().contains(display)){
this.otherProductsDataProvider.addDataDisplay(display);
}
}
@Override
public void redrawSelectedProductsGrid() {
this.selectedProductsGrid.redraw();
}
@Override
public void redrawOtherProductsGrid() {
this.otherProductsGrid.redraw();
}
@Override
public void handleEntityChanged() {
this.dtoStateChanged();
}
@Override
public void setStatus(int status) {
if (status == SlotStatus.Approved) {
this.statusField.setText(constants.booked());
this.statusField.getElement().getStyle().setProperty("color", "#336600");
} else if (status == SlotStatus.Ordered) {
this.statusField.setText(constants.ordered());
this.statusField.getElement().getStyle().setProperty("color", "#0000CC");
} else if (status == SlotStatus.Invoiced) {
this.statusField.setText(constants.pending());
this.statusField.getElement().getStyle().setProperty("color", "#FF6600");
} else if (status == SlotStatus.Cancelled) {
this.statusField.setText(constants.cancelled());
this.statusField.getElement().getStyle().setProperty("color", "#CC0033");
} else if (status == SlotStatus.Rejected) {
this.statusField.setText(constants.rejected());
this.statusField.getElement().getStyle().setProperty("color", "#CC0033");
} else {
this.statusField.setText(constants.unknown());
this.statusField.getElement().getStyle().setProperty("color", "#666666");
}
}
}