/*
 * Copyright
 *   2007 axYus - www.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 Lesser General Public License as published by
 * the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
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.Property;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.TextCritereItemDTO;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.widget.DlgSelectFromDistinctValues;
import java.util.List;

public class TextCritereWidget extends InputCritereWidget {

    private TextBox textBox;
    private Button helpButton;
    private HorizontalPanel panel;

    private Widget mainWidget;

    public TextCritereWidget(CritereLineContainer critereLineContainer, TextCritereItemDTO critereItem) {
        super(critereLineContainer, critereItem);
        this.textBox = new TextBox();
        mainWidget=textBox;
        if (critereItem.isUppercase()) {
            textBox.addKeyboardListener(new KeyboardListenerAdapter() {

                @Override
                public void onKeyPress(Widget sender, char keyCode, int modifiers) {
                    if (Character.isLowerCase(keyCode)) {
                        ((TextBox) sender).setKey(Character.toUpperCase(keyCode));
                    }
                }
            });
        }
        if(critereItem.getLastInputValue()!=null) {
            textBox.setText(critereItem.getLastInputValue());
        }
        if(critereItem.getHelpPath()!=null) {
            final String helpPath = critereItem.getHelpPath();
            helpButton = new Button("...", new ClickListener() {
                public void onClick(Widget sender) {
                    String searchSessionId = getCritereLineContainer().getCriteriaTable().getMainWidget().getSearchSessionId();
                    String collectivite = getCritereLineContainer().getCriteriaTable().getMainWidget().getCollectiviteId();
                    String budget = getCritereLineContainer().getCriteriaTable().getMainWidget().getBudgetId();
                    DlgSelectFromDistinctValues dlg = new DlgSelectFromDistinctValues(TextCritereWidget.this, collectivite, budget, searchSessionId,helpPath);
                }
            });
            helpButton.addStyleName("zoneSaisissable");
            helpButton.setTitle(new HTML("Liste des &eacute;l&eacute;ments").getHTML());
            panel = new HorizontalPanel();
            panel.add(textBox);
            panel.add(helpButton);
            mainWidget = panel;
        }
        initWidget(mainWidget);
    }
    public TextCritereWidget(CritereLineContainer critereLineContainer, TextCritereItemDTO critereItem,List<Property> properties) {
        super(critereLineContainer, critereItem);
        this.textBox = new TextBox();
        if (critereItem.isUppercase()) {
            textBox.addKeyboardListener(new KeyboardListenerAdapter() {

                @Override
                public void onKeyPress(Widget sender, char keyCode, int modifiers) {
                    if (Character.isLowerCase(keyCode)) {
                        ((TextBox) sender).setKey(Character.toUpperCase(keyCode));
                    }
                }
            });
        }
        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(lastValue);
        }
        initWidget(textBox);
    }

    @Override
    public void checkValidity() throws ValidityException {
        if (getInputValue() != null && getInputValue().contains("\"")) {
            throw new ValidityException(new HTML("Les guillemets ne sont pas autori&eacute;s").getHTML());
        }
    }

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

    @Override
    public void refresh() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public String getInputValue() {
        return textBox.getText();
    }
    public void setInputValue(String text) {
        textBox.setText(text);
    }
}
