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

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

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

  private static final List MY_PARAMETERS;

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

  private ContentBlock m_header;                 // header to display
  private String m_title = null;                 // title for box
  private String m_subtitle = null;              // subtitle for box
  private Object m_content = null;               // content for box
  private ButtonProvider m_buttonprov;           // button provider
  private ArrayList m_buttons = new ArrayList(); // list of buttons to display

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

  public StdTextMessage(ServiceProvider services) throws DatabaseException
  {
    // Get the header content block.
    ObjectProvider oprov = (ObjectProvider)(services.queryService(ObjectProvider.class));
    ServiceProvider sp = (ServiceProvider)(oprov.getObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,
							   "venice-content"));
    ContentBlockProvider cblkp = (ContentBlockProvider)(sp.queryService(ContentBlockProvider.class));
    m_header = cblkp.getContentBlock("content.header");

    // Get a reference to the button provider.
    sp = (ServiceProvider)(oprov.getObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"venice-buttons"));
    m_buttonprov = (ButtonProvider)(sp.queryService(ButtonProvider.class));

  } // end constructor

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

  public String getMenuSelector()
  {
    return null;

  } // end getMenuSelector

  public String getPageTitle()
  {
    if (m_subtitle==null)
      return m_title;
    else
      return m_title + " - " + m_subtitle;

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

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

  public Object getParameter(String key)
  {
    if (key.equals("header"))
      return m_header;
    if (key.equals("content"))
      return m_content;
    if (key.equals("buttons"))
      return Collections.unmodifiableList(m_buttons);
    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/stdtextmessage.vm";

  } // end getTemplateName

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

  public String getTitle()
  {
    return m_title;

  } // end getTitle

  public void setTitle(String title)
  {
    m_header.setContentParameter("title",title);
    m_title = title;

  } // end setTitle

  public String getSubtitle()
  {
    return m_subtitle;

  } // end getSubtitle

  public void setSubtitle(String subtitle)
  {
    m_header.setContentParameter("subtitle",subtitle);
    m_subtitle = subtitle;

  } // end setSubtitle

  public Object getContent()
  {
    return m_content;

  } // end getContent

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

  } // end setContent

  public void addButton(String linktype, String link, String button)
  {
    HashMap map = new HashMap();
    map.put("linktype",linktype);
    map.put("link",link);
    map.put("image",m_buttonprov.getButtonRenderObject(ButtonType.IMAGE,button));
    m_buttons.add(Collections.unmodifiableMap(map));

  } // end addButton

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

  static
  {
    ArrayList tmp = new ArrayList();
    tmp.add("header");
    tmp.add("content");
    tmp.add("buttons");
    tmp.trimToSize();
    MY_PARAMETERS = Collections.unmodifiableList(tmp);

  } // end static initializer

} // end class StdTextMessage
