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

import java.util.*;
import java.io.Writer;
import java.io.IOException;
import com.silverwrist.util.StringUtil;

public class ContentMenuPanel implements Cloneable, ContentRender, ColorSelectors
{
  /*--------------------------------------------------------------------------------
   * Internal class for storing content items
   *--------------------------------------------------------------------------------
   */

  class ContentMenuPanelItem
  {
    private String title;
    private String url;

    public ContentMenuPanelItem(String title, String url)
    {
      this.title = title;
      this.url = url;

    } // end constructor

    public void renderHere(Writer out, RenderData rdat, Hashtable params) throws IOException
    {
      StringBuffer buf = new StringBuffer(url);
      Enumeration enum = params.keys();
      while (enum.hasMoreElements())
      { // do the parameter replacement
	String curkey = (String)(enum.nextElement());
	String rparam = "$" + curkey;
	int p = buf.toString().indexOf(rparam);
	while (p>=0)
	{ // replace the parameter and look for another instance
	  buf.replace(p,p+rparam.length(),(String)(params.get(curkey)));
	  p = buf.toString().indexOf(rparam);
	  
	} // end while

      } // end while

      out.write("<A HREF=\"" + rdat.getEncodedServletPath(buf.toString()) + "\">");
      out.write(StringUtil.encodeHTML(title) + "</A>");

    } // end renderHere

  } // end class ContentMenuPanelItem

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

  private String title;                   // menu title
  private String subtitle;                // menu subtitle
  private Vector items;                   // list of menu items
  private Hashtable params;               // set of parameters

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  public ContentMenuPanel(String title, String subtitle)
  {
    this.title = title;
    this.subtitle = subtitle;
    this.items = new Vector();
    this.params = new Hashtable();

  } // end constructor

  protected ContentMenuPanel(ContentMenuPanel other)
  {
    this.title = other.title;
    this.subtitle = other.subtitle;
    this.items = other.items;
    this.params = (Hashtable)(other.params.clone());

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface VeniceContent
   *--------------------------------------------------------------------------------
   */

  public String getPageTitle(RenderData rdat)
  {
    return title;

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentRender
   *--------------------------------------------------------------------------------
   */

  public void renderHere(Writer out, RenderData rdat) throws IOException
  {
    rdat.writeContentHeader(out,title,subtitle);

    out.write("<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=2\n");

    Iterator it = items.iterator();
    while (it.hasNext())
    { // render all the menu items
      out.write("<TR VALIGN=MIDDLE>\n<TD ALIGN=CENTER WIDTH=14><IMG SRC=\""
		+ rdat.getFullImagePath("purple-ball.gif")
		+ "\" ALT=\"*\" WIDTH=14 HEIGHT=14 BORDER=0></TD>\n<TD ALIGN=LEFT>");
      out.write(rdat.getStdFontTag(CONTENT_FOREGROUND,2));
      ContentMenuPanelItem item = (ContentMenuPanelItem)(it.next());
      item.renderHere(out,rdat,params);
      out.write("</FONT></TD>\n</TR>\n");

    } // end while

    out.write("</TABLE>\n");

  } // end renderHere

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

  public void addChoice(String title, String url)
  {
    items.add(new ContentMenuPanelItem(title,url));

  } // end addChoice

  public void setParameter(String name, String value)
  {
    params.put(name,value);

  } // end setParameter

  public void setTitle(String s)
  {
    title = s;

  } // end setTitle

  public void setSubtitle(String s)
  {
    subtitle = s;

  } // end setSubtitle

  public Object clone()
  {
    return new ContentMenuPanel(this);

  } // end clone

} // end class ContentMenuPanel
