/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2009 C.Marchand - christophe.marchand@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;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.service.ServiceLocator;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.utils.GWTUtils;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.widget.search.criteria.TextCritereWidget;
import java.util.List;

/**
 *
 * @author chm
 */
public class DlgSelectFromDistinctValues extends DialogBox {

    private TextCritereWidget parent;
    private String collectivite, budget, searchSessionId, path;
    private ScrollPanel scroller;

    public DlgSelectFromDistinctValues(TextCritereWidget parent, String collectivite, String budget, String searchSessionId, String path) {
        super(false,true);
        this.parent=parent;
        this.collectivite=collectivite;
        this.budget=budget;
        this.searchSessionId=searchSessionId;
        this.path=path;
        setText("Valeurs");
        initComponents();
    }
    private void initComponents() {
        VerticalPanel vp = new VerticalPanel();
        final VerticalPanel list = new VerticalPanel();
        Image image = new Image(GWTUtils.getContextPath() + "/img/searching.gif");
        scroller = new ScrollPanel(image);
        scroller.setSize("150", "300");

        vp.add(scroller);

        //HorizontalPanel buttonPane = new HorizontalPanel();
        Button cancelButton = new Button("Annuler",new ClickListener() {
            public void onClick(Widget sender) {
                DlgSelectFromDistinctValues.this.hide();
            }
        });
        vp.add(cancelButton);
        center();
        setWidget(vp);
        setVisible(true);
        ServiceLocator.getXemeliosService().getDistinctValues(searchSessionId, collectivite, budget, path, new AsyncCallback<List<String>>() {
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }
            public void onSuccess(List<String> data) {
                for(String s:data) {
                    list.add(new Clickable(s));
                }
                scroller.setWidget(list);
            }
        });
    }

    private class Clickable extends Label {
        private String data;
        public Clickable(String data) {
            super(data,false);
            this.data=data;
            addClickListener(new ClickListener() {
                public void onClick(Widget sender) {
                    DlgSelectFromDistinctValues.this.parent.setInputValue(Clickable.this.data);
                    DlgSelectFromDistinctValues.this.hide();
                }
            });
        }
    }
}
