Package net.helipilot50.stocktrade.client

Source Code of net.helipilot50.stocktrade.client.OrderWindow

package net.helipilot50.stocktrade.client;

import java.util.ArrayList;
import java.util.List;

import net.helipilot50.stocktrade.model.Holding;
import net.helipilot50.stocktrade.model.Order;
import net.helipilot50.stocktrade.model.Stock;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.SimpleComboBoxFieldBinding;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonGroup;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
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.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class OrderWindow extends Window {
  public static final String BUY_ORDER = "Buy";
  public static final String SELL_ORDER = "Sell";
  private NumberField nmbrfldQuantity;
  private NumberField nmbrfldPrice;
  private SimpleComboBox<String> smplcmbxStock;
  private FormPanel frmpnlNewFormpanel;
  private ButtonGroup buttonGroup;
  private Button btnSubmit;
  private Button btnCancel;
 
  private Order order = null;

  public OrderWindow(final String orderType, final String customerName, final Holding holding) {
   
    TradeServiceAsync tradeSO = Registry.get(CitrusleafStockTrade.TRADE_SERVICE);
    tradeSO.nextOrderID(new AsyncCallback<Integer>() {
     
      @Override
      public void onSuccess(Integer result) {
        Order order = new Order();
        order.setOrderID(result);
        order.setQuantity(0);
        order.setPrice(0);
        order.setCustomerName(customerName);
        order.setType(orderType);
        setHeading(order.getType() + " Order: " + order.getOrderID());
        if (holding != null){
          order.setStockName(holding.getStockName());
        }
       
        OrderWindow.this.setOrder(order);
       
      }
     
      @Override
      public void onFailure(Throwable caught) {
        OrderWindow.this.showWindow(false);
        MessageBox.alert("Error geting next order ID", caught.getMessage(), null);
       
      }
    });
   
   
    setResizable(false);
    setLayout(new RowLayout(Orientation.VERTICAL));
   
    this.frmpnlNewFormpanel = new FormPanel();
    this.frmpnlNewFormpanel.setHeaderVisible(false);
    this.frmpnlNewFormpanel.setHeading("");
    this.frmpnlNewFormpanel.setCollapsible(true);
   
    this.smplcmbxStock = new SimpleComboBox<String>();
    this.smplcmbxStock.setForceSelection(true);
    this.smplcmbxStock.setEditable(false);
    this.smplcmbxStock.setAllowBlank(false);
    this.frmpnlNewFormpanel.add(this.smplcmbxStock, new FormData("100%"));
    this.smplcmbxStock.setFieldLabel("Stock");
    this.smplcmbxStock.setEmptyText("Select stock");
    this.smplcmbxStock.setTriggerAction(TriggerAction.ALL);
    populateStocks();
   
   
   
    this.nmbrfldQuantity = new NumberField();
    this.frmpnlNewFormpanel.add(this.nmbrfldQuantity, new FormData("100%"));
    this.nmbrfldQuantity.setFieldLabel("Quantity");
   
    this.nmbrfldPrice = new NumberField();
    this.frmpnlNewFormpanel.add(this.nmbrfldPrice, new FormData("100%"));
    this.nmbrfldPrice.setFieldLabel("Price");
    add(this.frmpnlNewFormpanel);
   
    this.buttonGroup = new ButtonGroup(1);
    TableLayout tableLayout = (TableLayout) this.buttonGroup.getLayout();
    tableLayout.setCellVerticalAlign(VerticalAlignment.MIDDLE);
    tableLayout.setCellHorizontalAlign(HorizontalAlignment.CENTER);
    tableLayout.setColumns(2);
    this.buttonGroup.setHeading("");
   
    this.btnSubmit = new Button("Submit");
    this.btnSubmit.addListener(Events.Select, new Listener<ButtonEvent>() {
      public void handleEvent(ButtonEvent e) {
        placeOrder();
        OrderWindow.this.showWindow(false);
      }
    });
    this.buttonGroup.add(this.btnSubmit);
   
    this.btnCancel = new Button("Cancel");
    this.btnCancel.addListener(Events.Select, new Listener<ButtonEvent>() {
      public void handleEvent(ButtonEvent e) {
        OrderWindow.this.showWindow(false);
      }
    });
    this.buttonGroup.add(this.btnCancel);
    add(this.buttonGroup);
    if (this.order != null)
      initDataBindings();
    if (orderType.equalsIgnoreCase("Sell")){
      this.smplcmbxStock.setReadOnly(true);
     
    }
  }

  private void populateStocks(){

    List<String> stockNames = Registry.get(CitrusleafStockTrade.STOCK_NAMES);
    if (stockNames != null){
      this.smplcmbxStock.add(stockNames);
      return;
    }
    ((CustomerServiceAsync)Registry.get(CitrusleafStockTrade.CUSTOMER_SERVICE)).getStocks(new AsyncCallback<List<Stock>>() {
       
        @Override
        public void onSuccess(List<Stock> result) {
          Registry.register(CitrusleafStockTrade.STOCKS, result);
          List <String> stockNamesLocal = new ArrayList<String>();
          for (Stock stock : result){
            stockNamesLocal.add(stock.getStockName());
          }
          Registry.register(CitrusleafStockTrade.STOCK_NAMES, stockNamesLocal);
          OrderWindow.this.smplcmbxStock.add(stockNamesLocal);
        }
       
        @Override
        public void onFailure(Throwable caught) {
          MessageBox.alert("Error getting Stocks", caught.getMessage(), null);
         
        }
      });
    return;
  }
  private void placeOrder(){
    TradeServiceAsync tradeSO = Registry.get(CitrusleafStockTrade.TRADE_SERVICE);
    tradeSO.takeOrder(getOrder(), new AsyncCallback<Void>() {
     
      @Override
      public void onSuccess(Void result) {
        Info.display("Success", "Order placed" );
       
      }
     
      @Override
      public void onFailure(Throwable caught) {
        MessageBox.alert("Error placing order", caught.getMessage(), null);
      }
    });
   
  }
  public Order getOrder() {
    return order;
  }
  public void setOrder(Order order) {
    this.order = order;
    initDataBindings();
  }
  protected void initDataBindings() {
    FieldBinding fieldBinding = new FieldBinding(nmbrfldPrice, "price");
    fieldBinding.bind(order);
    //
    FieldBinding fieldBinding_1 = new FieldBinding(nmbrfldQuantity, "quantity");
    fieldBinding_1.bind(order);
    //
    SimpleComboBoxFieldBinding fieldBinding_2 = new SimpleComboBoxFieldBinding(smplcmbxStock, "stockName");
    fieldBinding_2.bind(order);
  }
}
TOP

Related Classes of net.helipilot50.stocktrade.client.OrderWindow

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.