package com.casamind.adware.client.view;
import java.util.Comparator;
import java.util.Date;
import com.casamind.adware.client.presenter.ConsolePresenter;
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.SlotSummaryDTO;
import com.google.gwt.cell.client.ImageCell;
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.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
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.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
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.user.datepicker.client.CalendarUtil;
import com.google.gwt.user.datepicker.client.DateBox;
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 ConsoleView extends Composite implements ConsolePresenter.Display {
@UiField
RadioButton altOrderButton, seqOrderButton, allOrdersButton, noButton,
yesButton, allConfButton;
@UiField
RadioButton allStatusButton, pendingStatusButton, orderedStatusButton,
rejectedStatusButton, bookedStatusButton, cancelledStatusButton;
@UiField
Anchor statisticsLink, orderLink, calendarLink;
@UiField
HTMLPanel chartArea;
@UiField
Button searchButton;
@UiField
ListBox publisherList, companyList;
@UiField
DateBox startDate, endDate;
@UiField(provided = true)
CellTable<SlotSummaryDTO> slotsGrid;
@UiField(provided = true)
SimplePager slotsPager;
private static ConsoleViewUiBinder uiBinder = GWT.create(ConsoleViewUiBinder.class);
interface ConsoleViewUiBinder extends UiBinder<Widget, ConsoleView> {
}
private Column<SlotSummaryDTO, String> nextSlotsClickableColumn;
private ListDataProvider<SlotSummaryDTO> nextSlotsDataProvider;
private ProvidesKey<SlotSummaryDTO> 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);
private final Date defaultStartDate, defaultEndDate;
public ConsoleView() {
initCellTables();
initWidget(uiBinder.createAndBindUi(this));
this.startDate.getDatePicker().setWidth("205px");
this.endDate.getDatePicker().setWidth("205px");
this.startDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_MEDIUM)));
this.endDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_MEDIUM)));
this.defaultStartDate = new Date();
this.defaultEndDate = new Date();
CalendarUtil.addMonthsToDate(defaultEndDate, 1);
this.startDate.setValue(this.defaultStartDate);
this.endDate.setValue(this.defaultEndDate);
}
private void initCellTables() {
initSlotsTable();
}
private void initSlotsTable() {
this.SLOTS_KEY_PROVIDER = new ProvidesKey<SlotSummaryDTO>() {
public Object getKey(SlotSummaryDTO item) {
return item == null ? null : item.getId();
}
};
this.slotsGrid = new CellTable<SlotSummaryDTO>(SLOTS_KEY_PROVIDER);
this.nextSlotsDataProvider = new ListDataProvider<SlotSummaryDTO>();
// Set the message to display when the table is empty.
this.slotsGrid.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.nextSlotsDataProvider.getList());
this.slotsGrid.addColumnSortHandler(sortHandler);
// Create a Pager to control the table.
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
this.slotsPager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
this.slotsPager.setDisplay(this.slotsGrid);
// Add a selection model so we can select cells.
final SelectionModel<SlotSummaryDTO> selectionModel = new MultiSelectionModel<SlotSummaryDTO>(this.SLOTS_KEY_PROVIDER);
this.slotsGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<SlotSummaryDTO> createCheckboxManager());
// Initialize the columns.
initSlotsTableColumns(selectionModel, sortHandler);
}
private void initSlotsTableColumns(SelectionModel<SlotSummaryDTO> selectionModel, ListHandler<SlotSummaryDTO> sortHandler) {
this.slotsGrid.setWidth("100%", true);
// Start Date
Column<SlotSummaryDTO, String> startDateColumn = new Column<SlotSummaryDTO, String>(new TextCell()) {
@Override
public String getValue(SlotSummaryDTO object) {
return DateTimeFormat.getFormat(constants.shortDateFormat()).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.slotsGrid.addColumn(startDateColumn, this.constants.startDate());
// End Date
Column<SlotSummaryDTO, String> endDateColumn = new Column<SlotSummaryDTO, String>(new TextCell()) {
@Override
public String getValue(SlotSummaryDTO object) {
return DateTimeFormat.getFormat(constants.shortDateFormat()).format(object.getEndDate());
}
};
endDateColumn.setSortable(true);
sortHandler.setComparator(endDateColumn, new Comparator<SlotSummaryDTO>() {
public int compare(SlotSummaryDTO o1, SlotSummaryDTO o2) {
return o1.getEndDate().compareTo(o2.getEndDate());
}
});
this.slotsGrid.addColumn(endDateColumn, this.constants.endDate());
// Number of products.
Column<SlotSummaryDTO, Number> productsColumn = new Column<SlotSummaryDTO, Number>(new NumberCell()) {
@Override
public Number getValue(SlotSummaryDTO object) {
return object.getNbProducts();
}
};
productsColumn.setSortable(true);
sortHandler.setComparator(productsColumn, new Comparator<SlotSummaryDTO>() {
public int compare(SlotSummaryDTO o1, SlotSummaryDTO o2) {
return (int) (o2.getNbProducts() - o1.getNbProducts());
}
});
this.slotsGrid.addColumn(productsColumn, constants.products());
this.slotsGrid.setColumnWidth(productsColumn, 60, Unit.PT);
// 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.slotsGrid.addColumn(priceColumn, constants.price());
this.slotsGrid.setColumnWidth(priceColumn, 60, Unit.PT);
// Is configured.
Column<SlotSummaryDTO, String> isConfiguredColumn = new Column<SlotSummaryDTO, String>(new ImageCell()) {
@Override
public String getValue(SlotSummaryDTO object) {
return object.isConfigured() ? paths.check() : paths.warning();
}
};
isConfiguredColumn.setSortable(true);
sortHandler.setComparator(isConfiguredColumn, new Comparator<SlotSummaryDTO>() {
public int compare(SlotSummaryDTO o1, SlotSummaryDTO o2) {
return ((Boolean) o1.isConfigured()).compareTo((Boolean) o2.isConfigured());
}
});
isConfiguredColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
this.slotsGrid.addColumn(isConfiguredColumn, constants.isConfigured());
this.slotsGrid.setColumnWidth(isConfiguredColumn, 75, Unit.PT);
// Edit link
Column<SlotSummaryDTO, String> editColumn = new Column<SlotSummaryDTO, String>(new MyClickableCellText()) {
@Override
public String getValue(SlotSummaryDTO object) {
return object.isConfigured() ? constants.consult() : constants.configure();
}
};
editColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_END);
this.slotsGrid.addColumn(editColumn, "");
this.slotsGrid.setColumnWidth(editColumn, 70, Unit.PT);
this.nextSlotsClickableColumn = editColumn;
}
public boolean isPermanentView() {
return true;
}
@Override
public HasData<SlotSummaryDTO> getSlotsGrid() {
return this.slotsGrid;
}
@Override
public Column<SlotSummaryDTO, String> getSlotsClickableColumn() {
return this.nextSlotsClickableColumn;
}
@Override
public ListDataProvider<SlotSummaryDTO> getSlotsDataProvider() {
return this.nextSlotsDataProvider;
}
@Override
public void addSlotsDataDisplay(HasData<SlotSummaryDTO> display) {
if (!this.nextSlotsDataProvider.getDataDisplays().contains(display)) {
this.nextSlotsDataProvider.addDataDisplay(display);
}
}
@Override
public void redrawSlotsGrid() {
this.slotsGrid.redraw();
}
@Override
public HasClickHandlers getSearchButton() {
return this.searchButton;
}
@Override
public ListBox getPublisherList() {
return this.publisherList;
}
@Override
public ListBox getCompanyList() {
return this.companyList;
}
@Override
public Date getStartDate() {
if (this.startDate.getValue() == null) {
this.startDate.setValue(this.defaultStartDate);
}
return this.startDate.getValue();
}
@Override
public Date getEndDate() {
if (this.endDate.getValue() == null) {
this.endDate.setValue(this.defaultEndDate);
}
Date endDateValue = this.endDate.getValue();
CalendarUtil.addDaysToDate(endDateValue, 1) ;
return endDateValue;
}
@Override
public void setEnableSearchButton(boolean b) {
this.searchButton.setEnabled(b);
}
@Override
public HasClickHandlers getOrderLink() {
return orderLink;
}
@Override
public HasClickHandlers getStatisticsLink() {
return statisticsLink;
}
@Override
public HasClickHandlers getCalendarLink() {
return calendarLink;
}
@Override
public Boolean getIsConfigured() {
if (this.allConfButton.getValue() == true) {
return null;
} else if (this.noButton.getValue() == true) {
return false;
} else if (this.yesButton.getValue() == true) {
return true;
} else {
this.allConfButton.setValue(true);
return null;
}
}
@Override
public Boolean getIsSequential() {
if (this.allOrdersButton.getValue() == true) {
return null;
} else if (this.altOrderButton.getValue() == true) {
return false;
} else if (this.seqOrderButton.getValue() == true) {
return true;
} else {
this.allOrdersButton.setValue(true);
return null;
}
}
@Override
public Integer getStatus() {
if (this.allStatusButton.getValue() == true) {
return null;
} else 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.allStatusButton.setValue(true);
return null;
}
}
}