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

import java.io.IOException;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;

public class UtilMenuHeaderTag extends VeniceBodyTagSupport
{
  /*--------------------------------------------------------------------------------
   * Internal class for holding the menu links
   *--------------------------------------------------------------------------------
   */

  class MenuItem
  {
    private String href;
    private int type;
    private String text;

    MenuItem(String href, int type, String text)
    {
      this.href = href;
      this.type = type;
      this.text = text;

    } // end constructor

    final void write(JspWriter out, HTMLRendering html, boolean selected) throws IOException
    {
      if (selected)
	out.write("<B>");
      else
	out.write("<A HREF=\"" + html.formatURL(href,type) + "\">");
      out.write(text);
      if (selected)
	out.write("</B>");
      else
	out.write("</A>");

    } // end write

  } // end class MenuItem

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

  private String caption = null;
  private ArrayList items = new ArrayList();
  private MenuItem selected_item = null;

  /*--------------------------------------------------------------------------------
   * Overrides from class BodyTagSupport
   *--------------------------------------------------------------------------------
   */

  public int doStartTag() throws JspException
  {
    if (caption==null)
      throw new JspTagException("<util:menuheader/> caption= attribute not specified");
    return EVAL_BODY_BUFFERED;

  } // end doStartTag

  public int doAfterBody()
  {
    // just throw out the body content that's been collected
    return SKIP_BODY;

  } // end doAfterBody

  public int doEndTag() throws JspException
  {
    if (items.isEmpty())
      throw new JspTagException("<util:menuheader/> has no menu header items!");

    HTMLRendering html = (HTMLRendering)(getRequestOutput().queryService(HTMLRendering.class));

    try
    { // write out what we came here to accomplish
      JspWriter out = pageContext.getOut();
      out.write("<SPAN CLASS=\"chead1\">" + html.getFontTag(html.CONTENT_HEADER,"header") + "<B>"
		+ StringUtil.encodeHTML(caption) + "</B></FONT></SPAN>&nbsp;&nbsp;<SPAN CLASS=\"chead2\">"
		+ html.getFontTag(html.CONTENT_HEADER,"subhead"));
      boolean first = true;
      Iterator it = items.iterator();
      while (it.hasNext())
      { // extract and write each menu item
	MenuItem mi = (MenuItem)(it.next());
	if (first)
	{ // write the first item out differently
	  out.write("[");
	  first = false;

	} // end if
	else  // write the separator
	  out.write("|");
	out.write("&nbsp;");
	mi.write(out,html,(mi==selected_item));
	out.write("&nbsp;");

      } // end while

      // write the closure and finish up
      out.write("]</FONT></SPAN><HR ALIGN=LEFT SIZE=2 WIDTH=\"90%\" NOSHADE>\n");

    } // end try
    catch (IOException e)
    { // convert the I/O error into something the servlet engine can deal with
      throw new JspTagException("error writing content header - " + e.getMessage());

    } // end catch

    items.clear();
    selected_item = null;
    return EVAL_PAGE;

  } // end doEndTag

  public void release()
  {
    super.release();
    caption = null;

  } // end release

  /*--------------------------------------------------------------------------------
   * Attribute set functions
   *--------------------------------------------------------------------------------
   */

  public void setCaption(String s)
  {
    caption = s;

  } // end setCaption

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

  final void pushItem(String href, int type, String text, boolean selected) throws JspTagException
  {
    MenuItem mi = new MenuItem(href,type,text);
    items.add(mi);
    if (selected)
    { // this item is selected - push it on the list
      if (selected_item==null)
	selected_item = mi;
      else
	throw new JspTagException("<util:menuheader/> has two selected items");

    } // end if

  } // end pushItem

} // end class UtilMenuHeaderTag
