/*
 * Q70CascadeMenuDisplayer.java
 *
 * Created on March 21, 2002, 5:47 PM
 */
package es.ejie.frmk.presentacion.iu.taglib.html.menu.displayer;

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

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;


/**
 * Variables for each menu item: (** means that they have to be spesified!)
 * 0 name: The name of the item. This must be unique for each item. Do not use spaces or strange charachters in this one! **
 * 1 parent: The name of the menuitem you want this to "connect" to. This will be a submenu of the item that have the name you place in here. ** for all other then the topitems
 * 2 text: The text you want in the item. ** (except if you use images)
 * 3 link: The page you want this item to link to.
 * 4 target: The target window or frame you want the link to go to (Default is same window if you're not using frames, and the mainframe if you're using frames)
 * width: The width of the element. If not spesified it will get the default width spesified above.
 * height: The height of the element. If not spesified it will get the default height spesified above.
 * 5 img1: The "off" image for element if you want to use images.
 * 6 img2: The image that appears onmouseover if using images.
 * 7 bgcoloroff: The background color for this item. If not spesified it will get the default background color spesified above.
 * 8 bgcoloron: The "on" background color for this item. If not spesified it will get the default "on" background color spesified above.
 * 9 textcolor: The text color for this item. If not spesified it will get the default text color spesified above.
 *10 hovercolor: The "on" text color for this item. If not spesified it will get the default "on" text color spesified above. Netscape4 ignores this
 *11 onclick: If you want something to happen when the element is clicked (different from going to a link) spesifiy it here.
 * onmouseover: This will happen when you mouseover the element. Could be status text, another imageswap or whatever.
 * onmouseout: This will happen when you mouseout the element.
 * Remember you can have as many levels/sublevels as you want. Just make sure you spesify the correct "parent" for each item.
 * To set styles for each level see above.
 * @author  ssayles
 * @version 1.0
 */
public class Q70CascadeMenuDisplayer extends Q70MessageResourcesMenuDisplayer 
{
	private static Q70Traza tr = Q70Traza.getSingleton();

    // main message format of the menu.  only 10 args max in jdk1.3 :( 
    private static MessageFormat menuMessage =
        new MessageFormat(
            "oCMenu.makeMenu(''{0}'',''{1}'',''{2}'',''{3}'',''{4}'','''',''''," +
            "''{5}'',''{6}'',{7},{8},{9},");
    private static final String SCRIPT_START =
        "<script language=\"javascript\" type=\"text/javascript\"> \r\n";
    private static final String SCRIPT_END = "</script>\n";
    private static final String END_STATEMENT =
        "\noCMenu.makeStyle(); oCMenu.construct()\n";
    private static final String TOP_IMAGE = "cmTopMenuImage";
    private static final String SUB_IMAGE = "cmSubMenuImage";
    private static final String BGCOL_ON = "cmBGColorOn";
    private static final String BGCOL_OFF = "cmBGColorOff";
    private static final String TXTCOL = "cmTxtColor";
    private static final String HOVER = "cmHoverColor";
    private static final String DIS_BGCOL_ON = "cmDisBGColorOn";
    private static final String DIS_BGCOL_OFF = "cmDisBGColorOff";
    private static final String DIS_TXTCOL = "cmDisTxtColor";
    private static final String DIS_HOVER = "cmDisHoverColor";
    
    private static final String DEFAULT_JAVASCRIPTFILES ="cascademenus3.js,cascademenu-config.js";

    protected String getJavaScriptsFilesDefault() {
    	return DEFAULT_JAVASCRIPTFILES;
    }
    
    /**
     * @param context
     * @param mapping
     * @throws JspException
     */
    public void init(PageContext context, Q70MenuDisplayerMapping mapping) throws JspException
    {
        super.init(context, mapping);
        try 
        {
            out.println(SCRIPT_START);
        } 
        catch (Exception e) {
			if (tr.isTraceActive())
			{
				tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Q70CascadeMenuDisplayer: Throwable recogida en metodo init():" + e.getMessage());
			}            
        }
    }

    /**
     * Prints the appropriate javascript for CoolMenu using \
     * <code>menuMessage</code> as the format.
     * @param menu
     * @param pageContext
     */
    public void display(Q70MenuComponent menu,PageContext pageContext) throws JspException, IOException 
    {        
    	this.pageContext=pageContext;
		super.setSecureId(menu.getSecureId());
		boolean allowed =(Q70Constants.MODE_NOTDISPLAYED!=super.getDisplayMode()?true:false);
      
		StringBuffer sb = new StringBuffer();
       	buildMenuString(menu, sb, allowed,pageContext);
      	out.print(sb);
    }

    /**
     * This will output the ending javascript statements defined in
     * <code>END_STATEMENT</code> and <code>SCRIPT_END</code>
     * @param context
     */
    public void end(PageContext context) 
    {
        try 
        {
            out.print(END_STATEMENT);
            out.print(SCRIPT_END);
        } 
        catch (Exception e) 
        {
			if (tr.isTraceActive())
			{
				tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Q70CascadeMenuDisplayer: Throwable recogida en metodo end():" + e.getMessage());
			}          	
        }
    }

    /**
     * Metodo recursivo que construye el arbol de menus y submenus.
     * @param menu
     * @param sb
     * @param allowed
     * @param pageContext
     * @throws JspException
     */
    protected void buildMenuString(Q70MenuComponent menu, StringBuffer sb, boolean allowed,PageContext pageContext) throws JspException
    {
    	super.displayComponents(menu,pageContext);
		
        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("#");
        }
    	sb.append(menuMessage.format(getArgs(menu, allowed)));
        sb.append((allowed) ? HOVER : DIS_HOVER);
        sb.append(",'");
        sb.append((menu.getOnclick() == null) ? EMPTY : menu.getOnclick());
        sb.append("')\n");

        Q70MenuComponent[] subMenus;
		try {
			subMenus = menu.getMenuComponents();
		} catch (Q70FrameworkException e) {
			if (tr.isTraceActive())
			{
				tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Error en la obtención de los menus. Q70CascadeMenuDisplayer");
			}
			RequestUtils.saveException(pageContext, e);
			throw new JspException("Error en la obtención de los menus. Q70CascadeMenuDisplayer",e);

		}

        if (subMenus.length > 0) 
        {
            for (int i = 0; i < subMenus.length; i++) 
            {
				subMenus[i].setI18nKey(menu.getI18nKey());
				super.setSecureId(subMenus[i].getSecureId());
				subMenus[i].setPageContext(pageContext);
				subMenus[i].setListaI18nKey(this.getListaI18nKey());
												
				boolean subMenuAllowed =(Q70Constants.MODE_NOTDISPLAYED!=super.getDisplayMode()?true:false);
				buildMenuString(subMenus[i], sb, subMenuAllowed,pageContext);                
            }
        }
    }

    /**
     * @param menu
     * @param allowed
     * @return
     * @throws JspException
     */
    protected String[] getArgs(Q70MenuComponent menu, boolean allowed) throws JspException{
        String[] args = new String[10];
        args[0] = menu.getName();
        args[1] = getParentName(menu);
        args[2] = getTitle(menu);
        args[3] = (menu.getUrl() == null) ? EMPTY : ((allowed) ? menu.getUrl() : EMPTY);
        args[4] = getTarget(menu);
        args[5] = EMPTY;
        args[6] = EMPTY;
        args[7] = (allowed) ? BGCOL_OFF : DIS_BGCOL_OFF;
        args[8] = (allowed) ? BGCOL_ON : DIS_BGCOL_ON;
        args[9] = (allowed) ? TXTCOL : DIS_TXTCOL;

        return args;
    }

    /**
     * Return a translated title for the menu item that will contain
     * the <code>TOP_IMAGE</code> javscript variable if it is a parent
     * menu with sub menus, or the <code>SUB_IMAGE</code> variable if it
     * is a sub menu with sub menus.
     * @param menu
     * @throws JspException
     */
    protected String getTitle(Q70MenuComponent menu) throws JspException{
        String title = getMessage(menu.getListaI18nKey(), menu.getTitle());

        try {
			if (menu.getMenuComponents().length > 0) {

			    if (menu.getParent() == null) { //then us the top image
			        title = title + "'+" + TOP_IMAGE + "+'";
			    } else { //use the sub menu image
			        title = title + "'+" + SUB_IMAGE + "+'";
			    }
			}
		} catch (Q70FrameworkException e) {
			if (tr.isTraceActive())
			{
				tr.trace(COMPONENTE, Q70TraceLevel.ERROR, "Error en la obtención de los menus.Q70CascadeMenuDisplayer");
			}
			RequestUtils.saveException(pageContext, e);
			throw new JspException("Error en la obtención de los menus.Q70CascadeMenuDisplayer",e);
		}

        return title;
    }

    /**
     * @param menu
     * @return String 
     */
    protected String getParentName(Q70MenuComponent menu) {
        String name = null;

        if (menu.getParentMenu() == null) {
            name = "";
        } else {
            name = menu.getParentMenu().getName();
        }

        return name;
    }

    /**
     * @param menu
     * @return String 
     */
    protected String getTarget(Q70MenuComponent menu) {
        String theTarget = super.getTarget(menu);

        if (target == null) {
            theTarget = EMPTY;
        }

        return theTarget;
    }
}
