/*  Copyright (c) 2000 by Oracle Corporation. All Rights Reserved. */

package oracle.portal.provider.v1;

import java.io.PrintWriter;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;


import oracle.portal.provider.v1.resource.ProviderBundle;
import oracle.portal.provider.v1.http.ServletPortletRenderRequest;
import oracle.portal.utils.BiDiUtils;
import oracle.portal.utils.v1.ParameterMap;
import oracle.portal.utils.v1.SimpleParameterMap;
import oracle.webdb.utils.SimpleStringBuffer;

/**
 *  A PortletContainerRenderer is a utility class that renders the container
 *  for a portlet.
 *
 *  The container that is renderered may vary depending on the SHOW mode that
 *  is being requested. For example, Edit, Edit Defaults. Help, About, and
 *  Show Details are all rendered in "full screen" mode whereas the regular
 *  Show and Preview modes have variable size containers depending on the
 *  size of the portlet.
 */

public class MocContainerRenderer extends DefaultContainerRenderer
{

    /**
     *  Renders a portlet's titlebar. Portlets are responsible for rendering their container.  This method renders
     *  the standard portlet titlebar using information from the request and
     *  Portlet meta-data. Typically, clients will not use this method.  Rather they
     *  wil rely on renderPortletBegin() to render the entire container including the
     *  titlebar.  If however, renderPortletBegin is not used, this method is
     *  provided for convenience.
     *
     *  @param request the portlet render request
     *  @param title the label that should be displayed as the title.  if <b>null</b> then
     *  the method will render the static title retrieved via Portlet.getTitle();
     */
    protected void renderPortletTitlebar(PortletRenderRequest request,
                                         PrintWriter out,
                                         ParameterMap params)
        throws PortletException, IOException
    {
        int renderMode = request.getMode();
        Portlet p = request.getPortlet();
        String title = (String)params.getParameterValue(PortletContainerRenderer.PORTLET_TITLE);
        if (title == null)
        {
            try
            {
                title = PortletRendererUtil.getTitle(request);
            }
            catch (AccessControlException e)
            {
                throw new PortletException(e);
            }
        }

        out.print("<TABLE BORDER=\"0\" class=\"");
        out.print(PortletRenderer.PORTLET_HEADER_COLOR);
        out.println("\" CELLSPACING=\"0\" CELLPADDING=\"0\" WIDTH=\"100%\">");

        out.print("<TR class=\"");
        out.print(PortletRenderer.PORTLET_HEADER_COLOR);
        out.println("\">");

        out.print("<TD class=\"");
        out.print(PortletRenderer.LEFT_CURVE);
        out.print("\" valign=\"top\" width=\"10\" ");
        out.println("height=\"10\" NOWRAP>&nbsp;&nbsp;&nbsp;</TD>");

        if (p.hasShowDetails() && !(renderMode == PortletRenderer.MODE_PREVIEW)) {
            renderTitle(request, title, out, p,
                        getAnchorRef(request, p, PortletRenderer.MODE_SHOW_DETAILS, title));
        } else {
            renderTitle(request, title, out, p, null);
        }

        out.print("<TD class=\"");
        out.print(PortletRenderer.RIGHT_CURVE);
        out.println("\" width=\"10\" NOWRAP>&nbsp;&nbsp;</TD>");

        if (renderMode == PortletRenderer.MODE_PREVIEW)
        {
            renderDummyActions(out, p, request);
        }
        else
        {
            renderActions(out, p, request, title);
        }

        out.println("</TD></TR></TABLE>");
    }



    protected void renderTitle(PortletRenderRequest request,
                                    String title,
                                    PrintWriter out,
                                    Portlet p,
                                    String href)
        throws java.io.IOException
    {
        String endAnchor;
        if (href == null) {
            href = "";
            endAnchor = "";
        } else {
            endAnchor = "</a>";
        }

        out.print("<TD class=\"");
        out.print(PortletRenderer.PORTLET_HEADER_COLOR);
        out.print("\" NOWRAP=\"\" valign=\"top\">");
        out.print(href);
        out.print("<font class=\"");
        out.print(PortletRenderer.PORTLET_HEADER_TEXT);
        out.print("\">");
        out.print(title);
        out.print(" </font>");
        out.print(endAnchor);
        out.println("&nbsp;</TD>");
    }

    protected void renderActions(PrintWriter out,
                                 Portlet p,
                                 PortletRenderRequest request,
                                 String title)
        throws java.io.IOException
    {
        Locale l = request.getLocale();
       	out.print("<TD class=\"");
       	out.print(PortletRenderer.PAGE_COLOR);
       	out.print("\" align=\"");
       	out.print(BiDiUtils.getAlignment(l, BiDiUtils.RIGHT));
        out.println("\" NOWRAP=\"\" valign=\"top\" width=\"100%\">&nbsp;");

        ResourceBundle res = request.getProviderResource();

        if (p.hasShowEdit())
        {
            boolean showCustomize = false;
            if (p instanceof PortletXtra3)
            {
                // determine if user should be able to see customize link based
                // in new authentication level based methods

                // only show the customize link if the user's authentication level
                // is greater than or equal to the minimum authentication level
                // specified by the portlet.
                PortletXtra3 p3 = (PortletXtra3)p;
                int userAuthLevel = request.getUser().getAuthenticationLevel();
                showCustomize = (userAuthLevel >= p3.getMinEditAuthLevel());
            }
            else
            {
                // portlet does not support the newer interface required for the
                // check above so use the old, deprecated method
                showCustomize = (!request.getUser().isPublicUser() ||
                                 p.getShowEditToPublic());
            }

            if (showCustomize)
            {
                renderAction(out, p, request, PortletRenderer.MODE_EDIT, res.getString(ProviderBundle.CUSTOMIZE_LABEL), title);
            }
        }

        if (p.hasHelp())
        {
            renderAction(out, p, request, PortletRenderer.MODE_HELP,
                         res.getString(ProviderBundle.HELP_LABEL), title);
        }


        if (p.hasAbout())
        {
            renderAction(out, p, request, PortletRenderer.MODE_ABOUT,
                         res.getString(ProviderBundle.ABOUT_LABEL), title);
        }
    }

    protected void renderDummyActions(PrintWriter out,
                                      Portlet p,
                                      PortletRenderRequest request)
        throws java.io.IOException
    {
        out.print("<TD class=\"");
        out.print(PortletRenderer.PAGE_COLOR);
        out.println("\" NOWRAP=\"\" valign=\"top\" width=\"100%\">&nbsp;");

        ResourceBundle res = request.getProviderResource();

        if (p.hasShowEdit()) {
            renderDummyAction(out, res.getString(ProviderBundle.CUSTOMIZE_LABEL));
        }

        if (p.hasHelp()) {
            renderDummyAction(out, res.getString(ProviderBundle.HELP_LABEL));
        }

        if (p.hasAbout()) {
            renderDummyAction(out, res.getString(ProviderBundle.ABOUT_LABEL));
        }
    }

}


