/* 
 * This file is part of the Echo Web Application Framework (hereinafter "Echo").
 * Copyright (C) 2002-2005 NextApp, Inc.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */

package nextapp.echo2.app.button;

import java.util.EventListener;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.ImageReference;
import nextapp.echo2.app.event.ChangeEvent;
import nextapp.echo2.app.event.ChangeListener;

/**
 * An abstract base class for 'two state' buttons.
 */
public abstract class ToggleButton extends AbstractButton {
    
    public static final String PROPERTY_PRESSED_STATE_ICON = "pressedStateIcon";
    public static final String PROPERTY_PRESSED_SELECTED_STATE_ICON = "pressedSelectedStateIcon";
    public static final String PROPERTY_ROLLOVER_STATE_ICON = "rolloverStateIcon";
    public static final String PROPERTY_ROLLOVER_SELECTED_STATE_ICON = "rolloverSelectedStateIcon";
    public static final String PROPERTY_STATE_ALIGNMENT = "stateAlignment";
    public static final String PROPERTY_STATE_ICON = "stateIcon";
    public static final String PROPERTY_STATE_MARGIN = "stateMargin";
    public static final String PROPERTY_STATE_POSITION = "statePosition";
    public static final String PROPERTY_SELECTED_STATE_ICON = "selectedStateIcon";
    
    public static final String SELECTED_CHANGED_PROPERTY = "selected";
    
    /**
     * Forwards events generated by the model to listeners registered with the
     * component instance.
     */
    private ChangeListener changeForwarder = new ChangeListener() {

        /**
         * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
         */
        public void stateChanged(ChangeEvent e) {
            fireStateChanged();
            if (isSelected()) {
                firePropertyChange(SELECTED_CHANGED_PROPERTY, Boolean.FALSE, Boolean.TRUE);
            } else {
                firePropertyChange(SELECTED_CHANGED_PROPERTY, Boolean.TRUE, Boolean.FALSE);
            }
        }
    };

    /**
     * Adds a <code>ChangeListener</code> to receive notification of state
     * changes, i.e., to the selected state of a <code>ToggleButton</code>.
     * 
     * @param l  the listener to add
     */
    public void addChangeListener(ChangeListener l) {
        getEventListenerList().addListener(ChangeListener.class, l);
    }
    
    /**
     * Notifies all listeners that have registered for this event type.
     */
    public void fireStateChanged() {
        EventListener[] listeners = getEventListenerList().getListeners(ChangeListener.class);
        ChangeEvent e = new ChangeEvent(this);
        for (int index = 0; index < listeners.length; ++index) {
            ((ChangeListener) listeners[index]).stateChanged(e);
        }
    }
    
    /**
     * Returns the selected state icon displayed when the 
     * button is being pressed.
     * 
     * @return the icon
     */
    public ImageReference getPressedSelectedStateIcon() {
        return (ImageReference) getProperty(PROPERTY_PRESSED_SELECTED_STATE_ICON);
    }
    
    /**
     * Returns the default (non-selected) state icon displayed when the 
     * button is being pressed.
     * 
     * @return the icon
     */
    public ImageReference getPressedStateIcon() {
        return (ImageReference) getProperty(PROPERTY_PRESSED_STATE_ICON);
    }
    
    /**
     * Returns the selected state icon displayed when the mouse
     * cursor is inside the bounds of the button.
     * 
     * @return the icon
     */
    public ImageReference getRolloverSelectedStateIcon() {
        return (ImageReference) getProperty(PROPERTY_ROLLOVER_SELECTED_STATE_ICON);
    }
    
    /**
     * Returns the default (non-selected) state icon displayed when the mouse
     * cursor is inside the bounds of the button.
     * 
     * @return the icon
     */
    public ImageReference getRolloverStateIcon() {
        return (ImageReference) getProperty(PROPERTY_ROLLOVER_STATE_ICON);
    }
    
    /**
     * Returns the selected state icon.
     * 
     * @return the icon
     */
    public ImageReference getSelectedStateIcon() {
        return (ImageReference) getProperty(PROPERTY_SELECTED_STATE_ICON);
    }
    
    /**
     * Returns the alignment of the state icon relative to the button's
     * text/icon.
     * 
     * @return the state alignment
     */
    public Alignment getStateAlignment() {
        return (Alignment) getProperty(PROPERTY_STATE_ALIGNMENT);
    }

    /**
     * Returns the default (non-selected) state icon.
     * 
     * @return the icon
     */
    public ImageReference getStateIcon() {
        return (ImageReference) getProperty(PROPERTY_STATE_ICON);
    }
    
    /**
     * Returns the margin size between the state icon and the 
     * toggle button's icon and/or text.  The margin will only
     * be displayed in the case where icon and/or text are
     * present.
     * This property only supports <code>Extent</code>s with
     * fixed (i.e., not percent) units.
     * 
     * @return the margin size 
     */
    public Extent getStateMargin() {
        return (Extent) getProperty(PROPERTY_STATE_MARGIN);
    }

    /**
     * Returns the position of the state icon relative to the button's
     * text/icon.
     * 
     * @return the state position
     */
    public Alignment getStatePosition() {
        return (Alignment) getProperty(PROPERTY_STATE_POSITION);
    }
    
    /**
     * Determines the selection state.
     * 
     * @return the selection state
     */
    public boolean isSelected() {
        return ((ToggleButtonModel) getModel()).isSelected();
    }

    /**
     * Removes a <code>ChangeListener</code> from being notified of state
     * changes, i.e., to the selected state of a <code>ToggleButton</code>.
     * 
     * @param l the listener to remove
     */
    public void removeChangeListener(ChangeListener l) {
        getEventListenerList().removeListener(ChangeListener.class, l);
    }
    
    /**
     * @see nextapp.echo2.app.button.AbstractButton#setModel(nextapp.echo2.app.button.ButtonModel)
     */
    public void setModel(ButtonModel newValue) {
        if (!(newValue instanceof ToggleButtonModel)) {
            throw new IllegalArgumentException("Model is not a ToggleButtonModel.");
        }

        ToggleButtonModel oldValue = (ToggleButtonModel) getModel();

        if (oldValue != null) {
            oldValue.removeChangeListener(changeForwarder);
        }
        
        super.setModel(newValue);

        ((ToggleButtonModel) newValue).addChangeListener(changeForwarder);
    }

    /**
     * Sets the selected state icon displayed when the 
     * button is being pressed.
     * 
     * @param newValue the new icon
     */
    public void setPressedSelectedStateIcon(ImageReference newValue) {
        setProperty(PROPERTY_PRESSED_SELECTED_STATE_ICON, newValue);
    }
    
    /**
     * Sets the default (non-selected) state icon displayed when the 
     * button is being pressed.
     * 
     * @param newValue the new icon
     */
    public void setPressedStateIcon(ImageReference newValue) {
        setProperty(PROPERTY_PRESSED_STATE_ICON, newValue);
    }
    
    /**
     * Sets the selected state icon displayed when the mouse
     * cursor is inside the bounds of the button.
     * 
     * @param newValue the new icon
     */
    public void setRolloverSelectedStateIcon(ImageReference newValue) {
        setProperty(PROPERTY_ROLLOVER_SELECTED_STATE_ICON, newValue);
    }
    
    /**
     * Sets the default (non-selected) state icon displayed when the mouse
     * cursor is inside the bounds of the button.
     * 
     * @param newValue the new icon
     */
    public void setRolloverStateIcon(ImageReference newValue) {
        setProperty(PROPERTY_ROLLOVER_STATE_ICON, newValue);
    }
    
    /**
     * Sets the selected state icon.
     * 
     * @param newValue the new icon
     */
    public void setSelectedStateIcon(ImageReference newValue) {
        setProperty(PROPERTY_SELECTED_STATE_ICON, newValue);
    }
    
    /**
     * Sets the selection state.
     * 
     * @param newValue the new selection state
     */
    public void setSelected(boolean newValue) {
        ((ToggleButtonModel) getModel()).setSelected(newValue);
    }
    
    /**
     * Sets the alignment of the state icon relative to the text/icon
     * of the button.
     * Note that only one of the provided <code>Alignment</code>'s
     * settings should be non-default.
     * 
     * @param newValue the new state alignment
     */
    public void setStateAlignment(Alignment newValue) {
        setProperty(PROPERTY_STATE_ALIGNMENT, newValue);
    }
    
    /**
     * Sets the default (non-selected) state icon.
     * 
     * @param newValue the new icon
     */
    public void setStateIcon(ImageReference newValue) {
        setProperty(PROPERTY_STATE_ICON, newValue);
    }
    
    /**
     * Sets the margin size between the state icon and the 
     * toggle button's icon and/or text.  The margin will only
     * be displayed in the case where icon and/or text are
     * present.
     * This property only supports <code>Extent</code>s with
     * fixed (i.e., not percent) units.
     * 
     * @param newValue the new margin size 
     */
    public void setStateMargin(Extent newValue) {
        setProperty(PROPERTY_STATE_MARGIN, newValue);
    }

    /**
     * Sets the position of the state icon relative to the text/icon
     * of the button.
     * Note that only one of the provided <code>Alignment</code>'s
     * settings should be non-default.
     * 
     * @param newValue the new state position
     */
    public void setStatePosition(Alignment newValue) {
        setProperty(PROPERTY_STATE_POSITION, newValue);
    }
}
