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

import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import luxor.core.*;
import luxor.event.*;

public abstract class XulForm
{

   private String _id;

   private EventListenerList _listener = new EventListenerList();
   private XulFormHelper _xulFormHelper;

   public XulForm( String id )
   {
      _id = id;
      _xulFormHelper = new XulFormHelper( id );
   }

   /**
    *  Note: don't forget to call setup in derived class I can't put setup in
    *  constructor because it wouldn't allow setting of any member variables
    *  which might be needed in init
    */
   public void setup()
   {
      init();

      _xulFormHelper.setup();

      postInit();
   }

   public String getId()
   {
      return _id;
   }

   public Iterator getInputControls()
   {
      // fix: find a better method name
      //  check JavaScript DOM or WinForms
      //  - rename input to control?
      //  - rename component to gadget?

      // todo: should I return an array (that is, XulInput[])
      //   instead of an iterator?
      //  or should I return an order-preserving LinkedHashMap?
      return _xulFormHelper.getInputControls();
   }

   public JComponent getJComponent()
   {
      return _xulFormHelper.getJComponent();
   }

   public List getValidationErrors()
   {
      return _xulFormHelper.getValidationErrors();
   }

   public boolean isValid()
   {
      return _xulFormHelper.isValid();
   }

   public void addComponent( String id, JComponent component )
   {
      _xulFormHelper.addComponent( id, component );
   }

   public void addInput( XulInput input )
   {
      _xulFormHelper.addInput( input );
   }

   public void addListener( XulFormListener l )
   {
      _listener.add( XulFormListener.class, l );
   }

   public String exportToXml()
   {
      return _xulFormHelper.exportAsXml();
   }

   public void importFromXml( String xml )
   {
      _xulFormHelper.importFromXml( xml );
   }

   public abstract void init();

   public void postInit()
   {
      /*
       *  do nothing by default
       */
   }

   public void removeListener( XulFormListener l )
   {
      _listener.remove( XulFormListener.class, l );
   }

   protected void fireFocusGained( XulInput input )
   {
      XulFormListener l[] = ( XulFormListener[] ) _listener.getListeners( XulFormListener.class );

      for( int i = 0; i < l.length; i++ )
         l[i].focusGained( input );
   }

   protected void fireFocusLost( XulInput input )
   {
      XulFormListener l[] = ( XulFormListener[] ) _listener.getListeners( XulFormListener.class );

      for( int i = 0; i < l.length; i++ )
         l[i].focusLost( input );
   }
}