/*
 * 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.DOM;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.Widget;

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

    private FocusPanel focusPanel;
    private Image image;
    private Image disabledImage;
    private boolean enabled = true;
    private String toolTip;

    public ImageButton(String image, String toolTip) {
        this(image, image, toolTip);
    }

    public ImageButton(String image, String disabledImage, String toolTip) {
        this.enabled = true;
        this.focusPanel = new FocusPanel();
        this.image = new Image(image);
        this.disabledImage = new Image(disabledImage);
        this.toolTip = toolTip;
        this.focusPanel.add(this.image);
        this.focusPanel.addMouseListener(new MouseListenerAdapter() {

            @Override
            public void onMouseEnter(Widget sender) {
                mouseEnter();
            }

            @Override
            public void onMouseLeave(Widget sender) {
                mouseLeave();
            }
        });
        this.focusPanel.setTitle(toolTip);
        initWidget(this.focusPanel);
        setStyleName("imageButton");
        mouseLeave();
    }

    public void setEnable(boolean enabled) {
        this.enabled = enabled;
        focusPanel.setWidget(this.enabled ? image : disabledImage);
        focusPanel.setTitle(this.enabled ? toolTip : "");
        if (!enabled) {
            mouseLeave();
        }
    }

    public boolean isEnabled() {
        return enabled;
    }

    private void mouseEnter() {
        if (enabled) {
            DOM.setStyleAttribute(getElement(), "border-style", "outset");
            DOM.setStyleAttribute(getElement(), "border-width", "1px");
            DOM.setStyleAttribute(getElement(), "margin", "0px");
            DOM.setStyleAttribute(getElement(), "cursor", "hand");
        }
    }

    private void mouseLeave() {
        DOM.setStyleAttribute(getElement(), "margin", "1px");
        DOM.setStyleAttribute(getElement(), "border-width", "0px");
        DOM.setStyleAttribute(getElement(), "cursor", "default");
    }

    public void addClickListener(final ClickListener cl) {
        focusPanel.addClickListener(new ClickListener() {

            public void onClick(Widget arg0) {
                if (ImageButton.this.enabled) {
                    cl.onClick(ImageButton.this);
                    mouseLeave();
                }
            }
        });
    }
}
