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

import java.util.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.velocity.VelocityRenderable;
import com.silverwrist.venice.frame.FramedContent;
import com.silverwrist.venice.iface.Sidebox;

public class SideboxView implements FramedContent, VelocityRenderable
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  public static final int DEFAULT_WIDTH = 200;
  public static final int DEFAULT_MARGIN = 5;

  private static final List MY_PARAMETERS;

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

  private Object m_content;                     // page content (to left of sideboxes)
  private List m_sideboxes;                     // list of sideboxes
  private String m_configure_url = null;        // configure URL
  private String m_configure_url_type = null;   // configure URL type
  private int m_width = DEFAULT_WIDTH;          // width of sideboxes
  private int m_margin = DEFAULT_MARGIN;        // sidebox margins
  private String m_local_menu_selector = null;  // local menu selector
  private String m_local_title = null;          // local title
  private String m_local_qid = null;            // local QID

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

  public SideboxView(Object content)
  {
    m_content = content;
    m_sideboxes = new ArrayList();

  } // end constructor

  public SideboxView(Object content, Collection sideboxes)
  {
    m_content = content;
    m_sideboxes = new ArrayList();
    addSideboxes(sideboxes);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface FramedContent
   *--------------------------------------------------------------------------------
   */

  public String getMenuSelector()
  {
    if (m_local_menu_selector!=null)
      return m_local_menu_selector;
    if (m_content instanceof FramedContent)
      return ((FramedContent)m_content).getMenuSelector();
    else
      return null;

  } // end getMenuSelector

  public String getPageTitle()
  {
    if (m_local_title!=null)
      return m_local_title;
    if (m_content instanceof FramedContent)
      return ((FramedContent)m_content).getPageTitle();
    else
      return "SideboxView";

  } // end getPageTitle

  public String getPageQID()
  {
    if (m_local_qid!=null)
      return m_local_qid;
    if (m_content instanceof FramedContent)
      return ((FramedContent)m_content).getPageQID();
    else
      return null;

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface VelocityParamSupplier
   *--------------------------------------------------------------------------------
   */

  public Object getParameter(String key)
  {
    if (key.equals("content"))
      return m_content;
    else if (key.equals("sideboxes"))
      return Collections.unmodifiableList(m_sideboxes);
    else if (key.equals("config_URL"))
      return m_configure_url;
    else if (key.equals("config_type"))
      return m_configure_url_type;
    else if (key.equals("width"))
      return new Integer(m_width);
    else if (key.equals("margins"))
      return new Integer(m_margin);
    else
      return null;

  } // end getParameter

  public Collection getParameterNames()
  {
    return MY_PARAMETERS;

  } // end getParameterNames

  /*--------------------------------------------------------------------------------
   * Implementations from interface VelocityRenderable
   *--------------------------------------------------------------------------------
   */

  public String getMimeType()
  {
    return "text/html";

  } // end getMimeType

  public String getTemplateName()
  {
    return "common/sideboxview.vm";

  } // end getTemplateName

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

  public void setMenuSelector(String s)
  {
    m_local_menu_selector = s;

  } // end setMenuSelector

  public void setPageTitle(String s)
  {
    m_local_title = s;

  } // end setPageTitle

  public void setPageQID(String s)
  {
    m_local_qid = s;

  } // end setPageQID

  public Object getContent()
  {
    return m_content;

  } // end getContent

  public void setContent(Object obj)
  {
    m_content = obj;

  } // end setContent

  public void addSidebox(Sidebox box)
  {
    m_sideboxes.add(box);

  } // end addSidebox

  public void addSideboxes(Collection coll)
  {
    Iterator it = coll.iterator();
    while (it.hasNext())
    { // add only Sidebox instances to the list
      Object obj = it.next();
      if (obj instanceof Sidebox)
	m_sideboxes.add(obj);

    } // end while

  } // end addSideboxes

  public void setSideboxes(Collection coll)
  {
    m_sideboxes.clear();
    addSideboxes(coll);

  } // end setSideboxes

  public String getConfigureURL()
  {
    return m_configure_url;

  } // end getConfigureURL

  public void setConfigureURL(String s)
  {
    m_configure_url = s;

  } // end setConfigureURL

  public String getConfigureURLType()
  {
    return m_configure_url_type;

  } // end getConfigureURLType

  public void setConfigureURLType(String s)
  {
    m_configure_url_type = s;

  } // end setConfigureURLType

  public int getWidth()
  {
    return m_width;

  } // end getWidth

  public void setWidth(int w)
  {
    m_width = w;

  } // end setWidth

  public int getMargin()
  {
    return m_margin;

  } // end getMargin

  public void setMargin(int m)
  {
    m_margin = m;

  } // end setMargin

  /*--------------------------------------------------------------------------------
   * Static initializer
   *--------------------------------------------------------------------------------
   */

  static
  {
    ArrayList tmp = new ArrayList();
    tmp.add("content");
    tmp.add("sideboxes");
    tmp.add("config_URL");
    tmp.add("config_type");
    tmp.add("width");
    tmp.add("margins");
    tmp.trimToSize();
    MY_PARAMETERS = Collections.unmodifiableList(tmp);

  } // end static initializer

} // end class SideboxView
