package com.structsoft.struts.taglib.html;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.upload.FormFile;
import org.apache.struts.util.BeanUtils;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.PropertyUtils;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;


/**
 * Custom tag for input fields of type "text".
 *
 * @author Craig R. McClanahan
 * @version $Revision: 1.1 $ $Date: 2001/01/06 21:50:39 $
 */

public class GridTextTag extends org.apache.struts.taglib.html.TextTag {


    /**
     * Construct a new instance of this tag.
     */
    public GridTextTag() {
    	super();
    }

    protected String index = null;

    public String getIndex()
    {
        return index;
    }

    public void setIndex(String index)
    {
        this.index = index;
    }

    public void setIndex(int index)
    {
        this.index = String.valueOf(index);
    }


    public int doStartTag() throws JspException {

	// Create an appropriate "input" element based on our parameters
	StringBuffer results = new StringBuffer("<input type=\"");
	results.append(type);
	results.append("\" name=\"");

	if (index != null)
	{
        results.append(getName());
	    results.append("[");
	    results.append(index);
	    results.append("].");
	}
	
    results.append(property);
	
	results.append("\"");
	if (accesskey != null) {
	    results.append(" accesskey=\"");
	    results.append(accesskey);
	    results.append("\"");
	}
	if (accept != null) {
	    results.append(" accept=\"");
	    results.append(accept);
	    results.append("\"");
	}
	if (maxlength != null) {
	    results.append(" maxlength=\"");

        Object maxLengthValue = RequestUtils.lookup(pageContext, name, maxlength, null);
	    if (null != maxLengthValue)
	        results.append(maxLengthValue);
	    else
    	    results.append(maxlength);
	    
	    results.append("\"");
	}
	if (cols != null) {
	    results.append(" size=\"");
	    results.append(cols);
	    results.append("\"");
	}
	if (tabindex != null) {
	    results.append(" tabindex=\"");
	    results.append(tabindex);
	    results.append("\"");
	}
	results.append(" value=\"");
	if (value != null) {
	    results.append(BeanUtils.filter(value));
	} else if (redisplay || !"password".equals(type)) {
            /*
	    Object bean = pageContext.findAttribute(name);
	    if (bean == null)
		throw new JspException
		    (messages.getMessage("getter.bean", name));
	    try {
                String value = "";
                Object objvalue = PropertyUtils.getProperty(bean, property);
                if ((objvalue != null) &&
                    !(objvalue instanceof FormFile)) {
                    value = objvalue.toString();
                }
                if (value == null) {
                    value = "";
                }
		results.append(BeanUtils.filter(value));
	    } catch (IllegalAccessException e) {
		throw new JspException
		    (messages.getMessage("getter.access", property, name));
	    } catch (InvocationTargetException e) {
		Throwable t = e.getTargetException();
                t.printStackTrace();
		throw new JspException
		    (messages.getMessage("getter.result",
					 property, t.toString()));
	    } catch (NoSuchMethodException e) {
		throw new JspException
		    (messages.getMessage("getter.method", property, name));
	    }
            */
            Object value = RequestUtils.lookup(pageContext, name, property,
                                               null);
            if (value == null)
                value = "";
            results.append(ResponseUtils.filter(value.toString()));
	}
	results.append("\"");
	results.append(prepareEventHandlers());
	results.append(prepareStyles());
	results.append(">");

	// Print this field to our output writer
	JspWriter writer = pageContext.getOut();
	try {
	    writer.print(results.toString());
	} catch (IOException e) {
	    throw new JspException
		(messages.getMessage("common.io", e.toString()));
	}

	// Continue processing this page
	return (EVAL_BODY_TAG);

    }


}

