package examples.htmlkona;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import weblogic.html.*;
import weblogic.common.*;

/**
 * This servlet uses htmlKona forms to collect user input.
 * 
 * @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class InputWithForms extends HttpServlet {

  private boolean isempty = true;

  public synchronized void service(HttpServletRequest req, HttpServletResponse res)
       throws IOException
  {
    try {
      res.setStatus(HttpServletResponse.SC_OK);
      res.setContentType("text/html");
      
      FormElement fe = getForm1(req);
      
      ServletPage hp = new ServletPage();
      hp.getHead()
	.addElement(new TitleElement("Input with Forms"));
      hp.getBodyElement()
	.setAttribute(BodyElement.bgColor, HtmlColor.white);
      hp.getBody()
        .addElement(MarkupElement.HorizontalLine);
      
      if (isempty) 
        hp.getBody()
          .addElement(new HeadingElement("Type something and click the button", 2)
		      .asCenteredElement())
          .addElement(fe.asCenteredElement());
      else 
        hp.getBody()
          .addElement(new HeadingElement("Type something again and click the button", 2)
		      .asCenteredElement())
          .addElement(fe.asCenteredElement())
          .addElement(MarkupElement.BeginParagraph)
          .addElement(getForm2().asCenteredElement());
      
      hp.getBody()
        .addElement(MarkupElement.HorizontalLine);
      hp.output(res.getOutputStream());
    } catch (Exception e) {
      defaults.showException(e, res.getOutputStream());
    }
  }
  
  public FormElement getForm1(HttpServletRequest req)  {
    String numstr = (String)req.getParameter("numitems");
    int numitems  = 0;
    try {numitems  = Integer.valueOf(numstr).intValue();} catch (Exception e) {;}
    if (numstr != null)
      System.out.println("numitems now is = " + Integer.parseInt(numstr));
    
    SelectElement se = new SelectElement("selectlist");
    for (int i = 0; i < numitems; i++) {
      String itemval = (String)req.getParameter("item" + i);
      if (itemval != null)
        se.addElement(itemval, itemval);
    }
    
    String newitem = (String)req.getParameter("textfield");
    
    if (newitem != null && !newitem.equals(""))
      se.addElement(newitem, newitem)
        .selectElementByText(newitem);
    
    FormElement form = new FormElement(defaults.prefix() + "InputWithForms", "POST");
    
    TableElement tab = new TableElement();
    form.addElement(tab);
    
    tab.addElement(new TableRowElement()
		   .addElement(new InputElement("SUBMIT", FieldType.submit)
			       .setValue("Add new item"))
		   .addElement(new InputElement("textfield")
			       .setSize(20).setMaxlen(20)));
    
    form.addElement(new InputElement("numitems", FieldType.hidden)
		    .setValue(Integer.toString(se.getNumElements())));
    
    for (int i = 0; i < se.getNumElements(); i++) 
      form.addElement(new InputElement("item" + i, FieldType.hidden)
		      .setValue(se.getElementAt(i).getValue()));
    
    if (se.getNumElements() > 0) {
      tab.addElement(new TableRowElement()
		     .addElement(new TableHeadingElement("Items added:")
				 .setColSpan(2)
				 .setAlign(AlignType.center)));
      isempty = false;
      
      HtmlContainer hc = new HtmlContainer();
      for (int i = 0; i < se.getNumElements(); i++) {
        String str = se.getElementAt(i).getValue();
        hc.addElement(new InputElement("radioitems", FieldType.radio, str).setValue(str))
          .addElement(MarkupElement.Break);
      }
      
      tab.addElement(new TableRowElement()
		     .addElement(se)
		     .addElement(hc));
    } else {
      tab.addElement(new TableRowElement()
                     .addElement(new TableHeadingElement("No items present")
                                 .setColSpan(2)
                                 .setAlign(AlignType.center)));
    }
    return form;
  }
  
  public FormElement  getForm2() {
    FormElement form = new FormElement(defaults.prefix() + "InputWithForms", "POST");
    form.addElement(new InputElement("numitems", FieldType.hidden).setValue(0))
      .addElement(new InputElement("SUBMIT", FieldType.submit).setValue("Clear Items"));
    return form;
  }
}
