package net.helipilot50.stocktrade.client.gxt;
import net.helipilot50.stocktrade.client.CustomerSO;
import net.helipilot50.stocktrade.client.CustomerSOAsync;
import net.helipilot50.stocktrade.model.Customer;
import net.helipilot50.stocktrade.model.Holding;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.view.client.ListDataProvider;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
public class CustomerWindow extends Window {
private final CustomerSOAsync customerSO = GWT
.create(CustomerSO.class);
private FormPanel frmpnlNewFormpanel;
private TextField customerNameField;
private TextArea addressField;
private TextField phoneField;
private NumberField cashBalanceField;
private Customer customer = null;
private CellTable<Holding> holdingListField;
private TextColumn<Holding> stockNameColumn;
private Column<Holding, Number> quantityColumn;
private Column<Holding, Number> priceColumn;
private HorizontalPanel horizontalPanel;
private Button btnBuy;
private Button btnSell;
private Button btnExit;
private VerticalPanel verticalPanel;
public CustomerWindow() {
setSize("330", "385");
setHeading("Customer Details");
setLayout(new FitLayout());
this.verticalPanel = new VerticalPanel();
this.verticalPanel.setHorizontalAlign(HorizontalAlignment.CENTER);
this.frmpnlNewFormpanel = new FormPanel();
this.frmpnlNewFormpanel.setHeaderVisible(false);
this.frmpnlNewFormpanel.setHeading("");
this.frmpnlNewFormpanel.setCollapsible(true);
this.verticalPanel.add(this.frmpnlNewFormpanel);
this.customerNameField = new TextField();
this.customerNameField.setEnabled(false);
this.frmpnlNewFormpanel.add(this.customerNameField);
this.customerNameField.setFieldLabel("Name");
this.addressField = new TextArea();
this.addressField.setEnabled(false);
this.frmpnlNewFormpanel.add(this.addressField, new FormData("100%"));
this.addressField.setFieldLabel("Address");
this.phoneField = new TextField();
this.phoneField.setEnabled(false);
this.frmpnlNewFormpanel.add(this.phoneField, new FormData("100%"));
this.phoneField.setFieldLabel("Phone");
this.cashBalanceField = new NumberField();
this.cashBalanceField.setEnabled(false);
this.frmpnlNewFormpanel.add(this.cashBalanceField, new FormData("100%"));
this.cashBalanceField.setFieldLabel("Cash Balance");
this.holdingListField = new CellTable<Holding>();
this.holdingListField.setPageSize(5);
this.verticalPanel.add(this.holdingListField);
this.stockNameColumn = new TextColumn<Holding>() {
@Override
public String getValue(Holding object) {
return object.getStockName();
}
};
this.holdingListField.addColumn(this.stockNameColumn, "Stock Name");
this.quantityColumn = new Column<Holding, Number>(new NumberCell()) {
@Override
public Number getValue(Holding object) {
return (Number) object.getQuantity();
}
};
this.holdingListField.addColumn(this.quantityColumn, "Quantity");
this.priceColumn = new Column<Holding, Number>(new NumberCell()) {
@Override
public Number getValue(Holding object) {
return (Number) object.getPrice();
}
};
this.holdingListField.addColumn(this.priceColumn, "Price");
this.horizontalPanel = new HorizontalPanel();
this.verticalPanel.add(this.horizontalPanel);
this.horizontalPanel.setHorizontalAlign(HorizontalAlignment.CENTER);
this.btnBuy = new Button("Buy");
this.horizontalPanel.add(this.btnBuy);
this.btnSell = new Button("Sell");
this.horizontalPanel.add(this.btnSell);
this.btnExit = new Button("Exit");
this.horizontalPanel.add(this.btnExit);
add(this.verticalPanel);
}
public void loadCustomer(final String name){
customerSO.getCustomer(name, new AsyncCallback<Customer>() {
@Override
public void onSuccess(Customer result) {
setCustomer(result);
}
@Override
public void onFailure(Throwable caught) {
MessageBox.alert("Error", "Could not load customer " + name + "\nError: " + caught.getMessage(), null);
}
});
}
public void saveCustomer(){
}
public void newCustomer(){
}
public void setCustomer(Customer customer){
this.customer = customer;
customerNameField.setValue(this.customer.getCustomerName());
addressField.setValue(this.customer.getAddress());
phoneField.setValue(this.customer.getPhoneNumber());
cashBalanceField.setValue(this.customer.getCashBalance());
ListDataProvider<Holding> dataProvider = new ListDataProvider<Holding>(this.customer.getHoldingList());
dataProvider.addDataDisplay(holdingListField);
}
}