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

public abstract class PickListField extends BaseDialogField
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Object m_value;

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

  protected PickListField(Element elt) throws DialogException
  {
    super(false,elt);
    m_value = null;

  } // end constructor

  protected PickListField(PickListField other)
  {
    super(other);
    m_value = null;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Abstract operations
   *--------------------------------------------------------------------------------
   */

  protected abstract Iterator getChoices();

  protected abstract String getChoiceName(Object choice);

  protected abstract String getChoiceDisplay(Object choice);

  protected abstract Object getChoiceForName(String name);

  protected abstract boolean isValidChoice(Object choice);

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class BaseDialogField
   *--------------------------------------------------------------------------------
   */

  protected void renderField(TextRenderControl control, Map render_params)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    wr.write("<select name=\"" + getName() + "\" size=\"1\"");
    if (!isEnabled())
      wr.write(" disabled=\"disabled\"");
    wr.write(">\n");
    Iterator it = getChoices();
    while (it.hasNext())
    { // render each choice name and display value in turn
      Object ch = it.next();
      wr.write("<option value=\"" + getChoiceName(ch) + "\"");
      if (ch.equals(m_value))
	wr.write(" selected=\"selected\"");
      wr.write(">"
               + StringUtils.encodeHTML(StringUtils.replaceAllVariables(getChoiceDisplay(ch),render_params))
	       + "</option>\n");

    } // end while

    wr.write("</select>");

  } // end renderField

  protected void validateContents(Request r, Object data) throws ValidationException
  { // don't need to validate the contents on a pick list
  } // end validateContents

  public Object getValue()
  {
    return m_value;

  } // end getValue

  public boolean containsValue()
  {
    return (m_value!=null);

  } // end containsValue

  public void setValue(Object obj)
  {
    if ((obj==null) || isValidChoice(obj))
      m_value = obj;

  } // end setValue

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

  } // end setValueFrom

  public void reset()
  {
    m_value = null;

  } // return reset

} // end class PickListField
