Subversion Repositories SmartDukaan

Rev

Blame | 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;

/**
 * This dialog is used to add/edit item related prices (SellingPrice, MRP) for a source.
 *
 */
public class SourcePricesDialog extends DialogBox {

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

    /**
     * Listener for update button click.
     */
    public interface SourcePriceUpdateListener{
        boolean onUpdate(double mrp, double sellingPrice, long sourceId);
    }

    private SourcePriceUpdateListener sourcePriceUpdateListener;

    @UiField Button closeButton, updateButton;
    @UiField TextBox mrp, sellingPrice;
    @UiField ListBox source;

    public SourcePricesDialog() {
        setText("Source Prices");
        setWidget(binder.createAndBindUi(this));
        setAnimationEnabled(true);
        center();
        for(Entry<Long, String> e : Utils.getAllSources().entrySet()){
            source.addItem(e.getValue());
        }
    }

    public SourcePricesDialog(String mrp, String sellingPrice) {
        this();
        setPrices(mrp, sellingPrice);
    }

    public void setPrices(String mrp, String sellingPrice) {
        this.mrp.setText(mrp);
        this.sellingPrice.setText(sellingPrice);
        this.source.setEnabled(false);
    }

    /**
     * listener for click on update button is set in ItemDetails and ItemForm
     * @param vPriceUpdateListener
     */
    public void setSourcePriceUpdateListener(SourcePriceUpdateListener sPriceUpdateListener) {
        this.sourcePriceUpdateListener = sPriceUpdateListener;
    }

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

    @UiHandler("updateButton")
    void onEditClicked(ClickEvent event) {
        double mrp, sellingPrice;
        try {
            mrp = Double.parseDouble(this.mrp.getText());
            sellingPrice = Double.parseDouble(this.sellingPrice.getText());
        } catch(NumberFormatException ex) {
            Window.alert("Price format is not valid.");
            return;
        }
        long sourceId = Utils.getSourceId(this.source.getItemText(this.source.getSelectedIndex()));
        if(sourcePriceUpdateListener.onUpdate(mrp, sellingPrice, sourceId)) {
            hide();
        }
    }
}