Subversion Repositories SmartDukaan

Rev

Rev 1992 | Rev 2119 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed


package in.shop2020.catalog.dashboard.client;

import in.shop2020.catalog.dashboard.shared.Utils;

import java.util.Map.Entry;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;


public class VendorPricesDialog extends DialogBox {

  interface Binder extends UiBinder<Widget, VendorPricesDialog> { }
  private static final Binder binder = GWT.create(Binder.class);

  public interface VendorPriceUpdateListener{
      boolean onUpdate(String itemKey, double mop, double dp, double tp, long vendorId);
  }
  
  private VendorPriceUpdateListener vendorPriceUpdateListener;
  
  @UiField Button closeButton, updateButton;
  @UiField TextBox itemKey, mop, dealerPrice, transferPrice;
  @UiField ListBox vendor;

  public VendorPricesDialog() {
      setText("Vendor Prices");
      setWidget(binder.createAndBindUi(this));
      setAnimationEnabled(true);
      //setGlassEnabled(true);
      center();
      for(Entry<Long, String> e : Utils.getAllVendors().entrySet()){
          vendor.addItem(e.getValue());
      }
  }
  
  public VendorPricesDialog(String itemKey, String mop, String dp, String tp) {
    this();
    setPrices(itemKey, mop, dp, tp);
  }
  
  public void setPrices(String itemKey, String mop, String dp, String tp) {
    this.itemKey.setText(itemKey);
    this.mop.setText(mop);
    this.dealerPrice.setText(dp);
    this.transferPrice.setText(tp);
    this.vendor.setEnabled(false);
    this.itemKey.setEnabled(false);
  }

  /*@Override
  protected void onPreviewNativeEvent(NativePreviewEvent preview) {
    super.onPreviewNativeEvent(preview);

    NativeEvent evt = preview.getNativeEvent();
    if (evt.getType().equals("keydown")) {
      // Use the popup's key preview hooks to close the dialog when either
      // enter or escape is pressed.
      switch (evt.getKeyCode()) {
        case KeyCodes.KEY_ENTER:
        case KeyCodes.KEY_ESCAPE:
          hide();
          break;
      }
    }
  }*/
  
  public void setVendorDetailsUpdateListener(VendorPriceUpdateListener vPriceUpdateListener) {
      this.vendorPriceUpdateListener = vPriceUpdateListener;
  }

  @UiHandler("closeButton")
  void onCloseClicked(ClickEvent event) {
    hide();
  }
  
  @UiHandler("updateButton")
  void onEditClicked(ClickEvent event) {
      double mop;
      double dp;
      double tp;
      long vendorId;
      String key;
      try {
          mop = Double.parseDouble(this.mop.getText());
          dp = Double.parseDouble(this.dealerPrice.getText());
          tp = Double.parseDouble(this.transferPrice.getText());
          vendorId = Utils.getVendorId(this.vendor.getItemText(this.vendor.getSelectedIndex()));
          key = itemKey.getText().trim();
          if(key.isEmpty()) {
              Window.alert("Item key cannot be empty");
              return;
          }
      } catch(NumberFormatException ex) {
          Window.alert("Price format is not valid.");
          return;
      }
      if(vendorPriceUpdateListener.onUpdate(key, mop, dp, tp, vendorId)) {
          hide();
      }
  }
}