/*
 * Q70DropDownMenuDisplayer.java
 *
 * Created on February 6, 2001, 1:00 PM
 * 
 * @author  ssayles
 */
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;



/**
 * Clase que displaya en web todo un arbol de menu (submenus) en modo DropDown 
 * @version 1.0  Fecha: 6-feb-2001
 */
public class Q70DropDownMenuDisplayer extends Q70MessageResourcesMenuDisplayer 
{
	//Instancia logs de traza
	private static Q70Traza tr = Q70Traza.getSingleton();
    
    /**
     * @param pageContext
     * @param mapping
     * @throws JspException
     */
    public void init(PageContext pageContext, Q70MenuDisplayerMapping mapping) throws JspException {
        super.init(pageContext, mapping);

        StringBuffer sb = new StringBuffer();

        //display the javascript function
		sb.append(getConfigMessage("dd.js.start")); 
		sb.append(getConfigMessage("dd.js.image.src.expand", getConfigMessage("dd.image.src.expand")));
		sb.append(getConfigMessage("dd.js.image.src.expanded", getConfigMessage("dd.image.src.expanded")));
		sb.append(getConfigMessage("dd.js.toggle.display"));
		sb.append(getConfigMessage("dd.js.end"));

        try 
        {
            out.print(sb.toString());
        } 
        catch (Exception e) 
        {
			if (tr.isTraceActive())
			{
				tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Q70DropDownMenuDisplayer: Throwable recogida en metodo init():" + e.getMessage());
			}          
        }
    }

    /**
     * @param menu
     * @param pageContext
     * @throws JspException
     * @throws IOException
     */
    public void display(Q70MenuComponent menu,PageContext pageContext) throws JspException, IOException 
    {
    	this.pageContext=pageContext;
        String title = super.getMessage(menu.getListaI18nKey(), menu.getTitle());
        String menuName = menu.getName();
        StringBuffer sb = new StringBuffer();
        String img = EMPTY;

        if (menu.getImage() != null) {
            img = super.getConfigMessage("dd.image", menu.getImage());
        }

        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. Q70DropDownMenuDisplayer");
			}
			RequestUtils.saveException(pageContext, e);
			throw new JspException("Error en la obtención de los menus. Q70DropDownMenuDisplayer",e);
		}

		super.setSecureId(menu.getSecureId());
		if (Q70Constants.MODE_NOTDISPLAYED != super.getDisplayMode())
		{  
        	sb.append(getConfigMessage("dd.menu.top"));
        	if (components.length > 0) 
        	{
                sb.append(getConfigMessage("dd.menu.expander", 
									 	   menuName, 
									 	   menuName + "_img", 
									 	   getConfigMessage("dd.image.expander",
                            		 						menuName + "_img",
                            		 						getConfigMessage("dd.image.src.expand")
                            		 						) +
                           			 NBSP + img + title)
                          );
                displayComponents(menu, sb,pageContext);
                sb.append(getConfigMessage("dd.menu.restore", menuName, menuName + "_img"));
            } 
	        else 
	        {
	            sb.append(title);
	        }

        	sb.append(getConfigMessage("dd.menu.bottom"));
    	}
        out.println(sb.toString());
    }

	//-------------------------------------------------------------------------------------------- Private Methods

    /**
     * Metodo recursivo que displaya el arbol de menus y submenus
     * @param menu
     * @param sb
     * @param pageContext
     * @throws JspException
     * @throws IOException
     */
    private void displayComponents(Q70MenuComponent menu, StringBuffer sb,PageContext pageContext) throws JspException, IOException 
    {
        String title = null;
        String name = menu.getName();
        String href = EMPTY;
        String img = EMPTY;
        
        super.displayComponents(menu,pageContext);
        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. Q70DropDownMenuDisplayer");
			}
			RequestUtils.saveException(pageContext, e);
			throw new JspException("Error en la obtención de los menus. Q70DropDownMenuDisplayer",e);
		}

        sb.append(getConfigMessage("dd.menu.item.top", name));

        for (int i = 0; i < components.length; i++) 
        {
			components[i].setI18nKey(menu.getI18nKey());        	
			super.setSecureId(components[i].getSecureId());
			if (Q70Constants.MODE_NOTDISPLAYED != super.getDisplayMode())
			{  
				components[i].setPageContext(pageContext);
				components[i].setListaI18nKey(getListaI18nKey());
				setPageLocation(components[i]);
            	title = super.getMessage(components[i].getListaI18nKey(), components[i].getTitle());

	            if (components[i].getImage() != null) {
	                img = getConfigMessage("dd.image", components[i].getImage());
	            } else {
	                img = EMPTY;
	            }
	
	            href = components[i].getLocation();
	
	            sb.append(getConfigMessage("dd.menu.item.row.start"));

	            try {
					if (components[i].getMenuComponents().length > 0) 
					{
					    sb.append(getConfigMessage("dd.menu.expander",
					            			 	   components[i].getName(),
					            			 	   components[i].getName() + "_img",
												   getConfigMessage("dd.image.expander",
					                								components[i].getName() + "_img",
																	getConfigMessage("dd.image.src.expand")
					                				   			   ) +
					            			 NBSP + img + title)
					             );
					    displayComponents(components[i], sb,pageContext);
					    sb.append( getConfigMessage("dd.menu.restore", components[i].getName(), components[i].getName() + "_img") );
					} 
					else 
					{
					    sb.append(getConfigMessage("dd.link.start", 
					    					 	   href,
					            			 	   super.getMenuTarget(components[i]),
					            			 	   super.getMenuToolTip(components[i])) );
					    sb.append(NBSP);
					    sb.append(NBSP); //a couple of spaces
					    sb.append(img);
					    sb.append(title);
					    sb.append(getConfigMessage("dd.link.end"));            
					}
				} catch (Q70FrameworkException e) {
					if (tr.isTraceActive())
					{
						tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Error en la obtención de los menus. Q70DropDownMenuDisplayer");
					}
					RequestUtils.saveException(pageContext, e);
					throw new JspException("Error en la obtención de los menus. Q70DropDownMenuDisplayer",e);
				}
            	sb.append(getConfigMessage("dd.menu.item.row.end"));
        	}        	
        }
        sb.append(getConfigMessage("dd.menu.item.bottom"));
    }
}
