/*
 * $Id: TextTag.java,v 1.3 2005/02/02 23:00:33 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: TextTag.java,v $
 * Revision 1.3  2005/02/02 23:00:33  marbobo
 * + Verwendung einer Unicode-Sensitiven-Filtermethode
 *
 * Revision 1.2  2004/07/20 08:56:56  jonyp
 * added copyright info + cvs log
 *
 *
 */

package vkc.view.tag;

import javax.servlet.jsp.JspException;

import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import vkc.util.VKCStringUtils;
import vkc.view.InputFieldConstraints;

/**
 * Klasse erweitert den Struts TextTag um das Attribut maxlengthProperty
 * zur einfachen Längenbegrenzung von Input-Felder. Der Wert der Property
 * maxlengthProperty wird aus der Klasse InputFieldConstraints bestimmt
 * <p>
 * @author bigmac
 */
public class TextTag extends org.apache.struts.taglib.html.TextTag {

	protected String maxlengthProperty = null;

    public TextTag() {
	   super();
    }
    
    /**
     * @see org.apache.struts.taglib.html.TextTag
     */
	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=\"");
		// * @since Struts 1.1
		if (indexed)
			prepareIndex(results, name);
		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 (maxlengthProperty != null) {
			String len = InputFieldConstraints.getMaxLength( maxlengthProperty );
			if ( len.length()>0 ) {
				results.append(" maxlength=\"");
				results.append( len );
				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(VKCStringUtils.filterUnicodeChars(value));
		} else if (redisplay || !"password".equals(type)) {
			Object value = RequestUtils.lookup(pageContext, name, property, null);
			if (value == null)
				value = "";
			results.append(VKCStringUtils.filterUnicodeChars(value.toString()));
		}

		results.append( "\"" );
		results.append( prepareEventHandlers() );
		results.append( prepareStyles() );
		results.append( getElementClose() );

		// Print this field to our output writer
		ResponseUtils.write(pageContext, results.toString());

		// Continue processing this page
		return EVAL_PAGE;
	}

	public String getMaxlengthProperty() {
		return maxlengthProperty;
	}

	public void setMaxlengthProperty(String string) {
		maxlengthProperty = string;
	}

	
	public void release() {
		maxlengthProperty = null;
		super.release();
	}

}