/*
 * Q70AbstractMenuDisplayer.java
 *
 * Created on March 30, 2002, 3:34 PM
 */
package es.ejie.frmk.presentacion.iu.taglib.html.menu.displayer;

import java.io.IOException;
import java.util.StringTokenizer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import es.ejie.frmk.control.arranque.Q70GestorArranqueConfiguracion;
import es.ejie.frmk.infraestructura.traza.Q70TraceLevel;
import es.ejie.frmk.infraestructura.traza.Q70Traza;
import es.ejie.frmk.presentacion.iu.taglib.base.Q70Constants;
import es.ejie.frmk.presentacion.iu.taglib.html.Q70BaseHandlerTag;
import es.ejie.frmk.presentacion.iu.taglib.html.menu.base.Q70MenuComponent;

/**
 * Abstract implementation of <code>Q70MenuDisplayer</code> that can be used as
 * a basis for other menu displayers.
 *
 * @author  ssayles
 * @version
 */
public abstract class Q70AbstractMenuDisplayer extends Q70BaseHandlerTag 
implements Q70MenuDisplayer {

	private static Q70Traza tr = Q70Traza.getSingleton();
    
    //~ Instance fields ========================================================

    protected String name;
    protected JspWriter out;
    protected String target;
    protected String config;
    
    protected Q70MenuDisplayerMapping mapping;

    private static String FRMKSCRIPTSPATH = 
    		Q70GestorArranqueConfiguracion.getGlobalProperty(Q70Constants.FRMK_STATIC_PATH_KEY)
			+ Q70GestorArranqueConfiguracion.getGlobalProperty(Q70Constants.FRMK_STATIC_ALIAS_KEY)
			+ Q70Constants.FRMK_SCRIPTS_PATH;
        
    //~ Methods ================================================================

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

	public String getConfig() {
		return config;
	}

	public void setConfig(String config) {
		this.config = config;
	}

    public String getTarget() {
        return target;
    }

    /**
     * Convenience method that will first return the target for the displayer
     * if it is not null.  If the displayer target is null, then it will
     * return <code>menu.getTarget()</code>.
     *
     * @return the target for the menu link.
     */
    protected String getTarget(Q70MenuComponent menu) {
        String theTarget = null;

        if (target == null) {
            if (menu.getTarget() != null) {
                theTarget = menu.getTarget();
            }
        } else {
            theTarget = target;
        }

        return theTarget;
    }

    public void setTarget(String target) {
        this.target = target;
    }

     /**
     * Lifecycle method that should be called when the <code>Q70MenuDisplayer</code>
     * is being prepared for use.
     *
     * @param pageContext The JSP pageContext to give the displayer access
     *              to any resources it may need.
     * @param mapping The menu displayer mapping used to embody the xml
     *              definition.
     */
    public void init(PageContext pageContext, Q70MenuDisplayerMapping mapping) throws JspException {
    	super.pageContext = pageContext;
        this.out = pageContext.getOut();
        this.mapping = mapping;
        
        try 
        {
        	if (null == getJavaScriptFiles()) {
				if (null != getJavaScriptsFilesDefault()) {
					out.print(imprimirScriptJS(getJavaScriptsFilesDefault(), FRMKSCRIPTSPATH));
				}
			} else {
				out.print(imprimirScriptJS(getJavaScriptFiles(),""));
			}
        } 
        catch (Exception e) {
			if (tr.isTraceActive())
			{
				tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Q70AbstractMenuDisplayer: Throwable recogida en metodo init():" + e.getMessage());
			}            
        }
    }

    protected String getJavaScriptsFilesDefault(){
    	return null;
    }
    
	public abstract void display(Q70MenuComponent menu,PageContext pageContext)
    throws JspException, IOException;

    public void end(PageContext pageContext) {}

	/**
     * Se obtiene el String con los ficheros scripts
     * @param javaScriptFile
     * @param pathScript
     * @return String
     */
	private String imprimirScriptJS (String javaScriptFile,String pathScript)
	{
		if (javaScriptFile==null || javaScriptFile.equals("")){
			return "";
		}
		
		StringTokenizer st = new StringTokenizer(javaScriptFile,",");
		StringBuffer sb = new StringBuffer();
		while (st.hasMoreTokens())
		{
			sb.append("<script language=\"JavaScript\" src=\"");
			sb.append(pathScript);
			if (null!=pathScript && pathScript.trim().length()>0){
				sb.append("/"+st.nextToken()+"\"");
			}else{
				sb.append(st.nextToken()+"\"");
			}
			
			sb.append(" type=\"text/javascript\"></script> \r\n");  
		}
		return sb.toString();
	}  
}
