/*
 * 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 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.*;
import com.silverwrist.dynamo.util.*;

class HiddenField implements DialogHiddenItem
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_name;
  private String m_default_value;
  private String m_value;

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

  HiddenField(Element elt) throws DialogException
  {
    XMLLoader loader = XMLLoader.get();
    try
    { // get field parameters from XML
      m_name = loader.getAttribute(elt,"name");
      m_default_value = loader.getAttribute(elt,"value","");

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

    } // end catch

    m_value = m_default_value;

  } // end class HiddenField

  protected HiddenField(HiddenField other)
  {
    m_name = other.m_name;
    m_default_value = other.m_default_value;
    m_value = m_default_value;

  } // end constructor

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

  public String getName()
  {
    return m_name;

  } // end getName

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

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

  } // end clone

  public void render(TextRenderControl control, Map render_params) throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    wr.write("<input type=\"hidden\" name=\"" + m_name + "\" value=\""
	     + StringUtils.replaceAllVariables(m_value,render_params) + "\" />\n");

  } // end render

  public boolean isEnabled()
  {
    return true;

  } // end isEnabled

  public void setEnabled(boolean flag)
  { // do nothing
  } // end setEnabled

  /*--------------------------------------------------------------------------------
   * Implementations from interface DialogHiddenItem
   *--------------------------------------------------------------------------------
   */

  public Object getValue()
  {
    return m_value;

  } // end getValue

  public boolean containsValue()
  {
    return StringUtils.isNotEmpty(m_value);

  } // end containsValue

  public void setValue(Object obj)
  {
    m_value = ((obj==null) ? m_default_value : obj.toString());

  } // end setValue

  public void setValueFrom(Request r)
  {
    RequestHelper rh = new RequestHelper(r);
    m_value = rh.getParameterString(m_name);

  } // end setValueFrom

  public void reset()
  {
    m_value = m_default_value;

  } // end reset

} // end class HiddenField
