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

package oracle.portal.provider.v2;

import oracle.portal.provider.v2.PortletDefinition;
import oracle.portal.provider.v2.EventDefinition;
import oracle.portal.utils.xml.v2.*;
import java.util.*;

/**
 * DefaultParameterDefinition extends ParameterDefinition to handle
 * getting the the resource bundle for a parameter from it's parent portlet.
 * It is encouraged for all strings for a portlet to be specified in a single
 * resource bundle, rather than specifying a separate one for each parameter.
 */
public class DefaultParameterDefinition extends ParameterDefinition
    implements InitializableXMLObject
{
    /**
     * mParent is the portlet which is the parent for this parameter.
     */
    private PortletDefinition mParent;


    /**
     * Called during parsing of provider.xml when this object is created.
     *
     * Used here to store the parent portlet.  This could be a portlet or
     * an event.  If it's an event, then get it's parent portlet.
     */
    public void preInitialize(Object parent) throws NodeHandlerException
    {
        if (parent instanceof EventDefinition)
        {
            mParent = ((DefaultEventDefinition)parent).getParent();
        }
        else if (parent instanceof PortletDefinition)
        {
            mParent = (PortletDefinition)parent;
        }
        else
        {
            throw new NodeHandlerException("Parent of a Portlet must be a ProviderDefinition");
        }
    }

    /**
     * Not used.
     */
    public void postInitialize() throws NodeHandlerException
    {
    }

    /**
     * Gets the resource bundle for this parameter in the given Locale.
     *
     * @param l the Locale of the resource bundle being requested.
     * @return the resource bundle for this parameter.  <b>null</b> if
     * there isn't one.
     */
    public ResourceBundle getResourceBundle(Locale l)
    {
        // check if a resouce bundle was specified for this parameter.
        ResourceBundle b = super.getResourceBundle(l);

        // If none was specified, the portlet's resource bundle.
        if (b == null)
        {
            return mParent.getResourceBundle(l);
        }

        return b;
    }

}
