package com.backstagetech.turbine.util.template;

import org.apache.turbine.util.template.TemplatePageAttributes;
import org.apache.turbine.util.RunData;
import org.apache.turbine.services.pull.ApplicationTool;
import org.apache.ecs.html.Script;
import org.apache.ecs.html.Style;
import org.apache.ecs.html.Link;

/**
 * Provide for including references to javascript files.
 * <p>&copy;2002 <a href="http://www.backstagetech.com.au">Backstage
 * Technologies Pty. Ltd.</a>
 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
 */
public class TemplatePageAttributesEx
        extends TemplatePageAttributes
        implements ApplicationTool
{
    /** The RunData object. */
    private RunData data = null;

    /**
     * Default constructor. The init method must be called before use
     */
    public TemplatePageAttributesEx()
    {
        super();
    }

    /**
     * Construct a new instance with the given RunData object.
     *
     * @param data a RunData instance
     */
    public TemplatePageAttributesEx(RunData data)
    {
        super(data);
        this.data = data;
    }

    /**
     * Initialise this instance with the given RunData object.
     * (ApplicationTool method)
     *
     * @param data Assumed to be a RunData instance
     */
    public void init(Object data)
    {
        super.init(data);

        // we blithely cast to RunData as the runtime error thrown
        // if data is null or not RunData is appropriate.
        this.data = (RunData)data;
    }

    /**
     * Refresh method - does nothing
     */
    public void refresh()
    {
        // empty
    }

    /**
     * Adds a LINK to a javascript file to the HEAD of the page.
     *
     * @param url A String.
     * @return A TemplatePageAttributesEx (self).
     */
    public TemplatePageAttributes setScript(String url)
    {
        data.getPage().getHead().addElement(new Script().setSrc(url)
                .setType("text/javascript").setLanguage("JavaScript"));
        return this;
    }

    /**
     * Adds a LINK to a CSS stylesheet to the HEAD of the page, allowing the
     * media type to be specified.
     *
     * @param url The value for the <code>href</code> attribute.
     * @param media The value for the <code>media</code> attribute.
     * @return a <code>TemplatePageAttributes</code> (self).
     */
    public TemplatePageAttributes setStyleSheet(String url, String media)
    {
        data.getPage().getHead().addElement(new Link().setRel("stylesheet")
                .setType("text/css").setMedia(media).setHref(url));
        return this;
    }

//    /**
//     * Adds a STYLE element to the HEAD of the page.
//     *
//     * @param styleText The content of the style element.
//     * @param media The value for the <code>media</code> attribute.
//     * @return a <code>TemplatePageAttributes</code> (self).
//     */
//    public TemplatePageAttributes setStyleElement(String styleText, String media)
//    {
//        Style style = new Style("text/css", styleText);
//        style.setMedia(media);
//        data.getPage().getHead().addElement(style);
//        return this;
//    }

    /**
     * Adds a STYLE element to the HEAD of the page with the provided content.
     *
     * @param styleText The contents of the <code>style</code> tag.
     * @return a <code>TemplatePageAttributes</code> (self).
     */
    public TemplatePageAttributes setStyle(String styleText)
    {
        data.getPage().getHead().addElement(new Style("text/css", styleText));
        return this;
    }

}

