package com.casamind.adware.client.view;
import java.util.Comparator;
import com.casamind.adware.client.presenter.AdminSlotsPresenter;
import com.casamind.adware.client.resources.DisplayMessages;
import com.casamind.adware.client.resources.Paths;
import com.casamind.adware.client.resources.UIConstants;
import com.casamind.adware.shared.SlotStatus;
import com.casamind.adware.shared.model.SlotDTO;
import com.google.gwt.cell.client.CheckboxCell;
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.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
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.user.cellview.client.DataGrid;
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.Composite;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
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 AdminSlotsView extends Composite implements
AdminSlotsPresenter.Display {
@UiField
RadioButton pendingStatusButton, orderedStatusButton, rejectedStatusButton,
bookedStatusButton, cancelledStatusButton;
@UiField
Anchor resourcesLink;
@UiField
Button searchButton, cancelButton, invoiceButton, approveButton, rejectButton, reclaimButton;
@UiField
ListBox publisherList, companyList;
@UiField(provided = true)
DataGrid<SlotDTO> slotsGrid;
@UiField(provided = true)
SimplePager slotsPager;
private static AdminViewUiBinder uiBinder = GWT.create(AdminViewUiBinder.class);
private Column<SlotDTO, String> slotsClickableColumn, slotsApproveColumn,
slotsRejectColumn, slotsInvoiceColumn, slotsOwnerColumn;
private ListDataProvider<SlotDTO> slotsDataProvider;
private ProvidesKey<SlotDTO> SLOTS_KEY_PROVIDER;
private final Paths paths = GWT.create(Paths.class);
private final UIConstants constants = GWT.create(UIConstants.class);
private final DisplayMessages messages = GWT.create(DisplayMessages.class);
interface AdminViewUiBinder extends UiBinder<Widget, AdminSlotsView> {
}
public AdminSlotsView() {
initCellTables();
initWidget(uiBinder.createAndBindUi(this));
}
private void initCellTables() {
initSlotsTable();
}
private void initSlotsTable() {
this.SLOTS_KEY_PROVIDER = new ProvidesKey<SlotDTO>() {
public Object getKey(SlotDTO item) {
return item == null ? null : item.getId();
}
};
this.slotsGrid = new DataGrid<SlotDTO>(SLOTS_KEY_PROVIDER);
this.slotsDataProvider = new ListDataProvider<SlotDTO>();
// Set the message to display when the table is empty.
this.slotsGrid.setEmptyTableWidget(new Label(messages.noResultsFound()));
// Attach a column sort handler to the ListDataProvider to sort the
// list.
ListHandler<SlotDTO> sortHandler = new ListHandler<SlotDTO>(this.slotsDataProvider.getList());
slotsGrid.addColumnSortHandler(sortHandler);
// Create a Pager to control the table.
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
slotsPager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
slotsPager.setDisplay(slotsGrid);
// Add a selection model so we can select cells.
final SelectionModel<SlotDTO> selectionModel = new MultiSelectionModel<SlotDTO>(this.SLOTS_KEY_PROVIDER);
slotsGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<SlotDTO> createCheckboxManager());
// Initialize the columns.
initSlotsTableColumns(selectionModel, sortHandler);
}
private void initSlotsTableColumns(final SelectionModel<SlotDTO> selectionModel, ListHandler<SlotDTO> sortHandler) {
this.slotsGrid.setWidth("100%");
// mouse selection.
Column<SlotDTO, Boolean> checkColumn =
new Column<SlotDTO, Boolean>(new CheckboxCell(true, false)) {
@Override
public Boolean getValue(SlotDTO object) {
// Get the value from the selection model.
return selectionModel.isSelected(object);
}
};
this.slotsGrid.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
this.slotsGrid.setColumnWidth(checkColumn, 40, Unit.PX);
// Start Date
Column<SlotDTO, String> startDateColumn = new Column<SlotDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(SlotDTO object) {
return DateTimeFormat.getFormat(constants.shortDateFormat()).format(object.getStartDate());
}
};
startDateColumn.setSortable(true);
sortHandler.setComparator(startDateColumn, new Comparator<SlotDTO>() {
public int compare(SlotDTO o1, SlotDTO o2) {
return o1.getStartDate().compareTo(o2.getStartDate());
}
});
this.slotsGrid.addColumn(startDateColumn, this.constants.startDate());
this.slotsClickableColumn = startDateColumn;
// Owner
Column<SlotDTO, String> ownerColumn = new Column<SlotDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(SlotDTO object) {
return object.getOwnerId().longValue() == object.getCompany().getId().longValue()
? object.getCompany().getDisplayName()
: object.getPublisher().getDisplayName();
}
};
ownerColumn.setSortable(true);
sortHandler.setComparator(ownerColumn, new Comparator<SlotDTO>() {
public int compare(SlotDTO o1, SlotDTO o2) {
return o1.getDisplayName().compareTo(o2.getDisplayName());
}
});
this.slotsGrid.addColumn(ownerColumn, this.constants.owner());
this.slotsOwnerColumn = ownerColumn;
// Price.
Column<SlotDTO, Number> priceColumn = new Column<SlotDTO, Number>(new NumberCell()) {
@Override
public Number getValue(SlotDTO object) {
return object.getPrice();
}
};
priceColumn.setSortable(true);
sortHandler.setComparator(priceColumn, new Comparator<SlotDTO>() {
public int compare(SlotDTO o1, SlotDTO o2) {
return (int) (o2.getPrice() - o1.getPrice());
}
});
this.slotsGrid.addColumn(priceColumn, constants.price());
this.slotsGrid.setColumnWidth(priceColumn, 60, Unit.PT);
// Invoice link
Column<SlotDTO, String> invoiceColumn = new Column<SlotDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(SlotDTO object) {
return object.getStatus() == SlotStatus.Ordered ? constants.invoice() : "";
}
};
invoiceColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_END);
this.slotsGrid.addColumn(invoiceColumn, "");
this.slotsGrid.setColumnWidth(invoiceColumn, 70, Unit.PT);
this.slotsInvoiceColumn = invoiceColumn;
// Approve link
Column<SlotDTO, String> editColumn = new Column<SlotDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(SlotDTO object) {
return object.getStatus() == SlotStatus.Invoiced ? constants.approve() : "";
}
};
editColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_END);
this.slotsGrid.addColumn(editColumn, "");
this.slotsGrid.setColumnWidth(editColumn, 70, Unit.PT);
this.slotsApproveColumn = editColumn;
// Reject link
Column<SlotDTO, String> rejectColumn = new Column<SlotDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(SlotDTO object) {
return object.getStatus() == SlotStatus.Ordered || object.getStatus() == SlotStatus.Invoiced || object.getStatus() == SlotStatus.Approved ? constants.reject() : "";
}
};
rejectColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_END);
this.slotsGrid.addColumn(rejectColumn, "");
this.slotsGrid.setColumnWidth(rejectColumn, 70, Unit.PT);
this.slotsRejectColumn = rejectColumn;
}
@Override
public HasData<SlotDTO> getSlotsGrid() {
return this.slotsGrid;
}
@Override
public Column<SlotDTO, String> getSlotsClickableColumn() {
return this.slotsClickableColumn;
}
@Override
public Column<SlotDTO, String> getSlotsOwnerColumn() {
return this.slotsOwnerColumn;
}
@Override
public Column<SlotDTO, String> getSlotsApproveColumn() {
return this.slotsApproveColumn;
}
@Override
public Column<SlotDTO, String> getSlotsRejectColumn() {
return this.slotsRejectColumn;
}
@Override
public Column<SlotDTO, String> getSlotsInvoiceColumn() {
return this.slotsInvoiceColumn;
}
@Override
public ListDataProvider<SlotDTO> getSlotsDataProvider() {
return this.slotsDataProvider;
}
@Override
public void addSlotsDataDisplay(HasData<SlotDTO> display) {
if (!this.slotsDataProvider.getDataDisplays().contains(display)) {
this.slotsDataProvider.addDataDisplay(display);
}
}
@Override
public void redrawSlotsGrid() {
this.slotsGrid.redraw();
}
@Override
public HasClickHandlers getSearchButton() {
return this.searchButton;
}
@Override
public HasClickHandlers getCancelButton() {
return this.cancelButton;
}
@Override
public HasClickHandlers getInvoiceButton() {
return this.invoiceButton;
}
@Override
public HasClickHandlers getApproveButton() {
return this.approveButton;
}
@Override
public HasClickHandlers getRejectButton() {
return this.rejectButton;
}
@Override
public HasClickHandlers getReclaimButton() {
return this.reclaimButton;
}
@Override
public ListBox getPublisherList() {
return this.publisherList;
}
@Override
public ListBox getCompanyList() {
return this.companyList;
}
@Override
public void setEnableButton(HasClickHandlers clickable, boolean b) {
((FocusWidget) clickable).setEnabled(b);
}
@Override
public HasClickHandlers getResourcesLink() {
return resourcesLink;
}
@Override
public Integer getStatus() {
if (this.orderedStatusButton.getValue() == true) {
return SlotStatus.Ordered;
} else if (this.pendingStatusButton.getValue() == true) {
return SlotStatus.Invoiced;
} else if (this.bookedStatusButton.getValue() == true) {
return SlotStatus.Approved;
} else if (this.rejectedStatusButton.getValue() == true) {
return SlotStatus.Rejected;
} else if (this.cancelledStatusButton.getValue() == true) {
return SlotStatus.Cancelled;
} else {
this.orderedStatusButton.setValue(true);
return SlotStatus.Ordered;
}
}
}