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

import java.io.IOException;
import java.util.*;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.util.XMLLoader;

public class StaticPickListField extends PickListField
{
  /*--------------------------------------------------------------------------------
   * Internal class holding choices
   *--------------------------------------------------------------------------------
   */

  static class PickListChoice
  {
    private String name;
    private String value;

    PickListChoice(String name, String value)
    {
      this.name = name;
      this.value = value;

    } // end constructor

    final String getName()
    {
      return name;

    } // end getName

    final String getValue()
    {
      return value;

    } // end getValue

  } // end class PickListChoice

  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  public static final String TAGNAME = "list";

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

  public StaticPickListField(String name, String caption, String caption2, boolean required, String clist)
  {
    super(name,caption,caption2,required,createChoiceListFromString(clist));

  } // end constructor

  public StaticPickListField(Element elt) throws ConfigException
  {
    super(elt,createChoiceListFromXML(elt));

  } // end constructor

  public StaticPickListField(PickListField other)
  {
    super(other);

  } // end constructor

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

  private static final List createChoiceListFromString(String s)
  {
    char separator = s.charAt(0);
    String work = s.substring(1);
    ArrayList tmp = new ArrayList();
    while (work!=null)
    { // split up the "choice list" string into name/value pairs
      int p = work.indexOf(separator);
      if (p<0)
	break;
      String n1 = work.substring(0,p);
      work = work.substring(p+1);
      p = work.indexOf(separator);
      String v1;
      if (p<0)
      { // take the rest of the string
	v1 = work;
	work = null;

      } // end if
      else
      { // chop the string off the end
	v1 = work.substring(0,p);
	work = work.substring(p+1);
	if (work.length()==0)
	  work = null;

      } // end else

      tmp.add(new PickListChoice(n1,v1));

    } // end while

    tmp.trimToSize();
    if (tmp.isEmpty())
      return Collections.EMPTY_LIST;
    else
      return Collections.unmodifiableList(tmp);

  } // end createChoiceListFromString

  private static final List createChoiceListFromXML(Element elt) throws ConfigException
  {
    XMLLoader loader = XMLLoader.get();
    NodeList nl = elt.getChildNodes();
    ArrayList tmp = new ArrayList();
    for (int i=0; i<nl.getLength(); i++)
    { // look for associated elements
      Node n = nl.item(i);
      if (n.getNodeType()==Node.ELEMENT_NODE)
      { // make sure we have a <choice/> element here
	loader.configVerifyNodeName(n,"choice",elt);
	String n1 = loader.configGetAttribute((Element)n,"id");
	String v1 = loader.configGetText((Element)n);
	tmp.add(new PickListChoice(n1,v1));

      } // end if
      // else just ignore that

    } // end for

    tmp.trimToSize();
    if (tmp.isEmpty())
      return Collections.EMPTY_LIST;
    else
      return Collections.unmodifiableList(tmp);

  } // end createChoiceListFromXML

  /*--------------------------------------------------------------------------------
   * Overrides from class PickListField
   *--------------------------------------------------------------------------------
   */

  protected String getChoiceName(Object o)
  {
    return ((PickListChoice)o).getName();

  } // end getChoiceName

  protected String getChoiceValue(Object o)
  {
    return ((PickListChoice)o).getValue();

  } // end getChoiceValue

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

  public DialogField duplicate()
  {
    return new StaticPickListField(this);

  } // end duplicate

} // end class StaticPickListField
