/*
 * 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.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.venice.frame.FramedContent;

/**
 * A content object that implements an error message box.  The error message consists of a title, a message,
 * an optional exception (the stack trace of which is rendered within HTML comments), and an optional "back"
 * link.  It is rendered inside the standard Venice frame.
 * <P>Most scripting code will use the library function
 * {@link com.silverwrist.venice.script.LibraryVenice#stdErrorBox(com.silverwrist.dynamo.iface.Request,java.lang.String,java.lang.String) vlib.stdErrorBox()}
 * to generate one of these, rather than constructing an <CODE>ErrorBox</CODE> instance directly.
 * <P>The <CODE>ErrorBox</CODE> is rendered by
 * {@link com.silverwrist.venice.content.StandardContentSupplier StandardContentSupplier}.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class ErrorBox implements FramedContent
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_title;             // error box title
  private String m_message;           // error box message
  private Throwable m_except;         // exception to be dealt with
  private String m_back_type;         // back link type
  private String m_back;              // back link URL

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

  /**
   * Constructs a new <CODE>ErrorBox</CODE>.
   *
   * @param title Title for the error message.  If this is <CODE>null</CODE>, a default title will be used.
   * @param message The error message text itself.
   * @param back_type The URL rewriter type of the back link.  If this is <CODE>null</CODE>, it is assumed to be
   *                  "SERVLET".
   * @param back The URL to use for a back link.
   */
  public ErrorBox(String title, String message, String back_type, String back)
  {
    setTitle(title);
    m_message = message;
    m_except = null;
    m_back_type = ((back_type==null) ? "SERVLET" : back_type);
    m_back = back;

  } // end constructor

  /**
   * Constructs a new <CODE>ErrorBox</CODE>.
   *
   * @param title Title for the error message.  If this is <CODE>null</CODE>, a default title will be used.
   * @param message The error message text itself.
   * @param back The URL to use for a back link, assumed to be a "SERVLET" URL.
   */
  public ErrorBox(String title, String message, String back)
  {
    this(title,message,"SERVLET",back);

  } // end constructor

  /**
   * Constructs a new <CODE>ErrorBox</CODE>.
   *
   * @param title Title for the error message.  If this is <CODE>null</CODE>, a default title will be used.
   * @param message The error message text itself.
   */
  public ErrorBox(String title, String message)
  {
    setTitle(title);
    m_message = message;
    m_except = null;
    m_back_type = null;
    m_back = null;

  } // end constructor

  /**
   * Constructs a new <CODE>ErrorBox</CODE>.
   *
   * @param title Title for the error message.  If this is <CODE>null</CODE>, a default title will be used.
   * @param t The <CODE>Throwable</CODE> that caused this error; its stack trace will be rendered in HTML comments
   *          to the output.
   * @param back_type The URL rewriter type of the back link.  If this is <CODE>null</CODE>, it is assumed to be
   *                  "SERVLET".
   * @param back The URL to use for a back link.
   */
  public ErrorBox(String title, Throwable t, String back_type, String back)
  {
    this(title,t.getMessage(),t,back_type,back);

  } // end constructor

  /**
   * Constructs a new <CODE>ErrorBox</CODE>.
   *
   * @param title Title for the error message.  If this is <CODE>null</CODE>, a default title will be used.
   * @param t The <CODE>Throwable</CODE> that caused this error; its stack trace will be rendered in HTML comments
   *          to the output.
   * @param back The URL to use for a back link, assumed to be a "SERVLET" URL.
   */
  public ErrorBox(String title, Throwable t, String back)
  {
    this(title,t.getMessage(),t,"SERVLET",back);

  } // end constructor

  /**
   * Constructs a new <CODE>ErrorBox</CODE>.
   *
   * @param title Title for the error message.  If this is <CODE>null</CODE>, a default title will be used.
   * @param t The <CODE>Throwable</CODE> that caused this error; its stack trace will be rendered in HTML comments
   *          to the output.
   */
  public ErrorBox(String title, Throwable t)
  {
    this(title,t.getMessage(),t);

  } // end constructor

  /**
   * Constructs a new <CODE>ErrorBox</CODE>.
   *
   * @param title Title for the error message.  If this is <CODE>null</CODE>, a default title will be used.
   * @param message The error message text itself.
   * @param t The <CODE>Throwable</CODE> that caused this error; its stack trace will be rendered in HTML comments
   *          to the output.
   * @param back_type The URL rewriter type of the back link.  If this is <CODE>null</CODE>, it is assumed to be
   *                  "SERVLET".
   * @param back The URL to use for a back link.
   */
  public ErrorBox(String title, String message, Throwable t, String back_type, String back)
  {
    setTitle(title);
    m_message = message;
    m_except = t;
    m_back_type = ((back_type==null) ? "SERVLET" : back_type);
    m_back = back;

  } // end constructor

  /**
   * Constructs a new <CODE>ErrorBox</CODE>.
   *
   * @param title Title for the error message.  If this is <CODE>null</CODE>, a default title will be used.
   * @param message The error message text itself.
   * @param t The <CODE>Throwable</CODE> that caused this error; its stack trace will be rendered in HTML comments
   *          to the output.
   * @param back The URL to use for a back link, assumed to be a "SERVLET" URL.
   */
  public ErrorBox(String title, String message, Throwable t, String back)
  {
    this(title,message,t,"SERVLET",back);

  } // end constructor

  /**
   * Constructs a new <CODE>ErrorBox</CODE>.
   *
   * @param title Title for the error message.  If this is <CODE>null</CODE>, a default title will be used.
   * @param message The error message text itself.
   * @param t The <CODE>Throwable</CODE> that caused this error; its stack trace will be rendered in HTML comments
   *          to the output.
   */
  public ErrorBox(String title, String message, Throwable t)
  {
    setTitle(title);
    m_message = message;
    m_except = t;
    m_back_type = null;
    m_back = null;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Sets the title of the <CODE>ErrorBox</CODE>.
   *
   * @param title The title to use.  If this is <CODE>null/CODE>, a default title will be used.
   */
  private final void setTitle(String title)
  {
    if (title==null)
    { // set the title to the default
      ResourceBundle b = ResourceBundle.getBundle("com.silverwrist.venice.content.ContentMessages",
						  Locale.getDefault());
      m_title = b.getString("default.errorBox.title");

    } // end if
    else  // just use the one passed in
      m_title = title;

  } // end setTitle

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

  /**
   * Returns the desired menu selector for this page.  This value is <CODE>null</CODE>, which means the current
   * menu selector is not changed.
   *
   * @return The desired menu selector (<CODE>null</CODE>).
   */
  public String getMenuSelector()
  {
    return null;

  } // end getMenuSelector

  /**
   * Returns the desired title for the page.  This title is concatenated with the <EM>site title</EM> (set in
   * global properties) to form the actual page title that gets sent to the browser.
   *
   * @return The desired page title.
   */
  public String getPageTitle()
  {
    return m_title;

  } // end getPageTitle

  /**
   * Returns the desired quick ID for the page.  This value is <CODE>null</CODE>, which means the page quick ID
   * is not used.
   *
   * @return The desired page quick ID (<CODE>null</CODE>).
   */
  public String getPageQID()
  {
    return null;

  } // end getPageQID

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

  /**
   * Returns the title of the error message box.
   *
   * @return The title of the error message box.
   */
  public String getTitle()
  {
    return m_title;

  } // end getTitle

  /**
   * Returns the error message text.
   *
   * @return The error message text.
   */
  public String getMessage()
  {
    return m_message;

  } // end getMessage

  /**
   * Returns the <CODE>Throwable</CODE> wrapped by this <CODE>ErrorBox</CODE>, if any.
   *
   * @return The <CODE>Throwable</CODE> wrapped by this <CODE>ErrorBox</CODE>, or <CODE>null</CODE> if there is none.
   */
  public Throwable getException()
  {
    return m_except;

  } // end getException

  /**
   * Returns the link type of the "back" link for this error message, if any.
   *
   * @return The link type of the "back" link for this error message, or <CODE>null</CODE> if there is none.
   */
  public String getBackType()
  {
    return m_back_type;

  } // end getBackType

  /**
   * Returns the link URL of the "back" link for this error message, if any.
   *
   * @return The link URL of the "back" link for this error message, or <CODE>null</CODE> if there is none.
   */
  public String getBackLink()
  {
    return m_back;

  } // end getBackLink

} // end class ErrorBox
