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

import java.awt.Dimension ;
import java.awt.Rectangle ;
import java.util.ArrayList ;

import javax.swing.* ;
import javax.swing.JList ;

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


public class MMCDListView extends JList implements java.io.Serializable, Widget, ResizableWidget, ScrollableWidget
{
    protected String helpId = "" ;

    protected String widgetName = "" ;

    protected ArrayList listItemDataList = new ArrayList( ) ;

    protected String popupMenuName = null ;

    protected DynamicWidgetBounds dwbounds = null ;

    protected MMCDScrollPane scrollPane = null ;

    protected int zIndex ;

    /** Creates a new instance of MMCDTextField */
    public MMCDListView( )
    {
        super( ) ;
        setScrollPane( new MMCDScrollPane( this ) ) ;
        getSelectionModel( ).setSelectionMode( ListSelectionModel.SINGLE_SELECTION ) ;
        setAutoscrolls( true ) ;
    }

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

        this.setModel( new DefaultListModel( ) ) ;

        if( cpitems != null )
        {
            int index = 0 ;
            MCCompoundProperty item = cpitems.getCompoundProperty( "@item_" + index ) ;
            String listItem = null ;
            String listItemData = null ;
            while( item != null )
            {
                if( item.getAtomicProperty( "$text" ) != null )
                    listItem = item.getAtomicProperty( "$text" ).getValue( ) ;
                else
                    listItem = "-" ;

                if( item.getAtomicProperty( "$data" ) != null )
                    listItemData = item.getAtomicProperty( "$data" ).getValue( ) ;
                else
                    listItemData = null ;

                System.out.println( getModel( ).getClass( ).getName( ) ) ;

                ( ( DefaultListModel )getModel( ) ).addElement( listItem ) ;

                this.listItemDataList.add( listItemData ) ;

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

        }

        return true ;
    }

    public String getWidgetName( )
    {
        return widgetName ;
    }

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

    public int getListSize( )
    {
        if( this.listItemDataList == null )
            return 0 ;
        return this.listItemDataList.size( ) ;
    }

    public Object getListItemDataAt( int index )
    {
        return this.listItemDataList.get( index ) ;
    }

    public int getListItemDataIndex( Object listitem )
    {
        return this.listItemDataList.indexOf( listitem ) ;
    }

    public void addToList( String text, Object data )
    {
        ( ( DefaultListModel )getModel( ) ).addElement( text ) ;
        this.listItemDataList.add( data ) ;
    }

//    public void rebuildList( ArrayList listElements )
//    {
//        DefaultListModel newmodel = new DefaultListModel( ) ;
//        ArrayList alist = new ArrayList( ) ;
//                
//        System.err.println( "Building list" ) ;
//        for( int i = 0; i < listElements.size( ); ++i )
//        {
//            System.err.println( "adding to list:" + listElements.get( i ) ) ;
//            newmodel.addElement( listElements.get( i ) ) ;
//            alist.add( null ) ;
//        }
//
//        this.listItemDataList = alist ;
//        this.setModel( newmodel ) ;
//
//        this.repaint( ) ;
//        this.validate( ) ;
//    }
//
//    
    public void rebuildList( ArrayList listElements, ArrayList itemData )
    {
        DefaultListModel newmodel = new DefaultListModel( ) ;
        ArrayList alist = new ArrayList( ) ;
                
        //System.err.println( "Building list" ) ;
        for( int i = 0; i < listElements.size( ); ++i )
        {
            //System.err.println( "adding to list:" + listElements.get( i ) ) ;
            newmodel.addElement( listElements.get( i ) ) ;
            alist.add( itemData.get( i ) ) ;
        }

        this.listItemDataList = alist ;
        this.setModel( newmodel ) ;

        this.repaint( ) ;
        this.validate( ) ;
    }

    public void removeFromList( int index )
    {
        this.listItemDataList.remove( index ) ;
        ( ( DefaultListModel )getModel( ) ).remove( index ) ;
    }

    public DynamicWidgetBounds getDynamicWidgetBounds( )
    {
        if( getScrollPane( ) != null )
            return getScrollPane( ).getDynamicWidgetBounds( ) ;

        return dwbounds ;
    }

    public void setDynamicWidgetBounds( DynamicWidgetBounds dwb )
    {
        if( getScrollPane( ) != null )
        {
            getScrollPane( ).setDynamicWidgetBounds( dwb ) ;
        }
        else
        {
            this.dwbounds = dwb ;
        }
    }

    public void resizeWidget( Rectangle newParentBounds )
    {
        if( getScrollPane( ) != null )
        {
            getScrollPane( ).resizeWidget( newParentBounds ) ;
        }
        else
        {
            getDynamicWidgetBounds( ).calculateBounds( newParentBounds ) ;
            setBounds( getDynamicWidgetBounds( ).getBounds( ) ) ;
            setPreferredSize( new Dimension( getDynamicWidgetBounds( ).getBounds( ).width, getDynamicWidgetBounds( ).getBounds( ).height ) ) ;
        }
    }

    public MMCDScrollPane getScrollPane( )
    {
        return scrollPane ;
    }

    public void setScrollPane( MMCDScrollPane scrollPane )
    {
        this.scrollPane = scrollPane ;
    }

    public void setVisible( boolean visible )
    {
        super.setVisible( visible ) ;
        if( getScrollPane( ) != null )
            getScrollPane( ).setVisible( visible ) ;
    }

    public void setInheritsPopupMenu( boolean flag )
    {
        super.setInheritsPopupMenu( flag ) ;
        if( getScrollPane( ) != null )
            getScrollPane( ).setInheritsPopupMenu( flag ) ;
    }

    public void setComponentPopupMenu( JPopupMenu popup )
    {
        super.setComponentPopupMenu( popup ) ;
        if( getScrollPane( ) != null )
            getScrollPane( ).setComponentPopupMenu( popup ) ;
    }

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

}