/*
 * Q70SimpleMenuDisplayer.java
 *
 * Created on February 15, 2001, 11:14 AM
 */
package es.ejie.frmk.presentacion.iu.taglib.html.menu.displayer;

import java.io.IOException;
import java.net.MalformedURLException;

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

import org.apache.struts.util.RequestUtils;

import es.ejie.frmk.control.excepciones.Q70FrameworkException;
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.menu.base.Q70MenuComponent;


/**
 *
 * @author  ssayles
 * @version
 */
public class Q70SimpleMenuDisplayer extends Q70MessageResourcesMenuDisplayer 
{   
    //~ Static fields/initializers =============================================

	protected static final String nbsp = NBSP;

	/**
	 * Instancia del log de trazas
	 */
	private static Q70Traza tr = Q70Traza.getSingleton();
	


    //~ Methods ================================================================

    public void init(PageContext pageContext, Q70MenuDisplayerMapping mapping) throws JspException{
        super.init(pageContext, mapping);

        try 
        {
            out.println(getConfigMessage("smd.style"));            
        } 
        catch (Exception e) 
        {        
			if (tr.isTraceActive())
			{
				tr.trace(COMPONENTE, Q70TraceLevel.ERROR, e.getMessage());
			}                       
        }
    }
    
    public void display(Q70MenuComponent menu,PageContext pageContext) throws JspException,IOException 
    {
    	this.pageContext=pageContext;
    	super.setSecureId(menu.getSecureId());
    	if (Q70Constants.MODE_NOTDISPLAYED!=super.getDisplayMode()){
    		out.println(getConfigMessage("smd.menu.top"));
    		displayComponents(menu,0,pageContext);
    		out.println(getConfigMessage("smd.menu.bottom"));
    	}
    }
    
    protected void displayComponents(Q70MenuComponent menu, int level,PageContext pageContext)
    throws JspException, IOException 
    {
    	//this.pageContext=pageContext;
    	//menu.setListaI18nKey(getListaI18nKey());
	    
    	super.displayComponents(menu,pageContext);
		//setSecureId(menu.getSecureId());
		if (Q70Constants.MODE_NOTDISPLAYED != getDisplayMode())
		{ 
			try{
        		setPageLocation(menu);
        	}catch (MalformedURLException m)
        	 {	                    
				if (tr.isTraceActive())	{
					tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Action o forward incorrecto: " + m.getMessage());
					tr.trace(COMPONENTE, Q70TraceLevel.DEBUG, "Estableciendo location a #");
				} 																			                    
                menu.setLocation("#");
            }	
        	
    	    Q70MenuComponent[] components;
			try {
				components = menu.getMenuComponents();
			} catch (Q70FrameworkException e) {
				if (tr.isTraceActive())
				{
					tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Error en la obtención de los menus. Q70SimpleMenuDisplayer");
				}
				RequestUtils.saveException(pageContext, e);
				throw new JspException("Error en la obtención de los menus. Q70SimpleMenuDisplayer",e);
			}
	        if (components.length > 0) 
	        {
	        	out.println(getConfigMessage("smd.menu.item.top",
	                    			   		 getSpace(level) + 
	                    			   		 getConfigMessage("smd.menu.item.image.bullet") + 
	                    			   		 getMessage(menu.getListaI18nKey(),menu.getTitle()) ) );
	
	            for (int i = 0; i < components.length; i++) 
	            {
					components[i].setI18nKey(menu.getI18nKey());
	                try {
						if (components[i].getMenuComponents().length > 0) 
						{
						    displayComponents(components[i], level + 1,pageContext);
						} 
						else 
						{    
							printComponent("smd.menu.item", components[i], level + 1);
						}
					} catch (Q70FrameworkException e) {
						if (tr.isTraceActive())
						{
							tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Error en la obtención de los menus. Q70SimpleMenuDisplayer");
						}
						RequestUtils.saveException(pageContext, e);
						throw new JspException("Error en la obtención de los menus. Q70SimpleMenuDisplayer",e);
					}
	            }
	        } 
	        else 
	        {
				printComponent("smd.menu.item", menu, level);
	        }
		}		
	}
    
    protected String getSpace(int length) {
        String space = EMPTY;

        for (int i = 0; i < length; i++) {
            space = space + nbsp + nbsp;
        }

        return space;
    }

    protected String getImage(Q70MenuComponent menu) throws JspException
    {
        String imageTag = null;

        if ((menu.getImage() == null) || (menu.getImage() == EMPTY)) 
        {
            imageTag = EMPTY;
        } 
        else 
        {
            imageTag =
			getConfigMessage("smd.menu.item.image", menu.getImage(), super.getMenuToolTip(menu));
        }

        return imageTag;
    }
    
    //------------------------------------------------------------------------------------ Private methods
    
    /**
     * Manda al navegador el codigo para pintar el componente (solo si seguridad lo permite).
     * @param key: clave del recurso de internacionalizacion
     * @param menu: objeto menu 
     * @param level: nivel de profuncidad del menu
	 *
     * @throws JspException
     */
    private void printComponent(String key, Q70MenuComponent menu, int level)
		throws JspException, IOException 
    {
		super.setSecureId(menu.getSecureId());
		menu.setPageContext(pageContext);
    	menu.setListaI18nKey(getListaI18nKey());
    	setPageLocation(menu);
    	

    	if (Q70Constants.MODE_NOTDISPLAYED != super.getDisplayMode())
		{ 		        	
			out.println( getConfigMessage(key, 
		 	   			  				  menu.getUrl(), 
				   		  				  super.getMenuTarget(menu),
				   		  				  super.getMenuToolTip(menu),
				   		  				  this.getSpace(level) + getImage(menu) + getMessage(menu.getListaI18nKey(),menu.getTitle())) );
		}
    }
    
    
}
