/*
 * 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;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.dto.CritereTypeDTO;
import java.util.List;

/**
 * @author CBO
 */
public class SelectCritereWidget extends Composite {

    private List<CritereTypeDTO> criteres;
    private boolean readOnly;
    private ListBox critereListBox;

    public SelectCritereWidget(List<CritereTypeDTO> criteres, boolean readOnly) {
        this.criteres = criteres;
        this.readOnly = readOnly;
        if (!readOnly) {
            this.critereListBox = new ListBox();
            for (CritereTypeDTO critere : criteres) {
                this.critereListBox.addItem(critere.getLibelle(), critere.getId());
            }
            initWidget(this.critereListBox);
        } else {
            initWidget(new Label(criteres.get(0).getLibelle()));
        }
    }

    public void addChangeListener(final ChangeListener changeListener) {
        if (!readOnly) {
            this.critereListBox.addChangeListener(new ChangeListener() {

                public void onChange(Widget widget) {
                    changeListener.onChange(SelectCritereWidget.this);
                }
            });
        }
    }

    public CritereTypeDTO getSelectedCritere() {
        if (!this.readOnly) {
            if (this.critereListBox.getSelectedIndex() >= 0) {
                return this.criteres.get(this.critereListBox.getSelectedIndex());
            } else {
                return null;
            }
        } else {
            return !criteres.isEmpty() ? criteres.get(0) : null;
        }
    }

    public void setSelectedCritere(CritereTypeDTO critere) {
        if(!this.readOnly) {
            int index = -1;
            for(int i=0;i<criteres.size();i++) {
                if(criteres.get(i).equals(critere)) {
                    index = i;
                    break;
                }
            }
            this.critereListBox.setSelectedIndex(index);
//            this.critereListBox.
//            changeListener.onChange(SelectCritereWidget.this);
        }
    }
}
