package // must change;

import net.sf.tapestry.contrib.popup.PopupLink;
import net.sf.tapestry.IBinding;
import net.sf.tapestry.IEngine;
import net.sf.tapestry.IEngineService;
import net.sf.tapestry.Gesture;
import net.sf.tapestry.IRequestCycle;

/**
 * This component provides a popup <b>page</b> link to launch a new window using a
 * given tapestry page name,
 * windowName and windowFeatures for the javascript function:
 * window.open(<b>url of tapestry page</b>, windowName, windowFeatures).
 *
 * @author <a href="mailto:shomburg@hsofttec.com">Sven Homburg</a>
 * @version $Id$
 */
public class PopupPageLink extends PopupLink
{
    private String pageName = "PopupPageLink";
    private String linkText = "LinkText";

    /**
     * get the name of the search page
     * @return pageName
     */
    public String getPageName()
    {
        IBinding binding = getBinding("pageName");
        if(binding.getObject() != null)
        {
            IEngine engine = getPage().getEngine();
            IEngineService service = engine.getService(IEngineService.PAGE_SERVICE);
            Gesture g = service.buildGesture(this.getPage().getRequestCycle(), this,
                                             new String[]{binding.getObject().toString()});
            return g.getURL();
        }

        return pageName;
    }

    /**
     * set the name of the search page
     * @param pageName
     */
    public void setPageName(String pageName)
    {
        this.pageName = pageName;
    }

    /**
     * overides the method
     * @link PopupLink#getHref
     * @return pageName the page url
     */
    public String getHref()
    {
      return getPageName();
    }

    /**
     * get the text of the link
     * @return linkText
     */
    public String getLinkText()
    {
        IBinding binding = getBinding("linkText");
        if(binding.getObject() != null)
            return binding.getString();
        else
            return linkText;
    }

    /**
     * set the text of the link
     * @param linkText
     */
    public void setLinkText(String linkText)
    {
        this.linkText = linkText;
    }

    /**
     * Invoked by render(IMarkupWriter, IRequestCycle) after the component renders,
     * to clear any parameters back to null (or 0, or false). Primarily, this is used
     * to ensure that the component doesn't hold onto any objects that could otherwise
     * be garbage collected
     * @param cycle
     */
    public void cleanupAfterRender(IRequestCycle cycle)
    {
        pageName = null;
        linkText = null;
        super.cleanupAfterRender(cycle);
    }
}
