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

import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

class HeaderItem implements DialogField
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_name;                 // name of header
  private String m_caption;              // caption to display
  private String m_text;                 // text to display on right
  private boolean m_default_enabled;     // enabled or not?
  private boolean m_enabled;             // enabled or not?

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

  HeaderItem(Element elt) throws DialogException
  {
    XMLLoader loader = XMLLoader.get();
    try
    { // get the elements
      m_name = loader.getAttribute(elt,"name");
      m_caption = loader.getAttribute(elt,"caption");
      DOMElementHelper h = new DOMElementHelper(elt);
      m_text = h.getElementText();
      if (m_text!=null)
	m_text = m_text.trim();
      m_default_enabled = loader.getAttributeBoolean(elt,"enabled",true);

    } // end try
    catch (XMLLoadException e)
    { // translate XML loader exception
      throw new DialogException(e);

    } // end catch

    m_enabled = m_default_enabled;

  } // end constructor

  protected HeaderItem(HeaderItem other)
  {
    m_name = other.m_name;
    m_caption = other.m_caption;
    m_text = other.m_text;
    m_default_enabled = other.m_default_enabled;
    m_enabled = m_default_enabled;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface NamedObject
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return m_name;

  } // end getName

  /*--------------------------------------------------------------------------------
   * Implementations from interface DialogItem
   *--------------------------------------------------------------------------------
   */

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

  } // end clone

  public void render(TextRenderControl control, Map render_params) throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    wr.write("<tr valign=\"middle\">\n<td align=\"right\" class=\"content\"><span style=\"font-weight: bold;");
    if (!m_enabled)
      wr.write(" color: silver;");
    wr.write("\">" + StringUtils.encodeHTML(StringUtils.replaceAllVariables(m_caption,render_params))
	     + ":</span></td>\n<td align=\"left\" class=\"content\">");
    if (m_text==null)
      wr.write("&nbsp;");
    else
    { // write out the text on the right hand side
      if (!m_enabled)
	wr.write("<span style=\"color: silver;\">");
      wr.write(StringUtils.encodeHTML(StringUtils.replaceAllVariables(m_text,render_params)));
      if (!m_enabled)
	wr.write("</span>");

    } // end else

    wr.write("</td>\n</tr>\n");

  } // end render

  public boolean isEnabled()
  {
    return m_enabled;

  } // end isEnabled

  public void setEnabled(boolean flag)
  {
    m_enabled = flag;

  } // end setEnabled

  /*--------------------------------------------------------------------------------
   * Implementations from interface DialogField
   *--------------------------------------------------------------------------------
   */

  public Object getValue()
  {
    return null;

  } // end getValue

  public boolean containsValue()
  {
    return false;

  } // end containsValue

  public void setValue(Object obj)
  { // do nothing
  } // end setValue

  public void setValueFrom(Request r)
  { // do nothing
  } // end setValueFrom

  public void validate(Request r)
  { // do nothing
  } // end validate

  public void reset()
  { // do nothing
  } // end reset

  public int getFlags()
  {
    return 0;

  } // end getFlags

} // end class HeaderItem
