/*
** Luxor - XML User Interface Language (XUL) Toolkit
** Copyright (c) 2001, 2002 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/

package luxor.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import luxor.core.*;
import luxor.spi.*;
import luxor.status.*;

public abstract class AbstractContainer extends AbstractComponent
       implements NContainer, NComponentResolver
{
   /**
    *  holds all components (order preserved)
    */
   protected ArrayList _component = new ArrayList();
   /**
    *  lookup index for all components (including input controls and containers)
    */
   protected HashMap _componentIndex = new HashMap();
   /**
    *  holds containers only
    */
   protected ArrayList _container = new ArrayList();

   /**
    *  lookup index for input controls only
    */
   protected HashMap _inputIndex = new HashMap();
   static Logger T = Logger.getLogger( AbstractContainer.class );

   public AbstractContainer( XulNode def )
   {
      super( def );
   }

   public NComponent lookupNComponent( String id )
   {
      // for now check only our own children/components
      //   don't query subcontainers recursively
      //  if there is a need, I add it later

      return ( NComponent ) _componentIndex.get( id );
   }

   public NInput lookupNInput( String id )
   {
      NInput input = ( NInput ) _inputIndex.get( id );
      if( input != null )
         return input;

      // check embedded containers
      Iterator it = _container.iterator();
      while( it.hasNext() )
      {
         NContainer cont = ( NContainer ) it.next();
         input = ( NInput ) cont.lookupNInput( id );
         if( input != null )
            return input;
      }

      return null;
      // input control not found
   }

   public void resolveReferences( NComponentResolver resolver )
   {
      // do nothing; container resolves references after creation
      //  for now only our own direct children
      //    can be referenced for resolution
      //  if there is a need,
      //   to query subcontainers recursively, I add it later
   }


   protected void addComponent( NComponent comp )
   {
      _componentIndex.put( comp.getId(), comp );
      _component.add( comp );

      if( comp instanceof NInput )
      {
         _inputIndex.put( comp.getId(), ( NInput ) comp );
      }
      else if( comp instanceof NContainer )
      {
         _container.add( ( NContainer ) comp );
      }
   }

   protected void resolveReferences()
   {
      for( Iterator it = _component.iterator(); it.hasNext();  )
      {
         NComponent comp = ( NComponent ) it.next();
         comp.resolveReferences( this );
      }
   }
}