/*
 * MMCDButton.java
 *
 * Created on 15 Şubat 2005 Salı, 22:22
 */

package com.isoft.mmcd.skin.widgets ;

import java.awt.* ;
import java.awt.image.* ;
import javax.swing.* ;
import javax.imageio.* ;

import com.isoft.mmcd.skin.* ;
import com.isoft.mmcd.exceptions.* ;
import com.isoft.mmcd.utils.cfgreader.* ;

/**
 * @author Fatih
 */
public class MMCDButton extends JButton implements java.io.Serializable, Widget, ResizableWidget
{
    protected String helpId = "" ;

    protected String widgetName = "" ;

    protected DynamicWidgetBounds dwbounds = null ;

    protected BufferedImage mask = null ;

    protected String popupMenuName = null ;

    protected int zIndex ;

    /** Creates a new instance of MMCDButton */
    public MMCDButton( )
    {
        super( ) ;
    }

    public boolean init( MCCompoundProperty property ) throws MMCDException
    {
        try
        {
            MCCompoundProperty cp = property.getCompoundProperty( "@images" ) ;
            if( cp != null )
            {
                MCProperty p ;
                if( ( p = cp.getAtomicProperty( "$default" ) ) != null )
                    setIcon( new ImageIcon( ImageIO.read( p.getInputWrapper( ).getStream( ) ) ) ) ;
                if( ( p = cp.getAtomicProperty( "$pressed" ) ) != null )
                    setPressedIcon( new ImageIcon( ImageIO.read( p.getInputWrapper( ).getStream( ) ) ) ) ;
                if( ( p = cp.getAtomicProperty( "$mouseover" ) ) != null )
                    setRolloverIcon( new ImageIcon( ImageIO.read( p.getInputWrapper( ).getStream( ) ) ) ) ;
                if( ( p = cp.getAtomicProperty( "$disabled" ) ) != null )
                    setDisabledIcon( new ImageIcon( ImageIO.read( p.getInputWrapper( ).getStream( ) ) ) ) ;
                if( ( p = cp.getAtomicProperty( "$mask" ) ) != null )
                    mask = ImageIO.read( p.getInputWrapper( ).getStream( ) ) ;
            }

            if( property.getAtomicProperty( "$onlyimages" ) != null && property.getAtomicProperty( "$onlyimages" ).getBooleanValue( ) )
            {
                setBorder( null ) ;
                setBorderPainted( false ) ;
                setContentAreaFilled( false ) ;
                setFocusPainted( false ) ;
            }
        }
        catch( Exception e )
        {
            System.err.println( "Cannot initialize button" ) ;
            e.printStackTrace( ) ;
            throw new MMCDException( e ) ;
        }
        return true ;
    }

    public boolean contains( int x, int y )
    {
        if( mask == null )
            return super.contains( x, y ) ;

        if( ( x < 0 ) || ( y < 0 ) )
            return false ;

        if( ( x >= mask.getWidth( ) ) || ( y >= mask.getHeight( ) ) )
            return false ;

        if( mask.getRGB( x, y ) == Color.black.getRGB( ) )
            return true ;
        return false ;
    }

    public boolean contains( Point p )
    {
        if( mask == null )
            return super.contains( p ) ;

        if( ( p.x < 0 ) || ( p.y < 0 ) )
            return false ;

        if( ( p.x >= mask.getWidth( ) ) || ( p.y >= mask.getHeight( ) ) )
            return false ;

        if( mask.getRGB( p.x, p.y ) == Color.black.getRGB( ) )
            return true ;
        return false ;
    }

    public String getWidgetName( )
    {
        return widgetName ;
    }

    public void setWidgetName( String widgetName )
    {
        this.widgetName = widgetName ;
    }

    public DynamicWidgetBounds getDynamicWidgetBounds( )
    {
        return dwbounds ;
    }

    public void setDynamicWidgetBounds( DynamicWidgetBounds dwb )
    {
        this.dwbounds = dwb ;
    }

    public void resizeWidget( Rectangle newParentBounds )
    {
        getDynamicWidgetBounds( ).calculateBounds( newParentBounds ) ;
        setBounds( getDynamicWidgetBounds( ).getBounds( ) ) ;
        setPreferredSize( new Dimension( getDynamicWidgetBounds( ).getBounds( ).width, getDynamicWidgetBounds( ).getBounds( ).height ) ) ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#getPopUpMenuName()
     */
    public String getPopUpMenuName( )
    {

        return this.popupMenuName ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#setPopUpMenuName(java.lang.String)
     */
    public void setPopUpMenuName( String popupMenuName )
    {
        this.popupMenuName = popupMenuName ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#setHelpId(java.lang.String)
     */
    public void setHelpId( String helpId )
    {
        this.helpId = helpId ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#getHelpId()
     */
    public String getHelpId( )
    {
        return this.helpId ;
    }
}