/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.dialog;

import java.util.*;
import org.apache.log4j.Logger;
import org.w3c.dom.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;
import com.silverwrist.venice.ButtonType;
import com.silverwrist.venice.iface.*;

public class VeniceDialogManager implements NamedObject, ComponentInitialize, ComponentShutdown, DialogPLAF
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(VeniceDialogManager.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_name;
  private ButtonProvider m_buttonprov;
  private ContentBlockProvider m_cbprov;
  private LinkedList m_shutdown_list = new LinkedList();

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public VeniceDialogManager()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface NamedObject
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return m_name;

  } // end getName

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentInitialize
   *--------------------------------------------------------------------------------
   */

  /**
   * Initialize the component.
   *
   * @param config_root Pointer to the section of the Dynamo XML configuration file that configures this
   *                    particular component.  This is to be considered "read-only" by the component.
   * @param services An implementation of {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider}
   *                 which provides initialization services to the component.  This will include an implementation
   *                 of {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider} which may be used to
   *                 get information about other objects previously initialized by the application.
   * @exception com.silverwrist.dynamo.except.ConfigException If an error is encountered in the component
   *            configuration.
   */
  public void initialize(Element config_root, ServiceProvider services) throws ConfigException
  {
    logger.info("VeniceDialogManager initializing");

    XMLLoader loader = XMLLoader.get();
    String button_prov = null, content_prov = null;
    try
    { // verify the right node name
      loader.verifyNodeName(config_root,"object");

      // get the object's name
      m_name = loader.getAttribute(config_root,"name");

      // get the names of the default providers
      DOMElementHelper config_root_h = new DOMElementHelper(config_root);
      Element foo = loader.getSubElement(config_root_h,"providers");
      button_prov = loader.getAttribute(foo,"button");
      content_prov = loader.getAttribute(foo,"content");

    } // end try
    catch (XMLLoadException e)
    { // error loading XML config data
      logger.fatal("XML loader exception in StandardContentSupplier",e);
      throw new ConfigException(e);

    } // end catch

    // Get the initial objects.
    m_buttonprov = (ButtonProvider)(GetObjectUtils.getDynamoComponent(services,ButtonProvider.class,button_prov));
    m_cbprov = (ContentBlockProvider)(GetObjectUtils.getDynamoComponent(services,ContentBlockProvider.class,
									content_prov));

    // Register ourselves as providing the new dialog look-and-feel.
    DialogFactoryConfig dlg_conf = (DialogFactoryConfig)(services.queryService(DialogFactoryConfig.class));
    dlg_conf.setDialogPLAF(this);

    // Register our own field types.
    VeniceDialogItemFactory itemfact = new VeniceDialogItemFactory();
    m_shutdown_list.addFirst(dlg_conf.registerDialogItem("userphoto",itemfact));

  } // end initialize

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentShutdown
   *--------------------------------------------------------------------------------
   */

  public void shutdown()
  {
    while (m_shutdown_list.size()>0)
    { // shut down all components on the shutdown list
      ComponentShutdown cs = (ComponentShutdown)(m_shutdown_list.removeFirst());
      cs.shutdown();

    } // end while

    m_buttonprov = null;
    m_cbprov = null;

  } // end shutdown

  /*--------------------------------------------------------------------------------
   * Implementations from interface DialogPLAF
   *--------------------------------------------------------------------------------
   */

  public Object getButtonRendering(String button_name)
  {
    return m_buttonprov.getButtonRenderObject(ButtonType.INPUT,button_name);

  } // end getButtonRendering

  public boolean isButtonClicked(Request r, String button_name)
  {
    RequestHelper rh = new RequestHelper(r);
    return rh.isImageButtonClicked(button_name);

  } // end isButtonClicked

  public Object getContentHeader(String title, String subtitle)
  {
    try
    { // get and return the header block
      ContentBlock rc = m_cbprov.getContentBlock("content.header");
      rc.setContentParameter("title",title);
      if (subtitle!=null)
	rc.setContentParameter("subtitle",subtitle);
      return rc;

    } // end try
    catch (DatabaseException de)
    { // return a RenderingExceptionWrapper instead
      RenderingException re = new RenderingException(VeniceDialogManager.class,"VeniceDialogMessages",
						     "cheader.fail",de);
      re.setParameter(0,de.getMessage());
      return new RenderingExceptionWrapper(re);

    } // end catch

  } // end getContentHeader

} // end class VeniceDialogManager
