/*
 * Created on 14.Haz.2005
 */
package com.isoft.mmcd.skin.widgets ;

import java.awt.Adjustable ;
import java.awt.Dimension ;
import java.awt.Graphics ;
import java.awt.LayoutManager ;
import java.awt.Rectangle ;
import java.io.Serializable ;

import javax.swing.BoundedRangeModel ;
import javax.swing.DefaultBoundedRangeModel ;
import javax.swing.JScrollBar ;

import com.isoft.mmcd.exceptions.MMCDException ;
import com.isoft.mmcd.skin.DynamicWidgetBounds ;
import com.isoft.mmcd.skin.ResizableWidget ;
import com.isoft.mmcd.skin.Widget ;
import com.isoft.mmcd.utils.cfgreader.MCCompoundProperty ;

/**
 * @author Fatih
 */
public class MMCDScrollBar extends JScrollBar implements Widget, ResizableWidget, Serializable
{
    protected String helpId = "" ;

    protected String widgetName = "" ;

    protected String popupMenuName = null ;

    protected BoundedRangeModel brm = null ;

    protected DynamicWidgetBounds dwbounds = null ;

    protected int zIndex ;

    public MMCDScrollBar( )
    {
        super( ) ;
    }
    
    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#init(com.isoft.mmcd.utils.cfgreader.MCCompoundProperty)
     */
    public boolean init( MCCompoundProperty property ) throws MMCDException
    {
        brm = new DefaultBoundedRangeModel( ) ;

        if( property.getAtomicProperty( "$min" ) != null )
            brm.setMinimum( property.getAtomicProperty( "$min" ).getNumericValue( ) ) ;
        if( property.getAtomicProperty( "$max" ) != null )
            brm.setMaximum( property.getAtomicProperty( "$max" ).getNumericValue( ) ) ;
        if( property.getAtomicProperty( "$blockincrement" ) != null )
            setBlockIncrement( property.getAtomicProperty( "$blockincrement" ).getNumericValue( ) ) ;
        if( property.getAtomicProperty( "$visiblescrollsize" ) != null )
            brm.setExtent( property.getAtomicProperty( "$visiblescrollsize" ).getNumericValue( ) ) ;
        if( property.getAtomicProperty( "$initialvalue" ) != null )
            brm.setValue( property.getAtomicProperty( "$initialvalue" ).getNumericValue( ) ) ;
        if( property.getAtomicProperty( "$isvertical" ) != null )
            setOrientation( property.getAtomicProperty( "$isvertical" ).getBooleanValue( ) ? Adjustable.VERTICAL : Adjustable.HORIZONTAL ) ;

        setModel( brm ) ;

        System.err.println( "Wdt:" + getWidth( ) ) ;
        System.err.println( "Hgt:" + getHeight( ) ) ;

        getMaximumSize( ) ;

        return true ;
    }

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

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

    public DynamicWidgetBounds getDynamicWidgetBounds( )
    {
        return dwbounds ;
    }

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

    public void resizeWidget( Rectangle newParentBounds )
    {
        System.err.println( "ParentBounds:" + newParentBounds.toString( ) ) ;
        getDynamicWidgetBounds( ).calculateBounds( newParentBounds ) ;
        setBounds( getDynamicWidgetBounds( ).getBounds( ) ) ;
        setPreferredSize( new Dimension( getDynamicWidgetBounds( ).getBounds( ).width, getDynamicWidgetBounds( ).getBounds( ).height ) ) ;

        /*
         * Biz default olarak Layout ların hepsini remove ettiğimiz için, 
         * scrollpane update edilmiyordu. 
         * O yüzden, LayoutManager ın içinden componenti ayarlama kodunu kendimiz çağırmamız gerekiyor.
         * Belki daha şık yöntemleri de vardır. (Mesela AbsoluteLayout kullanmak)
         * Ama şimdilik böyle olsun..
         * */
        if( getUI( ) instanceof LayoutManager )
            ( ( LayoutManager )getUI( ) ).layoutContainer( this ) ;
    }

    public void paint( Graphics g )
    {
        super.paint( g ) ;
    }

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

}