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

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.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
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.Displayable;
import fr.gouv.finances.dgfip.xemelios.web.gwt.client.utils.GWTUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CBO
 * Permet de choisir un élément dans une liste
 */
public class ChooseOneItemDialog extends DialogBox {

    private VerticalPanel itemContainer;
    private SelectListenerCollection selectListeners;

    public ChooseOneItemDialog(String title) {
        setText(title);
        this.selectListeners = new SelectListenerCollection();
        VerticalPanel mainPanel = new VerticalPanel();
        ScrollPanel scrollPanel = new ScrollPanel();
        scrollPanel.setSize("350", "200");
        this.itemContainer = new VerticalPanel();
        scrollPanel.add(itemContainer);
        mainPanel.add(scrollPanel);
        // Les boutons Annuler et Valider :
        mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        mainPanel.add(createButtonsPanel());
        // Le widget Principal :
        setWidget(mainPanel);
    }

    public void addSelectListener(SelectListener selectListener) {
        this.selectListeners.add(selectListener);
    }

    private Widget createButtonsPanel() {
        HorizontalPanel horizontalPanel = new HorizontalPanel();
        Button cancelButton = new Button();
        cancelButton.setText("Annuler");
        cancelButton.addClickListener(new ClickListener() {

            public void onClick(Widget sender) {
                cancel();
            }
        });
        horizontalPanel.add(cancelButton);
        return horizontalPanel;
    }

    private void cancel() {
        hide();
    }

    public void setItems(List<? extends Displayable> items) {
        this.itemContainer.clear();
        for (final Displayable item : items) {
            ImageHLink hyperlink = new ImageHLink(item);
            hyperlink.addClickListener(new ClickListener() {

                public void onClick(Widget sender) {
                    selectListeners.fireSelect(item);
                    hide();
                }
            });
            itemContainer.add(hyperlink);
        }
    }

    static public interface SelectListener {

        void onSelect(Object selectedItem);
    }

    static public class SelectListenerCollection extends ArrayList<SelectListener> {

        public void fireSelect(Object selectedItem) {
            for (SelectListener listener : this) {
                listener.onSelect(selectedItem);
            }
        }
    }

    static private class ImageHLink extends HorizontalPanel {
        private Image image;
        private Hyperlink hlink;
        public ImageHLink(Displayable dd) {
            String imageUrl = GWTUtils.getContextPath().concat("/img/empty.png");
            if(dd.getResource()!=null) {
                imageUrl = GWTUtils.getContextPath().concat("/img/").concat(dd.getResource());
            }
            image = new Image(imageUrl);
            add(image);
            hlink = new Hyperlink();
            hlink.setText(dd.getLibelle());
            add(hlink);
        }
        public void addClickListener(ClickListener cl) {
            hlink.addClickListener(cl);
            image.addClickListener(cl);
        }
    }
}
