Subversion Repositories SmartDukaan

Rev

Rev 2359 | 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.CheckBox;
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;

/**
 * This dialog is used to add/edit vendor item key.
 *
 */
public class VendorMappingDialog extends DialogBox {

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

    public interface VendorMappingUpdateListener{
        boolean onUpdate(String itemKey, long vendorId);
    }

    private VendorMappingUpdateListener vendorMappingUpdateListener;

    @UiField TextBox productGroup, brand, modelNumber, color;
    @UiField CheckBox sameAsOurs;
    @UiField Button generateKeyButton;
    @UiField Button closeButton, updateButton;
    @UiField TextBox itemKey;
    @UiField ListBox vendor;

    private String ourProductGroup, ourBrand, ourColor, ourModelNum;
    
    public VendorMappingDialog(String ourProductGroup, String ourBrand, String ourModelNum, String ourColor) {
        setText("Vendor Item Mapping");
        setWidget(binder.createAndBindUi(this));
        setAnimationEnabled(true);
        center();
        for(Entry<Long, String> e : Utils.getAllVendors().entrySet()){
            vendor.addItem(e.getValue());
        }
        this.ourProductGroup = ourProductGroup;
        this.ourBrand = ourBrand;
        this.ourModelNum = ourModelNum;
        this.ourColor = ourColor;
    }

    public VendorMappingDialog(String ourProductGroup, String ourBrand, String ourModelNum, String ourColor, String itemKey) {
        this(ourProductGroup, ourBrand, ourModelNum, ourColor);
        setKey(itemKey);
    }
    
    private void setKey(String itemKey) {
        this.itemKey.setText(itemKey);
        this.vendor.setEnabled(false);
    }

    /**
     * listener for click on update button is set in ItemDetails and ItemForm
     * @param vMappingUpdateListener
     */
    public void setVendorMappingUpdateListener(VendorMappingUpdateListener vMappingUpdateListener) {
        this.vendorMappingUpdateListener = vMappingUpdateListener;
    }

    @UiHandler("closeButton")
    void onCloseClicked(ClickEvent event) {
        hide();
    }

    @UiHandler("updateButton")
    void onEditClicked(ClickEvent event) {
        String key = itemKey.getText().trim();
        if(key.isEmpty()) {
            Window.alert("Item key cannot be empty");
            return;
        }
        long vendorId = Utils.getVendorId(this.vendor.getItemText(this.vendor.getSelectedIndex()));
        if(vendorMappingUpdateListener.onUpdate(key, vendorId)) {
            hide();
        }
    }
    
    @UiHandler("generateKeyButton")
    void generateKey(ClickEvent event) {
        String pg = productGroup.getText().toLowerCase().trim();
        String br = brand.getText().toLowerCase().trim();
        String mn = modelNumber.getText().toLowerCase().trim();
        String clr = color.getText().toLowerCase().trim();
        if(pg.equals("") || br.equals("") || mn.equals("")) {
            Window.alert("Please fill up product group, brand and model number");
            return;
        }
        String key = pg + "|" + br + "|" + mn + "|" + clr;
        itemKey.setText(key);
     }
    
    @UiHandler("sameAsOurs")
    void setDefaultKeyParams(ClickEvent event) {
        boolean bool = sameAsOurs.getValue();
        productGroup.setText(bool ? ourProductGroup : "");
        brand.setText(bool ? ourBrand : "");
        modelNumber.setText(bool ? ourModelNum : "");
        color.setText(bool ? ourColor : "");
    }
}