Subversion Repositories SmartDukaan

Rev

Rev 5504 | Blame | Compare with Previous | Last modification | View Log | RSS feed


package in.shop2020.catalog.dashboard.client;

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

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 VoucherItemDialog extends DialogBox {

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

    /**
     * Listener for update button click.
     */
    public interface VoucherUpdateListener{
        boolean onUpdate(String voucherType, long voucherAmount);
    }

    private VoucherUpdateListener voucherUpdateListener;

    @UiField Button closeButton, updateButton;
    @UiField TextBox voucherAmount;
    @UiField ListBox voucherType;

    public VoucherItemDialog() {
        setText("Vouchers");
        setWidget(binder.createAndBindUi(this));
        setAnimationEnabled(true);
        center();
        for(String voucher: Utils.getVoucherType().values()){
                voucherType.addItem(voucher);
        }
    }

    public VoucherItemDialog(String voucherAmount) {
        this();
        setVoucherAmount(voucherAmount);
    }

    public void setVoucherAmount(String voucherAmount) {
        this.voucherAmount.setText(voucherAmount);
        this.voucherType.setEnabled(false);
    }

    /**
     * listener for click on update button is set in ItemDetails and ItemForm
     * @param voucherUpdateListener
     */
    public void setVoucherUpdateListener(VoucherUpdateListener voucherUpdateListener) {
        this.voucherUpdateListener = voucherUpdateListener;
    }

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

    @UiHandler("updateButton")
    void onEditClicked(ClickEvent event) {
        long voucherAmount;
        try {
                voucherAmount = Long.parseLong(this.voucherAmount.getText());
                if(voucherAmount == 0){
                        Window.alert("Amount is not valid.");
                return;
                }
        } catch(NumberFormatException ex) {
            Window.alert("Amount format is not valid.");
            return;
        }
        String voucherType = this.voucherType.getItemText(this.voucherType.getSelectedIndex());
        if(voucherUpdateListener.onUpdate(voucherType, voucherAmount)) {
            hide();
        }
    }
}