/*
 * Created on 10.Tem.2005
 */
package com.isoft.mmcd.skin.widgets ;

import java.awt.event.ActionListener ;
import java.util.ArrayList ;

import javax.swing.* ;
import javax.swing.JMenuItem ;
import javax.swing.JPopupMenu ;

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

/**
 * @author mentis
 */
public class MMCDPopUpMenu extends JPopupMenu implements java.io.Serializable, Widget
{
    protected String helpId = "" ;

    protected String widgetName = "" ;

    protected ArrayList menuitems = new ArrayList( ) ;

    protected ArrayList eventNames = new ArrayList( ) ;

    protected int zIndex ;

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

    public void addActionListener( ActionListener alistener )
    {
        System.out.println( "Adding an action listener to the menu of " + menuitems.size( ) + " elements" ) ;
        for( int i = 0; i < menuitems.size( ); ++i )
        {
            getMenuItemAt( i ).addActionListener( alistener ) ;
        }
    }

    public boolean init( MCCompoundProperty property ) throws MMCDException
    {
        MCCompoundProperty cpitems = property.getCompoundProperty( "@menuitems" ) ;

        if( cpitems != null )
        {
            int index = 0 ;
            MCCompoundProperty item = cpitems.getCompoundProperty( "@item_" + index ) ;
            JMenuItem mi = null ;

            while( item != null )
            {
                boolean issel = false ;

                if( item.getAtomicProperty( "$checked" ) != null )
                    issel = item.getAtomicProperty( "$checked" ).getBooleanValue( ) ;

                if( item.getAtomicProperty( "$type" ) == null || "default".equals( item.getAtomicProperty( "$type" ).getValue( ) ) )
                    mi = new JMenuItem( ) ;
                else if( "checkbox".equals( item.getAtomicProperty( "$type" ).getValue( ) ) )
                    mi = new JCheckBoxMenuItem( "", issel ) ;
                else if( "radio".equals( item.getAtomicProperty( "$type" ).getValue( ) ) )
                    mi = new JRadioButtonMenuItem( "", issel ) ;
                else if( "separator".equals( item.getAtomicProperty( "$type" ).getValue( ) ) )
                {
                    addSeparator( ) ;
                    ++index ;
                    item = cpitems.getCompoundProperty( "@item_" + index ) ;
                    continue ;
                }
                else
                    mi = new JMenuItem( ) ;

                if( item.getAtomicProperty( "$text" ) != null )
                    mi.setText( item.getAtomicProperty( "$text" ).getValue( ) ) ;

                if( item.getAtomicProperty( "$grup" ) != null )
                    MMCDCheckBox.getNotNullGroup( item.getAtomicProperty( "$grup" ).getValue( ) ).add( mi ) ;

                if( item.getAtomicProperty( "$event" ) != null )
                    eventNames.add( item.getAtomicProperty( "$event" ).getValues( ) ) ;
                else
                    eventNames.add( null ) ;

                menuitems.add( mi ) ;
                this.add( mi ) ;

                ++index ;
                item = cpitems.getCompoundProperty( "@item_" + index ) ;
            }

        }

        this.pack( ) ;
        return true ;
    }

    public String getWidgetName( )
    {
        return widgetName ;
    }

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

    public JMenuItem getMenuItemAt( int index )
    {
        return ( JMenuItem )menuitems.get( index ) ;
    }

    public String getEventNameAt( int index )
    {
        return ( ( String[ ] )eventNames.get( index ) )[ 0 ] ;
    }

    public String getEventNameAt( int index, int eventNum )
    {
        return ( ( String[ ] )eventNames.get( index ) )[ eventNum ] ;
    }

    public int getMenuItemIndex( Object menuitem )
    {
        return menuitems.indexOf( menuitem ) ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#getPopUpMenuName()
     */
    public String getPopUpMenuName( )
    {
        // a popup menu can not have another popup
        return null ;
    }

    /* (non-Javadoc)
     * @see com.isoft.mmcd.skin.Widget#setPopUpMenuName(java.lang.String)
     */
    public void setPopUpMenuName( String popupMenuName )
    {
        // this will not be functional for a popup itself
    }

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

}