package Sight.Generators;

import Sight.Structures.*;
import Sight.dds.*;
import Sight.tools.xForm.*;
import Sight.tools.xForm.fControls.*;
import java.util.*;

/**
 * Sight 1.00
 * System of Internet robots for silicon cloning.
 * Copyright:  Copyright (c) 2001
 * Company:    Ulm university
 * @author Audrius Meskauskas
 * @version 1.0
 */

public class Form extends abstractForm {

  public Map radioButtons = new TreeMap();
  public TreeSet f = new TreeSet();

  public boolean addAll(Form controls)
   {
     {
      Iterator iter = controls.f.iterator();
      while (iter.hasNext()) {
        Add(iter.next());
      }
     }
     radioButtons.putAll(controls.radioButtons);
     return true;
   };

  public boolean addAll(Collection controls)
   { if (controls==null) return false;
     {
      Iterator iter = controls.iterator();
      while (iter.hasNext()) {
        Add(iter.next());
      }
     }
     return true;
   };

  public Iterator iterator() { return f.iterator(); };
  public boolean Add(Object c)
   { if (c==null) return false;
     Control k = (Control) c;
     if (k.type.equals("radio") && ! (k instanceof Select) )
      {
        Select radio = (Select) radioButtons.get(k.name);
        if (radio==null)
         {
           radio = new Select(k.type, k.name, k.value);
           radio.choices.add(k.value,k.value);
           radioButtons.put(radio.name, radio);
           radio.inDocPos = k.inDocPos;
           return f.add(radio);
         } else
         {
           radio.choices.add(k.value, k.value);
           k.name = k.name+"*"+k.value;
           return true;
         };
      };
     return f.add(k);
    };

  public void Add(String type, Sight.Structures.diString settings, int pos)
   { Control c = new Control(type, settings);
     c.inDocPos = pos;
     Add(c);
   };

  public void setFromPara(AMap map)
   {
    for (int i = 0; i < map.length; i++) {
      diString d = map.at(i);
      feInput e = new feInput();
      e.setEntry(d.name, d.r);
      Add(d);
    }
   };

  public String get(String name)
   { Control g = getControl(name);
     if (g!=null)
      return g.value;
     else return "";
   };

  public Control getControl(String name)
   {
     {
      Control c;
      Iterator iter = f.iterator();
      while (iter.hasNext()) {
        c = (Control) iter.next();
        if (c.name.equals(name))
         return c;
      }
     }
     return null;
   };

  public void set(String name, String value)
   {
     Control aControl = getControl(name);
     if (aControl!=null)
      aControl.value = value;
      else
      Add(new Control("Sight",name,value));
   };

  public void Add(String type, String name, String value)
   {
      Add(new Control(type, name, value));
   };

  public Sight.Structures.AMap getArgs()
   {
     Sight.Structures.AMap args = new Sight.Structures.AMap();
      {
       Iterator iter = f.iterator();
       Control x;
       while (iter.hasNext()) {
        x= (Control)iter.next();
        if (x.isIncluded)
         {
           if (x.name==null)
            x.name=x.name;
           args.put(x.name, x.value);
          }
        }
      }
      return args;
   };

  /** Get field description records for type definition section. */
  public dField [] getFieldDefinitions()
   {
     Sight.Structures.AMap args = getArgs();
     dField [] rt = new dField[args.length];
     int p = 0;
     Iterator iter = f.iterator();
     Control x;
       while (iter.hasNext()) {
        x= (Control)iter.next();
        if (x.isMandatory)
          x.def = "";
         else
          x.def = x.value;
        if (x.isIncluded)
        if (x.isParameter)
         {
           rt[p++] = x;
         };
     }
     dField [] rtt = new dField[p];
     System.arraycopy(rt, 0, rtt, 0, p);
     return rtt;
   };

}