/*
 * 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) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.servlets.format.menus;

import java.io.Writer;
import java.io.IOException;
import java.util.*;
import org.apache.log4j.*;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.except.ConfigException;
import com.silverwrist.venice.servlets.format.ComponentRender;
import com.silverwrist.venice.servlets.format.RenderData;

public class LeftMenu implements ComponentRender
{
  /*--------------------------------------------------------------------------------
   * Internal class representing a header component
   *--------------------------------------------------------------------------------
   */

  static class Header implements ComponentRender
  {
    private TextItem item;

    Header(Element elt)
    {
      DOMElementHelper h = new DOMElementHelper(elt);
      item = new TextItem(h.getElementText());

    } // end constructor

    public void renderHere(Writer out, RenderData rdat) throws IOException
    {
      out.write("<B>");
      item.renderHere(out,rdat);
      out.write("</B><BR>\n");

    } // end renderHere

  } // end class Header

  /*--------------------------------------------------------------------------------
   * Internal class representing a separator component
   *--------------------------------------------------------------------------------
   */

  static class Separator implements ComponentRender
  {
    private ComponentRender cr;

    Separator()
    {
      this.cr = null;

    } // end constructor

    Separator(ComponentRender cr)
    {
      this.cr = cr;

    } // end constructor

    public void renderHere(Writer out, RenderData rdat) throws IOException
    {
      if (cr!=null)
	cr.renderHere(out,rdat);
      out.write("<BR>\n");

    } // end renderHere

  } // end class Separator

  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(LeftMenu.class);
  private static final Separator separator_singleton = new Separator(null);

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

  private String identifier;
  private ArrayList menu_items = new ArrayList();

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

  public LeftMenu(Element elt, String identifier) throws ConfigException
  {
    if (!(elt.getNodeName().equals("menudef")))
    { // just some shorts-checking here to make sure the element is OK
      logger.fatal("huh?!? this should have been a <menudef/> if it got here!");
      throw new ConfigException("not a <menudef/> element");

    } // end if

    NodeList items = elt.getChildNodes();
    for (int i=0; i<items.getLength(); i++)
    { // examine each of the child elements closely
      Node n = items.item(i);
      if (n.getNodeType()==Node.ELEMENT_NODE)
      { // we've found a child element - what type is it?
	if (n.getNodeName().equals("header"))
	  menu_items.add(new Header((Element)n));  // add a new header
	else if (n.getNodeName().equals("separator"))
	  menu_items.add(separator_singleton);  // all separators are exactly the same
	else if (n.getNodeName().equals("text"))
	{ // add a text item
	  DOMElementHelper h = new DOMElementHelper((Element)n);
	  menu_items.add(new Separator(new TextItem(h.getElementText())));

	} // end else if
	else if (n.getNodeName().equals("link"))
	  menu_items.add(new LinkItem((Element)n));
	else if (n.getNodeName().equals("image"))
	  menu_items.add(new Separator(new ImageItem((Element)n)));
	else
	{ // menu definition has an unknown item
	  logger.fatal("unknown element <" + n.getNodeName() + "/> inside <menudef/>");
	  throw new ConfigException("unknown element <" + n.getNodeName() + "/> inside <menudef/>",n);

	} // end else

      } // end if (an element node)

    } // end for (each child node)

    this.identifier = identifier;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentRender
   *--------------------------------------------------------------------------------
   */

  public void renderHere(Writer out, RenderData rdat) throws IOException
  {
    Iterator it = menu_items.iterator();
    while (it.hasNext())
    { // render each menu item in turn
      ComponentRender cr = (ComponentRender)(it.next());
      cr.renderHere(out,rdat);

    } // end while

  } // end renderHere

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public String getIdentifier()
  {
    return identifier;

  } // end getIdentifier

} // end class LeftMenu

