/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 C.Bosquet - charles.bosquet@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.web.gwt.client.widget.search.criteria;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.NumericCritereItemDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.Property;
import java.util.List;

/**
 * @author CBO
 */
public class NumericCritereWidget extends InputCritereWidget {

    private TextBox textBox;

    public NumericCritereWidget(CritereLineContainer critereLineContainer, NumericCritereItemDTO critereItem) {
        super(critereLineContainer, critereItem);
        this.textBox = new TextBox();
        this.textBox.setTextAlignment(TextBox.ALIGN_RIGHT);
        this.textBox.addKeyboardListener(new KeyboardListenerAdapter() {

            @Override
            public void onKeyPress(Widget sender, char keyCode, int modifiers) {
                if ((!Character.isDigit(keyCode)) && (keyCode != (char) KeyboardListenerAdapter.KEY_TAB) && (keyCode != 44) && (keyCode != 45) && (keyCode != (char) KeyboardListenerAdapter.KEY_BACKSPACE) && (keyCode != (char) KeyboardListenerAdapter.KEY_DELETE) && (keyCode != (char) KeyboardListenerAdapter.KEY_ENTER) && (keyCode != (char) KeyboardListenerAdapter.KEY_HOME) && (keyCode != (char) KeyboardListenerAdapter.KEY_END) && (keyCode != (char) KeyboardListenerAdapter.KEY_LEFT) && (keyCode != (char) KeyboardListenerAdapter.KEY_UP) && (keyCode != (char) KeyboardListenerAdapter.KEY_RIGHT) && (keyCode != (char) KeyboardListenerAdapter.KEY_DOWN)) {
                    ((TextBox) sender).cancelKey();
                }
            }
        });
        if(critereItem.getLastInputValue()!=null) {
            textBox.setText(critereItem.getLastInputValue());
        }
        initWidget(textBox);
    }
    public NumericCritereWidget(CritereLineContainer critereLineContainer, NumericCritereItemDTO critereItem,List<Property> properties) {
        super(critereLineContainer, critereItem);
        this.textBox = new TextBox();
        this.textBox.setTextAlignment(TextBox.ALIGN_RIGHT);
        this.textBox.addKeyboardListener(new KeyboardListenerAdapter() {

            @Override
            public void onKeyPress(Widget sender, char keyCode, int modifiers) {
                if ((!Character.isDigit(keyCode)) && (keyCode != (char) KeyboardListenerAdapter.KEY_TAB) && (keyCode != 44) && (keyCode != 45) && (keyCode != (char) KeyboardListenerAdapter.KEY_BACKSPACE) && (keyCode != (char) KeyboardListenerAdapter.KEY_DELETE) && (keyCode != (char) KeyboardListenerAdapter.KEY_ENTER) && (keyCode != (char) KeyboardListenerAdapter.KEY_HOME) && (keyCode != (char) KeyboardListenerAdapter.KEY_END) && (keyCode != (char) KeyboardListenerAdapter.KEY_LEFT) && (keyCode != (char) KeyboardListenerAdapter.KEY_UP) && (keyCode != (char) KeyboardListenerAdapter.KEY_RIGHT) && (keyCode != (char) KeyboardListenerAdapter.KEY_DOWN)) {
                    ((TextBox) sender).cancelKey();
                }
            }
        });
        String lastValue = null;
        for(Property  prop: properties) {
            if(prop.getName().equals(getInputId())) {
                lastValue = prop.getValue();
            }
        }
        if(lastValue==null) {
            lastValue = critereItem.getLastInputValue();
        }
        if(lastValue!=null) {
            textBox.setText(critereItem.getLastInputValue());
        }
        initWidget(textBox);
    }

    @Override
    public void setFocus() {
        this.textBox.setFocus(true);
    }

    @Override
    public String getInputValue() {
        return textBox.getText();
    }

    @Override
    public void refresh() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void checkValidity() throws ValidityException {
        if (isFilled()) {
            try {
                NumberFormat.getDecimalFormat().parse(getInputValue());
            } catch (NumberFormatException nfe) {
                throw new ValidityException("La valeur \"" + getInputValue() + "\" n'est pas une valeur num&eacute;rique valide");
            }
        }
    }
}
