/*
 * Creado el 03-dic-03
 *
 * Para cambiar la plantilla para este archivo generado vaya a
 * Ventana&gt;Preferencias&gt;Java&gt;Generación de código&gt;Código y comentarios
 */
package es.ejie.frmk.presentacion.iu.taglib.html;

import javax.servlet.jsp.JspException;

import org.apache.struts.util.ResponseUtils;

import es.ejie.frmk.infraestructura.traza.Q70TraceLevel;
import es.ejie.frmk.infraestructura.traza.Q70Traza;
import es.ejie.frmk.presentacion.iu.taglib.base.Q70Constants;

/**
 * Abstract base class for the various button tags.
 *
 * @author UFRM002
 * @version 1.0
 */
public class Q70BaseButtonTag extends Q70BaseHandlerTag 
{
	/**
	 * Instancia del log de trazas
	 */
	private static Q70Traza tr = Q70Traza.getSingleton();


	
	//Indica si el tipo de boton actual es de tipo "cancel"
	private boolean isCancel = false;


	//	----------------------------------------------------- Instance Variables

	/**
	 * The type of input field represented by this tag (button, cancel, 
	 * reset or submit).
	 */
	protected String type = null;


	/**
	 * The property name of the generated button.
	 */
	protected String property = null;


	/**
	 * The body content of this tag (if any).
	 */
	protected String text = null;


	/**
	 * The value of the button label.
	 */
	protected String value = null;



	//	------------------------------------------------------------- Properties


	/**
	 * Return the property name.
	 */
	public String getProperty() {
		return (property);
	}

	/**
	 * Set the property name.
	 *
	 * @param property The property name
	 */
	public void setProperty(String property) {
		this.property = property;
	}


	/**
	 * Return the label value.
	 */
	public String getValue() {
		return (value);
	}


	/**
	 * Set the label value.
	 * @param value The label value
	 */
	public void setValue(String value) {
		this.value = value;
	}


	//	--------------------------------------------------------- Public Methods

	/**
	 * Process the start of this tag.
	 * @exception JspException if a JSP exception has occurred
	 */
	public int doStartTag() throws JspException 
	{
		if (tr.isTraceActive())	tr.trace(COMPONENTE, Q70TraceLevel.DEBUG, "Generando tag de tipo " + this.getClass().toString()); 

		//Comprobar si el boton es de tipo "cancel"
		isCancel = ("cancel".equals(type) ? true : false);
			

		// Do nothing until doEndTag() is called
		this.text = null;
		
		if ( isCancel && (null==property) ) 
			this.property = Q70Constants.CANCEL_PROPERTY;
					
		// Devuelve EVAL_BODY_TAG. 
		// Se devuelve un int para evitar errores de deprecacion 
		// en compilacion al usar j2ee 1.3
		return 2;

	}


	/**
	 * Save the associated label from the body content.
	 *
	 * @exception JspException if a JSP exception has occurred
	 */
	public int doAfterBody() throws JspException {

		if (bodyContent != null) {
			String value = bodyContent.getString().trim();
			if (value.length() > 0)
				text = value;
		}
		return 0; //SKIP_BODY

	}


	/**
	 * Process the end of this tag.
	 * @exception JspException if a JSP exception has occurred
	 */
	public int doEndTag() throws JspException {

		int displayMode = getDisplayMode();
	
		//Si displayMode == Q70Constants.MODE_NOTDISPLAYED,
		// no hay que pintar nada
		if (displayMode != Q70Constants.MODE_NOTDISPLAYED ) {

			if (displayMode == Q70Constants.MODE_READONLY ) {	
				this.setDisabled(true);
			}

			// Render this element to our writer
			ResponseUtils.write(pageContext, renderButtonElement(label()));

		}
		
		// Evaluate the remainder of this page
		return 6; //EVAL_PAGE

	}


	/**
	 * Release any acquired resources.
	 */
	public void release() {

		super.release();
		property = null;
		if (isCancel) 
			property = Q70Constants.CANCEL_PROPERTY;		
		text = null;
		value = null;

	}




	/**
	 * Return the label value to display in the cancel button.
	 * @since Struts 1.1
	 */
	protected String label() {
		// Acquire the label value we will be generating
		String label = value;
		if ((label == null) && (text != null))
			label = text;
		if ((label == null) || (label.trim().length() < 1))
			label = (type!=null ? type : "Click");
		
		return label;	
	}

	/**
	 * Generate an HTML button of the specified type.
	 * @param label The text to be displayed on the button.
	 * @return A fully formed HTML button.
	 * @throws JspException
	 * @since Struts 1.1
	 */
	protected String renderButtonElement(String label) throws JspException {
		// Generate an HTML element
		StringBuffer results = new StringBuffer("<input type=\"");
	
		results.append( (isCancel) ? "submit" : type );
		results.append("\"");
		
		if (property != null) {
			results.append(" name=\"");
			results.append(property);

			// * @since Struts 1.1
			if ( indexed && ("button".equals(type)||("submit".equals(type)))) {
				prepareIndex( results, null );
			}			
			results.append("\"");
		}
		
		if (accesskey != null) {
			results.append(" accesskey=\"");
			results.append(accesskey);
			results.append("\"");
		}
		if (tabindex != null) {
			results.append(" tabindex=\"");
			results.append(tabindex);
			results.append("\"");
		}
		results.append(" value=\"");
		results.append(label);
		results.append("\"");
		results.append(prepareEventHandlers());
		results.append(prepareStyles());
        
		if (isCancel) {
			// if no onclick event was provided, put in the cancel script
			if(results.toString().indexOf("onclick=")==-1){
		  		results.append(" onclick=\"bCancel=true;\"");
			}
		}
        
		results.append(getElementClose());		
		
		return results.toString();
	}


}
