/********************************************************************
 *  Oracle JDeveloper
 *  Copyright (c) 1997, 1999, Oracle Corporation. All rights reserved.
 ********************************************************************/

package oracle.jdeveloper.html;

import oracle.jdeveloper.html.*;
import oracle.jbo.*;
import java.util.*;

/**
 * This class represents a Static PickList. It's data is provided by two string arrays, one for the labels and another for
 * the prompts . It supports various renderings:
 * combobox, listbox,radio group and checbox group.
 *
 * @author  Juan Oropeza
 * @version PUBLIC
 *
 **/
public class StaticPickList extends HTMLFieldRendererImpl
{
  public static final int TYPE_COMBOBOX = 0;
  public static final int TYPE_LISTBOX = 1;
  public static final int TYPE_CHECKBOX_GROUP = 2;
  public static final int TYPE_RADIO_GROUP = 3;

  protected   int nType = TYPE_COMBOBOX;

  protected   String    sQuery = "";
  protected   HTMLForm  aForm;
  protected   Hashtable values = new Hashtable();
  protected   Hashtable allvalues = new Hashtable();
  protected   String    [] aLabels = new String[0];
  protected   String    [] aValues = new String[0];
  /**
  *	Constructs object. Default rendering is combo box.
  */
  public StaticPickList()
  {
  }

  /**
  *	Tells StaticPickList to render as a combo box.
  */
  public void useComboBox()
  {
    nType = TYPE_COMBOBOX;
  }

  /**
  *	Tells StaticPickList to render as listbox
  */
  public void useListBox()
  {
    nType = TYPE_LISTBOX;
  }

  /**
  *	Tells StaticPickList ot render as radio button group
  */
  public void useRadioGroup()
  {
    nType = TYPE_RADIO_GROUP;
  }

  /**
  *	Tells StaticPickList ot render as checkbox group.
  */
  public void useCheckBoxGroup()
  {
    nType = TYPE_CHECKBOX_GROUP;
  }

  /**
  *     Configures the source arrays for the prompts and values of the static picklist
  */
  public void setDataSource(String [] labels , String [] values)
  {
    aLabels = labels;
    aValues = values;
  }
  
  protected void prepareForMultiValueListGeneration()
  {
      String sValue;

      allvalues.clear();
      values.clear();
      
      for(int i = 0; i < aValues.length; i++)
      {
        allvalues.put(aValues[i], "");
      }

      // split up the field value
      StringTokenizer tokens = new StringTokenizer(this.getValue() , ",");

      while(tokens.hasMoreElements())
      {
        values.put(tokens.nextToken(),"");
      }
  }

  protected void generateComboBox()
  {
    try
    {
      HTMLSelect aSelect = new HTMLSelect(this.getFieldName());
      String sLabel;
      String sValue;

      for(int i = 0 ; i < aLabels.length; i++)
      {
         sLabel = aLabels[i];
         sValue = aValues[i];

        if(sValue.equals(this.getValue()))
        {
          aSelect.addSelectedOption(sLabel, sValue);
        }
        else
        {
          aSelect.addOption(sLabel , sValue);
        }
      }

      aForm.addSelectField(this.getPromptText(), aSelect);
    }
    catch(Exception ex)
    {
      throw new RuntimeException(ex.getMessage());
    }
  }

  protected void generateListBox()
  {
    try
    {
      HTMLSelect aSelect = new HTMLSelect(this.getFieldName());
      String sLabel;
      String sValue;

      prepareForMultiValueListGeneration();

      for(int i = 0 ; i < aLabels.length; i++)
      {
        sLabel = aLabels[i];
        sValue = aValues[i];

        if(values.get(sValue) != null)
        {
          aSelect.addSelectedOption(sLabel, sValue);
        }
        else
        {
          aSelect.addOption(sLabel , sValue);
        }
      }

      aSelect.setMultiple();
      
      aForm.addSelectField(this.getPromptText(), aSelect);
    }
    catch(Exception ex)
    {
      throw new RuntimeException(ex.getMessage());
    }
  }

  protected void generateCheckBoxGroup()
  {
    try
    {
      String sLabel;
      String sValue;
      String sPromptText = getPromptText();
      
      prepareForMultiValueListGeneration();

      for(int i = 0 ; i < aLabels.length; i++)
      {
        sLabel = aLabels[i];
        sValue = aValues[i];

        if(values.get(sValue) != null)
        {
          aForm.addCheckBoxField(sPromptText, sLabel, getFieldName(), sValue, true);
        }
        else
        {
          aForm.addCheckBoxField(sPromptText, sLabel, getFieldName(), sValue, false);
        }
        // clear the prompt text so it only gets generated once.
        sPromptText = null;
      }
    }
    catch(Exception ex)
    {
      throw new RuntimeException(ex.getMessage());
    }
  }
  
  protected void generateRadioGroup()
  {
    try
    {
      String sLabel;
      String sValue;
      String sPromptText = getPromptText();
      
     for(int i = 0 ; i < aLabels.length; i++)
     {
        sLabel = aLabels[i];
        sValue = aValues[i];

        if(sValue.equals(this.getValue()))
        {
          aForm.addRadioButtonField(sPromptText, sLabel, getFieldName(), sValue, true);
        }
        else
        {
          aForm.addRadioButtonField(sPromptText, sLabel, getFieldName(), sValue, false);
        }
        // clear the prompt text so it only gets generated once.
        sPromptText = null;
      }
    }
    catch(Exception ex)
    {
      throw new RuntimeException(ex.getMessage());
    }
  }
  
}

