import java.awt.*;
import java.io.*;
import java.util.*;
/**
This creates source for a java class that handles widget creation
and placement. Pass it a container object and a class name.
It will read all components and create source to create that
container.
@author Arthur Sulger, asulger@ibm.net
*/
public class BuildIt
   {  
   File outFile;
   public BuildIt(Screen scr, String className, String layoutConstruct)
      {
      Container c = (Container)scr;
      try{
         outFile = new File(className + ".java");
         if (outFile.exists())
            {
            System.out.println(className + ".java" + " exists");
            System.out.println("this does not overwrite files");
            return;
            }
         FileOutputStream os = new FileOutputStream(outFile);
         PrintStream ps = new PrintStream(os);
         ps.println("import java.awt.*;");
         ps.println("import java.io.*;");
         ps.println("public class " + className + "\n\t{");
         ps.println("\tpublic " + className + 
         "(Container c, Component[] comp)\n\t\t{");
         ps.println("\t\tc." + layoutConstruct);
         if (layoutConstruct.regionMatches(14, "GridBagLayout", 0, 13))
            ps.println
            ("\t\tGridBagConstraints ct = new GridBagConstraints();"); 

         for (int i = 0; i < c.countComponents(); i++)      
            {
            StringTokenizer tok = new StringTokenizer
            (c.getComponent(i).toString().substring(9),
            "[", true);
            String compType = (String)tok.nextElement();

            if (compType.equals("Scrollbar"))
				// scroll bar needs orientation constructor
				//    so may as well do other scrollbar stuff also:
					{
               Scrollbar scrollbar = (Scrollbar)c.getComponent(i);
            	ps.println("\t\tcomp[" + i + "] = new " + 
            	compType + "(" + scrollbar.getOrientation() + ");");
               ps.println("\t\tScrollbar scrollbar" + i + 
               " = (Scrollbar)comp[" + i + "];");
               ps.println("\t\tscrollbar" + i + ".setLineIncrement(" +
               scrollbar.getLineIncrement() + ");");
               ps.println("\t\tscrollbar" + i + ".setPageIncrement(" +
               scrollbar.getPageIncrement() + ");");
               ps.println("\t\tscrollbar" + i + ".setValue(" +
               scrollbar.getValue() + ");");
               }
				else
					{
            	ps.println("\t\tcomp[" + i + "] = new " + 
            	compType + "();");
					}

         	if (layoutConstruct.regionMatches(14, "GridBagLayout", 0, 13))
               printConstraints(c, c.getComponent(i), "comp["+i+"]", ps);
            else
               {
               ps.print("\t\tcomp[" + i + "].reshape(");
               ps.print(c.getComponent(i).location().x + ", ");
               ps.print(c.getComponent(i).location().y + ", ");
               ps.print(c.getComponent(i).size().width + ", ");
               ps.println(c.getComponent(i).size().height + ");");
               }
            ps.println("\t\tc.add(comp[" + i + "]);");
            int count = 0;
            if (compType.equals("Button"))
               {
               Button button = (Button)c.getComponent(i);
               ps.println("\t\tButton button" + i + 
               " = (Button)comp[" + i + "];");
               ps.println("\t\tbutton" + i + ".setLabel(\"" +
               button.getLabel() + "\");");
               }
            else
            if (compType.equals("Checkbox"))
               {
               Checkbox checkbox = (Checkbox)c.getComponent(i);
               ps.println("\t\tCheckbox checkbox" + i + 
               " = (Checkbox)comp[" + i + "];");
               ps.println("\t\tcheckbox" + i + ".setLabel(\"" +
               checkbox.getLabel() + "\");");
               }
            else
            if (compType.equals("Choice"))
               {
               Choice choice = (Choice)c.getComponent(i);
               count = choice.countItems();
               if (count > 0)
                  ps.println("\t\tChoice choice" + i + 
                  " = (Choice)comp[" + i + "];");
               for (int j = 0; j < count; j++)
                  {
                  ps.println("\t\tchoice" + i + ".addItem(\"" +
                  choice.getItem(j) + "\");");
                  }
               }
            else
            if (compType.equals("Label"))
               {
               Label label = (Label)c.getComponent(i);
               ps.println("\t\t" + compType + " " 
               + compType.toLowerCase() + i +
                  " = (" + compType + ")comp[" + i + "];");
               ps.println("\t\t" + compType.toLowerCase() + i + 
               ".setText(\"" + label.getText() + "\");");
               ps.println("\t\t" + compType.toLowerCase() + i + 
               ".setAlignment("+label.getAlignment()+");");
               }
            else
            if (compType.equals("List"))
               {
               List list = (List)c.getComponent(i);
               count = list.countItems();
               if (count > 0)
                  ps.println("\t\tList list" + i + 
                  " = (List)comp[" + i + "];");
               for (int j = 0; j < count; j++)
                  {
                  ps.println("\t\tlist" + i + ".addItem(\"" +
                  list.getItem(j) + "\");");
                  }
               }
            else
            if (compType.equals("TextField"))
               {
               // insert some text in field so Flow sets correct size
               TextField textfield = (TextField)c.getComponent(i);
               ps.println("\t\tTextField textfield" + i + 
                  " = (TextField)comp[" + i + "];");
               // what is the width of a 'w'?
               int w = textfield.getFontMetrics(textfield.getFont()).charWidth('w');
               int num = c.getComponent(i).size().width / w;
               StringBuffer str = new StringBuffer();
               for (int j = 0; j < num; j++)
                  str.append('w');
               ps.println("\t\ttextfield" + i +
               ".setText(\"" + str.toString() + "\");"); 
               }
            // get and set general Component stuff:
            // need work on Color stuff:
            //ps.println("\t\tcomp[" + i + "].setForeground(\"" +
            //c.getComponent(i).getForeground() +"\");");
            //ps.println("\t\tcomp[" + i + "].setBackground(\"" +
            //c.getComponent(i).getBackground() +"\");");
            // needs correct Font constructor:
            //ps.println("\t\tcomp[" + i + "].setFont(new Font(\"" +
            //c.getComponent(i).getFont().getName() +"\"));");
            ps.println(" "); // space between widget code
            }
         ps.println("\t\tc.resize(" + 
            c.size().height + ", " + c.size().width + ");");
         ps.println("\t\t}");
         ps.println("\t}");
         }
      catch (IOException e)
          {System.out.println(e.getMessage());}
      catch (ArrayIndexOutOfBoundsException e)
          {System.out.println(e.getMessage());}
      catch (NoSuchElementException e)
          {System.out.println(e.getMessage());}
      }
   private void printConstraints
      (Container cntr, Component cmp, String cName, PrintStream ps)
      {
      GridBagLayout layout = (GridBagLayout)cntr.getLayout();
      GridBagConstraints c = layout.getConstraints(cmp);
      ps.println("\t\tct.gridx = "+c.gridx+"; ct.gridy = "+c.gridy+";"); 
      ps.println("\t\tct.gridwidth = "+c.gridwidth+
         "; ct.gridheight = "+c.gridheight+";"); 
      ps.println("\t\tct.fill = "+c.fill+"; ct.anchor = "+c.anchor+";"); 
      ps.println("\t\tct.weightx = "+c.weightx+"; ct.weighty = "+c.weighty+";"); 
      ps.println("\t\tct.insets = new Insets(" + 
         c.insets.top + "," + c.insets.left + "," + 
         c.insets.bottom + "," + c.insets.right + ");");
      ps.println("\t\t((GridBagLayout)c.getLayout()).setConstraints(" +
         cName + ", ct);");
		}
   }
