/*
 * DefaultWindow.java
 *
 * Created on 15 �ubat 2005 Sal�, 21:18
 */

package com.isoft.mmcd.skin ;

import com.isoft.mmcd.exceptions.* ;
import com.isoft.mmcd.skin.widgets.MMCDGlassPane ;
import com.isoft.mmcd.skin.widgets.MMCDPanel ;
import com.isoft.mmcd.utils.cfgreader.* ;

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

/**
 *
 * @author Fatih
 */
public class MMCDWindow extends javax.swing.JFrame implements java.io.Serializable, Widget
{
    static
    {
        boolean assertsEnabled = false ;
        assert ( assertsEnabled = true ) == true ; 
        if( !assertsEnabled )
            throw new RuntimeException( "Asserts must be enabled!!! Add -ea parameter to JVM" ) ;
    }

    protected String helpId = "" ;

    protected String widgetName = "" ;
    
    protected int zIndex ;

    public MMCDWindow( )
    {
        super( ) ;
    }

    public boolean init( MCCompoundProperty property ) throws MMCDException
    {
        if( property.getAtomicProperty( "$title" ) != null )
            setTitle( property.getAtomicProperty( "$title" ).getValue( ) ) ;

        if( property.getAtomicProperty( "$exitonclose" ) != null )
        {
            if( property.getAtomicProperty( "$exitonclose" ).getBooleanValue( ) )
                setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE ) ;
        }

        // setBounds( property.getCompoundProperty( "@position" ).getRectangle( ) ) ;
        getContentPane( ).setBounds( property.getCompoundProperty( "@position" ).getRectangle( ) ) ;
        ( ( JComponent )getContentPane( ) ).setPreferredSize( new Dimension( getContentPane( ).getBounds( ).width, getContentPane( ).getBounds( ).height ) ) ;

        // setBounds( property.getCompoundProperty( "@position" ).getRectangle( ) ) ;

        if( property.getAtomicProperty( "$minimumsize" ) != null )
        {
            // setMinimumSize( property.getAtomicProperty( "$minimumsize" ).getDimension( ) ) ;
            ( ( JComponent )getContentPane( ) ).setMinimumSize( property.getAtomicProperty( "$minimumsize" ).getDimension( ) ) ;
        }
        
        if( property.getAtomicProperty( "$color" ) != null )
            setForeground( property.getAtomicProperty( "$color" ).getColorValue( ) ) ;

        if( property.getAtomicProperty( "$bgcolor" ) != null )
            setBackground( property.getAtomicProperty( "$bgcolor" ).getColorValue( ) ) ;

        setEnabled( !( property.getAtomicProperty( "$disabled" ) != null && property.getAtomicProperty( "$disabled" ).getBooleanValue( ) ) ) ;

        if( property.getAtomicProperty( "$visible" ) != null )
            setVisible( property.getAtomicProperty( "$title" ).getBooleanValue( ) ) ;

        MMCDPanel p = new MMCDPanel( ) ;
        p.setOpaque( false ) ;

        this.setGlassPane( p ) ;
        this.getGlassPane( ).setVisible( true ) ;

        return true ;
    }

    public String getWidgetName( )
    {
        return widgetName ;
    }

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

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

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#setPopUpMenuName(java.lang.String)
     */
    public void setPopUpMenuName( String popupMenuName )
    {
        /* we do not have popups for windows */
    }

    public void activateGlassPane( Component component )
    {
        // Mount the glasspane on the component window
        MMCDGlassPane aPane = MMCDGlassPane.mount( component, true ) ;

        // keep track of the glasspane as an instance variable
        setGlassPane( aPane ) ;

        if( getGlassPane( ) != null )
        {
            // Start interception UI interactions
            getGlassPane( ).setVisible( true ) ;
        }
    }

    public void deactivateGlassPane( )
    {
        if( getGlassPane( ) != null )
        {
            // Stop UI interception
            getGlassPane( ).setVisible( false ) ;
        }
    }

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

}